From 2cb636b651c889243919d99eda8fa724d5c08392 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 19 Sep 2017 21:50:22 +0200 Subject: Added Container unit test --- tests/Unit/Container/ContainerTest.php | 98 ++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 tests/Unit/Container/ContainerTest.php (limited to 'tests/Unit/Container/ContainerTest.php') diff --git a/tests/Unit/Container/ContainerTest.php b/tests/Unit/Container/ContainerTest.php new file mode 100644 index 00000000..f0ba24e7 --- /dev/null +++ b/tests/Unit/Container/ContainerTest.php @@ -0,0 +1,98 @@ +instance('foo', $class); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::get + * @expectedException \Engelsystem\Container\NotFoundException + */ + public function testGetException() + { + $container = new Container(); + + $container->get('not.registered.service'); + } + + /** + * @covers \Engelsystem\Container\Container::instance + * @covers \Engelsystem\Container\Container::resolve + */ + public function testInstance() + { + $container = new Container(); + $class = new class + { + }; + + $container->instance('foo', $class); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::has + */ + public function testHas() + { + $container = new Container(); + + $this->assertFalse($container->has('test')); + + $class = new class + { + }; + + $container->instance('test', $class); + $this->assertTrue($container->has('test')); + } + + /** + * @covers \Engelsystem\Container\Container::singleton + */ + public function testSingleton() + { + $container = new Container(); + $class = new class + { + }; + + $container->singleton('foo', $class); + $this->assertSame($class, $container->get('foo')); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::setInstance + * @covers \Engelsystem\Container\Container::getInstance + */ + public function testContainerSingleton() + { + $container0 = new Container(); + $container = Container::getInstance(); + + $this->assertNotSame($container0, $container); + + $container1 = new Container; + Container::setInstance($container1); + + $this->assertSame($container1, Container::getInstance()); + } +} -- cgit v1.2.3-54-g00ecf From 3141c3f8b5fd1a45d337c1f5540cb715a5a7dbbf Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 20 Sep 2017 01:21:36 +0200 Subject: Fixed Container test --- phpunit.xml | 1 - tests/Unit/Container/ContainerTest.php | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'tests/Unit/Container/ContainerTest.php') diff --git a/phpunit.xml b/phpunit.xml index 92dc5a5b..bdc4b0b6 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -14,7 +14,6 @@ ./include/ - ./public/ ./src/ diff --git a/tests/Unit/Container/ContainerTest.php b/tests/Unit/Container/ContainerTest.php index f0ba24e7..89c34209 100644 --- a/tests/Unit/Container/ContainerTest.php +++ b/tests/Unit/Container/ContainerTest.php @@ -85,6 +85,12 @@ class ContainerTest extends TestCase */ public function testContainerSingleton() { + // Ensure that no container has been initialized + $reflection = new \ReflectionProperty(Container::class, 'instance'); + $reflection->setAccessible(true); + $reflection->setValue(null, null); + $reflection->setAccessible(false); + $container0 = new Container(); $container = Container::getInstance(); -- cgit v1.2.3-54-g00ecf