summaryrefslogtreecommitdiff
path: root/tests/Unit/Container/ContainerTest.php
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2017-09-30 11:25:58 +0200
committerGitHub <noreply@github.com>2017-09-30 11:25:58 +0200
commit801c17aa6cef91be988a25a90442be8d2078a70d (patch)
tree7f03157616509fe583b1b0aea6e2d06c53bd9c89 /tests/Unit/Container/ContainerTest.php
parente1d44e60e35d126dbd05aefa5f897fad16fbfceb (diff)
parent945fcb079a219fa29e8f6ee1afc3f1c0c5c822cb (diff)
Merge pull request #347 from MyIgel/master
Implemented Container, closes #342
Diffstat (limited to 'tests/Unit/Container/ContainerTest.php')
-rw-r--r--tests/Unit/Container/ContainerTest.php104
1 files changed, 104 insertions, 0 deletions
diff --git a/tests/Unit/Container/ContainerTest.php b/tests/Unit/Container/ContainerTest.php
new file mode 100644
index 00000000..89c34209
--- /dev/null
+++ b/tests/Unit/Container/ContainerTest.php
@@ -0,0 +1,104 @@
+<?php
+
+namespace Engelsystem\Test\Config;
+
+use Engelsystem\Container\Container;
+use PHPUnit\Framework\TestCase;
+
+class ContainerTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Container\Container::get
+ */
+ public function testGet()
+ {
+ $container = new Container();
+ $class = new class
+ {
+ };
+
+ $container->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()
+ {
+ // 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();
+
+ $this->assertNotSame($container0, $container);
+
+ $container1 = new Container;
+ Container::setInstance($container1);
+
+ $this->assertSame($container1, Container::getInstance());
+ }
+}