ContainerBuilderTest.php in Drupal 9
File
core/tests/Drupal/Tests/Core/DependencyInjection/ContainerBuilderTest.php
View source
<?php
namespace Drupal\Tests\Core\DependencyInjection;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\Core\DependencyInjection\Fixture\BarClass;
use Symfony\Component\DependencyInjection\Definition;
class ContainerBuilderTest extends UnitTestCase {
public function testGet() {
$container = new ContainerBuilder();
$container
->register('bar', 'Drupal\\Tests\\Core\\DependencyInjection\\Fixture\\BarClass');
$result = $container
->get('bar');
$this
->assertInstanceOf(BarClass::class, $result);
}
public function testSet() {
$container = new ContainerBuilder();
$class = new BarClass();
$container
->set('bar', $class);
$this
->assertEquals('bar', $class->_serviceId);
}
public function testSetException() {
$container = new ContainerBuilder();
$class = new BarClass();
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Service ID names must be lowercase: Bar');
$container
->set('Bar', $class);
}
public function testSetParameterException() {
$container = new ContainerBuilder();
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Parameter names must be lowercase: Buzz');
$container
->setParameter('Buzz', 'buzz');
}
public function testRegisterException() {
$container = new ContainerBuilder();
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Service ID names must be lowercase: Bar');
$container
->register('Bar');
}
public function testRegister() {
$container = new ContainerBuilder();
$service = $container
->register('bar');
$this
->assertTrue($service
->isPublic());
}
public function testSetDefinition() {
$container = new ContainerBuilder();
$definition = new Definition();
$definition
->setPublic(TRUE);
$service = $container
->setDefinition('foo', $definition);
$this
->assertTrue($service
->isPublic());
$definition = new Definition();
$definition
->setPublic(FALSE);
$service = $container
->setDefinition('foo', $definition);
$this
->assertFalse($service
->isPublic());
}
public function testLegacySetDefinition() {
$container = new ContainerBuilder();
$definition = new Definition();
$this
->expectDeprecation('Not marking service definitions as public is deprecated in drupal:9.2.0 and is required in drupal:10.0.0. Call $definition->setPublic(TRUE) before calling ::setDefinition(). See https://www.drupal.org/node/3194517');
$service = $container
->setDefinition('foo', $definition);
$this
->assertTrue($service
->isPublic());
}
public function testSetAlias() {
$container = new ContainerBuilder();
$container
->register('bar');
$alias = $container
->setAlias('foo', 'bar');
$this
->assertTrue($alias
->isPublic());
}
public function testSerialize() {
$container = new ContainerBuilder();
$this
->expectException(\AssertionError::class);
serialize($container);
}
public function testConstructor() {
class_alias(TestInterface::class, 'Symfony\\Component\\Config\\Resource\\ResourceInterface');
$container = new ContainerBuilder();
$this
->assertFalse($container
->isTrackingResources());
}
}
interface TestInterface {
}
Classes
Interfaces
Name |
Description |
TestInterface |
A test interface for testing ContainerBuilder::__construct(). |