Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ContainerBuilderTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContainerBuilderTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ContainerBuilderTest extends \PHPUnit_Framework_TestCase |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setDefinitions |
||
| 35 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getDefinitions |
||
| 36 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setDefinition |
||
| 37 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getDefinition |
||
| 38 | */ |
||
| 39 | public function testDefinitions() |
||
| 40 | { |
||
| 41 | $builder = new ContainerBuilder(); |
||
| 42 | $definitions = array( |
||
| 43 | 'foo' => new Definition('Bar\FooClass'), |
||
| 44 | 'bar' => new Definition('BarClass'), |
||
| 45 | ); |
||
| 46 | $builder->setDefinitions($definitions); |
||
| 47 | $this->assertEquals($definitions, $builder->getDefinitions(), '->setDefinitions() sets the service definitions'); |
||
| 48 | $this->assertTrue($builder->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists'); |
||
| 49 | $this->assertFalse($builder->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist'); |
||
| 50 | |||
| 51 | $builder->setDefinition('foobar', $foo = new Definition('FooBarClass')); |
||
| 52 | $this->assertEquals($foo, $builder->getDefinition('foobar'), '->getDefinition() returns a service definition if defined'); |
||
| 53 | $this->assertTrue($builder->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fluid interface by returning the service reference'); |
||
| 54 | |||
| 55 | $builder->addDefinitions($defs = array('foobar' => new Definition('FooBarClass'))); |
||
| 56 | $this->assertEquals(array_merge($definitions, $defs), $builder->getDefinitions(), '->addDefinitions() adds the service definitions'); |
||
| 57 | |||
| 58 | try { |
||
| 59 | $builder->getDefinition('baz'); |
||
| 60 | $this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist'); |
||
| 61 | } catch (\InvalidArgumentException $e) { |
||
| 62 | $this->assertEquals('The service definition "baz" does not exist.', $e->getMessage(), '->getDefinition() throws an InvalidArgumentException if the service definition does not exist'); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::register |
||
| 68 | */ |
||
| 69 | public function testRegister() |
||
| 70 | { |
||
| 71 | $builder = new ContainerBuilder(); |
||
| 72 | $builder->register('foo', 'Bar\FooClass'); |
||
| 73 | $this->assertTrue($builder->hasDefinition('foo'), '->register() registers a new service definition'); |
||
| 74 | $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $builder->getDefinition('foo'), '->register() returns the newly created Definition instance'); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::has |
||
| 79 | */ |
||
| 80 | public function testHas() |
||
| 81 | { |
||
| 82 | $builder = new ContainerBuilder(); |
||
| 83 | $this->assertFalse($builder->has('foo'), '->has() returns false if the service does not exist'); |
||
| 84 | $builder->register('foo', 'Bar\FooClass'); |
||
| 85 | $this->assertTrue($builder->has('foo'), '->has() returns true if a service definition exists'); |
||
| 86 | $builder->set('bar', new \stdClass()); |
||
| 87 | $this->assertTrue($builder->has('bar'), '->has() returns true if a service exists'); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::get |
||
| 92 | */ |
||
| 93 | public function testGet() |
||
| 94 | { |
||
| 95 | $builder = new ContainerBuilder(); |
||
| 96 | try { |
||
| 97 | $builder->get('foo'); |
||
| 98 | $this->fail('->get() throws an InvalidArgumentException if the service does not exist'); |
||
| 99 | } catch (\InvalidArgumentException $e) { |
||
| 100 | $this->assertEquals('The service definition "foo" does not exist.', $e->getMessage(), '->get() throws an InvalidArgumentException if the service does not exist'); |
||
| 101 | } |
||
| 102 | |||
| 103 | $this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument'); |
||
| 104 | |||
| 105 | $builder->register('foo', 'stdClass'); |
||
| 106 | $this->assertInternalType('object', $builder->get('foo'), '->get() returns the service definition associated with the id'); |
||
| 107 | $builder->set('bar', $bar = new \stdClass()); |
||
| 108 | $this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id'); |
||
| 109 | $builder->register('bar', 'stdClass'); |
||
| 110 | $this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id even if a definition has been defined'); |
||
| 111 | |||
| 112 | $builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz'))); |
||
| 113 | try { |
||
| 114 | @$builder->get('baz'); |
||
|
|
|||
| 115 | $this->fail('->get() throws a ServiceCircularReferenceException if the service has a circular reference to itself'); |
||
| 116 | } catch (\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException $e) { |
||
| 117 | $this->assertEquals('Circular reference detected for service "baz", path: "baz".', $e->getMessage(), '->get() throws a LogicException if the service has a circular reference to itself'); |
||
| 118 | } |
||
| 119 | |||
| 120 | $builder->register('foobar', 'stdClass')->setScope('container'); |
||
| 121 | $this->assertTrue($builder->get('bar') === $builder->get('bar'), '->get() always returns the same instance if the service is shared'); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @covers \Symfony\Component\DependencyInjection\ContainerBuilder::get |
||
| 126 | * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
||
| 127 | * @expectedExceptionMessage You have requested a synthetic service ("foo"). The DIC does not know how to construct this service. |
||
| 128 | */ |
||
| 129 | public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException() |
||
| 130 | { |
||
| 131 | $builder = new ContainerBuilder(); |
||
| 132 | $builder->register('foo', 'stdClass')->setSynthetic(true); |
||
| 133 | |||
| 134 | // we expect a RuntimeException here as foo is synthetic |
||
| 135 | try { |
||
| 136 | $builder->get('foo'); |
||
| 137 | } catch (RuntimeException $e) { |
||
| 138 | } |
||
| 139 | |||
| 140 | // we must also have the same RuntimeException here |
||
| 141 | $builder->get('foo'); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::get |
||
| 146 | */ |
||
| 147 | public function testGetReturnsNullOnInactiveScope() |
||
| 148 | { |
||
| 149 | $builder = new ContainerBuilder(); |
||
| 150 | $builder->register('foo', 'stdClass')->setScope('request'); |
||
| 151 | |||
| 152 | $this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE)); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::get |
||
| 157 | */ |
||
| 158 | public function testGetReturnsNullOnInactiveScopeWhenServiceIsCreatedByAMethod() |
||
| 159 | { |
||
| 160 | $builder = new ProjectContainer(); |
||
| 161 | |||
| 162 | $this->assertNull($builder->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE)); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getServiceIds |
||
| 167 | */ |
||
| 168 | public function testGetServiceIds() |
||
| 169 | { |
||
| 170 | $builder = new ContainerBuilder(); |
||
| 171 | $builder->register('foo', 'stdClass'); |
||
| 172 | $builder->bar = $bar = new \stdClass(); |
||
| 173 | $builder->register('bar', 'stdClass'); |
||
| 174 | $this->assertEquals(array('foo', 'bar', 'service_container'), $builder->getServiceIds(), '->getServiceIds() returns all defined service ids'); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setAlias |
||
| 179 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::hasAlias |
||
| 180 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getAlias |
||
| 181 | */ |
||
| 182 | public function testAliases() |
||
| 183 | { |
||
| 184 | $builder = new ContainerBuilder(); |
||
| 185 | $builder->register('foo', 'stdClass'); |
||
| 186 | $builder->setAlias('bar', 'foo'); |
||
| 187 | $this->assertTrue($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists'); |
||
| 188 | $this->assertFalse($builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist'); |
||
| 189 | $this->assertEquals('foo', (string) $builder->getAlias('bar'), '->getAlias() returns the aliased service'); |
||
| 190 | $this->assertTrue($builder->has('bar'), '->setAlias() defines a new service'); |
||
| 191 | $this->assertTrue($builder->get('bar') === $builder->get('foo'), '->setAlias() creates a service that is an alias to another one'); |
||
| 192 | |||
| 193 | try { |
||
| 194 | $builder->setAlias('foobar', 'foobar'); |
||
| 195 | $this->fail('->setAlias() throws an InvalidArgumentException if the alias references itself'); |
||
| 196 | } catch (\InvalidArgumentException $e) { |
||
| 197 | $this->assertEquals('An alias can not reference itself, got a circular reference on "foobar".', $e->getMessage(), '->setAlias() throws an InvalidArgumentException if the alias references itself'); |
||
| 198 | } |
||
| 199 | |||
| 200 | try { |
||
| 201 | $builder->getAlias('foobar'); |
||
| 202 | $this->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist'); |
||
| 203 | } catch (\InvalidArgumentException $e) { |
||
| 204 | $this->assertEquals('The service alias "foobar" does not exist.', $e->getMessage(), '->getAlias() throws an InvalidArgumentException if the alias does not exist'); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getAliases |
||
| 210 | */ |
||
| 211 | public function testGetAliases() |
||
| 212 | { |
||
| 213 | $builder = new ContainerBuilder(); |
||
| 214 | $builder->setAlias('bar', 'foo'); |
||
| 215 | $builder->setAlias('foobar', 'foo'); |
||
| 216 | $builder->setAlias('moo', new Alias('foo', false)); |
||
| 217 | |||
| 218 | $aliases = $builder->getAliases(); |
||
| 219 | $this->assertEquals('foo', (string) $aliases['bar']); |
||
| 220 | $this->assertTrue($aliases['bar']->isPublic()); |
||
| 221 | $this->assertEquals('foo', (string) $aliases['foobar']); |
||
| 222 | $this->assertEquals('foo', (string) $aliases['moo']); |
||
| 223 | $this->assertFalse($aliases['moo']->isPublic()); |
||
| 224 | |||
| 225 | $builder->register('bar', 'stdClass'); |
||
| 226 | $this->assertFalse($builder->hasAlias('bar')); |
||
| 227 | |||
| 228 | $builder->set('foobar', 'stdClass'); |
||
| 229 | $builder->set('moo', 'stdClass'); |
||
| 230 | $this->assertCount(0, $builder->getAliases(), '->getAliases() does not return aliased services that have been overridden'); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setAliases |
||
| 235 | */ |
||
| 236 | View Code Duplication | public function testSetAliases() |
|
| 237 | { |
||
| 238 | $builder = new ContainerBuilder(); |
||
| 239 | $builder->setAliases(array('bar' => 'foo', 'foobar' => 'foo')); |
||
| 240 | |||
| 241 | $aliases = $builder->getAliases(); |
||
| 242 | $this->assertTrue(isset($aliases['bar'])); |
||
| 243 | $this->assertTrue(isset($aliases['foobar'])); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addAliases |
||
| 248 | */ |
||
| 249 | View Code Duplication | public function testAddAliases() |
|
| 250 | { |
||
| 251 | $builder = new ContainerBuilder(); |
||
| 252 | $builder->setAliases(array('bar' => 'foo')); |
||
| 253 | $builder->addAliases(array('foobar' => 'foo')); |
||
| 254 | |||
| 255 | $aliases = $builder->getAliases(); |
||
| 256 | $this->assertTrue(isset($aliases['bar'])); |
||
| 257 | $this->assertTrue(isset($aliases['foobar'])); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addCompilerPass |
||
| 262 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getCompilerPassConfig |
||
| 263 | */ |
||
| 264 | public function testAddGetCompilerPass() |
||
| 265 | { |
||
| 266 | $builder = new ContainerBuilder(); |
||
| 267 | $builder->setResourceTracking(false); |
||
| 268 | $builderCompilerPasses = $builder->getCompiler()->getPassConfig()->getPasses(); |
||
| 269 | $builder->addCompilerPass($this->getMock('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface')); |
||
| 270 | |||
| 271 | $this->assertCount(count($builder->getCompiler()->getPassConfig()->getPasses()) - 1, $builderCompilerPasses); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService |
||
| 276 | */ |
||
| 277 | public function testCreateService() |
||
| 278 | { |
||
| 279 | $builder = new ContainerBuilder(); |
||
| 280 | $builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php'); |
||
| 281 | $this->assertInstanceOf('\Bar\FooClass', $builder->get('foo1'), '->createService() requires the file defined by the service definition'); |
||
| 282 | $builder->register('foo2', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/%file%.php'); |
||
| 283 | $builder->setParameter('file', 'foo'); |
||
| 284 | $this->assertInstanceOf('\Bar\FooClass', $builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition'); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService |
||
| 289 | */ |
||
| 290 | public function testCreateProxyWithRealServiceInstantiator() |
||
| 291 | { |
||
| 292 | $builder = new ContainerBuilder(); |
||
| 293 | |||
| 294 | $builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php'); |
||
| 295 | $builder->getDefinition('foo1')->setLazy(true); |
||
| 296 | |||
| 297 | $foo1 = $builder->get('foo1'); |
||
| 298 | |||
| 299 | $this->assertSame($foo1, $builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls'); |
||
| 300 | $this->assertSame('Bar\FooClass', get_class($foo1)); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService |
||
| 305 | */ |
||
| 306 | public function testCreateServiceClass() |
||
| 307 | { |
||
| 308 | $builder = new ContainerBuilder(); |
||
| 309 | $builder->register('foo1', '%class%'); |
||
| 310 | $builder->setParameter('class', 'stdClass'); |
||
| 311 | $this->assertInstanceOf('\stdClass', $builder->get('foo1'), '->createService() replaces parameters in the class provided by the service definition'); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService |
||
| 316 | */ |
||
| 317 | public function testCreateServiceArguments() |
||
| 318 | { |
||
| 319 | $builder = new ContainerBuilder(); |
||
| 320 | $builder->register('bar', 'stdClass'); |
||
| 321 | $builder->register('foo1', 'Bar\FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'), '%%unescape_it%%')); |
||
| 322 | $builder->setParameter('value', 'bar'); |
||
| 323 | $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->arguments, '->createService() replaces parameters and service references in the arguments provided by the service definition'); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService |
||
| 328 | */ |
||
| 329 | public function testCreateServiceFactory() |
||
| 330 | { |
||
| 331 | $builder = new ContainerBuilder(); |
||
| 332 | $builder->register('foo', 'Bar\FooClass')->setFactory('Bar\FooClass::getInstance'); |
||
| 333 | $builder->register('qux', 'Bar\FooClass')->setFactory(array('Bar\FooClass', 'getInstance')); |
||
| 334 | $builder->register('bar', 'Bar\FooClass')->setFactory(array(new Definition('Bar\FooClass'), 'getInstance')); |
||
| 335 | $builder->register('baz', 'Bar\FooClass')->setFactory(array(new Reference('bar'), 'getInstance')); |
||
| 336 | |||
| 337 | $this->assertTrue($builder->get('foo')->called, '->createService() calls the factory method to create the service instance'); |
||
| 338 | $this->assertTrue($builder->get('qux')->called, '->createService() calls the factory method to create the service instance'); |
||
| 339 | $this->assertTrue($builder->get('bar')->called, '->createService() uses anonymous service as factory'); |
||
| 340 | $this->assertTrue($builder->get('baz')->called, '->createService() uses another service as factory'); |
||
| 341 | } |
||
| 342 | |||
| 343 | public function testLegacyCreateServiceFactory() |
||
| 344 | { |
||
| 345 | $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); |
||
| 346 | |||
| 347 | $builder = new ContainerBuilder(); |
||
| 348 | $builder->register('bar', 'Bar\FooClass'); |
||
| 349 | $builder |
||
| 350 | ->register('foo1', 'Bar\FooClass') |
||
| 351 | ->setFactoryClass('%foo_class%') |
||
| 352 | ->setFactoryMethod('getInstance') |
||
| 353 | ->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'))) |
||
| 354 | ; |
||
| 355 | $builder->setParameter('value', 'bar'); |
||
| 356 | $builder->setParameter('foo_class', 'Bar\FooClass'); |
||
| 357 | $this->assertTrue($builder->get('foo1')->called, '->createService() calls the factory method to create the service instance'); |
||
| 358 | $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the factory method'); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService |
||
| 363 | */ |
||
| 364 | public function testLegacyCreateServiceFactoryService() |
||
| 365 | { |
||
| 366 | $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); |
||
| 367 | |||
| 368 | $builder = new ContainerBuilder(); |
||
| 369 | $builder->register('foo_service', 'Bar\FooClass'); |
||
| 370 | $builder |
||
| 371 | ->register('foo', 'Bar\FooClass') |
||
| 372 | ->setFactoryService('%foo_service%') |
||
| 373 | ->setFactoryMethod('getInstance') |
||
| 374 | ; |
||
| 375 | $builder->setParameter('foo_service', 'foo_service'); |
||
| 376 | $this->assertTrue($builder->get('foo')->called, '->createService() calls the factory method to create the service instance'); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService |
||
| 381 | */ |
||
| 382 | public function testCreateServiceMethodCalls() |
||
| 383 | { |
||
| 384 | $builder = new ContainerBuilder(); |
||
| 385 | $builder->register('bar', 'stdClass'); |
||
| 386 | $builder->register('foo1', 'Bar\FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar')))); |
||
| 387 | $builder->setParameter('value', 'bar'); |
||
| 388 | $this->assertEquals(array('bar', $builder->get('bar')), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments'); |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService |
||
| 393 | */ |
||
| 394 | public function testCreateServiceConfigurator() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService |
||
| 419 | * @expectedException \RuntimeException |
||
| 420 | */ |
||
| 421 | public function testCreateSyntheticService() |
||
| 422 | { |
||
| 423 | $builder = new ContainerBuilder(); |
||
| 424 | $builder->register('foo', 'Bar\FooClass')->setSynthetic(true); |
||
| 425 | $builder->get('foo'); |
||
| 426 | } |
||
| 427 | |||
| 428 | public function testCreateServiceWithExpression() |
||
| 429 | { |
||
| 430 | $builder = new ContainerBuilder(); |
||
| 431 | $builder->setParameter('bar', 'bar'); |
||
| 432 | $builder->register('bar', 'BarClass'); |
||
| 433 | $builder->register('foo', 'Bar\FooClass')->addArgument(array('foo' => new Expression('service("bar").foo ~ parameter("bar")'))); |
||
| 434 | $this->assertEquals('foobar', $builder->get('foo')->arguments['foo']); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::resolveServices |
||
| 439 | */ |
||
| 440 | public function testResolveServices() |
||
| 441 | { |
||
| 442 | $builder = new ContainerBuilder(); |
||
| 443 | $builder->register('foo', 'Bar\FooClass'); |
||
| 444 | $this->assertEquals($builder->get('foo'), $builder->resolveServices(new Reference('foo')), '->resolveServices() resolves service references to service instances'); |
||
| 445 | $this->assertEquals(array('foo' => array('foo', $builder->get('foo'))), $builder->resolveServices(array('foo' => array('foo', new Reference('foo')))), '->resolveServices() resolves service references to service instances in nested arrays'); |
||
| 446 | $this->assertEquals($builder->get('foo'), $builder->resolveServices(new Expression('service("foo")')), '->resolveServices() resolves expressions'); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::merge |
||
| 451 | */ |
||
| 452 | public function testMerge() |
||
| 453 | { |
||
| 454 | $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo'))); |
||
| 455 | $container->setResourceTracking(false); |
||
| 456 | $config = new ContainerBuilder(new ParameterBag(array('foo' => 'bar'))); |
||
| 457 | $container->merge($config); |
||
| 458 | $this->assertEquals(array('bar' => 'foo', 'foo' => 'bar'), $container->getParameterBag()->all(), '->merge() merges current parameters with the loaded ones'); |
||
| 459 | |||
| 460 | $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo'))); |
||
| 461 | $container->setResourceTracking(false); |
||
| 462 | $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%'))); |
||
| 463 | $container->merge($config); |
||
| 464 | $container->compile(); |
||
| 465 | $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones'); |
||
| 466 | |||
| 467 | $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo'))); |
||
| 468 | $container->setResourceTracking(false); |
||
| 469 | $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%', 'baz' => '%foo%'))); |
||
| 470 | $container->merge($config); |
||
| 471 | $container->compile(); |
||
| 472 | $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones'); |
||
| 473 | |||
| 474 | $container = new ContainerBuilder(); |
||
| 475 | $container->setResourceTracking(false); |
||
| 476 | $container->register('foo', 'Bar\FooClass'); |
||
| 477 | $container->register('bar', 'BarClass'); |
||
| 478 | $config = new ContainerBuilder(); |
||
| 479 | $config->setDefinition('baz', new Definition('BazClass')); |
||
| 480 | $config->setAlias('alias_for_foo', 'foo'); |
||
| 481 | $container->merge($config); |
||
| 482 | $this->assertEquals(array('foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones'); |
||
| 483 | |||
| 484 | $aliases = $container->getAliases(); |
||
| 485 | $this->assertTrue(isset($aliases['alias_for_foo'])); |
||
| 486 | $this->assertEquals('foo', (string) $aliases['alias_for_foo']); |
||
| 487 | |||
| 488 | $container = new ContainerBuilder(); |
||
| 489 | $container->setResourceTracking(false); |
||
| 490 | $container->register('foo', 'Bar\FooClass'); |
||
| 491 | $config->setDefinition('foo', new Definition('BazClass')); |
||
| 492 | $container->merge($config); |
||
| 493 | $this->assertEquals('BazClass', $container->getDefinition('foo')->getClass(), '->merge() overrides already defined services'); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::merge |
||
| 498 | * @expectedException \LogicException |
||
| 499 | */ |
||
| 500 | public function testMergeLogicException() |
||
| 501 | { |
||
| 502 | $container = new ContainerBuilder(); |
||
| 503 | $container->setResourceTracking(false); |
||
| 504 | $container->compile(); |
||
| 505 | $container->merge(new ContainerBuilder()); |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::findTaggedServiceIds |
||
| 510 | */ |
||
| 511 | public function testfindTaggedServiceIds() |
||
| 512 | { |
||
| 513 | $builder = new ContainerBuilder(); |
||
| 514 | $builder |
||
| 515 | ->register('foo', 'Bar\FooClass') |
||
| 516 | ->addTag('foo', array('foo' => 'foo')) |
||
| 517 | ->addTag('bar', array('bar' => 'bar')) |
||
| 518 | ->addTag('foo', array('foofoo' => 'foofoo')) |
||
| 519 | ; |
||
| 520 | $this->assertEquals($builder->findTaggedServiceIds('foo'), array( |
||
| 521 | 'foo' => array( |
||
| 522 | array('foo' => 'foo'), |
||
| 523 | array('foofoo' => 'foofoo'), |
||
| 524 | ), |
||
| 525 | ), '->findTaggedServiceIds() returns an array of service ids and its tag attributes'); |
||
| 526 | $this->assertEquals(array(), $builder->findTaggedServiceIds('foobar'), '->findTaggedServiceIds() returns an empty array if there is annotated services'); |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::findDefinition |
||
| 531 | */ |
||
| 532 | public function testFindDefinition() |
||
| 533 | { |
||
| 534 | $container = new ContainerBuilder(); |
||
| 535 | $container->setDefinition('foo', $definition = new Definition('Bar\FooClass')); |
||
| 536 | $container->setAlias('bar', 'foo'); |
||
| 537 | $container->setAlias('foobar', 'bar'); |
||
| 538 | $this->assertEquals($definition, $container->findDefinition('foobar'), '->findDefinition() returns a Definition'); |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addObjectResource |
||
| 543 | */ |
||
| 544 | View Code Duplication | public function testAddObjectResource() |
|
| 566 | |||
| 567 | /** |
||
| 568 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addClassResource |
||
| 569 | */ |
||
| 570 | View Code Duplication | public function testAddClassResource() |
|
| 592 | |||
| 593 | /** |
||
| 594 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::compile |
||
| 595 | */ |
||
| 596 | public function testCompilesClassDefinitionsOfLazyServices() |
||
| 597 | { |
||
| 598 | $container = new ContainerBuilder(); |
||
| 599 | |||
| 600 | $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking'); |
||
| 601 | |||
| 602 | $container->register('foo', 'BarClass'); |
||
| 603 | $container->getDefinition('foo')->setLazy(true); |
||
| 604 | |||
| 605 | $container->compile(); |
||
| 606 | |||
| 607 | $classesPath = realpath(__DIR__.'/Fixtures/includes/classes.php'); |
||
| 608 | $matchingResources = array_filter( |
||
| 609 | $container->getResources(), |
||
| 610 | function (ResourceInterface $resource) use ($classesPath) { |
||
| 611 | return $resource instanceof FileResource && $classesPath === realpath($resource->getResource()); |
||
| 612 | } |
||
| 613 | ); |
||
| 614 | |||
| 615 | $this->assertNotEmpty($matchingResources); |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getResources |
||
| 620 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addResource |
||
| 621 | */ |
||
| 622 | public function testResources() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::registerExtension |
||
| 640 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getExtension |
||
| 641 | */ |
||
| 642 | public function testExtension() |
||
| 653 | |||
| 654 | public function testRegisteredButNotLoadedExtension() |
||
| 665 | |||
| 666 | public function testRegisteredAndLoadedExtension() |
||
| 678 | |||
| 679 | public function testPrivateServiceUser() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @expectedException \BadMethodCallException |
||
| 699 | */ |
||
| 700 | public function testThrowsExceptionWhenSetServiceOnAFrozenContainer() |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @expectedException \BadMethodCallException |
||
| 711 | */ |
||
| 712 | public function testThrowsExceptionWhenAddServiceOnAFrozenContainer() |
||
| 718 | |||
| 719 | public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * @group legacy |
||
| 732 | */ |
||
| 733 | public function testLegacySetOnSynchronizedService() |
||
| 751 | |||
| 752 | /** |
||
| 753 | * @group legacy |
||
| 754 | */ |
||
| 755 | public function testLegacySynchronizedServiceWithScopes() |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @expectedException \BadMethodCallException |
||
| 788 | */ |
||
| 789 | public function testThrowsExceptionWhenSetDefinitionOnAFrozenContainer() |
||
| 796 | |||
| 797 | /** |
||
| 798 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getExtensionConfig |
||
| 799 | * @covers Symfony\Component\DependencyInjection\ContainerBuilder::prependExtensionConfig |
||
| 800 | */ |
||
| 801 | public function testExtensionConfig() |
||
| 818 | |||
| 819 | public function testLazyLoadedService() |
||
| 851 | } |
||
| 852 | |||
| 864 |
If you suppress an error, we recommend checking for the error condition explicitly: