From d9028db4ddae33f49b2a01bad2473a805d2d868f Mon Sep 17 00:00:00 2001 From: Yuen-Chi Lian Date: Sun, 20 Feb 2011 02:30:12 +0800 Subject: [PATCH 1/2] [DependencyInjection] Test coverage improvements and corrections on Container. --- .../DependencyInjection/ContainerTest.php | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) mode change 100644 => 100755 tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php diff --git a/tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php b/tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php old mode 100644 new mode 100755 index 68ac876aeef6d..843068655d240 --- a/tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php +++ b/tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php @@ -33,7 +33,7 @@ public function testConstructor() /** * @covers Symfony\Component\DependencyInjection\Container::compile */ - public function testcompile() + public function testCompile() { $sc = new Container(new ParameterBag(array('foo' => 'bar'))); $sc->compile(); @@ -98,7 +98,7 @@ public function testGetServiceIds() $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids'); $sc = new ProjectServiceContainer(); - $this->assertEquals(array('scoped', 'scoped_foo', 'bar', 'foo_bar', 'foo.baz', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods'); + $this->assertEquals(array('scoped', 'scoped_foo', 'bar', 'foo_bar', 'foo.baz', 'circular', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods'); } /** @@ -155,7 +155,7 @@ public function testGet() $this->assertEquals($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined'); $sc->set('bar', $bar = new \stdClass()); - $this->assertSame($sc->get('bar'), $bar, '->getServiceIds() prefers to return a service defined with a getXXXService() method than one defined with set()'); + $this->assertEquals($bar, $sc->get('bar'), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()'); try { $sc->get(''); @@ -167,6 +167,19 @@ public function testGet() $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE)); } + public function testGetCircularReference() + { + + $sc = new ProjectServiceContainer(); + try { + $sc->get('circular'); + $this->fail('->get() throws a \LogicException if it contains circular reference'); + } catch (\Exception $e) { + $this->assertInstanceOf('\LogicException', $e, '->get() throws a \LogicException if it contains circular reference'); + $this->assertStringStartsWith('Circular reference detected for service "circular"', $e->getMessage(), '->get() throws a \LogicException if it contains circular reference'); + } + } + /** * @covers Symfony\Component\DependencyInjection\Container::has */ @@ -237,6 +250,28 @@ public function testEnterLeaveScopeWithChildScopes() $this->assertFalse($container->has('a')); } + public function testLeaveScopeNotActive() + { + $container = new Container(); + $container->addScope(new Scope('foo')); + + try { + $container->leaveScope('foo'); + $this->fail('->leaveScope() throws a \LogicException if the scope is not active yet'); + } catch (\Exception $e) { + $this->assertInstanceOf('\LogicException', $e, '->leaveScope() throws a \LogicException if the scope is not active yet'); + $this->assertEquals('The scope "foo" is not active.', $e->getMessage(), '->leaveScope() throws a \LogicException if the scope is not active yet'); + } + + try { + $container->leaveScope('bar'); + $this->fail('->leaveScope() throws a \LogicException if the scope does not exist'); + } catch (\Exception $e) { + $this->assertInstanceOf('\LogicException', $e, '->leaveScope() throws a \LogicException if the scope does not exist'); + $this->assertEquals('The scope "bar" is not active.', $e->getMessage(), '->leaveScope() throws a \LogicException if the scope does not exist'); + } + } + /** * @expectedException \InvalidArgumentException * @dataProvider getBuiltInScopes @@ -372,4 +407,8 @@ protected function getFoo_BazService() { return $this->__foo_baz; } + + protected function getCircularService(){ + return $this->get('circular'); + } } From aefc703f43218d38720b8619ef76b8df75b0ad1c Mon Sep 17 00:00:00 2001 From: Yuen-Chi Lian Date: Sun, 20 Feb 2011 02:33:39 +0800 Subject: [PATCH 2/2] [DependencyInjection] Aesthetic changes on ContainerTest, consequence of not using a formatter. --- .../Tests/Component/DependencyInjection/ContainerTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php b/tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php index 843068655d240..b829313b1acab 100755 --- a/tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php +++ b/tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php @@ -408,7 +408,8 @@ protected function getFoo_BazService() return $this->__foo_baz; } - protected function getCircularService(){ + protected function getCircularService() + { return $this->get('circular'); } }