Version 5.1 - All the GraphQL #32
@ -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.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
27
src/Aviat/Ion/Transformer/AbstractTransformer.php
Normal file
27
src/Aviat/Ion/Transformer/AbstractTransformer.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Aviat\Ion\Transformer;
|
||||
|
||||
abstract class AbstractTransformer implements TransformerInterface {
|
||||
|
||||
/**
|
||||
* Mutate the data structure
|
||||
*
|
||||
* @param array|object $item
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function transform($item);
|
||||
|
||||
/**
|
||||
* Transform a set of structures
|
||||
*
|
||||
* @param array|object $collection
|
||||
* @return array
|
||||
*/
|
||||
public function transform_collection($collection)
|
||||
{
|
||||
$list = (array) $collection;
|
||||
return array_map([$this, 'transform'], $list);
|
||||
}
|
||||
}
|
||||
// End of AbstractTransformer.php
|
14
src/Aviat/Ion/Transformer/TransformerInterface.php
Normal file
14
src/Aviat/Ion/Transformer/TransformerInterface.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Aviat\Ion\Transformer;
|
||||
|
||||
interface TransformerInterface {
|
||||
|
||||
/**
|
||||
* Mutate the data structure
|
||||
*
|
||||
* @param array|object $item
|
||||
* @return mixed
|
||||
*/
|
||||
public function transform($item);
|
||||
}
|
39
tests/Ion/Di/ContainerAwareTest.php
Normal file
39
tests/Ion/Di/ContainerAwareTest.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
use Aviat\Ion\Di\Container;
|
||||
use Aviat\Ion\Di\ContainerAware;
|
||||
use Aviat\Ion\Di\ContainerInterface;
|
||||
use Aviat\Ion\Di\Exception\ContainerException;
|
||||
|
||||
class Aware {
|
||||
use ContainerAware;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->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());
|
||||
}
|
||||
}
|
51
tests/Ion/Di/ContainerTest.php
Normal file
51
tests/Ion/Di/ContainerTest.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
use Aviat\Ion\Di\Container;
|
||||
use Aviat\Ion\Di\Exception\ContainerException;
|
||||
|
||||
|
||||
class ContainerTest extends AnimeClient_TestCase {
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
107
tests/Ion/Transformer/AbstractTransformerTest.php
Normal file
107
tests/Ion/Transformer/AbstractTransformerTest.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
use Aviat\Ion\Transformer\AbstractTransformer;
|
||||
|
||||
class TestTransformer extends AbstractTransformer {
|
||||
|
||||
public function transform($item)
|
||||
{
|
||||
$out = [];
|
||||
$genre_list = (array) $item;
|
||||
|
||||
foreach($genre_list as $genre)
|
||||
{
|
||||
$out[] = $genre['name'];
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
class AbstractTransformerTest extends AnimeClient_TestCase {
|
||||
|
||||
protected $transformer;
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user