From 1683f462796e87368cf7fb8a6e3bd1906555b4c6 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 8 Apr 2010 10:43:42 +0200 Subject: [PATCH] [DependencyInjection] reverted 2ac6faaa0b723e84e29cf940610806bad047bd15 --- .../Components/DependencyInjection/Container.php | 10 +++++----- .../DependencyInjection/ContainerTest.php | 2 +- .../DependencyInjection/Dumper/PhpDumperTest.php | 13 +++++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Components/DependencyInjection/Container.php b/src/Symfony/Components/DependencyInjection/Container.php index e403657353..a45387c0b4 100644 --- a/src/Symfony/Components/DependencyInjection/Container.php +++ b/src/Symfony/Components/DependencyInjection/Container.php @@ -193,16 +193,16 @@ class Container implements ContainerInterface, \ArrayAccess throw new \InvalidArgumentException(sprintf('A service id should be a string (%s given).', str_replace("\n", '', var_export($id, true)))); } - if (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_')).'Service') && 'getService' !== $method) - { - return $this->$method(); - } - if (isset($this->services[$id])) { return $this->services[$id]; } + if (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_')).'Service') && 'getService' !== $method) + { + return $this->$method(); + } + if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) { throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $id)); diff --git a/tests/Symfony/Tests/Components/DependencyInjection/ContainerTest.php b/tests/Symfony/Tests/Components/DependencyInjection/ContainerTest.php index 5d8210c519..147a2092bf 100644 --- a/tests/Symfony/Tests/Components/DependencyInjection/ContainerTest.php +++ b/tests/Symfony/Tests/Components/DependencyInjection/ContainerTest.php @@ -126,7 +126,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase $this->assertTrue($sc->hasService('bar'), '->hasService() returns true if the service has been defined as a getXXXService() method'); $sc->setService('bar', $bar = new \stdClass()); - $this->assertNotEquals(spl_object_hash($sc->getService('bar')), spl_object_hash($bar), '->getService() prefers to return a service defined with a getXXXService() method than one defined with setService()'); + $this->assertEquals(spl_object_hash($sc->getService('bar')), spl_object_hash($bar), '->getService() prefers to return a service defined with a getXXXService() method than one defined with setService()'); try { diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Dumper/PhpDumperTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/PhpDumperTest.php index ecdbdaf35e..93a52ae5d3 100644 --- a/tests/Symfony/Tests/Components/DependencyInjection/Dumper/PhpDumperTest.php +++ b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/PhpDumperTest.php @@ -57,4 +57,17 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase { } } + + public function testOverrideServiceWhenUsingADumpedContainer() + { + require_once self::$fixturesPath.'/php/services9.php'; + require_once self::$fixturesPath.'/includes/foo.php'; + + $container = new \ProjectServiceContainer(); + $container->setService('bar', $bar = new \stdClass()); + $container->setParameter('foo_bar', 'foo_bar'); + + $this->assertEquals($bar, $container->getBarService(), '->setService() overrides an already defined service'); + $this->assertEquals($bar, $container->getService('bar'), '->setService() overrides an already defined service'); + } }