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

View File

@ -1,17 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<phpunit <phpunit
colors="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
bootstrap="tests/bootstrap.php" colors="true"
beStrictAboutTestsThatDoNotTestAnything="true"> stopOnFailure="false"
<filter> bootstrap="tests/bootstrap.php"
<whitelist> beStrictAboutTestsThatDoNotTestAnything="true">
<directory suffix=".php">src</directory> <filter>
</whitelist> <whitelist>
</filter> <directory suffix=".php">src</directory>
<testsuites> </whitelist>
<testsuite name="Ion"> </filter>
<directory>tests</directory> <testsuites>
</testsuite> <testsuite name="Ion">
</testsuites> <directory>tests</directory>
</testsuite>
</testsuites>
</phpunit> </phpunit>

View File

@ -44,14 +44,25 @@ class Config implements ConfigInterface {
$this->map = $this->arr($configArray); $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 * Get a config value
* *
* @param array|string $key * @param array|string|null $key
* @return mixed * @return mixed
* @throws ConfigException * @throws ConfigException
*/ */
public function get($key) public function get($key = NULL)
{ {
if (\is_array($key)) if (\is_array($key))
{ {

View File

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

View File

@ -211,8 +211,8 @@ class Container implements ContainerInterface {
*/ */
private function applyContainer($obj) private function applyContainer($obj)
{ {
$trait_name = __NAMESPACE__ . '\\ContainerAware'; $trait_name = ContainerAware::class;
$interface_name = __NAMESPACE__ . '\\ContainerAwareInterface'; $interface_name = ContainerAwareInterface::class;
$uses_trait = \in_array($trait_name, class_uses($obj), TRUE); $uses_trait = \in_array($trait_name, class_uses($obj), TRUE);
$implements_interface = \in_array($interface_name, class_implements($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 * Determine whether a logger channel is registered
*
* @param string $id The logger channel * @param string $id The logger channel
* @return boolean * @return boolean
*/ */

View File

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

View File

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

View File

@ -18,8 +18,10 @@ namespace Aviat\Ion\Transformer;
use Aviat\Ion\StringWrapper; use Aviat\Ion\StringWrapper;
use BadMethodCallException;
/** /**
* Base class for data trasformation * Base class for data transformation
*/ */
abstract class AbstractTransformer implements TransformerInterface { abstract class AbstractTransformer implements TransformerInterface {
@ -36,13 +38,32 @@ abstract class AbstractTransformer implements TransformerInterface {
/** /**
* Transform a set of structures * Transform a set of structures
* *
* @param array|object $collection * @param iterable $collection
* @return array * @return array
*/ */
public function transformCollection($collection): array public function transformCollection(iterable $collection): array
{ {
$list = (array)$collection; $list = (array)$collection;
return array_map([$this, 'transform'], $list); 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 // End of AbstractTransformer.php

View File

@ -114,11 +114,28 @@ class ArrayType {
/** /**
* Does the passed key exist in the current array? * Does the passed key exist in the current array?
* *
* @param int|string $key * @param int|string|array $key
* @return bool * @return bool
*/ */
public function hasKey($key): 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); return array_key_exists($key, $this->arr);
} }

View File

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

View File

@ -25,10 +25,24 @@ class ConfigTest extends Ion_TestCase {
$this->config = new Config([ $this->config = new Config([
'foo' => 'bar', 'foo' => 'bar',
'asset_path' => '/assets', '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() public function testConfigGet()
{ {
$this->assertEquals('bar', $this->config->get('foo')); $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', $apple['sauce']['is'], "Config value not set correctly");
$this->assertEquals('great', $this->config->get(['apple', 'sauce', 'is']), "Array argument get for config failed."); $this->assertEquals('great', $this->config->get(['apple', 'sauce', 'is']), "Array argument get for config failed.");
} }
public function testConfigBadSet() public function testConfigBadSet()

View File

@ -17,16 +17,18 @@
namespace Aviat\Ion\Tests\Transformer; namespace Aviat\Ion\Tests\Transformer;
use Aviat\Ion\Tests\Ion_TestCase; use Aviat\Ion\Tests\Ion_TestCase;
use Aviat\Ion\Tests\TestTransformer; use Aviat\Ion\Tests\{TestTransformer, TestTransformerUntransform};
class AbstractTransformerTest extends Ion_TestCase { class AbstractTransformerTest extends Ion_TestCase {
protected $transformer; protected $transformer;
protected $untransformer;
public function setUp() public function setUp()
{ {
$this->transformer = new TestTransformer(); $this->transformer = new TestTransformer();
$this->untransformer = new TestTransformerUntransform();
} }
public function dataTransformCollection() 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() public function testTransform()
{ {
$data = $this->dataTransformCollection(); $data = $this->dataTransformCollection();
@ -105,4 +137,22 @@ class AbstractTransformerTest extends Ion_TestCase {
$actual = $this->transformer->transformCollection($original); $actual = $this->transformer->transformCollection($original);
$this->assertEquals($expected, $actual); $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')); $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() public function testHas()
{ {
$obj = $this->arr([1, 2, 6, 8, 11]); $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 { trait MockViewOutputTrait {
/*protected function output() { /*protected function output() {
$reflect = new ReflectionClass($this); $reflect = new ReflectionClass($this);