ServiceProviderTest.php in Drupal 10
File
core/tests/Drupal/KernelTests/Core/ServiceProvider/ServiceProviderTest.php
View source
<?php
namespace Drupal\KernelTests\Core\ServiceProvider;
use Drupal\KernelTests\KernelTestBase;
class ServiceProviderTest extends KernelTestBase {
protected static $modules = [
'file',
'service_provider_test',
'system',
];
public function testServiceProviderRegistration() {
$definition = $this->container
->getDefinition('file.usage');
$this
->assertSame('Drupal\\service_provider_test\\TestFileUsage', $definition
->getClass(), 'Class has been changed');
$this
->assertTrue(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service has been registered to the DIC');
}
public function testServiceProviderRegistrationDynamic() {
\Drupal::service('module_installer')
->uninstall([
'service_provider_test',
]);
$this
->assertFalse(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service does not exist in the DIC.');
\Drupal::service('module_installer')
->install([
'service_provider_test',
]);
$this
->assertTrue(\Drupal::hasService('service_provider_test_class'), 'The service_provider_test_class service exists in the DIC.');
}
}