Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] more check and test for filter processors #15 #27

Merged
merged 2 commits into from Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Reader/Iterable/Processor/Equals.php
Expand Up @@ -15,6 +15,9 @@ public function getOperator(): string

public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) !== 2) {
throw new \RuntimeException('$arguments should contain exactly two elements!');
}
[$field, $value] = $arguments;
return $item[$field] == $value;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Reader/Iterable/Processor/GreaterThan.php
Expand Up @@ -15,6 +15,9 @@ public function getOperator(): string

public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) !== 2) {
throw new \RuntimeException('$arguments should contain exactly two elements!');
}
[$field, $value] = $arguments;
return $item[$field] > $value;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Reader/Iterable/Processor/GreaterThanOrEqual.php
Expand Up @@ -15,6 +15,9 @@ public function getOperator(): string

public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) !== 2) {
throw new \RuntimeException('$arguments should contain exactly two elements!');
}
[$field, $value] = $arguments;
return $item[$field] >= $value;
}
Expand Down
23 changes: 19 additions & 4 deletions src/Reader/Iterable/Processor/GroupProcessor.php
Expand Up @@ -17,17 +17,32 @@ abstract protected function checkResult($result): ?bool;
*/
public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) < 1) {
throw new \RuntimeException('At least one argument should be provided!');
} elseif (!is_array($arguments[0])) {
throw new \RuntimeException('Sub filters is not an array!');
}
$results = [];
foreach ($arguments[0] as $subFilter) {
$operation = array_shift($subFilter);
if (!is_array($subFilter)) {
throw new \RuntimeException('Sub filter is not an array!');
} elseif (count($subFilter) < 1) {
throw new \RuntimeException('At least operator should be provided!');
}
$operator = array_shift($subFilter);
if (!is_string($operator)) {
throw new \RuntimeException('Operator is not a string!');
} elseif (strlen($operator) === 0) {
throw new \RuntimeException('The operator string cannot be empty!');
}

$processor = $filterProcessors[$operation] ?? null;
$processor = $filterProcessors[$operator] ?? null;
if ($processor === null) {
throw new \RuntimeException(sprintf('Operation "%s" is not supported', $operation));
throw new \RuntimeException(sprintf('"%s" operator is not supported!', $operator));
}
/* @var $processor IterableProcessorInterface */
$result = $processor->match($item, $subFilter, $filterProcessors);
if(is_bool($this->checkResult($result))) {
if (is_bool($this->checkResult($result))) {
return $result;
}
$results[] = $result;
Expand Down
6 changes: 6 additions & 0 deletions src/Reader/Iterable/Processor/In.php
Expand Up @@ -15,7 +15,13 @@ public function getOperator(): string

public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) !== 2) {
throw new \RuntimeException('$arguments should contain exactly two elements!');
}
[$field, $values] = $arguments;
if (!is_array($values)) {
throw new \RuntimeException('The values not an array!');
samdark marked this conversation as resolved.
Show resolved Hide resolved
}
return in_array($item[$field], $values, false);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Reader/Iterable/Processor/LessThan.php
Expand Up @@ -15,6 +15,9 @@ public function getOperator(): string

public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) !== 2) {
throw new \RuntimeException('$arguments should contain exactly two elements!');
}
[$field, $value] = $arguments;
return $item[$field] < $value;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Reader/Iterable/Processor/LessThanOrEqual.php
Expand Up @@ -15,6 +15,9 @@ public function getOperator(): string

public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) !== 2) {
throw new \RuntimeException('$arguments should contain exactly two elements!');
}
[$field, $value] = $arguments;
return $item[$field] <= $value;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Reader/Iterable/Processor/Like.php
Expand Up @@ -15,6 +15,9 @@ public function getOperator(): string

public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) !== 2) {
throw new \RuntimeException('$arguments should contain exactly two elements!');
}
[$field, $value] = $arguments;
return stripos($item[$field], $value) !== false;
}
Expand Down
20 changes: 16 additions & 4 deletions src/Reader/Iterable/Processor/Not.php
Expand Up @@ -15,11 +15,23 @@ public function getOperator(): string

public function match(array $item, array $arguments, array $filterProcessors): bool
{
$operation = array_shift($arguments[0]);
if (count($arguments) !== 1) {
throw new \RuntimeException('$arguments should contain exactly one element!');
} elseif (!is_array($arguments[0])) {
throw new \RuntimeException('$arguments[0] is not an array!');
} elseif (count($arguments[0]) < 1) {
throw new \RuntimeException('At least operator should be provided!');
}
$operator = array_shift($arguments[0]);
if (!is_string($operator)) {
throw new \RuntimeException('Operator is not a string!');
} elseif (strlen($operator) === 0) {
throw new \RuntimeException('The operator string cannot be empty!');
}

$processor = $filterProcessors[$operation] ?? null;
if($processor === null) {
throw new \RuntimeException(sprintf('Operation "%s" is not supported', $operation));
$processor = $filterProcessors[$operator] ?? null;
if ($processor === null) {
throw new \RuntimeException(sprintf('"%s" operator is not supported!', $operator));
}
/* @var $processor IterableProcessorInterface */
return !$processor->match($item, $arguments[0], $filterProcessors);
Expand Down
47 changes: 45 additions & 2 deletions tests/Reader/FilterProcessorTest.php
Expand Up @@ -3,6 +3,15 @@

namespace Yiisoft\Data\Tests\Reader;

use Yiisoft\Data\Reader\Iterable\Processor\All;
use Yiisoft\Data\Reader\Iterable\Processor\GreaterThan;
use Yiisoft\Data\Reader\Iterable\Processor\GreaterThanOrEqual;
use Yiisoft\Data\Reader\Iterable\Processor\In;
use Yiisoft\Data\Reader\Iterable\Processor\IterableProcessorInterface;
use Yiisoft\Data\Reader\Iterable\Processor\LessThan;
use Yiisoft\Data\Reader\Iterable\Processor\LessThanOrEqual;
use Yiisoft\Data\Reader\Iterable\Processor\Like;
use Yiisoft\Data\Reader\Iterable\Processor\Not;
use Yiisoft\Data\Tests\TestCase;
use Yiisoft\Data\Reader\Iterable\Processor\Equals;
use Yiisoft\Data\Reader\Iterable\IterableDataReader;
Expand Down Expand Up @@ -43,10 +52,11 @@ public function testCustomEquals(): void

$dataReader = (new IterableDataReader($this->getDataSet()))
->withSort($sort)
->withFilterProcessors(new class extends Equals {
->withFilterProcessors(new class extends Equals
{
public function match(array $item, array $arguments, array $filterUnits): bool
{
[$field, ] = $arguments;
[$field,] = $arguments;
if ($item[$field] === 2) {
return true;
}
Expand All @@ -64,4 +74,37 @@ public function match(array $item, array $arguments, array $filterUnits): bool

$this->assertSame($expected, $this->iterableToArray($dataReader->read()));
}

public function invalidFiltersArrayDataProvider()
{
return [
'equalsArgumentsTooSmall' => [new Equals(), ['id'], []],
'greaterThanArgumentsTooSmall' => [new GreaterThan(), ['id'], []],
'greaterThanOrEqualArgumentsTooSmall' => [new GreaterThanOrEqual(), ['id'], []],
'lessThanArgumentsTooSmall' => [new LessThan(), ['id'], []],
'lessThanOrEqualArgumentsTooSmall' => [new LessThanOrEqual(), ['id'], []],
'likeArgumentsTooSmall' => [new Like(), ['id'], []],
'inArgumentsTooSmall' => [new In(), ['id'], []],
'inValuesNotArray' => [new In(), ['id', false], []],
'notArgumentsTooSmall' => [new Not(), [], []],
'notArguments[0]notArray' => [new Not(), [false], []],
'notArguments[0]tooSmall' => [new Not(), [[]], []],
'notInvalidOperator' => [new Not(), [['']], ['=' => new Equals()]],
'groupInvalidArguments' => [new All(), [], []],
'groupInvalidArgumentsNotArray' => [new All(), [false], []],
'groupInvalidSubFilter' => [new All(), [[false]], []],
'groupInvalidSubFilterCountFail' => [new All(), [[[]]], []],
'groupInvalidSubFilterOperatorFail' => [new All(), [[['']]], []],
];
}

/**
* @dataProvider invalidFiltersArrayDataProvider
*/
public function testInvalidFiltersArray(IterableProcessorInterface $processor, $arguments, array $filterProcessors)
{
$item = $this->getDataSet()[0];
$this->expectException(\RuntimeException::class);
$processor->match($item, $arguments, $filterProcessors);
}
}
18 changes: 17 additions & 1 deletion tests/Reader/IterableDataReaderTest.php
Expand Up @@ -393,7 +393,8 @@ public function testGeneratorAsDataSet(): void

public function testCustomFilter(): void
{
$digitalFilter = new class /*Digital*/ ('name') implements FilterInterface
$digitalFilter = new class /*Digital*/
('name') implements FilterInterface
{
private $field;

Expand Down Expand Up @@ -432,4 +433,19 @@ protected function matchFilter(array $item, array $filter): bool
$this->assertCount(1, $filtered);
$this->assertSame('007', $filtered[0]['name']);
}

public function testNotSupportedOperator()
{
$dataReader = (new IterableDataReader($this->getDataSet()))
->withFilter(new class('id', 2) extends \Yiisoft\Data\Reader\Filter\Equals
{
public static function getOperator(): string
{
return '----';
}
});
$this->expectException(\RuntimeException::class);
$dataReader->read();
}

}