ServiceProviderTest.php 1.68 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\system\Tests\ServiceProvider\ServiceProviderTest.
6 7 8 9
 */

namespace Drupal\system\Tests\ServiceProvider;

10
use Drupal\simpletest\KernelTestBase;
11 12 13

/**
 * Tests service provider registration to the DIC.
14 15
 *
 * @group ServiceProvider
16
 */
17
class ServiceProviderTest extends KernelTestBase {
18 19 20 21 22 23 24 25 26 27 28 29

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('file', 'service_provider_test');

  /**
   * Tests that services provided by module service providers get registered to the DIC.
   */
  function testServiceProviderRegistration() {
30 31
    $definition = $this->container->getDefinition('file.usage');
    $this->assertTrue($definition->getClass() == 'Drupal\\service_provider_test\\TestFileUsage', 'Class has been changed');
32
    $this->assertTrue(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service has been registered to the DIC');
33 34 35 36 37 38
  }

  /**
   * Tests that the DIC keeps up with module enable/disable in the same request.
   */
  function testServiceProviderRegistrationDynamic() {
39
    // Uninstall the module and ensure the service provider's service is not registered.
40
    \Drupal::service('module_installer')->uninstall(array('service_provider_test'));
41
    $this->assertFalse(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service does not exist in the DIC.');
42

43
    // Install the module and ensure the service provider's service is registered.
44
    \Drupal::service('module_installer')->install(array('service_provider_test'));
45
    $this->assertTrue(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service exists in the DIC.');
46 47 48
  }

}