More tests for Ion

This commit is contained in:
Timothy Warren 2015-09-18 22:55:40 -04:00
parent 60109b3531
commit f87dd2636d
6 changed files with 239 additions and 3 deletions

View File

@ -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.");
}
/**

View 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

View 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);
}

View 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());
}
}

View 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());
}
}
}

View 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);
}
}