diff --git a/src/Aviat/Ion/Di/Container.php b/src/Aviat/Ion/Di/Container.php index 098cfd3d..b80716cd 100644 --- a/src/Aviat/Ion/Di/Container.php +++ b/src/Aviat/Ion/Di/Container.php @@ -3,8 +3,6 @@ namespace Aviat\Ion\Di; use ArrayObject; -use Aviat\Ion\Di\Exception\ContainerException; -use Aviat\Ion\Di\Exception\NotFoundException; /** * Dependency container @@ -50,7 +48,7 @@ class Container implements ContainerInterface { return $this->container[$id]; } - throw new Exception\NotFoundException("Item {$id} does not exist in container."); + throw new Exception\NotFoundException("Item '{$id}' does not exist in container."); } /** diff --git a/src/Aviat/Ion/Transformer/AbstractTransformer.php b/src/Aviat/Ion/Transformer/AbstractTransformer.php new file mode 100644 index 00000000..ed6997a2 --- /dev/null +++ b/src/Aviat/Ion/Transformer/AbstractTransformer.php @@ -0,0 +1,27 @@ +container = $container; + } +} + + +class ContainerAwareTest extends AnimeClient_TestCase { + + public function setUp() + { + $this->container = new Container(); + $this->aware = new Aware($this->container); + } + + public function testContainerAwareTrait() + { + // The container was set in setup + // check that the get method returns the same + $this->assertSame($this->container, $this->aware->getContainer()); + + $container2 = new Container([ + 'foo' => 'bar', + 'baz' => 'foobar' + ]); + $this->aware->setContainer($container2); + $this->assertSame($container2, $this->aware->getContainer()); + } +} \ No newline at end of file diff --git a/tests/Ion/Di/ContainerTest.php b/tests/Ion/Di/ContainerTest.php new file mode 100644 index 00000000..f01cca74 --- /dev/null +++ b/tests/Ion/Di/ContainerTest.php @@ -0,0 +1,51 @@ +container = new Container(); + } + + public function dataGetWithException() + { + return [ + 'Bad index type: number' => [ + 'id' => 42, + 'exception' => 'Aviat\Ion\Di\Exception\ContainerException', + 'message' => 'Id must be a string' + ], + 'Bad index type: array' => [ + 'id' => [], + 'exception' => 'Aviat\Ion\Di\Exception\ContainerException', + 'message' => 'Id must be a string' + ], + 'Non-existent id' => [ + 'id' => 'foo', + 'exception' => 'Aviat\Ion\Di\Exception\NotFoundException', + 'message' => "Item 'foo' does not exist in container." + ] + ]; + } + + /** + * @dataProvider dataGetWithException + */ + public function testGetWithException($id, $exception, $message) + { + try + { + $this->container->get($id); + } + catch(ContainerException $e) + { + $this->assertInstanceOf($exception, $e); + $this->assertEquals($message, $e->getMessage()); + } + } + +} \ No newline at end of file diff --git a/tests/Ion/Transformer/AbstractTransformerTest.php b/tests/Ion/Transformer/AbstractTransformerTest.php new file mode 100644 index 00000000..56c1104d --- /dev/null +++ b/tests/Ion/Transformer/AbstractTransformerTest.php @@ -0,0 +1,107 @@ +transformer = new TestTransformer(); + } + + public function dataTransformCollection() + { + return [ + 'object' => [ + 'original' => [ + (object)[ + ['name' => 'Comedy'], + ['name' => 'Romance'], + ['name' => 'School'], + ['name' => 'Harem'] + ], + (object)[ + ['name' => 'Action'], + ['name' => 'Comedy'], + ['name' => 'Magic'], + ['name' => 'Fantasy'], + ['name' => 'Mahou Shoujo'] + ], + (object)[ + ['name' => 'Comedy'], + ['name' => 'Sci-Fi'] + ] + ], + 'expected' => [ + ['Comedy', 'Romance', 'School', 'Harem'], + ['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'], + ['Comedy', 'Sci-Fi'] + ] + ], + 'array' => [ + 'original' => [ + [ + ['name' => 'Comedy'], + ['name' => 'Romance'], + ['name' => 'School'], + ['name' => 'Harem'] + ], + [ + ['name' => 'Action'], + ['name' => 'Comedy'], + ['name' => 'Magic'], + ['name' => 'Fantasy'], + ['name' => 'Mahou Shoujo'] + ], + [ + ['name' => 'Comedy'], + ['name' => 'Sci-Fi'] + ] + ], + 'expected' => [ + ['Comedy', 'Romance', 'School', 'Harem'], + ['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'], + ['Comedy', 'Sci-Fi'] + ] + ], + ]; + } + + public function testTransform() + { + $data = $this->dataTransformCollection(); + $original = $data['object']['original'][0]; + $expected = $data['object']['expected'][0]; + + $actual = $this->transformer->transform($original); + $this->assertEquals($expected, $actual); + } + + /** + * @dataProvider dataTransformCollection + */ + public function testTransformCollection($original, $expected) + { + $actual = $this->transformer->transform_collection($original); + $this->assertEquals($expected, $actual); + } +} \ No newline at end of file