More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / DependencyInjection / ContainerBuilderTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\DependencyInjection;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\Tests\Core\DependencyInjection\Fixture\BarClass;
8 use Symfony\Component\DependencyInjection\Definition;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\DependencyInjection\ContainerBuilder
12  * @group DependencyInjection
13  */
14 class ContainerBuilderTest extends UnitTestCase {
15
16   /**
17    * @covers ::get
18    */
19   public function testGet() {
20     $container = new ContainerBuilder();
21     $container->register('bar', 'Drupal\Tests\Core\DependencyInjection\Fixture\BarClass');
22
23     $result = $container->get('bar');
24     $this->assertTrue($result instanceof BarClass);
25   }
26
27   /**
28    * @covers ::set
29    */
30   public function testSet() {
31     $container = new ContainerBuilder();
32     $class = new BarClass();
33     $container->set('bar', $class);
34     $this->assertEquals('bar', $class->_serviceId);
35   }
36
37   /**
38    * @covers ::set
39    */
40   public function testSetException() {
41     $container = new ContainerBuilder();
42     $class = new BarClass();
43     $this->setExpectedException(\InvalidArgumentException::class, 'Service ID names must be lowercase: Bar');
44     $container->set('Bar', $class);
45   }
46
47   /**
48    * @covers ::setParameter
49    */
50   public function testSetParameterException() {
51     $container = new ContainerBuilder();
52     $this->setExpectedException(\InvalidArgumentException::class, 'Parameter names must be lowercase: Buzz');
53     $container->setParameter('Buzz', 'buzz');
54   }
55
56   /**
57    * @covers ::register
58    */
59   public function testRegisterException() {
60     $container = new ContainerBuilder();
61     $this->setExpectedException(\InvalidArgumentException::class, 'Service ID names must be lowercase: Bar');
62     $container->register('Bar');
63   }
64
65   /**
66    * @covers ::register
67    */
68   public function testRegister() {
69     $container = new ContainerBuilder();
70     $service = $container->register('bar');
71     $this->assertTrue($service->isPublic());
72   }
73
74   /**
75    * @covers ::setDefinition
76    */
77   public function testSetDefinition() {
78     // Test a service with defaults.
79     $container = new ContainerBuilder();
80     $definition = new Definition();
81     $service = $container->setDefinition('foo', $definition);
82     $this->assertTrue($service->isPublic());
83     $this->assertFalse($service->isPrivate());
84
85     // Test a service with public set to false.
86     $definition = new Definition();
87     $definition->setPublic(FALSE);
88     $service = $container->setDefinition('foo', $definition);
89     $this->assertFalse($service->isPublic());
90     $this->assertFalse($service->isPrivate());
91
92     // Test a service with private set to true. Drupal does not support this.
93     // We only support using setPublic() to make things not available outside
94     // the container.
95     $definition = new Definition();
96     $definition->setPrivate(TRUE);
97     $service = $container->setDefinition('foo', $definition);
98     $this->assertTrue($service->isPublic());
99     $this->assertFalse($service->isPrivate());
100   }
101
102   /**
103    * @covers ::setAlias
104    */
105   public function testSetAlias() {
106     $container = new ContainerBuilder();
107     $container->register('bar');
108     $alias = $container->setAlias('foo', 'bar');
109     $this->assertTrue($alias->isPublic());
110   }
111
112   /**
113    * Tests serialization.
114    */
115   public function testSerialize() {
116     $container = new ContainerBuilder();
117     $this->setExpectedException(\AssertionError::class);
118     serialize($container);
119   }
120
121   /**
122    * Tests constructor and resource tracking disabling.
123    *
124    * This test runs in a separate process to ensure the aliased class does not
125    * affect any other tests.
126    *
127    * @runInSeparateProcess
128    * @preserveGlobalState disabled
129    */
130   public function testConstructor() {
131     class_alias(testInterface::class, 'Symfony\Component\Config\Resource\ResourceInterface');
132     $container = new ContainerBuilder();
133     $this->assertFalse($container->isTrackingResources());
134   }
135
136 }
137
138 /**
139  * A test interface for testing ContainerBuilder::__construct().
140  *
141  * @see \Drupal\Tests\Core\DependencyInjection\ContainerBuilderTest::testConstructor()
142  */
143 interface testInterface {
144 }