3 namespace Drupal\KernelTests\Core\ServiceProvider;
5 use Drupal\KernelTests\KernelTestBase;
8 * Tests service provider registration to the DIC.
10 * @group ServiceProvider
12 class ServiceProviderTest extends KernelTestBase {
19 public static $modules = ['file', 'service_provider_test', 'system'];
22 * Tests that services provided by module service providers get registered to the DIC.
24 public function testServiceProviderRegistration() {
25 $definition = $this->container->getDefinition('file.usage');
26 $this->assertTrue($definition->getClass() == 'Drupal\\service_provider_test\\TestFileUsage', 'Class has been changed');
27 $this->assertTrue(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service has been registered to the DIC');
31 * Tests that the DIC keeps up with module enable/disable in the same request.
33 public function testServiceProviderRegistrationDynamic() {
34 // Uninstall the module and ensure the service provider's service is not registered.
35 \Drupal::service('module_installer')->uninstall(['service_provider_test']);
36 $this->assertFalse(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service does not exist in the DIC.');
38 // Install the module and ensure the service provider's service is registered.
39 \Drupal::service('module_installer')->install(['service_provider_test']);
40 $this->assertTrue(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service exists in the DIC.');