Add some new helper methods, with updated tests

This commit is contained in:
Timothy Warren 2018-10-05 11:32:12 -04:00
parent 77ebf107de
commit c8856ecac0
15 changed files with 192 additions and 34 deletions

View File

@ -21,9 +21,10 @@
"aura/html": "2.*",
"container-interop/container-interop": "1.*",
"danielstjules/stringy": "^3.0.0",
"ext-json": "*",
"psr/http-message": "~1.0",
"psr/log": "~1.0",
"zendframework/zend-diactoros": "^1.4.0"
"zendframework/zend-diactoros": "^2.0.0"
},
"require-dev": {
"aura/session": "^2.1.0",
@ -33,9 +34,9 @@
"phploc/phploc": "^4.0",
"phpmd/phpmd": "^2.4",
"phpstan/phpstan": "^0.9.1",
"phpunit/phpunit": "^6.0",
"robmorgan/phinx": "^0.9.2",
"sebastian/phpcpd": "^3.0",
"phpunit/phpunit": "^6.5.13",
"robmorgan/phinx": "^0.10.6",
"sebastian/phpcpd": "^3.0.1",
"squizlabs/php_codesniffer": "^3.0.0",
"theseer/phpdox": "^0.11.0"
},
@ -44,7 +45,7 @@
"build": "robo build",
"docs": "cd build && ../vendor/bin/phpdox && cd ..",
"phpstan": "phpstan analyse -l 7 -c phpstan.neon src tests",
"test": "phpunit"
"test": "phpunit -c phpunit.dist.xml"
},
"suggest": {
"monolog/monolog": "Provides implementation of psr/log"

View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
colors="true"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"

View File

@ -44,14 +44,25 @@ class Config implements ConfigInterface {
$this->map = $this->arr($configArray);
}
/**
* Does the config item exist?
*
* @param string|int|array $key
* @return bool
*/
public function has($key): bool
{
return $this->map->hasKey($key);
}
/**
* Get a config value
*
* @param array|string $key
* @param array|string|null $key
* @return mixed
* @throws ConfigException
*/
public function get($key)
public function get($key = NULL)
{
if (\is_array($key))
{

View File

@ -20,13 +20,21 @@ namespace Aviat\Ion;
* Standard interface for retrieving/setting configuration values
*/
interface ConfigInterface {
/**
* Does the config item exist?
*
* @param string|int|array $key
* @return bool
*/
public function has($key): bool;
/**
* Get a config value
*
* @param array|string $key
* @param array|string|null $key
* @return mixed
*/
public function get($key);
public function get($key = NULL);
/**
* Set a config value

View File

@ -211,8 +211,8 @@ class Container implements ContainerInterface {
*/
private function applyContainer($obj)
{
$trait_name = __NAMESPACE__ . '\\ContainerAware';
$interface_name = __NAMESPACE__ . '\\ContainerAwareInterface';
$trait_name = ContainerAware::class;
$interface_name = ContainerAwareInterface::class;
$uses_trait = \in_array($trait_name, class_uses($obj), TRUE);
$implements_interface = \in_array($interface_name, class_implements($obj), TRUE);

View File

@ -75,6 +75,7 @@ interface ContainerInterface {
/**
* Determine whether a logger channel is registered
*
* @param string $id The logger channel
* @return boolean
*/

View File

@ -27,6 +27,7 @@ abstract class Enum {
* Return the list of constant values for the Enum
*
* @return array
* @throws \ReflectionException
*/
public static function getConstList(): array
{
@ -47,6 +48,7 @@ abstract class Enum {
*
* @param mixed $key
* @return boolean
* @throws \ReflectionException
*/
public static function isValid($key): bool
{

View File

@ -44,6 +44,7 @@ class Friend {
*
* @param mixed $obj
* @throws InvalidArgumentException
* @throws \ReflectionException
*/
public function __construct($obj)
{
@ -115,6 +116,7 @@ class Friend {
* @param array $args
* @return mixed
* @throws BadMethodCallException
* @throws \ReflectionException
*/
public function __call(string $method, array $args)
{

View File

@ -18,8 +18,10 @@ namespace Aviat\Ion\Transformer;
use Aviat\Ion\StringWrapper;
use BadMethodCallException;
/**
* Base class for data trasformation
* Base class for data transformation
*/
abstract class AbstractTransformer implements TransformerInterface {
@ -36,13 +38,32 @@ abstract class AbstractTransformer implements TransformerInterface {
/**
* Transform a set of structures
*
* @param array|object $collection
* @param iterable $collection
* @return array
*/
public function transformCollection($collection): array
public function transformCollection(iterable $collection): array
{
$list = (array)$collection;
return array_map([$this, 'transform'], $list);
}
/**
* Untransform a set of structures
*
* Requires an 'untransform' method in the extending class
*
* @param iterable $collection
* @return array
*/
public function untransformCollection(iterable $collection): array
{
if ( ! method_exists($this, 'untransform'))
{
throw new BadMethodCallException('untransform() method does not exist.');
}
$list = (array)$collection;
return array_map([$this, 'untransform'], $list);
}
}
// End of AbstractTransformer.php

View File

@ -114,11 +114,28 @@ class ArrayType {
/**
* Does the passed key exist in the current array?
*
* @param int|string $key
* @param int|string|array $key
* @return bool
*/
public function hasKey($key): bool
{
if (\is_array($key))
{
$pos =& $this->arr;
foreach($key as $level)
{
if ( ! array_key_exists($level, $pos))
{
return FALSE;
}
$pos =& $pos[$level];
}
return TRUE;
}
return array_key_exists($key, $this->arr);
}

View File

@ -20,7 +20,7 @@ namespace Aviat\Ion;
* Joins paths together. Variadic to take an
* arbitrary number of arguments
*
* @param string[] ...$args
* @param string ...$args
* @return string
*/
function _dir(string ...$args): string

View File

@ -25,10 +25,24 @@ class ConfigTest extends Ion_TestCase {
$this->config = new Config([
'foo' => 'bar',
'asset_path' => '/assets',
'bar' => 'baz'
'bar' => 'baz',
'a' => [
'b' => [
'c' => TRUE,
],
],
]);
}
public function testConfigHas()
{
$this->assertTrue($this->config->has('foo'));
$this->assertTrue($this->config->has(['a', 'b', 'c']));
$this->assertFalse($this->config->has('baz'));
$this->assertFalse($this->config->has(['c', 'b', 'a']));
}
public function testConfigGet()
{
$this->assertEquals('bar', $this->config->get('foo'));
@ -48,7 +62,6 @@ class ConfigTest extends Ion_TestCase {
$this->assertEquals('great', $apple['sauce']['is'], "Config value not set correctly");
$this->assertEquals('great', $this->config->get(['apple', 'sauce', 'is']), "Array argument get for config failed.");
}
public function testConfigBadSet()

View File

@ -17,16 +17,18 @@
namespace Aviat\Ion\Tests\Transformer;
use Aviat\Ion\Tests\Ion_TestCase;
use Aviat\Ion\Tests\TestTransformer;
use Aviat\Ion\Tests\{TestTransformer, TestTransformerUntransform};
class AbstractTransformerTest extends Ion_TestCase {
protected $transformer;
protected $untransformer;
public function setUp()
{
$this->transformer = new TestTransformer();
$this->untransformer = new TestTransformerUntransform();
}
public function dataTransformCollection()
@ -87,6 +89,36 @@ class AbstractTransformerTest extends Ion_TestCase {
];
}
public function dataUnTransformCollection()
{
return [
'object' => [
'original' => [
(object)['Comedy', 'Romance', 'School', 'Harem'],
(object)['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'],
(object)['Comedy', 'Sci-Fi']
],
'expected' => [
['Comedy', 'Romance', 'School', 'Harem'],
['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'],
['Comedy', 'Sci-Fi']
]
],
'array' => [
'original' => [
['Comedy', 'Romance', 'School', 'Harem'],
['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'],
['Comedy', 'Sci-Fi']
],
'expected' => [
['Comedy', 'Romance', 'School', 'Harem'],
['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'],
['Comedy', 'Sci-Fi']
]
]
];
}
public function testTransform()
{
$data = $this->dataTransformCollection();
@ -105,4 +137,22 @@ class AbstractTransformerTest extends Ion_TestCase {
$actual = $this->transformer->transformCollection($original);
$this->assertEquals($expected, $actual);
}
/**
* @dataProvider dataUnTransformCollection
*/
public function testUntransformCollection($original, $expected)
{
$actual = $this->untransformer->untransformCollection($original);
$this->assertEquals($expected, $actual);
}
/**
* @dataProvider dataUnTransformCollection
*/
public function testUntransformCollectionWithException($original, $expected)
{
$this->expectException(\BadMethodCallException::class);
$this->transformer->untransformCollection($original);
}
}

View File

@ -169,6 +169,29 @@ class ArrayTypeTest extends Ion_TestCase {
$this->assertFalse($obj->hasKey('b'));
}
public function testHasKeyArray()
{
$obj = $this->arr([
'foo' => [
'bar' => [
'baz' => [
'foobar' => NULL,
'one' => 1,
],
],
],
]);
$this->assertTrue($obj->hasKey(['foo']));
$this->assertTrue($obj->hasKey(['foo', 'bar']));
$this->assertTrue($obj->hasKey(['foo', 'bar', 'baz']));
$this->assertTrue($obj->hasKey(['foo', 'bar', 'baz', 'one']));
$this->assertTrue($obj->hasKey(['foo', 'bar', 'baz', 'foobar']));
$this->assertFalse($obj->hasKey(['foo', 'baz']));
$this->assertFalse($obj->hasKey(['bar', 'baz']));
}
public function testHas()
{
$obj = $this->arr([1, 2, 6, 8, 11]);

View File

@ -81,6 +81,13 @@ class TestTransformer extends AbstractTransformer {
}
}
class TestTransformerUntransform extends TestTransformer {
public function untransform($item)
{
return (array)$item;
}
}
trait MockViewOutputTrait {
/*protected function output() {
$reflect = new ReflectionClass($this);