From 4a699f424a07ffc1a24aa5e49e6784a374834552 Mon Sep 17 00:00:00 2001 From: Anthony Moutte Date: Fri, 8 Dec 2023 19:39:00 +0100 Subject: [PATCH] Drop php7 and drop doctrine/mongodb --- .gitignore | 1 + composer.json | 31 +- phpunit.xml.dist | 37 +- .../Common/ExpressionBuilderAdapter.php | 133 +--- src/Bridge/Doctrine/MongoDb/ExprBuilder.php | 228 ------ src/Bridge/Doctrine/ORM/ExprAdapter.php | 136 +--- src/Bridge/MongoDB/ExprBuilder.php | 108 +++ src/Expr/ClosureExpressionBuilder.php | 118 +--- src/Expr/ExpressionBuilderInterface.php | 100 +-- src/Expr/HtmlExpressionBuilder.php | 165 +---- src/Expr/MapperExpressionBuilder.php | 34 +- src/Parser.php | 49 +- .../Common/ExpressionBuilderAdapterTest.php | 130 ++-- tests/Bridge/Doctrine/Common/ParserTest.php | 176 +++-- .../Doctrine/MongoDb/ExprBuilderTest.php | 285 -------- tests/Bridge/Doctrine/MongoDb/ParserTest.php | 212 ------ tests/Bridge/Doctrine/ORM/ExprAdapterTest.php | 141 ++-- tests/Bridge/Doctrine/ORM/ParserTest.php | 191 +++-- tests/Bridge/MongoDB/ExprBuilderTest.php | 131 ++++ tests/Bridge/MongoDB/ParserTest.php | 230 ++++++ tests/Expr/ClosureExpressionBuilderTest.php | 345 ++++----- tests/Expr/HtmlExpressionBuilderTest.php | 185 +++-- tests/Expr/MapperExpressionBuilderTest.php | 140 ++-- tests/LexerTest.php | 338 +++++---- tests/ParserTest.php | 652 ++++++++---------- tests/QueryStringParserTest.php | 212 +++--- 26 files changed, 1754 insertions(+), 2754 deletions(-) delete mode 100644 src/Bridge/Doctrine/MongoDb/ExprBuilder.php create mode 100644 src/Bridge/MongoDB/ExprBuilder.php delete mode 100644 tests/Bridge/Doctrine/MongoDb/ExprBuilderTest.php delete mode 100644 tests/Bridge/Doctrine/MongoDb/ParserTest.php create mode 100644 tests/Bridge/MongoDB/ExprBuilderTest.php create mode 100644 tests/Bridge/MongoDB/ParserTest.php diff --git a/.gitignore b/.gitignore index 7579f74..50b321e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor composer.lock +.phpunit.result.cache diff --git a/composer.json b/composer.json index 6a6543f..b0cd9cd 100644 --- a/composer.json +++ b/composer.json @@ -8,24 +8,37 @@ ], "type": "library", "description": "Xpression is a simple PHP implementation of Specification pattern", - "keywords": ["dsl", "doctrine", "orm", "mongodb", "collections", "specification", "expression", "parser", "lexer", "query", "builder", "query builder"], + "keywords": [ + "dsl", + "doctrine", + "orm", + "mongodb", + "collections", + "specification", + "expression", + "parser", + "lexer", + "query", + "builder", + "query builder" + ], "homepage": "https://github.com/symftony/Xpression", "require": { - "php": ">=7.0", + "php": "^8.0", "doctrine/lexer": "^1.0" }, - "scripts": { - "test": "vendor/bin/phpunit" - }, "require-dev": { - "phpunit/phpunit": "^5", "doctrine/collections": "^1.0", - "doctrine/orm": "^2.4.0" + "doctrine/orm": "^2.4.0", + "phpspec/prophecy": "^1.18", + "phpunit/phpunit": "^9.6" + }, + "scripts": { + "test": "vendor/bin/phpunit" }, "suggest": { "doctrine/collections": "If you want filter an ArrayCollection", - "doctrine/orm": "If you want filter an ORM query builder", - "doctrine/mongodb": "If you want filter an Mongodb query builder" + "doctrine/orm": "If you want filter an ORM query builder" }, "license": "MIT", "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f62072a..f865b8e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,29 +1,26 @@ - - - - - - - - tests - - - - - - src - - src/Exception - - - + + + src + + + src/Exception + + + + + + + + tests + + diff --git a/src/Bridge/Doctrine/Common/ExpressionBuilderAdapter.php b/src/Bridge/Doctrine/Common/ExpressionBuilderAdapter.php index 4f59bfa..1869139 100644 --- a/src/Bridge/Doctrine/Common/ExpressionBuilderAdapter.php +++ b/src/Bridge/Doctrine/Common/ExpressionBuilderAdapter.php @@ -2,8 +2,6 @@ namespace Symftony\Xpression\Bridge\Doctrine\Common; -use Doctrine\Common\Collections\Expr\Comparison; -use Doctrine\Common\Collections\Expr\CompositeExpression; use Doctrine\Common\Collections\ExpressionBuilder; use Symftony\Xpression\Expr\ExpressionBuilderInterface; use Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException; @@ -11,151 +9,77 @@ class ExpressionBuilderAdapter implements ExpressionBuilderInterface { - /** - * @var ExpressionBuilder - */ - private $expressionBuilder; + private ExpressionBuilder $expressionBuilder; public function __construct(ExpressionBuilder $expressionBuilder) { $this->expressionBuilder = $expressionBuilder; } - /** - * @return int - */ - public function getSupportedTokenType() + public function getSupportedTokenType(): int { return Lexer::T_ALL - Lexer::T_NOT_AND - Lexer::T_NOT_OR - Lexer::T_XOR - Lexer::T_NOT_DOUBLE_OPEN_CURLY_BRACKET; } - /** - * @param $value - * @param bool $isValue - * - * @return mixed - */ - public function parameter($value, $isValue = false) + public function parameter(mixed $value, bool $isValue = false): mixed { return $value; } - /** - * @param $value - * @return mixed - */ - public function string($value) + public function string($value): mixed { return $value; } - /** - * @param string $field - * - * @return Comparison - */ - public function isNull($field) + public function isNull(string $field) { return $this->expressionBuilder->isNull($field); } - /** - * @param string $field - * @param mixed $value - * - * @return Comparison - */ - public function eq($field, $value) + public function eq(string $field, mixed $value) { return $this->expressionBuilder->eq($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Comparison - */ - public function neq($field, $value) + public function neq(string $field, mixed $value) { return $this->expressionBuilder->neq($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Comparison - */ - public function gt($field, $value) + public function gt(string $field, mixed $value) { return $this->expressionBuilder->gt($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Comparison - */ - public function gte($field, $value) + public function gte(string $field, mixed $value) { return $this->expressionBuilder->gte($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Comparison - */ - public function lt($field, $value) + public function lt(string $field, mixed $value) { return $this->expressionBuilder->lt($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Comparison - */ - public function lte($field, $value) + public function lte(string $field, mixed $value) { return $this->expressionBuilder->lte($field, $value); } - /** - * @param string $field - * @param mixed $values - * - * @return Comparison - */ - public function in($field, array $values) + public function in(string $field, array $values) { return $this->expressionBuilder->in($field, $values); } - /** - * @param string $field - * @param mixed $values - * - * @return Comparison - */ - public function notIn($field, array $values) + public function notIn(string $field, array $values) { return $this->expressionBuilder->notIn($field, $values); } /** * /!\ Contains operator appear only in doctrine/common v1.1 /!\ - * - * @param string $field - * @param mixed $value - * - * @return Comparison */ - public function contains($field, $value) + public function contains(string $field, mixed $value) { if (!method_exists($this->expressionBuilder, 'contains')) { throw new UnsupportedExpressionTypeException('contains'); @@ -164,54 +88,31 @@ public function contains($field, $value) return $this->expressionBuilder->contains($field, $value); } - /** - * @param string $field - * @param mixed $value - */ - public function notContains($field, $value) + public function notContains(string $field, mixed $value) { throw new UnsupportedExpressionTypeException('notContains'); } - /** - * @param array $expressions - * - * @return CompositeExpression - */ public function andX(array $expressions) { - return call_user_func_array(array($this->expressionBuilder, 'andX'), $expressions); + return call_user_func_array([$this->expressionBuilder, 'andX'], $expressions); } - /** - * @param array $expressions - */ public function nandX(array $expressions) { throw new UnsupportedExpressionTypeException('nandX'); } - /** - * @param array $expressions - * - * @return CompositeExpression - */ public function orX(array $expressions) { - return call_user_func_array(array($this->expressionBuilder, 'orX'), $expressions); + return call_user_func_array([$this->expressionBuilder, 'orX'], $expressions); } - /** - * @param array $expressions - */ public function norX(array $expressions) { throw new UnsupportedExpressionTypeException('norX'); } - /** - * @param array $expressions - */ public function xorX(array $expressions) { throw new UnsupportedExpressionTypeException('xorX'); diff --git a/src/Bridge/Doctrine/MongoDb/ExprBuilder.php b/src/Bridge/Doctrine/MongoDb/ExprBuilder.php deleted file mode 100644 index 8d49666..0000000 --- a/src/Bridge/Doctrine/MongoDb/ExprBuilder.php +++ /dev/null @@ -1,228 +0,0 @@ -createExpr()->field($field)->equals(null); - } - - /** - * @param string $field - * @param mixed $value - * - * @return array - */ - public function eq($field, $value) - { - return $this->createExpr()->field($field)->equals($value); - } - - /** - * @param string $field - * @param mixed $value - * - * @return array - */ - public function neq($field, $value) - { - return $this->createExpr()->field($field)->notEqual($value); - } - - /** - * @param string $field - * @param mixed $value - * - * @return array - */ - public function gt($field, $value) - { - return $this->createExpr()->field($field)->gt($value); - } - - /** - * @param string $field - * @param mixed $value - * - * @return array - */ - public function gte($field, $value) - { - return $this->createExpr()->field($field)->gte($value); - } - - /** - * @param string $field - * @param mixed $value - * - * @return array - */ - public function lt($field, $value) - { - return $this->createExpr()->field($field)->lt($value); - } - - /** - * @param string $field - * @param mixed $value - * - * @return array - */ - public function lte($field, $value) - { - return $this->createExpr()->field($field)->lte($value); - } - - /** - * @param string $field - * @param mixed $values - * - * @return array - */ - public function in($field, array $values) - { - return $this->createExpr()->field($field)->in($values); - } - - /** - * @param string $field - * @param mixed $values - * - * @return array - */ - public function notIn($field, array $values) - { - return $this->createExpr()->field($field)->notIn($values); - } - - /** - * @param string $field - * @param mixed $value - * - * @return array - */ - public function contains($field, $value) - { - return $this->createExpr()->field($field)->equals(new \MongoRegex(sprintf('/.*%s.*/', $value))); - } - - /** - * @param string $field - * @param mixed $value - * - * @return array - */ - public function notContains($field, $value) - { - return $this->createExpr()->field($field)->equals(new \MongoRegex(sprintf('/^((?!%s).)*$/', $value))); - } - - /** - * @param array $expressions - * - * @return array - */ - public function andX(array $expressions) - { - $expr = $this->createExpr(); - foreach ($expressions as $expression) { - $expr->addAnd($expression); - } - - return $expr; - } - - /** - * @param array $expressions - */ - public function nandX(array $expressions) - { - throw new UnsupportedExpressionTypeException('nandX'); - } - - /** - * @param array $expressions - * - * @return array - */ - public function orX(array $expressions) - { - $expr = $this->createExpr(); - foreach ($expressions as $expression) { - $expr->addOr($expression); - } - - return $expr; - } - - /** - * @param array $expressions - * - * @return array - */ - public function norX(array $expressions) - { - $expr = $this->createExpr(); - foreach ($expressions as $expression) { - $expr->addNor($expression); - } - - return $expr; - } - - /** - * @param array $expressions - */ - public function xorX(array $expressions) - { - throw new UnsupportedExpressionTypeException('xorX'); - } -} diff --git a/src/Bridge/Doctrine/ORM/ExprAdapter.php b/src/Bridge/Doctrine/ORM/ExprAdapter.php index c9cd2de..3a1bf59 100644 --- a/src/Bridge/Doctrine/ORM/ExprAdapter.php +++ b/src/Bridge/Doctrine/ORM/ExprAdapter.php @@ -3,213 +3,109 @@ namespace Symftony\Xpression\Bridge\Doctrine\ORM; use Doctrine\ORM\Query\Expr; -use Doctrine\ORM\Query\Parameter; use Symftony\Xpression\Expr\ExpressionBuilderInterface; use Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException; use Symftony\Xpression\Lexer; class ExprAdapter implements ExpressionBuilderInterface { - /** - * @var Expr - */ - private $expr; - - /** - * @param Expr $expr - */ + private Expr $expr; + public function __construct(Expr $expr) { $this->expr = $expr; } - /** - * @return int - */ - public function getSupportedTokenType() + public function getSupportedTokenType(): int { return Lexer::T_ALL - Lexer::T_NOT_AND - Lexer::T_NOT_OR - Lexer::T_XOR; } - /** - * @param $value - * @param bool $isValue - * - * @return mixed - */ - public function parameter($value, $isValue = false) + public function parameter(mixed $value, bool $isValue = false): mixed { return $isValue ? $this->expr->literal($value) : $value; } - /** - * @param $value - * @return Expr\Literal - */ - public function string($value) + public function string($value): mixed { return $this->expr->literal($value); } - /** - * @param string $field - * - * @return string - */ - public function isNull($field) + public function isNull(string $field) { return $this->expr->isNull($field); } - /** - * @param string $field - * @param mixed $value - * - * @return Expr\Comparison - */ - public function eq($field, $value) + public function eq(string $field, mixed $value) { return $this->expr->eq($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Expr\Comparison - */ - public function neq($field, $value) + public function neq(string $field, mixed $value) { return $this->expr->neq($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Expr\Comparison - */ - public function gt($field, $value) + public function gt(string $field, mixed $value) { return $this->expr->gt($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Expr\Comparison - */ - public function gte($field, $value) + public function gte(string $field, mixed $value) { return $this->expr->gte($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Expr\Comparison - */ - public function lt($field, $value) + public function lt(string $field, mixed $value) { return $this->expr->lt($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Expr\Comparison - */ - public function lte($field, $value) + public function lte(string $field, mixed $value) { return $this->expr->lte($field, $value); } - /** - * @param string $field - * @param mixed $values - * - * @return Expr\Func - */ public function in($field, array $values) { return $this->expr->in($field, $values); } - /** - * @param string $field - * @param mixed $values - * - * @return Expr\Func - */ public function notIn($field, array $values) { return $this->expr->notIn($field, $values); } - /** - * @param string $field - * @param mixed $value - * - * @return Expr\Comparison - */ - public function contains($field, $value) + public function contains(string $field, mixed $value) { return $this->expr->like($field, $value); } - /** - * @param string $field - * @param mixed $value - * - * @return Expr\Comparison - */ - public function notContains($field, $value) + public function notContains(string $field, mixed $value) { return $this->expr->notLike($field, $value); } - /** - * @param array $expressions - * - * @return Expr\Andx - */ public function andX(array $expressions) { - return call_user_func_array(array($this->expr, 'andX'), $expressions); + return call_user_func_array([$this->expr, 'andX'], $expressions); } - /** - * @param array $expressions - */ public function nandX(array $expressions) { throw new UnsupportedExpressionTypeException('nandX'); } - /** - * @param array $expressions - * - * @return Expr\Orx - */ public function orX(array $expressions) { - return call_user_func_array(array($this->expr, 'orX'), $expressions); + return call_user_func_array([$this->expr, 'orX'], $expressions); } - /** - * @param array $expressions - */ public function norX(array $expressions) { throw new UnsupportedExpressionTypeException('norX'); } - /** - * @param array $expressions - */ public function xorX(array $expressions) { throw new UnsupportedExpressionTypeException('xorX'); diff --git a/src/Bridge/MongoDB/ExprBuilder.php b/src/Bridge/MongoDB/ExprBuilder.php new file mode 100644 index 0000000..f41130f --- /dev/null +++ b/src/Bridge/MongoDB/ExprBuilder.php @@ -0,0 +1,108 @@ + null]; + } + + public function eq(string $field, mixed $value) + { + return [$field => ['$eq' => $value]]; + } + + public function neq(string $field, mixed $value) + { + return [$field => ['$ne' => $value]]; + } + + public function gt(string $field, mixed $value) + { + return [$field => ['$gt' => $value]]; + } + + public function gte(string $field, mixed $value) + { + return [$field => ['$gte' => $value]]; + } + + public function lt(string $field, mixed $value) + { + return [$field => ['$lt' => $value]]; + } + + public function lte(string $field, mixed $value) + { + return [$field => ['$lte' => $value]]; + } + + public function in($field, array $values) + { + return [$field => ['$in' => $values]]; + } + + public function notIn($field, array $values) + { + return [$field => ['$nin' => $values]]; + } + + public function contains(string $field, mixed $value) + { + return [$field => ['$regex' => $value]]; + } + + public function notContains(string $field, mixed $value) + { + return ['$not' => $this->contains($field, $value)]; + } + + public function andX(array $expressions) + { + return ['$and' => $expressions]; + } + + // Not A AND B = Not A OR Not B + public function nandX(array $expressions) + { + return $this->orX(array_map(function ($expression) { + return ['$not' => $expression]; + }, $expressions)); + } + + public function orX(array $expressions) + { + return ['$or' => $expressions]; + } + + public function norX(array $expressions) + { + return ['$nor' => $expressions]; + } + + public function xorX(array $expressions) + { + throw new UnsupportedExpressionTypeException('xorX'); + } +} diff --git a/src/Expr/ClosureExpressionBuilder.php b/src/Expr/ClosureExpressionBuilder.php index 89f1798..5a10bfc 100644 --- a/src/Expr/ClosureExpressionBuilder.php +++ b/src/Expr/ClosureExpressionBuilder.php @@ -18,7 +18,7 @@ public static function getObjectFieldValue($object, $field) return $object[$field]; } - $accessors = array('get', 'is'); + $accessors = ['get', 'is']; foreach ($accessors as $accessor) { $accessor .= $field; @@ -67,7 +67,7 @@ public static function getObjectFieldValue($object, $field) /** * @return int */ - public function getSupportedTokenType() + public function getSupportedTokenType(): int { return Lexer::T_ALL; } @@ -78,25 +78,16 @@ public function getSupportedTokenType() * * @return mixed */ - public function parameter($value, $isValue = false) + public function parameter($value, $isValue = false): mixed { return $value; } - /** - * @param $value - * @return mixed - */ - public function string($value) + public function string($value): mixed { return $value; } - /** - * @param string $field - * - * @return callable - */ public function isNull($field) { return function ($object) use ($field) { @@ -104,90 +95,48 @@ public function isNull($field) }; } - /** - * @param string $field - * @param mixed $value - * - * @return callable - */ - public function eq($field, $value) + public function eq(string $field, mixed $value) { return function ($object) use ($field, $value) { return ClosureExpressionBuilder::getObjectFieldValue($object, $field) === $value; }; } - /** - * @param string $field - * @param mixed $value - * - * @return callable - */ - public function neq($field, $value) + public function neq(string $field, mixed $value) { return function ($object) use ($field, $value) { return ClosureExpressionBuilder::getObjectFieldValue($object, $field) !== $value; }; } - /** - * @param string $field - * @param mixed $value - * - * @return callable - */ - public function gt($field, $value) + public function gt(string $field, mixed $value) { return function ($object) use ($field, $value) { return ClosureExpressionBuilder::getObjectFieldValue($object, $field) > $value; }; } - /** - * @param string $field - * @param mixed $value - * - * @return callable - */ - public function gte($field, $value) + public function gte(string $field, mixed $value) { return function ($object) use ($field, $value) { return ClosureExpressionBuilder::getObjectFieldValue($object, $field) >= $value; }; } - /** - * @param string $field - * @param mixed $value - * - * @return callable - */ - public function lt($field, $value) + public function lt(string $field, mixed $value) { return function ($object) use ($field, $value) { return ClosureExpressionBuilder::getObjectFieldValue($object, $field) < $value; }; } - /** - * @param string $field - * @param mixed $value - * - * @return callable - */ - public function lte($field, $value) + public function lte(string $field, mixed $value) { return function ($object) use ($field, $value) { return ClosureExpressionBuilder::getObjectFieldValue($object, $field) <= $value; }; } - /** - * @param string $field - * @param mixed $values - * - * @return callable - */ public function in($field, array $values) { return function ($object) use ($field, $values) { @@ -195,12 +144,6 @@ public function in($field, array $values) }; } - /** - * @param string $field - * @param mixed $values - * - * @return callable - */ public function notIn($field, array $values) { return function ($object) use ($field, $values) { @@ -208,26 +151,14 @@ public function notIn($field, array $values) }; } - /** - * @param string $field - * @param mixed $value - * - * @return callable - */ - public function contains($field, $value) + public function contains(string $field, mixed $value) { return function ($object) use ($field, $value) { return false !== strpos(ClosureExpressionBuilder::getObjectFieldValue($object, $field), $value); }; } - /** - * @param string $field - * @param mixed $value - * - * @return callable - */ - public function notContains($field, $value) + public function notContains(string $field, mixed $value) { $self = $this; return function ($object) use ($self, $field, $value) { @@ -236,11 +167,6 @@ public function notContains($field, $value) }; } - /** - * @param array $expressions - * - * @return callable - */ public function andX(array $expressions) { return function ($object) use ($expressions) { @@ -254,11 +180,6 @@ public function andX(array $expressions) }; } - /** - * @param array $expressions - * - * @return callable - */ public function nandX(array $expressions) { $self = $this; @@ -268,11 +189,6 @@ public function nandX(array $expressions) }; } - /** - * @param array $expressions - * - * @return callable - */ public function orX(array $expressions) { return function ($object) use ($expressions) { @@ -286,11 +202,6 @@ public function orX(array $expressions) }; } - /** - * @param array $expressions - * - * @return callable - */ public function norX(array $expressions) { $self = $this; @@ -300,11 +211,6 @@ public function norX(array $expressions) }; } - /** - * @param array $expressions - * - * @return callable - */ public function xorX(array $expressions) { return function ($object) use ($expressions) { diff --git a/src/Expr/ExpressionBuilderInterface.php b/src/Expr/ExpressionBuilderInterface.php index 5cbce80..19ca0bb 100644 --- a/src/Expr/ExpressionBuilderInterface.php +++ b/src/Expr/ExpressionBuilderInterface.php @@ -2,115 +2,49 @@ namespace Symftony\Xpression\Expr; +use Doctrine\Common\Collections\Expr\CompositeExpression; +use Doctrine\Common\Collections\Expr\Expression; + interface ExpressionBuilderInterface { /** * Must return all supported token type - * - * @return int */ - public function getSupportedTokenType(); + public function getSupportedTokenType(): int; - /** - * @param $value - * @param bool $isValue - * @return mixed - */ - public function parameter($value, $isValue = false); + public function parameter(mixed $value, bool $isValue = false): mixed; - /** - * @param $value - * @return mixed - */ - public function string($value); + public function string($value): mixed; - /** - * @param string $field - */ - public function isNull($field); + public function isNull(string $field); - /** - * @param string $field - * @param mixed $value - */ - public function eq($field, $value); + public function eq(string $field, mixed $value); - /** - * @param string $field - * @param mixed $value - */ - public function neq($field, $value); + public function neq(string $field, mixed $value); - /** - * @param string $field - * @param mixed $value - */ - public function gt($field, $value); + public function gt(string $field, mixed $value); - /** - * @param string $field - * @param mixed $value - */ - public function gte($field, $value); + public function gte(string $field, mixed $value); - /** - * @param string $field - * @param mixed $value - */ - public function lt($field, $value); + public function lt(string $field, mixed $value); - /** - * @param string $field - * @param mixed $value - */ - public function lte($field, $value); + public function lte(string $field, mixed $value); - /** - * @param string $field - * @param mixed $values - */ - public function in($field, array $values); + public function in(string $field, array $values); - /** - * @param string $field - * @param mixed $values - */ - public function notIn($field, array $values); + public function notIn(string $field, array $values); - /** - * @param string $field - * @param mixed $value - */ - public function contains($field, $value); + public function contains(string $field, mixed $value); - /** - * @param string $field - * @param mixed $value - */ - public function notContains($field, $value); + public function notContains(string $field, mixed $value); - /** - * @param array $expressions - */ public function andX(array $expressions); - /** - * @param array $expressions - */ public function nandX(array $expressions); - /** - * @param array $expressions - */ public function orX(array $expressions); - /** - * @param array $expressions - */ public function norX(array $expressions); - /** - * @param array $expressions - */ public function xorX(array $expressions); } diff --git a/src/Expr/HtmlExpressionBuilder.php b/src/Expr/HtmlExpressionBuilder.php index 9f3eaad..b019788 100644 --- a/src/Expr/HtmlExpressionBuilder.php +++ b/src/Expr/HtmlExpressionBuilder.php @@ -25,8 +25,8 @@ class HtmlExpressionBuilder implements ExpressionBuilderInterface private $compositeHtmlBuilder; /** - * @param $comparisonHtmlBuilder - * @param $compositeHtmlBuilder + * @param callable $comparisonHtmlBuilder + * @param callable $compositeHtmlBuilder */ public function __construct(callable $comparisonHtmlBuilder = null, callable $compositeHtmlBuilder = null) { @@ -35,208 +35,105 @@ public function __construct(callable $comparisonHtmlBuilder = null, callable $co }; $this->compositeHtmlBuilder = $compositeHtmlBuilder ?: function (array $expressions, $type) { return str_replace( - array('{type}', '{expressions}'), - array($type, implode('', $expressions)), + ['{type}', '{expressions}'], + [$type, implode('', $expressions)], '
{type}{expressions}
' ); }; } - /** - * @return int - */ - public function getSupportedTokenType() + public function getSupportedTokenType(): int { return Lexer::T_ALL; } - /** - * @param $value - * @param bool $isValue - * - * @return mixed - */ - public function parameter($value, $isValue = false) + public function parameter(mixed $value, bool $isValue = false): mixed { return $value; } - /** - * @param $value - * @return mixed - */ - public function string($value) + public function string($value): mixed { return '"' . $value . '"'; } - /** - * @param string $field - * - * @return string - */ public function isNull($field) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, 'is', 'null')); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, 'is', 'null']); } - /** - * @param string $field - * @param mixed $value - * - * @return string - */ - public function eq($field, $value) + public function eq(string $field, mixed $value) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, '=', $value)); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, '=', $value]); } - /** - * @param string $field - * @param mixed $value - * - * @return string - */ - public function neq($field, $value) + public function neq(string $field, mixed $value) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, '≠', $value)); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, '≠', $value]); } - /** - * @param string $field - * @param mixed $value - * - * @return string - */ - public function gt($field, $value) + public function gt(string $field, mixed $value) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, '>', $value)); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, '>', $value]); } - /** - * @param string $field - * @param mixed $value - * - * @return string - */ - public function gte($field, $value) + public function gte(string $field, mixed $value) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, '≥', $value)); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, '≥', $value]); } - /** - * @param string $field - * @param mixed $value - * - * @return string - */ - public function lt($field, $value) + public function lt(string $field, mixed $value) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, '<', $value)); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, '<', $value]); } - /** - * @param string $field - * @param mixed $value - * - * @return string - */ - public function lte($field, $value) + public function lte(string $field, mixed $value) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, '≤', $value)); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, '≤', $value]); } - /** - * @param string $field - * @param mixed $values - * - * @return string - */ public function in($field, array $values) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, 'value in', implode(', ', $values))); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, 'value in', implode(', ', $values)]); } - /** - * @param string $field - * @param mixed $values - * - * @return string - */ public function notIn($field, array $values) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, 'value not in', implode(', ', $values))); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, 'value not in', implode(', ', $values)]); } - /** - * @param string $field - * @param mixed $value - * - * @return string - */ - public function contains($field, $value) + public function contains(string $field, mixed $value) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, 'contains', $value)); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, 'contains', $value]); } - /** - * @param string $field - * @param mixed $value - * - * @return string - */ - public function notContains($field, $value) + public function notContains(string $field, mixed $value) { - return call_user_func_array($this->comparisonHtmlBuilder, array($field, 'notContains', $value)); + return call_user_func_array($this->comparisonHtmlBuilder, [$field, 'notContains', $value]); } - /** - * @param array $expressions - * - * @return string - */ public function andX(array $expressions) { - return call_user_func_array($this->compositeHtmlBuilder, array($expressions, 'and')); + return call_user_func_array($this->compositeHtmlBuilder, [$expressions, 'and']); } - /** - * @param array $expressions - * - * @return string - */ public function nandX(array $expressions) { - return call_user_func_array($this->compositeHtmlBuilder, array($expressions, 'not-and')); + return call_user_func_array($this->compositeHtmlBuilder, [$expressions, 'not-and']); } - /** - * @param array $expressions - * - * @return string - */ public function orX(array $expressions) { - return call_user_func_array($this->compositeHtmlBuilder, array($expressions, 'or')); + return call_user_func_array($this->compositeHtmlBuilder, [$expressions, 'or']); } - /** - * @param array $expressions - * - * @return string - */ public function norX(array $expressions) { - return call_user_func_array($this->compositeHtmlBuilder, array($expressions, 'not-or')); + return call_user_func_array($this->compositeHtmlBuilder, [$expressions, 'not-or']); } - /** - * @param array $expressions - * - * @return string - */ public function xorX(array $expressions) { - return call_user_func_array($this->compositeHtmlBuilder, array($expressions, 'exclusive-or')); + return call_user_func_array($this->compositeHtmlBuilder, [$expressions, 'exclusive-or']); } } diff --git a/src/Expr/MapperExpressionBuilder.php b/src/Expr/MapperExpressionBuilder.php index fc6d6ae..5e4c5c1 100644 --- a/src/Expr/MapperExpressionBuilder.php +++ b/src/Expr/MapperExpressionBuilder.php @@ -4,15 +4,9 @@ class MapperExpressionBuilder implements ExpressionBuilderInterface { - /** - * @var ExpressionBuilderInterface - */ - private $expressionBuilder; + private ExpressionBuilderInterface $expressionBuilder; - /** - * @var array - */ - private $fieldMapping; + private array $fieldMapping; /** * @param ExpressionBuilderInterface $expressionBuilder @@ -24,17 +18,17 @@ public function __construct(ExpressionBuilderInterface $expressionBuilder, array $this->fieldMapping = $fieldMapping; } - public function getSupportedTokenType() + public function getSupportedTokenType(): int { return $this->expressionBuilder->getSupportedTokenType(); } - public function parameter($value, $isValue = false) + public function parameter(mixed $value, bool $isValue = false): mixed { return $this->expressionBuilder->parameter($value, $isValue); } - public function string($value) + public function string($value): mixed { return $this->expressionBuilder->string($value); } @@ -44,32 +38,32 @@ public function isNull($field) return $this->expressionBuilder->isNull($this->mapField($field)); } - public function eq($field, $value) + public function eq(string $field, mixed $value) { return $this->expressionBuilder->eq($this->mapField($field), $value); } - public function neq($field, $value) + public function neq(string $field, mixed $value) { return $this->expressionBuilder->neq($this->mapField($field), $value); } - public function gt($field, $value) + public function gt(string $field, mixed $value) { return $this->expressionBuilder->gt($this->mapField($field), $value); } - public function gte($field, $value) + public function gte(string $field, mixed $value) { return $this->expressionBuilder->gte($this->mapField($field), $value); } - public function lt($field, $value) + public function lt(string $field, mixed $value) { return $this->expressionBuilder->lt($this->mapField($field), $value); } - public function lte($field, $value) + public function lte(string $field, mixed $value) { return $this->expressionBuilder->lte($this->mapField($field), $value); } @@ -84,12 +78,12 @@ public function notIn($field, array $values) return $this->expressionBuilder->notIn($this->mapField($field), $values); } - public function contains($field, $value) + public function contains(string $field, mixed $value) { return $this->expressionBuilder->contains($this->mapField($field), $value); } - public function notContains($field, $value) + public function notContains(string $field, mixed $value) { return $this->expressionBuilder->notContains($this->mapField($field), $value); } @@ -138,4 +132,4 @@ private function mapField($field) return $field; } -} \ No newline at end of file +} diff --git a/src/Parser.php b/src/Parser.php index e7dbc97..ec89052 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -2,6 +2,7 @@ namespace Symftony\Xpression; +use Doctrine\Common\Collections\Expr\Expression; use Symftony\Xpression\Exception\Lexer\LexerException; use Symftony\Xpression\Exception\Parser\ForbiddenTokenException; use Symftony\Xpression\Exception\Parser\InvalidExpressionException; @@ -16,38 +17,35 @@ class Parser /** * @var array */ - protected $precedence = array( + protected array $precedence = [ Lexer::T_AND => 15, Lexer::T_NOT_AND => 14, Lexer::T_OR => 10, Lexer::T_XOR => 9, Lexer::T_NOT_OR => 8, - ); + ]; /** * @var int Keep the lexer current index */ - public $lexerIndex = 0; + public int $lexerIndex = 0; - /** - * @var Lexer - */ - private $lexer; + private Lexer $lexer; /** * @var ExpressionBuilderInterface */ - private $expressionBuilder; + private ExpressionBuilderInterface $expressionBuilder; /** * @var int Bitwise of all allowed operator. Default was Lexer::T_ALL */ - private $allowedTokenType; + private int $allowedTokenType; /** * @var int Bitwise of ExpressionBuilder supported operator. */ - private $supportedTokenType; + private int $supportedTokenType; /** * @param ExpressionBuilderInterface $expressionBuilder @@ -58,28 +56,16 @@ public function __construct(ExpressionBuilderInterface $expressionBuilder) $this->expressionBuilder = $expressionBuilder; } - /** - * @return int - */ - public function getAllowedTokenType() + public function getAllowedTokenType(): int { return $this->allowedTokenType; } /** - * @param $input - * @param null $allowedTokenType - * - * @return mixed - * * @throws InvalidExpressionException */ - public function parse($input, $allowedTokenType = null) + public function parse(string $input, ?int $allowedTokenType = null) { - if (null !== $allowedTokenType && !is_integer($allowedTokenType)) { - throw new \InvalidArgumentException('Allowed operator must be an integer.'); - } - $this->allowedTokenType = $allowedTokenType ?: Lexer::T_ALL; $this->supportedTokenType = $this->expressionBuilder->getSupportedTokenType(); @@ -108,7 +94,7 @@ private function getExpression($previousExpression = null) { $expression = $previousExpression ?: null; $expectedTokenType = null !== $previousExpression ? Lexer::T_COMPOSITE : Lexer::T_OPEN_PARENTHESIS | Lexer::T_INPUT_PARAMETER; - $expressions = array(); + $expressions = []; $tokenPrecedence = null; $hasOpenParenthesis = false; @@ -188,8 +174,7 @@ private function getExpression($previousExpression = null) $currentTokenValue = null; break; } - - $expression = call_user_func_array(array($this->expressionBuilder, $comparisonMethod), array($comparisonFirstOperande, $currentTokenValue)); + $expression = call_user_func_array([$this->expressionBuilder, $comparisonMethod], [$comparisonFirstOperande, $currentTokenValue]); $comparisonFirstOperande = null; $comparisonMethod = null; $currentTokenValue = null; @@ -221,16 +206,16 @@ private function getExpression($previousExpression = null) break; case Lexer::T_NOT_OPEN_SQUARE_BRACKET: $comparisonMethod = 'notIn'; - $comparisonMultipleOperande = array(); + $comparisonMultipleOperande = []; $expectedTokenType = Lexer::T_OPERANDE | Lexer::T_CLOSE_SQUARE_BRACKET; break; case Lexer::T_OPEN_SQUARE_BRACKET: $comparisonMethod = 'in'; - $comparisonMultipleOperande = array(); + $comparisonMultipleOperande = []; $expectedTokenType = Lexer::T_OPERANDE | Lexer::T_CLOSE_SQUARE_BRACKET; break; case Lexer::T_CLOSE_SQUARE_BRACKET: - $expression = call_user_func_array(array($this->expressionBuilder, $comparisonMethod), array($comparisonFirstOperande, $comparisonMultipleOperande)); + $expression = call_user_func_array([$this->expressionBuilder, $comparisonMethod], [$comparisonFirstOperande, $comparisonMultipleOperande]); $comparisonMethod = null; $comparisonFirstOperande = null; $comparisonMultipleOperande = false; @@ -247,7 +232,7 @@ private function getExpression($previousExpression = null) $expectedTokenType = Lexer::T_OPERANDE | Lexer::T_DOUBLE_CLOSE_CURLY_BRACKET; break; case Lexer::T_DOUBLE_CLOSE_CURLY_BRACKET: - $expression = call_user_func_array(array($this->expressionBuilder, $comparisonMethod), array($comparisonFirstOperande, $containsValue)); + $expression = call_user_func_array([$this->expressionBuilder, $comparisonMethod], [$comparisonFirstOperande, $containsValue]); $comparisonMethod = null; $comparisonFirstOperande = null; $contains = false; @@ -272,7 +257,7 @@ private function getExpression($previousExpression = null) if ($currentTokenPrecedence < $tokenPrecedence) { $expressions[] = $expression; $expression = null; - $expressions = array($this->buildComposite($compositeOperator, $expressions)); + $expressions = [$this->buildComposite($compositeOperator, $expressions)]; $compositeOperator = $currentTokenType; $tokenPrecedence = $currentTokenPrecedence; $expectedTokenType = Lexer::T_OPEN_PARENTHESIS | Lexer::T_INPUT_PARAMETER; diff --git a/tests/Bridge/Doctrine/Common/ExpressionBuilderAdapterTest.php b/tests/Bridge/Doctrine/Common/ExpressionBuilderAdapterTest.php index f8f4ddd..bcb776d 100644 --- a/tests/Bridge/Doctrine/Common/ExpressionBuilderAdapterTest.php +++ b/tests/Bridge/Doctrine/Common/ExpressionBuilderAdapterTest.php @@ -7,15 +7,13 @@ use Doctrine\Common\Collections\Expr\CompositeExpression; use Doctrine\Common\Collections\ExpressionBuilder; use Symftony\Xpression\Bridge\Doctrine\Common\ExpressionBuilderAdapter; +use Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException; class ExpressionBuilderAdapterTest extends TestCase { - /** - * @var ExpressionBuilderAdapter - */ - private $expressionBuilderAdapter; + private ExpressionBuilderAdapter $expressionBuilderAdapter; - public function setUp() + public function setUp(): void { if (!class_exists('Doctrine\Common\Collections\ExpressionBuilder')) { $this->markTestSkipped('This test is run when you have "doctrine/orm" installed.'); @@ -24,17 +22,17 @@ public function setUp() $this->expressionBuilderAdapter = new ExpressionBuilderAdapter(new ExpressionBuilder()); } - public function testParameter() + public function testParameter(): void { $this->assertEquals('my_fake_data', $this->expressionBuilderAdapter->parameter('my_fake_data')); } - public function testString() + public function testString(): void { $this->assertEquals('my_fake_data', $this->expressionBuilderAdapter->string('my_fake_data')); } - public function testIsNull() + public function testIsNull(): void { $isv0 = !defined('Doctrine\Common\Collections\Expr\Comparison::CONTAINS'); @@ -45,24 +43,21 @@ public function testIsNull() ); } - public function comparisonDataProvider() + public function comparisonDataProvider(): array { if (!class_exists('Doctrine\Common\Collections\ExpressionBuilder')) { - return array(); + return []; } - return array( - array('field', 'value'), - ); + return [ + ['field', 'value'], + ]; } /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testEq($field, $value) + public function testEq(string $field, string $value): void { $this->assertEquals( new Comparison('field', '=', 'value'), @@ -72,11 +67,8 @@ public function testEq($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testNeq($field, $value) + public function testNeq(string $field, string $value): void { $this->assertEquals( new Comparison('field', '<>', 'value'), @@ -86,11 +78,8 @@ public function testNeq($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testGt($field, $value) + public function testGt(string $field, string $value): void { $this->assertEquals( new Comparison('field', '>', 'value'), @@ -100,11 +89,8 @@ public function testGt($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testGte($field, $value) + public function testGte(string $field, string $value): void { $this->assertEquals( new Comparison('field', '>=', 'value'), @@ -114,11 +100,8 @@ public function testGte($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testLt($field, $value) + public function testLt(string $field, string $value): void { $this->assertEquals( new Comparison('field', '<', 'value'), @@ -128,11 +111,8 @@ public function testLt($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testLte($field, $value) + public function testLte(string $field, string $value): void { $this->assertEquals( new Comparison('field', '<=', 'value'), @@ -142,42 +122,33 @@ public function testLte($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testIn($field, $value) + public function testIn(string $field, string $value): void { $this->assertEquals( - new Comparison('field', 'IN', array('value')), - $this->expressionBuilderAdapter->in($field, array($value)) + new Comparison('field', 'IN', ['value']), + $this->expressionBuilderAdapter->in($field, [$value]) ); } /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testNotIn($field, $value) + public function testNotIn(string $field, string $value): void { $this->assertEquals( - new Comparison('field', 'NIN', array('value')), - $this->expressionBuilderAdapter->notIn($field, array($value)) + new Comparison('field', 'NIN', ['value']), + $this->expressionBuilderAdapter->notIn($field, [$value]) ); } /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testContains($field, $value) + public function testContains(string $field, string $value): void { if (!method_exists('Doctrine\Common\Collections\ExpressionBuilder', 'contains')) { - $this->expectException('\Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException'); + $this->expectException(UnsupportedExpressionTypeException::class); $this->expectExceptionMessage('Unsupported expression type "contains".'); $this->assertNull($this->expressionBuilderAdapter->contains($field, $value)); @@ -191,38 +162,33 @@ public function testContains($field, $value) /** * @dataProvider comparisonDataProvider - * @expectedException \Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException - * - * @param $field - * @param $value */ - public function testNotContains($field, $value) + public function testNotContains(string $field, string $value): void { + $this->expectException(UnsupportedExpressionTypeException::class); $this->expressionBuilderAdapter->notContains($field, $value); } - public function compositeDataProvider() + public function compositeDataProvider(): array { if (!class_exists('Doctrine\Common\Collections\ExpressionBuilder')) { - return array(); + return []; } - return array( - array( - array( + return [ + [ + [ new Comparison('fieldA', '=', 1), - new Comparison('fieldB', '>', 2) - ), - ), - ); + new Comparison('fieldB', '>', 2), + ], + ], + ]; } /** * @dataProvider compositeDataProvider - * - * @param array $expressions */ - public function testAndX(array $expressions) + public function testAndX(array $expressions): void { $this->assertEquals( new CompositeExpression('AND', $expressions), @@ -232,21 +198,17 @@ public function testAndX(array $expressions) /** * @dataProvider compositeDataProvider - * @expectedException \Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException - * - * @param array $expressions */ - public function testNandX(array $expressions) + public function testNandX(array $expressions): void { + $this->expectException(UnsupportedExpressionTypeException::class); $this->expressionBuilderAdapter->nandX($expressions); } /** * @dataProvider compositeDataProvider - * - * @param array $expressions */ - public function testOrX(array $expressions) + public function testOrX(array $expressions): void { $this->assertEquals( new CompositeExpression('OR', $expressions), @@ -256,23 +218,19 @@ public function testOrX(array $expressions) /** * @dataProvider compositeDataProvider - * @expectedException \Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException - * - * @param array $expressions */ - public function testNorX(array $expressions) + public function testNorX(array $expressions): void { + $this->expectException(UnsupportedExpressionTypeException::class); $this->expressionBuilderAdapter->norX($expressions); } /** * @dataProvider compositeDataProvider - * @expectedException \Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException - * - * @param array $expressions */ - public function testXorX(array $expressions) + public function testXorX(array $expressions): void { + $this->expectException(UnsupportedExpressionTypeException::class); $this->expressionBuilderAdapter->xorX($expressions); } } diff --git a/tests/Bridge/Doctrine/Common/ParserTest.php b/tests/Bridge/Doctrine/Common/ParserTest.php index 493a5c5..cc84976 100644 --- a/tests/Bridge/Doctrine/Common/ParserTest.php +++ b/tests/Bridge/Doctrine/Common/ParserTest.php @@ -2,11 +2,13 @@ namespace Tests\Symftony\Xpression\Bridge\Doctrine\Common; +use Doctrine\Common\Collections\Expr\Expression; use PHPUnit\Framework\TestCase; use Doctrine\Common\Collections\Expr\Comparison; use Doctrine\Common\Collections\Expr\CompositeExpression; use Doctrine\Common\Collections\ExpressionBuilder; use Symftony\Xpression\Bridge\Doctrine\Common\ExpressionBuilderAdapter; +use Symftony\Xpression\Exception\Parser\InvalidExpressionException; use Symftony\Xpression\Parser; class ParserTest extends TestCase @@ -21,182 +23,176 @@ class ParserTest extends TestCase */ private $parser; - public function setUp() + public function setUp(): void { if (!class_exists('Doctrine\Common\Collections\ExpressionBuilder')) { $this->markTestSkipped('This test is run when you have "doctrine/collection" installed.'); } - $this->expressionBuilderAdapter = new ExpressionBuilderAdapter(new ExpressionBuilder()); $this->parser = new Parser($this->expressionBuilderAdapter); } - public function parseSuccessDataProvider() + public function parseSuccessDataProvider(): array { if (!class_exists('Doctrine\Common\Collections\ExpressionBuilder')) { - return array(); + return []; } - return array( - array( + return [ + [ 'fieldA=1', new Comparison('fieldA', '=', 1), - ), - array( + ], + [ 'fieldA="string"', new Comparison('fieldA', '=', 'string'), - ), - array( + ], + [ 'fieldA≥1', new Comparison('fieldA', '>=', 1), - ), - array( + ], + [ 'fieldA>=1', new Comparison('fieldA', '>=', 1), - ), - array( + ], + [ 'fieldA≤1', new Comparison('fieldA', '<=', 1), - ), - array( + ], + [ 'fieldA<=1', new Comparison('fieldA', '<=', 1), - ), - array( + ], + [ 'fieldA≠1', new Comparison('fieldA', '<>', 1), - ), - array( + ], + [ 'fieldA!=1', new Comparison('fieldA', '<>', 1), - ), - array( + ], + [ 'fieldA[1,2]', - new Comparison('fieldA', 'IN', array(1, 2)), - ), - array( + new Comparison('fieldA', 'IN', [1, 2]), + ], + [ 'fieldA![1,2]', - new Comparison('fieldA', 'NIN', array(1, 2)), - ), - array( + new Comparison('fieldA', 'NIN', [1, 2]), + ], + [ 'fieldA=1|fieldB=2|fieldC=3', - new CompositeExpression('OR', array( + new CompositeExpression('OR', [ new Comparison('fieldA', '=', 1), new Comparison('fieldB', '=', 2), new Comparison('fieldC', '=', 3), - )), - ), - array( + ]), + ], + [ 'fieldA=1&fieldB=2&fieldC=3', - new CompositeExpression('AND', array( + new CompositeExpression('AND', [ new Comparison('fieldA', '=', 1), new Comparison('fieldB', '=', 2), new Comparison('fieldC', '=', 3), - )), - ), + ]), + ], // Precedences - array( + [ 'fieldA=1|fieldB=2|fieldC=3&fieldD=4', - new CompositeExpression('OR', array( + new CompositeExpression('OR', [ new Comparison('fieldA', '=', 1), new Comparison('fieldB', '=', 2), - new CompositeExpression('AND', array( + new CompositeExpression('AND', [ new Comparison('fieldC', '=', 3), new Comparison('fieldD', '=', 4), - )), - )), - ), - array( + ]), + ]), + ], + [ 'fieldA=1&fieldB=2&fieldC=3|fieldD=4', - new CompositeExpression('OR', array( - new CompositeExpression('AND', array( + new CompositeExpression('OR', [ + new CompositeExpression('AND', [ new Comparison('fieldA', '=', 1), new Comparison('fieldB', '=', 2), new Comparison('fieldC', '=', 3), - )), + ]), new Comparison('fieldD', '=', 4), - )), - ), - array( + ]), + ], + [ 'fieldA=1&fieldB=2|fieldC=3&fieldD=4', - new CompositeExpression('OR', array( - new CompositeExpression('AND', array( + new CompositeExpression('OR', [ + new CompositeExpression('AND', [ new Comparison('fieldA', '=', 1), new Comparison('fieldB', '=', 2), - )), - new CompositeExpression('AND', array( + ]), + new CompositeExpression('AND', [ new Comparison('fieldC', '=', 3), new Comparison('fieldD', '=', 4), - )), - )), - ), + ]), + ]), + ], //Parenthesis - array( + [ '((fieldA=1))', new Comparison('fieldA', '=', 1), - ), - array( + ], + [ '(fieldA=1|fieldB=2)&fieldC=3', - new CompositeExpression('AND', array( - new CompositeExpression('OR', array( + new CompositeExpression('AND', [ + new CompositeExpression('OR', [ new Comparison('fieldA', '=', 1), new Comparison('fieldB', '=', 2), - )), + ]), new Comparison('fieldC', '=', 3), - )), - ), - array( + ]), + ], + [ 'fieldA=1|(fieldB=2&fieldC=3)', - new CompositeExpression('OR', array( + new CompositeExpression('OR', [ new Comparison('fieldA', '=', 1), - new CompositeExpression('AND', array( + new CompositeExpression('AND', [ new Comparison('fieldB', '=', 2), new Comparison('fieldC', '=', 3), - )), - )), - ), - ); + ]), + ]), + ], + ]; } /** * @dataProvider parseSuccessDataProvider - * - * @param $input - * @param $expectedExpression */ - public function testParser($input, $expectedExpression) + public function testParser(string $input, Expression $expectedExpression): void { $this->assertEquals($expectedExpression, $this->parser->parse($input)); } - public function unsupportedExpressionTypeDataProvider() + public function unsupportedExpressionTypeDataProvider(): array { if (!class_exists('Doctrine\Common\Collections\ExpressionBuilder')) { - return array(); + return []; } - return array( - array('fieldA=1|fieldB=2|fieldC=3⊕fieldD=4'), - array('fieldA=1!|fieldB=2!|fieldC=3'), - array('fieldA=1^|fieldB=2'), - array('fieldA=1⊕fieldB=2'), - array('fieldA=1!&fieldB=2!&fieldC=3'), - array('fieldA=1&fieldB=2&fieldC=3!&fieldD=4'), - array('fieldA=1|fieldB=2|fieldC=3!|fieldD=4'), - array('fieldA!{{1}}'), - ); + return [ + ['fieldA=1|fieldB=2|fieldC=3⊕fieldD=4'], + ['fieldA=1!|fieldB=2!|fieldC=3'], + ['fieldA=1^|fieldB=2'], + ['fieldA=1⊕fieldB=2'], + ['fieldA=1!&fieldB=2!&fieldC=3'], + ['fieldA=1&fieldB=2&fieldC=3!&fieldD=4'], + ['fieldA=1|fieldB=2|fieldC=3!|fieldD=4'], + ['fieldA!{{1}}'], + ]; } /** * @dataProvider unsupportedExpressionTypeDataProvider - * @expectedException \Symftony\Xpression\Exception\Parser\InvalidExpressionException - * - * @param $input */ - public function testParserThrowUnsupportedExpressionTypeException($input) + public function testParserThrowUnsupportedExpressionTypeException(string $input): void { + $this->expectException(InvalidExpressionException::class); $this->parser->parse($input); } } diff --git a/tests/Bridge/Doctrine/MongoDb/ExprBuilderTest.php b/tests/Bridge/Doctrine/MongoDb/ExprBuilderTest.php deleted file mode 100644 index 4d74928..0000000 --- a/tests/Bridge/Doctrine/MongoDb/ExprBuilderTest.php +++ /dev/null @@ -1,285 +0,0 @@ -markTestSkipped('This test is run when you have "doctrine/mongodb-odm" installed.'); - } - - $this->exprBuilder = new ExprBuilder(); - } - - public function testValueAsString() - { - $this->assertEquals('my_fake_data', $this->exprBuilder->valueAsString('my_fake_data')); - } - - public function testIsNull() - { - $field = 'fake_field'; - $this->assertEquals( - $this->createExpr()->field('fake_field')->equals(null), - $this->exprBuilder->isNull($field) - ); - } - - public function comparisonDataProvider() - { - if (!class_exists('Doctrine\MongoDB\Query\Expr')) { - return array(); - } - - return array( - array('field', 'value'), - ); - } - - /** - * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value - */ - public function testEq($field, $value) - { - $this->assertEquals( - $this->createExpr()->field('field')->equals('value'), - $this->exprBuilder->eq($field, $value) - ); - } - - /** - * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value - */ - public function testNeq($field, $value) - { - $this->assertEquals( - $this->createExpr()->field('field')->notEqual('value'), - $this->exprBuilder->neq($field, $value) - ); - } - - /** - * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value - */ - public function testGt($field, $value) - { - $this->assertEquals( - $this->createExpr()->field('field')->gt('value'), - $this->exprBuilder->gt($field, $value) - ); - } - - /** - * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value - */ - public function testGte($field, $value) - { - $this->assertEquals( - $this->createExpr()->field('field')->gte('value'), - $this->exprBuilder->gte($field, $value) - ); - } - - /** - * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value - */ - public function testLt($field, $value) - { - $this->assertEquals( - $this->createExpr()->field('field')->lt('value'), - $this->exprBuilder->lt($field, $value) - ); - } - - /** - * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value - */ - public function testLte($field, $value) - { - $this->assertEquals( - $this->createExpr()->field('field')->lte('value'), - $this->exprBuilder->lte($field, $value) - ); - } - - /** - * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value - */ - public function testIn($field, $value) - { - $this->assertEquals( - $this->createExpr()->field('field')->in(array('value')), - $this->exprBuilder->in($field, array($value)) - ); - } - - /** - * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value - */ - public function testNotIn($field, $value) - { - $this->assertEquals( - $this->createExpr()->field('field')->notIn(array('value')), - $this->exprBuilder->notIn($field, array($value)) - ); - } - - /** - * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value - */ - public function testContains($field, $value) - { - if (!class_exists('\MongoRegex')) { - $this->markTestSkipped('This test need "\MongoRegex" installed.'); - } - - $this->assertEquals( - $this->createExpr()->field('field')->equals(new \MongoRegex('/.*value.*/')), - $this->exprBuilder->contains($field, $value) - ); - } - - /** - * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value - */ - public function testNotContains($field, $value) - { - if (!class_exists('\MongoRegex')) { - $this->markTestSkipped('This test need "\MongoRegex" installed.'); - } - - $this->assertEquals( - $this->createExpr()->field('field')->equals(new \MongoRegex('/^((?!value).)*$/')), - $this->exprBuilder->notContains($field, $value) - ); - } - - public function compositeDataProvider() - { - if (!class_exists('Doctrine\MongoDB\Query\Expr')) { - return array(); - } - - return array( - array( - array( - array('fieldA' => 'value'), - array('fieldB' => array('$gt' => 2)), - ), - ), - ); - } - - /** - * @dataProvider compositeDataProvider - * - * @param array $expressions - */ - public function testAndX(array $expressions) - { - $this->assertEquals( - $this->createExpr() - ->addAnd($this->createExpr()->field('fieldA')->equals('value')) - ->addAnd($this->createExpr()->field('fieldB')->gt(2)), - $this->exprBuilder->andX($expressions) - ); - } - - /** - * @dataProvider compositeDataProvider - * @expectedException \Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException - * - * @param array $expressions - */ - public function testNandX(array $expressions) - { - $this->exprBuilder->nandX($expressions); - } - - /** - * @dataProvider compositeDataProvider - * - * @param array $expressions - */ - public function testOrX(array $expressions) - { - $this->assertEquals( - $this->createExpr() - ->addOr($this->createExpr()->field('fieldA')->equals('value')) - ->addOr($this->createExpr()->field('fieldB')->gt(2)), - $this->exprBuilder->orX($expressions) - ); - } - - /** - * @dataProvider compositeDataProvider - * - * @param array $expressions - */ - public function testNorX(array $expressions) - { - $this->assertEquals( - $this->createExpr() - ->addNor($this->createExpr()->field('fieldA')->equals('value')) - ->addNor($this->createExpr()->field('fieldB')->gt(2)), - $this->exprBuilder->norX($expressions) - ); - } - - /** - * @dataProvider compositeDataProvider - * @expectedException \Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException - * - * @param array $expressions - */ - public function testXorX(array $expressions) - { - $this->exprBuilder->xorX($expressions); - } - - private function createExpr() - { - return new Expr(); - } -} diff --git a/tests/Bridge/Doctrine/MongoDb/ParserTest.php b/tests/Bridge/Doctrine/MongoDb/ParserTest.php deleted file mode 100644 index cf473ef..0000000 --- a/tests/Bridge/Doctrine/MongoDb/ParserTest.php +++ /dev/null @@ -1,212 +0,0 @@ -markTestSkipped('This test is run when you have "doctrine/mongodb-odm" installed.'); - } - - $this->exprBuilder = new ExprBuilder(); - $this->parser = new Parser($this->exprBuilder); - } - - public function parseSuccessDataProvider() - { - if (!class_exists('Doctrine\MongoDB\Query\Expr')) { - return array(); - } - - return array( - array( - 'fieldA=1', - array('fieldA' => 1), - ), - array( - 'fieldA="string"', - array('fieldA' => 'string'), - ), - array( - 'fieldA≥1', - array('fieldA' => array('$gte' => 1)), - ), - array( - 'fieldA>=1', - array('fieldA' => array('$gte' => 1)), - ), - array( - 'fieldA>1', - array('fieldA' => array('$gt' => 1)), - ), - array( - 'fieldA≤1', - array('fieldA' => array('$lte' => 1)), - ), - array( - 'fieldA<=1', - array('fieldA' => array('$lte' => 1)), - ), - array( - 'fieldA<1', - array('fieldA' => array('$lt' => 1)), - ), - array( - 'fieldA≠1', - array('fieldA' => array('$ne' => 1)), - ), - array( - 'fieldA!=1', - array('fieldA' => array('$ne' => 1)), - ), - array( - 'fieldA[1,2]', - array('fieldA' => array('$in' => array(1, 2))), - ), - array( - 'fieldA![1,2]', - array('fieldA' => array('$nin' => array(1, 2))), - ), - array( - 'fieldA{{1}}', - array('fieldA' => new \MongoRegex('/.*1.*/')), - ), - array( - 'fieldA!{{1}}', - array('fieldA' => new \MongoRegex('/^((?!1).)*$/')), - ), - array( - 'fieldA=1|fieldB=2|fieldC=3', - array('$or' => array( - array('fieldA' => 1), - array('fieldB' => 2), - array('fieldC' => 3), - )), - ), - array( - 'fieldA=1&fieldB=2&fieldC=3', - array('$and' => array( - array('fieldA' => 1), - array('fieldB' => 2), - array('fieldC' => 3), - )), - ), - - // Precedences - array( - 'fieldA=1|fieldB=2|fieldC=3&fieldD=4', - array('$or' => array( - array('fieldA' => 1), - array('fieldB' => 2), - array('$and' => array( - array('fieldC' => 3), - array('fieldD' => 4) - )), - )), - ), - array( - 'fieldA=1&fieldB=2&fieldC=3|fieldD=4', - array('$or' => array( - array('$and' => array( - array('fieldA' => 1), - array('fieldB' => 2), - array('fieldC' => 3), - )), - array('fieldD' => 4) - )), - ), - array( - 'fieldA=1&fieldB=2|fieldC=3&fieldD=4', - array('$or' => array( - array('$and' => array( - array('fieldA' => 1), - array('fieldB' => 2), - )), - array('$and' => array( - array('fieldC' => 3), - array('fieldD' => 4) - )) - )), - ), - - //Parenthesis - array( - '((fieldA=1))', - array('fieldA' => 1), - ), - array( - '(fieldA=1|fieldB=2)&fieldC=3', - array('$and' => array( - array('$or' => array( - array('fieldA' => 1), - array('fieldB' => 2), - )), - array('fieldC' => 3) - )), - ), - array( - 'fieldA=1|(fieldB=2&fieldC=3)', - array('$or' => array( - array('fieldA' => 1), - array('$and' => array( - array('fieldB' => 2), - array('fieldC' => 3) - )) - )), - ), - ); - } - - /** - * @dataProvider parseSuccessDataProvider - * - * @param $input - * @param $expectedExpression - */ - public function testParser($input, $expectedExpression) - { - $this->assertEquals($expectedExpression, $this->parser->parse($input)->getQuery()); - } - - public function unsupportedExpressionTypeDataProvider() - { - if (!class_exists('Doctrine\MongoDB\Query\Expr')) { - return array(); - } - - return array( - array('fieldA=1|fieldB=2|fieldC=3⊕fieldD=4'), - array('fieldA=1^|fieldB=2'), - array('fieldA=1⊕fieldB=2'), - array('fieldA=1!&fieldB=2!&fieldC=3'), - array('fieldA=1&fieldB=2&fieldC=3!&fieldD=4'), - ); - } - - /** - * @dataProvider unsupportedExpressionTypeDataProvider - * @expectedException \Symftony\Xpression\Exception\Parser\InvalidExpressionException - * - * @param $input - */ - public function testParserThrowUnsupportedExpressionTypeException($input) - { - $this->parser->parse($input); - } -} diff --git a/tests/Bridge/Doctrine/ORM/ExprAdapterTest.php b/tests/Bridge/Doctrine/ORM/ExprAdapterTest.php index 765b8dd..3837acf 100644 --- a/tests/Bridge/Doctrine/ORM/ExprAdapterTest.php +++ b/tests/Bridge/Doctrine/ORM/ExprAdapterTest.php @@ -2,9 +2,11 @@ namespace Tests\Symftony\Xpression\Bridge\Doctrine\ORM; +use Doctrine\Common\Collections\Expr\Expression; use PHPUnit\Framework\TestCase; use Doctrine\ORM\Query\Expr; use Symftony\Xpression\Bridge\Doctrine\ORM\ExprAdapter; +use Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException; class ExprAdapterTest extends TestCase { @@ -13,7 +15,7 @@ class ExprAdapterTest extends TestCase */ private $exprAdapter; - public function setUp() + public function setUp(): void { if (!class_exists('Doctrine\ORM\Query\Expr')) { $this->markTestSkipped('This test is run when you have "doctrine/orm" installed.'); @@ -22,12 +24,7 @@ public function setUp() $this->exprAdapter = new ExprAdapter(new Expr()); } - public function testValueAsString() - { - $this->assertEquals((new Expr())->literal('my_fake_data'), $this->exprAdapter->valueAsString('my_fake_data')); - } - - public function testIsNull() + public function testIsNull(): void { $field = 'fake_field'; $this->assertEquals('fake_field IS NULL', $this->exprAdapter->isNull($field)); @@ -36,21 +33,18 @@ public function testIsNull() public function comparisonDataProvider() { if (!class_exists('Doctrine\ORM\Query\Expr')) { - return array(); + return []; } - return array( - array('field', 'value'), - ); + return [ + ['field', 'value'], + ]; } /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testEq($field, $value) + public function testEq(string $field, string $value): void { $this->assertEquals( new Expr\Comparison($field, Expr\Comparison::EQ, $value), @@ -60,11 +54,8 @@ public function testEq($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testNeq($field, $value) + public function testNeq(string $field, string $value): void { $this->assertEquals( new Expr\Comparison($field, Expr\Comparison::NEQ, $value), @@ -74,11 +65,8 @@ public function testNeq($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testGt($field, $value) + public function testGt(string $field, string $value): void { $this->assertEquals( new Expr\Comparison($field, Expr\Comparison::GT, $value), @@ -88,11 +76,8 @@ public function testGt($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testGte($field, $value) + public function testGte(string $field, string $value): void { $this->assertEquals( new Expr\Comparison($field, Expr\Comparison::GTE, $value), @@ -102,11 +87,8 @@ public function testGte($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testLt($field, $value) + public function testLt(string $field, string $value): void { $this->assertEquals( new Expr\Comparison($field, Expr\Comparison::LT, $value), @@ -116,11 +98,8 @@ public function testLt($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testLte($field, $value) + public function testLte(string $field, string $value): void { $this->assertEquals( new Expr\Comparison($field, Expr\Comparison::LTE, $value), @@ -130,39 +109,30 @@ public function testLte($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testIn($field, $value) + public function testIn(string $field, string $value): void { $this->assertEquals( - new Expr\Func('field IN', array("'value'")), - $this->exprAdapter->in($field, array($value)) + new Expr\Func('field IN', ["'value'"]), + $this->exprAdapter->in($field, [$value]) ); } /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testNotIn($field, $value) + public function testNotIn(string $field, string $value): void { $this->assertEquals( - new Expr\Func('field NOT IN', array("'value'")), - $this->exprAdapter->notIn($field, array($value)) + new Expr\Func('field NOT IN', ["'value'"]), + $this->exprAdapter->notIn($field, [$value]) ); } /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testContains($field, $value) + public function testContains(string $field, string $value): void { $this->assertEquals( new Expr\Comparison('field', 'LIKE', $value), @@ -172,11 +142,8 @@ public function testContains($field, $value) /** * @dataProvider comparisonDataProvider - * - * @param $field - * @param $value */ - public function testNotContains($field, $value) + public function testNotContains(string $field, string $value): void { $this->assertEquals( new Expr\Comparison('field', 'NOT LIKE', $value), @@ -184,42 +151,42 @@ public function testNotContains($field, $value) ); } - public function compositeDataProvider() + public function compositeDataProvider(): array { if (!class_exists('Doctrine\ORM\Query\Expr')) { - return array(); + return []; } - return array( - array(array( + return [ + [[ new Expr\Comparison('field', Expr\Comparison::EQ, 'value'), - )), - array(array( - new Expr\Func('field', array('value')) - )), - array(array( + ]], + [[ + new Expr\Func('field', ['value']), + ]], + [[ new Expr\Andx( - array( + [ new Expr\Comparison('field', Expr\Comparison::EQ, 'value'), - ) - ) - )), - array(array( + ] + ), + ]], + [[ new Expr\Orx( - array( + [ new Expr\Comparison('field', Expr\Comparison::EQ, 'value'), - ) - ) - )), - ); + ] + ), + ]], + ]; } /** * @dataProvider compositeDataProvider * - * @param $expressions + * @param Expression[] $expressions */ - public function testAndX($expressions) + public function testAndX(array $expressions): void { $this->assertEquals( new Expr\Andx($expressions), @@ -229,21 +196,21 @@ public function testAndX($expressions) /** * @dataProvider compositeDataProvider - * @expectedException \Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException * - * @param $expressions + * @param Expression[] $expressions */ - public function testNandX($expressions) + public function testNandX(array $expressions): void { + $this->expectException(UnsupportedExpressionTypeException::class); $this->exprAdapter->nandX($expressions); } /** * @dataProvider compositeDataProvider * - * @param $expressions + * @param Expression[] $expressions */ - public function testOrX($expressions) + public function testOrX(array $expressions): void { $this->assertEquals( new Expr\Orx($expressions), @@ -253,23 +220,23 @@ public function testOrX($expressions) /** * @dataProvider compositeDataProvider - * @expectedException \Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException * - * @param $expressions + * @param Expression[] $expressions */ - public function testNorX($expressions) + public function testNorX(array $expressions): void { + $this->expectException(UnsupportedExpressionTypeException::class); $this->exprAdapter->norX($expressions); } /** * @dataProvider compositeDataProvider - * @expectedException \Symftony\Xpression\Exception\Expr\UnsupportedExpressionTypeException * - * @param $expressions + * @param Expression[] $expressions */ - public function testXorX($expressions) + public function testXorX(array $expressions): void { + $this->expectException(UnsupportedExpressionTypeException::class); $this->exprAdapter->xorX($expressions); } } diff --git a/tests/Bridge/Doctrine/ORM/ParserTest.php b/tests/Bridge/Doctrine/ORM/ParserTest.php index 4b39c72..3f0e782 100644 --- a/tests/Bridge/Doctrine/ORM/ParserTest.php +++ b/tests/Bridge/Doctrine/ORM/ParserTest.php @@ -2,25 +2,19 @@ namespace Tests\Symftony\Xpression\Bridge\Doctrine\ORM; -use Doctrine\ORM\Query\AST\Literal; use PHPUnit\Framework\TestCase; use Doctrine\ORM\Query\Expr; use Symftony\Xpression\Bridge\Doctrine\ORM\ExprAdapter; +use Symftony\Xpression\Exception\Parser\InvalidExpressionException; use Symftony\Xpression\Parser; class ParserTest extends TestCase { - /** - * @var ExprAdapter - */ - private $exprAdapter; + private ExprAdapter $exprAdapter; - /** - * @var Parser - */ - private $parser; + private Parser $parser; - public function setUp() + public function setUp(): void { if (!class_exists('Doctrine\ORM\Query\Expr')) { $this->markTestSkipped('This test is run when you have "doctrine/orm" installed.'); @@ -29,179 +23,174 @@ public function setUp() $this->parser = new Parser($this->exprAdapter); } - public function parseSuccessDataProvider() + public function parseSuccessDataProvider(): array { if (!class_exists('Doctrine\ORM\Query\Expr')) { - return array(); + return []; } - return array( - array( + return [ + [ 'fieldA=1', new Expr\Comparison('fieldA', '=', 1), - ), - array( + ], + [ 'fieldA="string"', new Expr\Comparison('fieldA', '=', (new Expr())->literal('string')), - ), - array( + ], + [ 'fieldA≥1', new Expr\Comparison('fieldA', '>=', 1), - ), - array( + ], + [ 'fieldA>=1', new Expr\Comparison('fieldA', '>=', 1), - ), - array( + ], + [ 'fieldA≤1', new Expr\Comparison('fieldA', '<=', 1), - ), - array( + ], + [ 'fieldA<=1', new Expr\Comparison('fieldA', '<=', 1), - ), - array( + ], + [ 'fieldA≠1', new Expr\Comparison('fieldA', '<>', 1), - ), - array( + ], + [ 'fieldA!=1', new Expr\Comparison('fieldA', '<>', 1), - ), - array( + ], + [ 'fieldA[1,2]', - new Expr\Func('fieldA IN', array(1, 2)), - ), - array( + new Expr\Func('fieldA IN', [1, 2]), + ], + [ 'fieldA![1,2]', - new Expr\Func('fieldA NOT IN', array(1, 2)), - ), - array( + new Expr\Func('fieldA NOT IN', [1, 2]), + ], + [ 'fieldA{{1}}', new Expr\Comparison('fieldA', 'LIKE', 1), - ), - array( + ], + [ 'fieldA!{{1}}', new Expr\Comparison('fieldA', 'NOT LIKE', 1), - ), - array( + ], + [ 'fieldA=1|fieldB=2|fieldC=3', - new Expr\Orx(array( + new Expr\Orx([ new Expr\Comparison('fieldA', '=', 1), new Expr\Comparison('fieldB', '=', 2), new Expr\Comparison('fieldC', '=', 3), - )), - ), - array( + ]), + ], + [ 'fieldA=1&fieldB=2&fieldC=3', - new Expr\Andx(array( + new Expr\Andx([ new Expr\Comparison('fieldA', '=', 1), new Expr\Comparison('fieldB', '=', 2), new Expr\Comparison('fieldC', '=', 3), - )), - ), + ]), + ], // Precedences - array( + [ 'fieldA=1|fieldB=2|fieldC=3&fieldD=4', - new Expr\Orx(array( + new Expr\Orx([ new Expr\Comparison('fieldA', '=', 1), new Expr\Comparison('fieldB', '=', 2), - new Expr\Andx(array( + new Expr\Andx([ new Expr\Comparison('fieldC', '=', 3), new Expr\Comparison('fieldD', '=', 4), - )), - )), - ), - array( + ]), + ]), + ], + [ 'fieldA=1&fieldB=2&fieldC=3|fieldD=4', - new Expr\Orx(array( - new Expr\Andx(array( + new Expr\Orx([ + new Expr\Andx([ new Expr\Comparison('fieldA', '=', 1), new Expr\Comparison('fieldB', '=', 2), new Expr\Comparison('fieldC', '=', 3), - )), + ]), new Expr\Comparison('fieldD', '=', 4), - )), - ), - array( + ]), + ], + [ 'fieldA=1&fieldB=2|fieldC=3&fieldD=4', - new Expr\Orx(array( - new Expr\Andx(array( + new Expr\Orx([ + new Expr\Andx([ new Expr\Comparison('fieldA', '=', 1), new Expr\Comparison('fieldB', '=', 2), - )), - new Expr\Andx(array( + ]), + new Expr\Andx([ new Expr\Comparison('fieldC', '=', 3), new Expr\Comparison('fieldD', '=', 4), - )), - )), - ), + ]), + ]), + ], //Parenthesis - array( + [ '((fieldA=1))', new Expr\Comparison('fieldA', '=', 1), - ), - array( + ], + [ '(fieldA=1|fieldB=2)&fieldC=3', - new Expr\Andx(array( - new Expr\Orx(array( + new Expr\Andx([ + new Expr\Orx([ new Expr\Comparison('fieldA', '=', 1), new Expr\Comparison('fieldB', '=', 2), - )), + ]), new Expr\Comparison('fieldC', '=', 3), - )), - ), - array( + ]), + ], + [ 'fieldA=1|(fieldB=2&fieldC=3)', - new Expr\Orx(array( + new Expr\Orx([ new Expr\Comparison('fieldA', '=', 1), - new Expr\Andx(array( + new Expr\Andx([ new Expr\Comparison('fieldB', '=', 2), new Expr\Comparison('fieldC', '=', 3), - )), - )), - ), - ); + ]), + ]), + ], + ]; } /** * @dataProvider parseSuccessDataProvider - * - * @param $input - * @param $expectedExpression */ - public function testParser($input, $expectedExpression) + public function testParser(string $input, $expectedExpression): void { $this->assertEquals($expectedExpression, $this->parser->parse($input)); } - public function unsupportedExpressionTypeDataProvider() + public function unsupportedExpressionTypeDataProvider(): array { if (!class_exists('Doctrine\ORM\Query\Expr')) { - return array(); + return []; } - return array( - array('fieldA=1!|fieldB=2!|fieldC=3'), - array('fieldA=1^|fieldB=2'), - array('fieldA=1⊕fieldB=2'), - array('fieldA=1!&fieldB=2!&fieldC=3'), - array('fieldA=1&fieldB=2&fieldC=3!&fieldD=4'), - array('fieldA=1|fieldB=2|fieldC=3!|fieldD=4'), - array('fieldA=1|fieldB=2|fieldC=3⊕fieldD=4'), - ); + return [ + ['fieldA=1!|fieldB=2!|fieldC=3'], + ['fieldA=1^|fieldB=2'], + ['fieldA=1⊕fieldB=2'], + ['fieldA=1!&fieldB=2!&fieldC=3'], + ['fieldA=1&fieldB=2&fieldC=3!&fieldD=4'], + ['fieldA=1|fieldB=2|fieldC=3!|fieldD=4'], + ['fieldA=1|fieldB=2|fieldC=3⊕fieldD=4'], + ]; } /** * @dataProvider unsupportedExpressionTypeDataProvider - * @expectedException \Symftony\Xpression\Exception\Parser\InvalidExpressionException - * - * @param $input */ - public function testParserThrowUnsupportedExpressionTypeException($input) + public function testParserThrowUnsupportedExpressionTypeException(string $input): void { + $this->expectException(InvalidExpressionException::class); $this->parser->parse($input); } } diff --git a/tests/Bridge/MongoDB/ExprBuilderTest.php b/tests/Bridge/MongoDB/ExprBuilderTest.php new file mode 100644 index 0000000..3768dbd --- /dev/null +++ b/tests/Bridge/MongoDB/ExprBuilderTest.php @@ -0,0 +1,131 @@ +exprAdapter = new ExprBuilder(); + } + + public function testGetSupportedTokenType(): void + { + $this->assertEquals(Lexer::T_ALL - Lexer::T_XOR, $this->exprAdapter->getSupportedTokenType()); + } + + public function testIsNull(): void + { + $field = 'fake_field'; + $this->assertEquals(['fake_field' => null], $this->exprAdapter->isNull($field)); + } + + public function testEq(): void + { + $this->assertEquals( + ['fieldA' => ['$eq' => 1]], + $this->exprAdapter->eq('fieldA', 1) + ); + } + + public function testNeq(): void + { + $this->assertEquals( + ['fieldA' => ['$ne' => 1]], + $this->exprAdapter->neq('fieldA', 1) + ); + } + + public function testGt(): void + { + $this->assertEquals( + ['fieldA' => ['$gt' => 1]], + $this->exprAdapter->gt('fieldA', 1) + ); + } + + public function testGte(): void + { + $this->assertEquals( + ['fieldA' => ['$gte' => 1]], + $this->exprAdapter->gte('fieldA', 1) + ); + } + + public function testLt(): void + { + $this->assertEquals( + ['fieldA' => ['$lt' => 1]], + $this->exprAdapter->lt('fieldA', 1) + ); + } + + public function testLte(): void + { + $this->assertEquals( + ['fieldA' => ['$lte' => 1]], + $this->exprAdapter->lte('fieldA', 1) + ); + } + + public function testIn(): void + { + $this->assertEquals( + ['fieldA' => ['$in' => [1, 2]]], + $this->exprAdapter->in('fieldA', [1, 2]) + ); + } + + public function testNin(): void + { + $this->assertEquals( + ['fieldA' => ['$nin' => [1, 2]]], + $this->exprAdapter->notIn('fieldA', [1, 2]) + ); + } + + public function testAnd(): void + { + $this->assertEquals( + ['$and' => [['expression1'], ['expression2']]], + $this->exprAdapter->andX([['expression1'], ['expression2']]) + ); + } + + public function testNand(): void + { + $this->assertEquals( + ['$or' => [['$not' => ['expression1']], ['$not' => ['expression2']]]], + $this->exprAdapter->nandX([['expression1'], ['expression2']]) + ); + } + + public function testOr(): void + { + $this->assertEquals( + ['$or' => [['expression1'], ['expression2']]], + $this->exprAdapter->orX([['expression1'], ['expression2']]) + ); + } + + public function testNor(): void + { + $this->assertEquals( + ['$nor' => [['expression1'], ['expression2']]], + $this->exprAdapter->norX([['expression1'], ['expression2']]) + ); + } + + public function testXorX(): void + { + $this->expectException(UnsupportedExpressionTypeException::class); + $this->exprAdapter->xorX([]); + } +} diff --git a/tests/Bridge/MongoDB/ParserTest.php b/tests/Bridge/MongoDB/ParserTest.php new file mode 100644 index 0000000..6e50f3a --- /dev/null +++ b/tests/Bridge/MongoDB/ParserTest.php @@ -0,0 +1,230 @@ +exprBuilder = new ExprBuilder(); + $this->parser = new Parser($this->exprBuilder); + } + + public function parseSuccessDataProvider(): array + { + return [ + [ + 'fieldA=1', + [ + 'fieldA' => ['$eq' => 1], + ], + ], + [ + 'fieldA="string"', + [ + 'fieldA' => ['$eq' => 'string'], + ], + ], + [ + 'fieldA≥1', + [ + 'fieldA' => ['$gte' => 1], + ], + ], + [ + 'fieldA>=1', + [ + 'fieldA' => ['$gte' => 1], + ], + ], + [ + 'fieldA≤1', + [ + 'fieldA' => ['$lte' => 1], + ], + ], + [ + 'fieldA<=1', + [ + 'fieldA' => ['$lte' => 1], + ], + ], + [ + 'fieldA≠1', + [ + 'fieldA' => ['$ne' => 1], + ], + ], + [ + 'fieldA!=1', + [ + 'fieldA' => ['$ne' => 1], + ], + ], + [ + 'fieldA[1,2]', + [ + 'fieldA' => ['$in' => [1, 2]], + ], + ], + [ + 'fieldA![1,2]', + [ + 'fieldA' => ['$nin' => [1, 2]], + ], + ], + [ + 'fieldA{{1}}', + [ + 'fieldA' => ['$regex' => 1], + ], + ], + [ + 'fieldA!{{1}}', + [ + '$not' => ['fieldA' => ['$regex' => 1]], + ], + ], + [ + 'fieldA=1|fieldB=2|fieldC=3', + [ + '$or' => [ + ['fieldA' => ['$eq' => 1]], + ['fieldB' => ['$eq' => 2]], + ['fieldC' => ['$eq' => 3]], + ], + ], + ], + [ + 'fieldA=1&fieldB=2&fieldC=3', + [ + '$and' => [ + ['fieldA' => ['$eq' => 1]], + ['fieldB' => ['$eq' => 2]], + ['fieldC' => ['$eq' => 3]], + ], + ], + ], + + // Precedences + [ + 'fieldA=1|fieldB=2|fieldC=3&fieldD=4', + [ + '$or' => [ + ['fieldA' => ['$eq' => 1]], + ['fieldB' => ['$eq' => 2]], + [ + '$and' => [ + ['fieldC' => ['$eq' => 3]], + ['fieldD' => ['$eq' => 4]], + ], + ], + ], + ], + ], + [ + 'fieldA=1&fieldB=2&fieldC=3|fieldD=4', + [ + '$or' => [ + [ + '$and' => [ + ['fieldA' => ['$eq' => 1]], + ['fieldB' => ['$eq' => 2]], + ['fieldC' => ['$eq' => 3]], + ], + ], + ['fieldD' => ['$eq' => 4]], + ], + ], + ], + [ + 'fieldA=1&fieldB=2|fieldC=3&fieldD=4', + [ + '$or' => [ + [ + '$and' => [ + ['fieldA' => ['$eq' => 1]], + ['fieldB' => ['$eq' => 2]], + ], + ], + [ + '$and' => [ + ['fieldC' => ['$eq' => 3]], + ['fieldD' => ['$eq' => 4]], + ], + ], + ], + ], + ], + + //Parenthesis + [ + '((fieldA=1))', + ['fieldA' => ['$eq' => 1]], + ], + [ + '(fieldA=1|fieldB=2)&fieldC=3', + [ + '$and' => [ + [ + '$or' => [ + ['fieldA' => ['$eq' => 1]], + ['fieldB' => ['$eq' => 2]], + ], + ], + ['fieldC' => ['$eq' => 3]], + ], + ], + ], + [ + 'fieldA=1|(fieldB=2&fieldC=3)', + [ + '$or' => [ + ['fieldA' => ['$eq' => 1]], + [ + '$and' => [ + ['fieldB' => ['$eq' => 2]], + ['fieldC' => ['$eq' => 3]], + ], + ], + ], + ], + ], + ]; + } + + /** + * @dataProvider parseSuccessDataProvider + */ + public function testParser(string $input, $expectedExpression): void + { + $this->assertEquals($expectedExpression, $this->parser->parse($input)); + } + + public function unsupportedExpressionTypeDataProvider(): array + { + return [ + ['fieldA=1^|fieldB=2'], + ['fieldA=1⊕fieldB=2'], + ['fieldA=1|fieldB=2|fieldC=3⊕fieldD=4'], + ]; + } + + /** + * @dataProvider unsupportedExpressionTypeDataProvider + */ + public function testParserThrowUnsupportedExpressionTypeException(string $input): void + { + $this->expectException(InvalidExpressionException::class); + $this->parser->parse($input); + } +} diff --git a/tests/Expr/ClosureExpressionBuilderTest.php b/tests/Expr/ClosureExpressionBuilderTest.php index 8567730..e63294f 100644 --- a/tests/Expr/ClosureExpressionBuilderTest.php +++ b/tests/Expr/ClosureExpressionBuilderTest.php @@ -1,6 +1,6 @@ exampleData = array( + $this->exampleData = [ 'field_null' => null, 'field_number_5' => 5, 'field_number_10' => 10, 'field_string' => 'my_fake_string', - ); + ]; $this->closureExpressionBuilder = new ClosureExpressionBuilder(); } - public function getObjectFieldValueDataProvider() + public function getObjectFieldValueDataProvider(): array { $object = new \stdClass(); $object->property = 'fake_property'; $object->_property4 = 'fake_property'; - return array( - array( - array('fake_key' => 'fake_value'), + return [ + [ + ['fake_key' => 'fake_value'], 'fake_key', - 'fake_value' - ), - array( + 'fake_value', + ], + [ new FakeClass(), 'property1', - 'fake_is_property' - ), - array( + 'fake_is_property', + ], + [ new FakeClass(), 'property2', - 'fake_get_property' - ), - array( + 'fake_get_property', + ], + [ new FakeClass(), 'callProperty', - 'getcallProperty' - ), - array( + 'getcallProperty', + ], + [ new FakeArrayAccess(), 'callProperty', - 'callProperty' - ), - array( + 'callProperty', + ], + [ $object, 'property', - 'fake_property' - ), - array( + 'fake_property', + ], + [ new FakeClass2(), '_property4', - 'property4' - ), - array( + 'property4', + ], + [ $object, '_property4', - 'fake_property' - ), - ); + 'fake_property', + ], + ]; } /** * @dataProvider getObjectFieldValueDataProvider - * - * @param $object - * @param $value - * @param $expectedResult */ - public function testGetObjectFieldValue($object, $value, $expectedResult) + public function testGetObjectFieldValue(mixed $object, mixed $value, mixed $expectedResult): void { $this->assertEquals($expectedResult, ClosureExpressionBuilder::getObjectFieldValue($object, $value)); } - public function testGetSupportedTokenType() + public function testGetSupportedTokenType(): void { $this->assertEquals(Lexer::T_ALL, $this->closureExpressionBuilder->getSupportedTokenType()); } - public function testParameter() + public function testParameter(): void { $this->assertEquals('my_fake_data', $this->closureExpressionBuilder->parameter('my_fake_data')); $this->assertEquals('my_fake_data', $this->closureExpressionBuilder->parameter('my_fake_data', true)); } - public function testString() + public function testString(): void { $this->assertEquals('my_fake_data', $this->closureExpressionBuilder->string('my_fake_data')); } - public function isNullDataProvider() + public function isNullDataProvider(): array { - return array( - array('field_null', true), - array('field_number_5', false), - ); + return [ + ['field_null', true], + ['field_number_5', false], + ]; } /** * @dataProvider isNullDataProvider - * - * @param $field - * @param $expectedResult */ - public function testIsNull($field, $expectedResult) + public function testIsNull(mixed $field, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->isNull($field); $this->assertEquals( @@ -130,23 +117,19 @@ public function testIsNull($field, $expectedResult) ); } - public function eqDataProvider() + public function eqDataProvider(): array { - return array( - array('field_number_5', 1, false), - array('field_number_5', 5, true), - array('field_number_5', 10, false), - ); + return [ + ['field_number_5', 1, false], + ['field_number_5', 5, true], + ['field_number_5', 10, false], + ]; } /** * @dataProvider eqDataProvider - * - * @param $field - * @param $value - * @param $expectedResult */ - public function testEq($field, $value, $expectedResult) + public function testEq(mixed $field, mixed $value, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->eq($field, $value); $this->assertEquals( @@ -155,23 +138,19 @@ public function testEq($field, $value, $expectedResult) ); } - public function neqDataProvider() + public function neqDataProvider(): array { - return array( - array('field_number_5', 1, true), - array('field_number_5', 5, false), - array('field_number_5', 10, true), - ); + return [ + ['field_number_5', 1, true], + ['field_number_5', 5, false], + ['field_number_5', 10, true], + ]; } /** * @dataProvider neqDataProvider - * - * @param $field - * @param $value - * @param $expectedResult */ - public function testNeq($field, $value, $expectedResult) + public function testNeq(mixed $field, mixed $value, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->neq($field, $value); $this->assertEquals( @@ -180,23 +159,19 @@ public function testNeq($field, $value, $expectedResult) ); } - public function gtDataProvider() + public function gtDataProvider(): array { - return array( - array('field_number_5', 1, true), - array('field_number_5', 5, false), - array('field_number_5', 10, false), - ); + return [ + ['field_number_5', 1, true], + ['field_number_5', 5, false], + ['field_number_5', 10, false], + ]; } /** * @dataProvider gtDataProvider - * - * @param $field - * @param $value - * @param $expectedResult */ - public function testGt($field, $value, $expectedResult) + public function testGt(mixed $field, mixed $value, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->gt($field, $value); $this->assertEquals( @@ -205,23 +180,19 @@ public function testGt($field, $value, $expectedResult) ); } - public function gteDataProvider() + public function gteDataProvider(): array { - return array( - array('field_number_5', 1, true), - array('field_number_5', 5, true), - array('field_number_5', 10, false), - ); + return [ + ['field_number_5', 1, true], + ['field_number_5', 5, true], + ['field_number_5', 10, false], + ]; } /** * @dataProvider gteDataProvider - * - * @param $field - * @param $value - * @param $expectedResult */ - public function testGte($field, $value, $expectedResult) + public function testGte(mixed $field, mixed $value, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->gte($field, $value); $this->assertEquals( @@ -230,23 +201,19 @@ public function testGte($field, $value, $expectedResult) ); } - public function ltDataProvider() + public function ltDataProvider(): array { - return array( - array('field_number_5', 1, false), - array('field_number_5', 5, false), - array('field_number_5', 10, true), - ); + return [ + ['field_number_5', 1, false], + ['field_number_5', 5, false], + ['field_number_5', 10, true], + ]; } /** * @dataProvider ltDataProvider - * - * @param $field - * @param $value - * @param $expectedResult */ - public function testLt($field, $value, $expectedResult) + public function testLt(mixed $field, mixed $value, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->lt($field, $value); $this->assertEquals( @@ -255,23 +222,19 @@ public function testLt($field, $value, $expectedResult) ); } - public function lteDataProvider() + public function lteDataProvider(): array { - return array( - array('field_number_5', 1, false), - array('field_number_5', 5, true), - array('field_number_5', 10, true), - ); + return [ + ['field_number_5', 1, false], + ['field_number_5', 5, true], + ['field_number_5', 10, true], + ]; } /** * @dataProvider lteDataProvider - * - * @param $field - * @param $value - * @param $expectedResult */ - public function testLte($field, $value, $expectedResult) + public function testLte(mixed $field, mixed $value, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->lte($field, $value); $this->assertEquals( @@ -280,22 +243,18 @@ public function testLte($field, $value, $expectedResult) ); } - public function inDataProvider() + public function inDataProvider(): array { - return array( - array('field_number_5', array(1), false), - array('field_number_5', array(1, 2, 3, 4, 5), true), - ); + return [ + ['field_number_5', [1], false], + ['field_number_5', [1, 2, 3, 4, 5], true], + ]; } /** * @dataProvider inDataProvider - * - * @param $field - * @param $value - * @param $expectedResult */ - public function testIn($field, $value, $expectedResult) + public function testIn(mixed $field, mixed $value, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->in($field, $value); $this->assertEquals( @@ -306,12 +265,8 @@ public function testIn($field, $value, $expectedResult) /** * @dataProvider inDataProvider - * - * @param $field - * @param $value - * @param $expectedResult */ - public function testNotIn($field, $value, $expectedResult) + public function testNotIn(mixed $field, mixed $value, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->notIn($field, $value); $this->assertEquals( @@ -320,22 +275,18 @@ public function testNotIn($field, $value, $expectedResult) ); } - public function containsDataProvider() + public function containsDataProvider(): array { - return array( - array('field_string', 'toto', false), - array('field_string', 'fake', true), - ); + return [ + ['field_string', 'toto', false], + ['field_string', 'fake', true], + ]; } /** * @dataProvider containsDataProvider - * - * @param $field - * @param $value - * @param $expectedResult */ - public function testContains($field, $value, $expectedResult) + public function testContains(mixed $field, mixed $value, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->contains($field, $value); $this->assertEquals( @@ -346,12 +297,8 @@ public function testContains($field, $value, $expectedResult) /** * @dataProvider containsDataProvider - * - * @param $field - * @param $value - * @param $expectedResult */ - public function testNotContains($field, $value, $expectedResult) + public function testNotContains(mixed $field, mixed $value, mixed $expectedResult): void { $expression = $this->closureExpressionBuilder->notContains($field, $value); $this->assertEquals( @@ -360,23 +307,20 @@ public function testNotContains($field, $value, $expectedResult) ); } - public function andXDataProvider() + public function andXDataProvider(): array { - return array( - array(array(false, false), false), - array(array(false, true), false), - array(array(true, false), false), - array(array(true, true), true), - ); + return [ + [[false, false], false], + [[false, true], false], + [[true, false], false], + [[true, true], true], + ]; } /** * @dataProvider andXDataProvider - * - * @param array $expressions - * @param $expectedResult */ - public function testAndX(array $expressions, $expectedResult) + public function testAndX(array $expressions, mixed $expectedResult): void { $expressionsCallable = array_map(function ($value) { return function () use ($value) { @@ -392,11 +336,8 @@ public function testAndX(array $expressions, $expectedResult) /** * @dataProvider andXDataProvider - * - * @param array $expressions - * @param $expectedResult */ - public function testNandX(array $expressions, $expectedResult) + public function testNandX(array $expressions, mixed $expectedResult): void { $expressionsCallable = array_map(function ($value) { return function () use ($value) { @@ -410,23 +351,20 @@ public function testNandX(array $expressions, $expectedResult) ); } - public function orXDataProvider() + public function orXDataProvider(): array { - return array( - array(array(false, false), false), - array(array(false, true), true), - array(array(true, false), true), - array(array(true, true), true), - ); + return [ + [[false, false], false], + [[false, true], true], + [[true, false], true], + [[true, true], true], + ]; } /** * @dataProvider orXDataProvider - * - * @param array $expressions - * @param $expectedResult */ - public function testOrX(array $expressions, $expectedResult) + public function testOrX(array $expressions, mixed $expectedResult): void { $expressionsCallable = array_map(function ($value) { return function () use ($value) { @@ -442,11 +380,8 @@ public function testOrX(array $expressions, $expectedResult) /** * @dataProvider orXDataProvider - * - * @param array $expressions - * @param $expectedResult */ - public function testNorX(array $expressions, $expectedResult) + public function testNorX(array $expressions, mixed $expectedResult): void { $expressionsCallable = array_map(function ($value) { return function () use ($value) { @@ -460,32 +395,29 @@ public function testNorX(array $expressions, $expectedResult) ); } - public function xorXDataProvider() + public function xorXDataProvider(): array { - return array( - array(array(false, false), false), - array(array(false, true), true), - array(array(true, false), true), - array(array(true, true), false), + return [ + [[false, false], false], + [[false, true], true], + [[true, false], true], + [[true, true], false], - array(array(false, false, false), false), - array(array(false, false, true), true), - array(array(false, true, false), true), - array(array(false, true, true), false), - array(array(true, false, false), true), - array(array(true, false, true), false), - array(array(true, true, false), false), - array(array(true, true, true), true), - ); + [[false, false, false], false], + [[false, false, true], true], + [[false, true, false], true], + [[false, true, true], false], + [[true, false, false], true], + [[true, false, true], false], + [[true, true, false], false], + [[true, true, true], true], + ]; } /** * @dataProvider xorXDataProvider - * - * @param array $expressions - * @param $expectedResult */ - public function testXorX(array $expressions, $expectedResult) + public function testXorX(array $expressions, mixed $expectedResult): void { $expressionsCallable = array_map(function ($value) { return function () use ($value) { @@ -543,20 +475,21 @@ public function getPROPERTY4() class FakeArrayAccess implements \ArrayAccess { - public function offsetExists($offset) + public function offsetExists($offset): bool { + return false; } - public function offsetGet($offset) + public function offsetGet($offset): mixed { return $offset; } - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { } - public function offsetUnset($offset) + public function offsetUnset($offset): void { } } diff --git a/tests/Expr/HtmlExpressionBuilderTest.php b/tests/Expr/HtmlExpressionBuilderTest.php index 66ee6d7..9d309dd 100644 --- a/tests/Expr/HtmlExpressionBuilderTest.php +++ b/tests/Expr/HtmlExpressionBuilderTest.php @@ -14,7 +14,7 @@ class HtmlExpressionBuilderTest extends TestCase */ private $closureExpressionBuilder; - public function setUp() + public function setUp(): void { $this->closureExpressionBuilder = new HtmlExpressionBuilder(); } @@ -37,10 +37,10 @@ public function testString() public function isNullDataProvider() { - return array( - array('field_null', '
field_null is null
'), - array('field_number_5', '
field_number_5 is null
'), - ); + return [ + ['field_null', '
field_null is null
'], + ['field_number_5', '
field_number_5 is null
'], + ]; } /** @@ -59,11 +59,11 @@ public function testIsNull($field, $expectedResult) public function eqDataProvider() { - return array( - array('field_number_5', 1, '
field_number_5 = 1
'), - array('field_number_5', 5, '
field_number_5 = 5
'), - array('field_number_5', 10, '
field_number_5 = 10
'), - ); + return [ + ['field_number_5', 1, '
field_number_5 = 1
'], + ['field_number_5', 5, '
field_number_5 = 5
'], + ['field_number_5', 10, '
field_number_5 = 10
'], + ]; } /** @@ -83,11 +83,11 @@ public function testEq($field, $value, $expectedResult) public function neqDataProvider() { - return array( - array('field_number_5', 1, '
field_number_5 ≠ 1
'), - array('field_number_5', 5, '
field_number_5 ≠ 5
'), - array('field_number_5', 10, '
field_number_5 ≠ 10
'), - ); + return [ + ['field_number_5', 1, '
field_number_5 ≠ 1
'], + ['field_number_5', 5, '
field_number_5 ≠ 5
'], + ['field_number_5', 10, '
field_number_5 ≠ 10
'], + ]; } /** @@ -107,11 +107,11 @@ public function testNeq($field, $value, $expectedResult) public function gtDataProvider() { - return array( - array('field_number_5', 1, '
field_number_5 > 1
'), - array('field_number_5', 5, '
field_number_5 > 5
'), - array('field_number_5', 10, '
field_number_5 > 10
'), - ); + return [ + ['field_number_5', 1, '
field_number_5 > 1
'], + ['field_number_5', 5, '
field_number_5 > 5
'], + ['field_number_5', 10, '
field_number_5 > 10
'], + ]; } /** @@ -131,11 +131,11 @@ public function testGt($field, $value, $expectedResult) public function gteDataProvider() { - return array( - array('field_number_5', 1, '
field_number_5 ≥ 1
'), - array('field_number_5', 5, '
field_number_5 ≥ 5
'), - array('field_number_5', 10, '
field_number_5 ≥ 10
'), - ); + return [ + ['field_number_5', 1, '
field_number_5 ≥ 1
'], + ['field_number_5', 5, '
field_number_5 ≥ 5
'], + ['field_number_5', 10, '
field_number_5 ≥ 10
'], + ]; } /** @@ -155,11 +155,11 @@ public function testGte($field, $value, $expectedResult) public function ltDataProvider() { - return array( - array('field_number_5', 1, '
field_number_5 < 1
'), - array('field_number_5', 5, '
field_number_5 < 5
'), - array('field_number_5', 10, '
field_number_5 < 10
'), - ); + return [ + ['field_number_5', 1, '
field_number_5 < 1
'], + ['field_number_5', 5, '
field_number_5 < 5
'], + ['field_number_5', 10, '
field_number_5 < 10
'], + ]; } /** @@ -179,11 +179,11 @@ public function testLt($field, $value, $expectedResult) public function lteDataProvider() { - return array( - array('field_number_5', 1, '
field_number_5 ≤ 1
'), - array('field_number_5', 5, '
field_number_5 ≤ 5
'), - array('field_number_5', 10, '
field_number_5 ≤ 10
'), - ); + return [ + ['field_number_5', 1, '
field_number_5 ≤ 1
'], + ['field_number_5', 5, '
field_number_5 ≤ 5
'], + ['field_number_5', 10, '
field_number_5 ≤ 10
'], + ]; } /** @@ -203,10 +203,10 @@ public function testLte($field, $value, $expectedResult) public function inDataProvider() { - return array( - array('field_number_5', array(1), '
field_number_5 value in 1
'), - array('field_number_5', array(1, 2, 3, 4, 5), '
field_number_5 value in 1, 2, 3, 4, 5
'), - ); + return [ + ['field_number_5', [1], '
field_number_5 value in 1
'], + ['field_number_5', [1, 2, 3, 4, 5], '
field_number_5 value in 1, 2, 3, 4, 5
'], + ]; } /** @@ -226,10 +226,10 @@ public function testIn($field, $value, $expectedResult) public function notInDataProvider() { - return array( - array('field_number_5', array(1), '
field_number_5 value not in 1
'), - array('field_number_5', array(1, 2, 3, 4, 5), '
field_number_5 value not in 1, 2, 3, 4, 5
'), - ); + return [ + ['field_number_5', [1], '
field_number_5 value not in 1
'], + ['field_number_5', [1, 2, 3, 4, 5], '
field_number_5 value not in 1, 2, 3, 4, 5
'], + ]; } /** @@ -249,10 +249,10 @@ public function testNotIn($field, $value, $expectedResult) public function containsDataProvider() { - return array( - array('field_string', 'toto', '
field_string contains toto
'), - array('field_string', 'fake', '
field_string contains fake
'), - ); + return [ + ['field_string', 'toto', '
field_string contains toto
'], + ['field_string', 'fake', '
field_string contains fake
'], + ]; } /** @@ -272,10 +272,10 @@ public function testContains($field, $value, $expectedResult) public function notContainsDataProvider() { - return array( - array('field_string', 'toto', '
field_string notContains toto
'), - array('field_string', 'fake', '
field_string notContains fake
'), - ); + return [ + ['field_string', 'toto', '
field_string notContains toto
'], + ['field_string', 'fake', '
field_string notContains fake
'], + ]; } /** @@ -295,12 +295,12 @@ public function testNotContains($field, $value, $expectedResult) public function andXDataProvider() { - return array( - array(array('false', 'false'), '
andfalsefalse
'), - array(array('false', 'true'), '
andfalsetrue
'), - array(array('true', 'false'), '
andtruefalse
'), - array(array('true', 'true'), '
andtruetrue
'), - ); + return [ + [['false', 'false'], '
andfalsefalse
'], + [['false', 'true'], '
andfalsetrue
'], + [['true', 'false'], '
andtruefalse
'], + [['true', 'true'], '
andtruetrue
'], + ]; } /** @@ -319,12 +319,12 @@ public function testAndX(array $expressions, $expectedResult) public function nandXDataProvider() { - return array( - array(array('false', 'false'), '
not-andfalsefalse
'), - array(array('false', 'true'), '
not-andfalsetrue
'), - array(array('true', 'false'), '
not-andtruefalse
'), - array(array('true', 'true'), '
not-andtruetrue
'), - ); + return [ + [['false', 'false'], '
not-andfalsefalse
'], + [['false', 'true'], '
not-andfalsetrue
'], + [['true', 'false'], '
not-andtruefalse
'], + [['true', 'true'], '
not-andtruetrue
'], + ]; } /** @@ -343,12 +343,12 @@ public function testNandX(array $expressions, $expectedResult) public function orXDataProvider() { - return array( - array(array('false', 'false'), '
orfalsefalse
'), - array(array('false', 'true'), '
orfalsetrue
'), - array(array('true', 'false'), '
ortruefalse
'), - array(array('true', 'true'), '
ortruetrue
'), - ); + return [ + [['false', 'false'], '
orfalsefalse
'], + [['false', 'true'], '
orfalsetrue
'], + [['true', 'false'], '
ortruefalse
'], + [['true', 'true'], '
ortruetrue
'], + ]; } /** @@ -367,12 +367,12 @@ public function testOrX(array $expressions, $expectedResult) public function norXDataProvider() { - return array( - array(array('false', 'false'), '
not-orfalsefalse
'), - array(array('false', 'true'), '
not-orfalsetrue
'), - array(array('true', 'false'), '
not-ortruefalse
'), - array(array('true', 'true'), '
not-ortruetrue
'), - ); + return [ + [['false', 'false'], '
not-orfalsefalse
'], + [['false', 'true'], '
not-orfalsetrue
'], + [['true', 'false'], '
not-ortruefalse
'], + [['true', 'true'], '
not-ortruetrue
'], + ]; } /** @@ -391,30 +391,27 @@ public function testNorX(array $expressions, $expectedResult) public function xorXDataProvider() { - return array( - array(array('false', 'false'), '
exclusive-orfalsefalse
'), - array(array('false', 'true'), '
exclusive-orfalsetrue
'), - array(array('true', 'false'), '
exclusive-ortruefalse
'), - array(array('true', 'true'), '
exclusive-ortruetrue
'), - - array(array('false', 'false', 'false'), '
exclusive-orfalsefalsefalse
'), - array(array('false', 'false', 'true'), '
exclusive-orfalsefalsetrue
'), - array(array('false', 'true', 'false'), '
exclusive-orfalsetruefalse
'), - array(array('false', 'true', 'true'), '
exclusive-orfalsetruetrue
'), - array(array('true', 'false', 'false'), '
exclusive-ortruefalsefalse
'), - array(array('true', 'false', 'true'), '
exclusive-ortruefalsetrue
'), - array(array('true', 'true', 'false'), '
exclusive-ortruetruefalse
'), - array(array('true', 'true', 'true'), '
exclusive-ortruetruetrue
'), - ); + return [ + [['false', 'false'], '
exclusive-orfalsefalse
'], + [['false', 'true'], '
exclusive-orfalsetrue
'], + [['true', 'false'], '
exclusive-ortruefalse
'], + [['true', 'true'], '
exclusive-ortruetrue
'], + + [['false', 'false', 'false'], '
exclusive-orfalsefalsefalse
'], + [['false', 'false', 'true'], '
exclusive-orfalsefalsetrue
'], + [['false', 'true', 'false'], '
exclusive-orfalsetruefalse
'], + [['false', 'true', 'true'], '
exclusive-orfalsetruetrue
'], + [['true', 'false', 'false'], '
exclusive-ortruefalsefalse
'], + [['true', 'false', 'true'], '
exclusive-ortruefalsetrue
'], + [['true', 'true', 'false'], '
exclusive-ortruetruefalse
'], + [['true', 'true', 'true'], '
exclusive-ortruetruetrue
'], + ]; } /** * @dataProvider xorXDataProvider - * - * @param array $expressions - * @param $expectedResult */ - public function testXorX(array $expressions, $expectedResult) + public function testXorX(array $expressions, mixed $expectedResult) { $this->assertEquals( $expectedResult, diff --git a/tests/Expr/MapperExpressionBuilderTest.php b/tests/Expr/MapperExpressionBuilderTest.php index a290a1d..507d43c 100644 --- a/tests/Expr/MapperExpressionBuilderTest.php +++ b/tests/Expr/MapperExpressionBuilderTest.php @@ -3,36 +3,40 @@ namespace Tests\Symftony\Xpression\Expr; use PHPUnit\Framework\TestCase; +use Prophecy\Prophecy\ObjectProphecy; +use Prophecy\Prophet; use Symftony\Xpression\Expr\ExpressionBuilderInterface; use Symftony\Xpression\Expr\MapperExpressionBuilder; class MapperExpressionBuilderTest extends TestCase { - /** - * @var ExpressionBuilderInterface - */ - private $expressionBuilderMock; + private ExpressionBuilderInterface|ObjectProphecy $expressionBuilderMock; - /** - * @var MapperExpressionBuilder - */ - private $mapperExpressionBuilder; + private MapperExpressionBuilder $mapperExpressionBuilder; - public function setUp() - { - $this->expressionBuilderMock = $this->prophesize(ExpressionBuilderInterface::class); + private Prophet $prophet; + public function setUp(): void + { + $this->prophet = new Prophet(); + $this->expressionBuilderMock = $this->prophet->prophesize(ExpressionBuilderInterface::class); $this->mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal()); } - public function testGetSupportedTokenType() + protected function tearDown(): void { + $this->prophet->checkPredictions(); + } + + public function testGetSupportedTokenType(): void + { + $this->expectNotToPerformAssertions(); $this->expressionBuilderMock->getSupportedTokenType()->shouldBeCalled(); $this->mapperExpressionBuilder->getSupportedTokenType(); } - public function testParameter() + public function testParameter(): void { $this->expressionBuilderMock->parameter('fake_field', false)->willReturn('fake_return')->shouldBeCalled(); @@ -42,7 +46,7 @@ public function testParameter() ); } - public function testParameterAsValue() + public function testParameterAsValue(): void { $this->expressionBuilderMock->parameter('fake_field', true)->willReturn('fake_return')->shouldBeCalled(); @@ -52,7 +56,7 @@ public function testParameterAsValue() ); } - public function testString() + public function testString(): void { $this->expressionBuilderMock->string('fake_field')->willReturn('fake_return')->shouldBeCalled(); @@ -64,12 +68,8 @@ public function testString() /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testIsNull($fieldMapping, $field, $expectedMappedField) + public function testIsNull(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -83,12 +83,8 @@ public function testIsNull($fieldMapping, $field, $expectedMappedField) /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testEq($fieldMapping, $field, $expectedMappedField) + public function testEq(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -102,12 +98,8 @@ public function testEq($fieldMapping, $field, $expectedMappedField) /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testNeq($fieldMapping, $field, $expectedMappedField) + public function testNeq(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -121,12 +113,8 @@ public function testNeq($fieldMapping, $field, $expectedMappedField) /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testGt($fieldMapping, $field, $expectedMappedField) + public function testGt(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -140,12 +128,8 @@ public function testGt($fieldMapping, $field, $expectedMappedField) /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testGte($fieldMapping, $field, $expectedMappedField) + public function testGte(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -159,12 +143,8 @@ public function testGte($fieldMapping, $field, $expectedMappedField) /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testLt($fieldMapping, $field, $expectedMappedField) + public function testLt(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -178,12 +158,8 @@ public function testLt($fieldMapping, $field, $expectedMappedField) /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testLte($fieldMapping, $field, $expectedMappedField) + public function testLte(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -197,12 +173,8 @@ public function testLte($fieldMapping, $field, $expectedMappedField) /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testIn($fieldMapping, $field, $expectedMappedField) + public function testIn(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -216,12 +188,8 @@ public function testIn($fieldMapping, $field, $expectedMappedField) /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testNotIn($fieldMapping, $field, $expectedMappedField) + public function testNotIn(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -235,12 +203,8 @@ public function testNotIn($fieldMapping, $field, $expectedMappedField) /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testContains($fieldMapping, $field, $expectedMappedField) + public function testContains(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -254,12 +218,8 @@ public function testContains($fieldMapping, $field, $expectedMappedField) /** * @dataProvider fieldMappingProvider - * - * @param $fieldMapping - * @param $field - * @param $expectedMappedField */ - public function testNotContains($fieldMapping, $field, $expectedMappedField) + public function testNotContains(array $fieldMapping, mixed $field, mixed $expectedMappedField): void { $mapperExpressionBuilder = new MapperExpressionBuilder($this->expressionBuilderMock->reveal(), $fieldMapping); @@ -271,33 +231,33 @@ public function testNotContains($fieldMapping, $field, $expectedMappedField) ); } - public function fieldMappingProvider() + public function fieldMappingProvider(): array { - return array( - array( - array(), + return [ + [ + [], 'fake_field', 'fake_field', - ), - array( - array('*' => 'fake_%s_mapping'), + ], + [ + ['*' => 'fake_%s_mapping'], 'fake_field', 'fake_fake_field_mapping', - ), - array( - array('other' => 'fake_%s_mapping'), + ], + [ + ['other' => 'fake_%s_mapping'], 'fake_field', 'fake_field', - ), - array( - array('fake_field' => 'fake_%s_mapping'), + ], + [ + ['fake_field' => 'fake_%s_mapping'], 'fake_field', 'fake_fake_field_mapping', - ), - ); + ], + ]; } - public function testAndX() + public function testAndX(): void { $this->expressionBuilderMock->andX(['fake_expression'])->willReturn('fake_return')->shouldBeCalled(); @@ -307,7 +267,7 @@ public function testAndX() ); } - public function testNandX() + public function testNandX(): void { $this->expressionBuilderMock->nandX(['fake_expression'])->willReturn('fake_return')->shouldBeCalled(); @@ -317,7 +277,7 @@ public function testNandX() ); } - public function testOrX() + public function testOrX(): void { $this->expressionBuilderMock->orX(['fake_expression'])->willReturn('fake_return')->shouldBeCalled(); @@ -327,7 +287,7 @@ public function testOrX() ); } - public function testNorX() + public function testNorX(): void { $this->expressionBuilderMock->norX(['fake_expression'])->willReturn('fake_return')->shouldBeCalled(); @@ -337,7 +297,7 @@ public function testNorX() ); } - public function testXorX() + public function testXorX(): void { $this->expressionBuilderMock->xorX(['fake_expression'])->willReturn('fake_return')->shouldBeCalled(); diff --git a/tests/LexerTest.php b/tests/LexerTest.php index d9ebac3..a5fbfd0 100644 --- a/tests/LexerTest.php +++ b/tests/LexerTest.php @@ -3,193 +3,188 @@ namespace Tests\Symftony\Xpression; use PHPUnit\Framework\TestCase; +use Symftony\Xpression\Exception\Lexer\UnknownTokenTypeException; use Symftony\Xpression\Lexer; class LexerTest extends TestCase { - /** - * @var Lexer - */ - private $lexer; + private Lexer $lexer; - public function setUp() + public function setUp(): void { $this->lexer = new Lexer(); } - public function setInputSuccessDataProvider() + public function setInputSuccessDataProvider():array { - return array( - array( + return [ + [ 'a=1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 2), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 2], + ], + ], + [ 'a.b=1', - array( - array('value' => 'a.b', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 3), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4), - ), - ), - array( + [ + ['value' => 'a.b', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 3], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4], + ], + ], + [ 'a-b=1', - array( - array('value' => 'a-b', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 3), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4), - ), - ), - array( + [ + ['value' => 'a-b', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 3], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4], + ], + ], + [ 'a≠1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '≠', 'type' => Lexer::T_NOT_EQUALS, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '≠', 'type' => Lexer::T_NOT_EQUALS, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4], + ], + ], + [ 'a!=1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '≠', 'type' => Lexer::T_NOT_EQUALS, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '≠', 'type' => Lexer::T_NOT_EQUALS, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3], + ], + ], + [ 'a="value"', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 1), - array('value' => 'value', 'type' => Lexer::T_STRING, 'position' => 2), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 1], + ['value' => 'value', 'type' => Lexer::T_STRING, 'position' => 2], + ], + ], + [ "a='value'", - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 1), - array('value' => 'value', 'type' => Lexer::T_STRING, 'position' => 2), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 1], + ['value' => 'value', 'type' => Lexer::T_STRING, 'position' => 2], + ], + ], + [ 'a>1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '>', 'type' => Lexer::T_GREATER_THAN, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 2), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '>', 'type' => Lexer::T_GREATER_THAN, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 2], + ], + ], + [ 'a>=1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '≥', 'type' => Lexer::T_GREATER_THAN_EQUALS, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '≥', 'type' => Lexer::T_GREATER_THAN_EQUALS, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3], + ], + ], + [ 'a≥1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '≥', 'type' => Lexer::T_GREATER_THAN_EQUALS, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '≥', 'type' => Lexer::T_GREATER_THAN_EQUALS, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4], + ], + ], + [ 'a<1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '<', 'type' => Lexer::T_LOWER_THAN, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 2), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '<', 'type' => Lexer::T_LOWER_THAN, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 2], + ], + ], + [ 'a<=1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '≤', 'type' => Lexer::T_LOWER_THAN_EQUALS, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '≤', 'type' => Lexer::T_LOWER_THAN_EQUALS, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3], + ], + ], + [ 'a≤1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '≤', 'type' => Lexer::T_LOWER_THAN_EQUALS, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '≤', 'type' => Lexer::T_LOWER_THAN_EQUALS, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4], + ], + ], + [ 'a|1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '|', 'type' => Lexer::T_OR, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 2), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '|', 'type' => Lexer::T_OR, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 2], + ], + ], + [ 'a!|1', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '!|', 'type' => Lexer::T_NOT_OR, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '!|', 'type' => Lexer::T_NOT_OR, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3], + ], + ], + [ 'a[1,2]', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '[', 'type' => Lexer::T_OPEN_SQUARE_BRACKET, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 2), - array('value' => ',', 'type' => Lexer::T_COMMA, 'position' => 3), - array('value' => '2', 'type' => Lexer::T_INTEGER, 'position' => 4), - array('value' => ']', 'type' => Lexer::T_CLOSE_SQUARE_BRACKET, 'position' => 5), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '[', 'type' => Lexer::T_OPEN_SQUARE_BRACKET, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 2], + ['value' => ',', 'type' => Lexer::T_COMMA, 'position' => 3], + ['value' => '2', 'type' => Lexer::T_INTEGER, 'position' => 4], + ['value' => ']', 'type' => Lexer::T_CLOSE_SQUARE_BRACKET, 'position' => 5], + ], + ], + [ 'a![1,2]', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '![', 'type' => Lexer::T_NOT_OPEN_SQUARE_BRACKET, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3), - array('value' => ',', 'type' => Lexer::T_COMMA, 'position' => 4), - array('value' => '2', 'type' => Lexer::T_INTEGER, 'position' => 5), - array('value' => ']', 'type' => Lexer::T_CLOSE_SQUARE_BRACKET, 'position' => 6), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '![', 'type' => Lexer::T_NOT_OPEN_SQUARE_BRACKET, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3], + ['value' => ',', 'type' => Lexer::T_COMMA, 'position' => 4], + ['value' => '2', 'type' => Lexer::T_INTEGER, 'position' => 5], + ['value' => ']', 'type' => Lexer::T_CLOSE_SQUARE_BRACKET, 'position' => 6], + ], + ], + [ 'a{{1}}', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '{{', 'type' => Lexer::T_DOUBLE_OPEN_CURLY_BRACKET, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3), - array('value' => '}}', 'type' => Lexer::T_DOUBLE_CLOSE_CURLY_BRACKET, 'position' => 4), - ), - ), - array( + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '{{', 'type' => Lexer::T_DOUBLE_OPEN_CURLY_BRACKET, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 3], + ['value' => '}}', 'type' => Lexer::T_DOUBLE_CLOSE_CURLY_BRACKET, 'position' => 4], + ], + ], + [ 'a!{{1}}', - array( - array('value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0), - array('value' => '!{{', 'type' => Lexer::T_NOT_DOUBLE_OPEN_CURLY_BRACKET, 'position' => 1), - array('value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4), - array('value' => '}}', 'type' => Lexer::T_DOUBLE_CLOSE_CURLY_BRACKET, 'position' => 5), - ), - ), - ); + [ + ['value' => 'a', 'type' => Lexer::T_INPUT_PARAMETER, 'position' => 0], + ['value' => '!{{', 'type' => Lexer::T_NOT_DOUBLE_OPEN_CURLY_BRACKET, 'position' => 1], + ['value' => '1', 'type' => Lexer::T_INTEGER, 'position' => 4], + ['value' => '}}', 'type' => Lexer::T_DOUBLE_CLOSE_CURLY_BRACKET, 'position' => 5], + ], + ], + ]; } /** * @dataProvider setInputSuccessDataProvider - * - * @param $input - * @param $expectedTokens */ - public function testSetInputSuccess($input, $expectedTokens) + public function testSetInputSuccess(string $input, array $expectedTokens) { $this->lexer->setInput($input); $this->lexer->moveNext(); @@ -204,38 +199,35 @@ public function testSetInputSuccess($input, $expectedTokens) public function unexpectedValueExceptionProvider() { - return array( - array( + return [ + [ '!', - ), - array( + ], + [ '§', - ), - array( + ], + [ '^', - ), - array( + ], + [ ';', - ), - array( + ], + [ ':', - ), - array( + ], + [ '/', - ), - ); + ], + ]; } /** * @dataProvider unexpectedValueExceptionProvider - * - * @expectedException \Symftony\Xpression\Exception\Lexer\UnknownTokenTypeException - * @expectedExceptionMessageRegExp /Unknown token type ".+"\./ - * - * @param $input */ - public function testUnexpectedValueException($input) + public function testUnexpectedValueException(string $input) { + $this->expectException(UnknownTokenTypeException::class); + $this->expectExceptionMessageMatches('/Unknown token type ".+"\./'); $this->lexer->setInput($input); } } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 363e74f..5db8e6f 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -2,437 +2,381 @@ namespace Tests\Symftony\Xpression; +use Doctrine\Common\Collections\Expr\CompositeExpression; +use Doctrine\Common\Collections\Expr\Expression; use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; +use Prophecy\Prophet; +use Symftony\Xpression\Exception\Parser\InvalidExpressionException; use Symftony\Xpression\Expr\ExpressionBuilderInterface; use Symftony\Xpression\Lexer; use Symftony\Xpression\Parser; class ParserTest extends TestCase { - /** - * @var ExpressionBuilderInterface|ObjectProphecy - */ - private $expressionBuilderMock; + private ExpressionBuilderInterface|ObjectProphecy $expressionBuilderMock; - /** - * @var Parser - */ - private $parser; + private Parser $parser; + private Prophet $prophet; - public function setUp() + public function setUp(): void { - $this->expressionBuilderMock = $this->prophesize('Symftony\Xpression\Expr\ExpressionBuilderInterface'); + $this->prophet = new Prophet(); + $this->expressionBuilderMock = $this->prophet->prophesize(ExpressionBuilderInterface::class); $this->expressionBuilderMock->getSupportedTokenType()->willReturn(Lexer::T_ALL); $this->parser = new Parser($this->expressionBuilderMock->reveal()); } - public function parseSuccessDataProvider() + public function parseSuccessDataProvider(): array { - return array( - array( + $expr1 = $this->createMock(Expression::class); + $expr2 = $this->createMock(Expression::class); + $expr3 = $this->createMock(Expression::class); + $expr4 = $this->createMock(Expression::class); + $composite1 = $this->createMock(CompositeExpression::class); + $composite2 = $this->createMock(CompositeExpression::class); + $composite3 = $this->createMock(CompositeExpression::class); + return [ + [ 'fieldA=1', - array(array('eq', 'fieldA', 1, 'my_fake_comparison_A')), - array(), - 'my_fake_comparison_A', - ), - array( - 'fieldA="string"', - array(array('eq', 'fieldA', 'my_fake_string', 'my_fake_comparison_A')), - array(array('valueAsString', 'string', 'my_fake_string')), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], +// [ +// 'fieldA="string"', +// [['eq', 'fieldA', 'my_fake_string', 'my_fake_comparison_A']], +// [['valueAsString', 'string', 'my_fake_string']], +// 'my_fake_comparison_A', +// ], + [ 'fieldA>1', - array(array('gt', 'fieldA', 1, 'my_fake_comparison_A')), - array(), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->gt('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA≥1', - array( - array('gte', 'fieldA', 1, 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->gte('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA>=1', - array( - array('gte', 'fieldA', 1, 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->gte('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA<1', - array( - array('lt', 'fieldA', 1, 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->lt('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA≤1', - array( - array('lte', 'fieldA', 1, 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->lte('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA<=1', - array( - array('lte', 'fieldA', 1, 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->lte('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA≠1', - array( - array('neq', 'fieldA', 1, 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->neq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA!=1', - array( - array('neq', 'fieldA', 1, 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->neq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA[1,2]', - array( - array('in', 'fieldA', array(1, 2), 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->in('fieldA', [1, 2])->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA![1,2]', - array( - array('notIn', 'fieldA', array(1, 2), 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->notIn('fieldA', [1, 2])->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA{{1}}', - array( - array('contains', 'fieldA', 1, 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A', - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->contains('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ 'fieldA!{{1}}', - array( - array('notContains', 'fieldA', 1, 'my_fake_comparison_A') - ), - array(), - 'my_fake_comparison_A' - ), + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1) { + $mock->notContains('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], // Composite - array( + [ 'fieldA=1|fieldB=2|fieldC=3', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - ), - array( - array('orX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_orX_composite'), - ), - 'my_fake_orX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $composite1) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->orX([$expr1, $expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + }, + $composite1, + ], + [ 'fieldA=1!|fieldB=2!|fieldC=3', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - ), - array( - array('norX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_norX_composite'), - ), - 'my_fake_norX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $composite1) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->norX([$expr1, $expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + }, + $composite1, + ], + [ 'fieldA=1^|fieldB=2^|fieldC=3', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - ), - array( - array('xorX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_xorX_composite'), - ), - 'my_fake_xorX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $composite1) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->xorX([$expr1, $expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + }, + $composite1, + ], + [ 'fieldA=1⊕fieldB=2⊕fieldC=3', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - ), - array( - array('xorX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_xorX_composite'), - ), - 'my_fake_xorX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $composite1) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->xorX([$expr1, $expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + }, + $composite1, + ], + [ 'fieldA=1&fieldB=2&fieldC=3', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - ), - array( - array('andX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_andX_composite'), - ), - 'my_fake_andX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $composite1) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->andX([$expr1, $expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + }, + $composite1, + ], + [ 'fieldA{{value}}&fieldB=2', - array( - array('contains', 'fieldA', 'value', 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - ), - array( - array('andX', array('my_fake_comparison_A', 'my_fake_comparison_B'), 'my_fake_andX_composite'), - ), - 'my_fake_andX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $composite1) { + $mock->contains('fieldA', 'value')->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->andX([$expr1, $expr2])->willReturn($composite1)->shouldBeCalled(); + }, + $composite1, + ], + [ 'fieldA=1!&fieldB=2!&fieldC=3', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - ), - array( - array('nandX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_nandX_composite'), - ), - 'my_fake_nandX_composite' - ), + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $composite1) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->nandX([$expr1, $expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + }, + $composite1, + ], // Precedences - array( + [ 'fieldA=1|fieldB=2|fieldC=3&fieldD=4', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - array('eq', 'fieldD', 4, 'my_fake_comparison_D'), - ), - array( - array('andX', array('my_fake_comparison_C', 'my_fake_comparison_D'), 'my_fake_andX_composite'), - array('orX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_andX_composite'), 'my_fake_orX_composite'), - ), - 'my_fake_orX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $expr4, $composite1, $composite2) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->eq('fieldD', 4)->willReturn($expr4)->shouldBeCalled(); + // And was before OR + $mock->andX([$expr3, $expr4])->willReturn($composite1)->shouldBeCalled(); + $mock->orX([$expr1, $expr2, $composite1])->willReturn($composite2)->shouldBeCalled(); + }, + $composite2, + ], + [ 'fieldA=1&fieldB=2&fieldC=3!&fieldD=4', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - array('eq', 'fieldD', 4, 'my_fake_comparison_D'), - ), - array( - array('andX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_andX_composite'), - array('nandX', array('my_fake_andX_composite', 'my_fake_comparison_D'), 'my_fake_orX_composite'), - ), - 'my_fake_orX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $expr4, $composite1, $composite2) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->eq('fieldD', 4)->willReturn($expr4)->shouldBeCalled(); + $mock->andX([$expr1, $expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + $mock->nandX([$composite1, $expr4])->willReturn($composite2)->shouldBeCalled(); + }, + $composite2, + ], + [ 'fieldA=1|fieldB=2|fieldC=3!|fieldD=4', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - array('eq', 'fieldD', 4, 'my_fake_comparison_D'), - ), - array( - array('orX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_orX_composite'), - array('norX', array('my_fake_orX_composite', 'my_fake_comparison_D'), 'my_fake_norX_composite'), - ), - 'my_fake_norX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $expr4, $composite1, $composite2) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->eq('fieldD', 4)->willReturn($expr4)->shouldBeCalled(); + $mock->orX([$expr1, $expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + $mock->norX([$composite1, $expr4])->willReturn($composite2)->shouldBeCalled(); + }, + $composite2, + ], + [ 'fieldA=1&fieldB=2&fieldC=3|fieldD=4', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - array('eq', 'fieldD', 4, 'my_fake_comparison_D'), - ), - array( - array('andX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_andX_composite'), - array('orX', array('my_fake_andX_composite', 'my_fake_comparison_D'), 'my_fake_orX_composite'), - ), - 'my_fake_orX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $expr4, $composite1, $composite2) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->eq('fieldD', 4)->willReturn($expr4)->shouldBeCalled(); + $mock->andX([$expr1, $expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + $mock->orX([$composite1, $expr4])->willReturn($composite2)->shouldBeCalled(); + }, + $composite2, + ], + [ 'fieldA=1&fieldB=2|fieldC=3&fieldD=4', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - array('eq', 'fieldD', 4, 'my_fake_comparison_D'), - ), - array( - array('andX', array('my_fake_comparison_A', 'my_fake_comparison_B'), 'my_fake_andX_composite_1'), - array('andX', array('my_fake_comparison_C', 'my_fake_comparison_D'), 'my_fake_andX_composite_2'), - array('orX', array('my_fake_andX_composite_1', 'my_fake_andX_composite_2'), 'my_fake_orX_composite'), - ), - 'my_fake_orX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $expr4, $composite1, $composite2, $composite3) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->eq('fieldD', 4)->willReturn($expr4)->shouldBeCalled(); + $mock->andX([$expr1, $expr2])->willReturn($composite1)->shouldBeCalled(); + $mock->andX([$expr3, $expr4])->willReturn($composite2)->shouldBeCalled(); + $mock->orX([$composite1, $composite2])->willReturn($composite3)->shouldBeCalled(); + }, + $composite3, + ], + [ 'fieldA=1|fieldB=2|fieldC=3⊕fieldD=4', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - array('eq', 'fieldD', 4, 'my_fake_comparison_D'), - ), - array( - array('orX', array('my_fake_comparison_A', 'my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_orX_composite'), - array('xorX', array('my_fake_orX_composite', 'my_fake_comparison_D'), 'my_fake_xorX_composite'), - ), - 'my_fake_xorX_composite' - ), + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $expr4, $composite1, $composite2) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->eq('fieldD', 4)->willReturn($expr4)->shouldBeCalled(); + $mock->orX([$expr1, $expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + $mock->xorX([$composite1, $expr4])->willReturn($composite2)->shouldBeCalled(); + }, + $composite2, + ], //Parenthesis - array( + [ '((fieldA=1))', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - ), - array(), - 'my_fake_comparison_A' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $expr4, $composite1, $composite2) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + }, + $expr1, + ], + [ '(fieldA=1|fieldB=2)&fieldC=3', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - ), - array( - array('orX', array('my_fake_comparison_A', 'my_fake_comparison_B'), 'my_fake_orX_composite'), - array('andX', array('my_fake_orX_composite', 'my_fake_comparison_C'), 'my_fake_andX_composite'), - ), - 'my_fake_andX_composite' - ), - array( + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $composite1, $composite2) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->orX([$expr1, $expr2])->willReturn($composite1)->shouldBeCalled(); + $mock->andX([$composite1, $expr3])->willReturn($composite2)->shouldBeCalled(); + }, + $composite2, + ], + [ 'fieldA=1|(fieldB=2&fieldC=3)', - array( - array('eq', 'fieldA', 1, 'my_fake_comparison_A'), - array('eq', 'fieldB', 2, 'my_fake_comparison_B'), - array('eq', 'fieldC', 3, 'my_fake_comparison_C'), - ), - array( - array('andX', array('my_fake_comparison_B', 'my_fake_comparison_C'), 'my_fake_andX_composite'), - array('orX', array('my_fake_comparison_A', 'my_fake_andX_composite'), 'my_fake_orX_composite'), - ), - 'my_fake_orX_composite' - ), - ); + function (ExpressionBuilderInterface|ObjectProphecy $mock) use ($expr1, $expr2, $expr3, $composite1, $composite2) { + $mock->eq('fieldA', 1)->willReturn($expr1)->shouldBeCalled(); + $mock->eq('fieldB', 2)->willReturn($expr2)->shouldBeCalled(); + $mock->eq('fieldC', 3)->willReturn($expr3)->shouldBeCalled(); + $mock->andX([$expr2, $expr3])->willReturn($composite1)->shouldBeCalled(); + $mock->orX([$expr1, $composite1])->willReturn($composite2)->shouldBeCalled(); + }, + $composite2, + ], + ]; } /** * @dataProvider parseSuccessDataProvider - * - * @param $input - * @param $comparisonMethods - * @param $compositeMethods - * @param $expectedResult */ - public function testParseSuccess($input, $comparisonMethods, $compositeMethods, $expectedResult) + public function testParseSuccess(string $input, callable $configureExpressionBuilderMock, $expectedResult): void { - foreach ($comparisonMethods as $comparisonMethod) { - $this->expressionBuilderMock - ->{$comparisonMethod[0]}($comparisonMethod[1], $comparisonMethod[2]) - ->willReturn($comparisonMethod[3]) - ->shouldBeCalled(); - } - - foreach ($compositeMethods as $compositeMethod) { - $this->expressionBuilderMock - ->{$compositeMethod[0]}($compositeMethod[1]) - ->willReturn($compositeMethod[2]) - ->shouldBeCalled(); - } + $configureExpressionBuilderMock($this->expressionBuilderMock); $this->assertEquals($expectedResult, $this->parser->parse($input)); } - public function forbiddenTokenDataProvider() + public function forbiddenTokenDataProvider(): array { - return array( - array(','), - array('9'), - array('"string"'), - array("'string'"), - array('param'), - array('4.5'), - array('='), - array('≠'), - array('>'), - array('≥'), - array('<'), - array('≤'), - array('&'), - array('!&'), - array('|'), - array('!|'), - array('^|'), - array('⊕'), - array('('), - array(')'), - array('['), - array('!['), - array(']'), - array('{{'), - array('!{{'), - array('}}'), - ); + return [ + [','], + ['9'], + ['"string"'], + ["'string'"], + ['param'], + ['4.5'], + ['='], + ['≠'], + ['>'], + ['≥'], + ['<'], + ['≤'], + ['&'], + ['!&'], + ['|'], + ['!|'], + ['^|'], + ['⊕'], + ['('], + [')'], + ['['], + ['!['], + [']'], + ['{{'], + ['!{{'], + ['}}'], + ]; } /** * @dataProvider forbiddenTokenDataProvider - * @expectedException \Symftony\Xpression\Exception\Parser\InvalidExpressionException - * - * @param $input */ - public function testForbiddenToken($input) + public function testForbiddenToken(string $input): void { + $this->expectException(InvalidExpressionException::class); $this->parser->parse($input, Lexer::T_NONE); } - /** - * @expectedException \Symftony\Xpression\Exception\Parser\InvalidExpressionException - */ - public function testUnexpectedToken() + public function testUnexpectedToken(): void { - $this->expressionBuilderMock->eq('fieldA', 'foo')->willReturn('fake_return'); + $this->expectException(InvalidExpressionException::class); - $this->parser->parse('fieldA=foo=1'); + $this->parser->parse('fieldA==foo=1'); } - /** - * @expectedException \Symftony\Xpression\Exception\Parser\InvalidExpressionException - */ - public function testUnsupportedToken() + public function testUnsupportedToken(): void { + $this->expectException(InvalidExpressionException::class); $this->expressionBuilderMock->getSupportedTokenType()->willReturn(Lexer::T_NONE)->shouldBeCalled(); $this->parser->parse('fieldA=1'); diff --git a/tests/QueryStringParserTest.php b/tests/QueryStringParserTest.php index 6c4beed..558acf9 100644 --- a/tests/QueryStringParserTest.php +++ b/tests/QueryStringParserTest.php @@ -7,194 +7,190 @@ class QueryStringParserTest extends TestCase { - public function parseDataProvider() + public function parseDataProvider(): array { - return array( + return [ // Default querystring - array( + [ 'param-A', 'param-A', - array( + [ 'param-A' => '', - ) - ), - array( + ], + ], + [ 'param-A=', 'param-A=', - array( + [ 'param-A' => '', - ) - ), - array( + ], + ], + [ 'param-A=valueA', 'param-A=valueA', - array( + [ 'param-A' => 'valueA', - ) - ), - array( + ], + ], + [ 'param-A[]=valueA', 'param-A[]=valueA', - array( - 'param-A' => array('valueA'), - ) - ), - array( + [ + 'param-A' => ['valueA'], + ], + ], + [ 'param-A[subA]=valueA', 'param-A[subA]=valueA', - array( - 'param-A' => array('subA' => 'valueA'), - ) - ), - array( + [ + 'param-A' => ['subA' => 'valueA'], + ], + ], + [ 'param-A¶m-B', 'param-A¶m-B', - array( + [ 'param-A' => '', 'param-B' => '', - ) - ), - array( + ], + ], + [ 'param-A=¶m-B', 'param-A=¶m-B', - array( + [ 'param-A' => '', 'param-B' => '', - ) - ), - array( + ], + ], + [ 'param-A=valueA¶m-B', 'param-A=valueA¶m-B', - array( + [ 'param-A' => 'valueA', 'param-B' => '', - ) - ), - array( + ], + ], + [ 'param-A[]=valueA¶m-B', 'param-A[]=valueA¶m-B', - array( - 'param-A' => array('valueA'), + [ + 'param-A' => ['valueA'], 'param-B' => '', - ) - ), - array( + ], + ], + [ 'param-A[subA]=valueA¶m-B', 'param-A[subA]=valueA¶m-B', - array( - 'param-A' => array('subA' => 'valueA'), + [ + 'param-A' => ['subA' => 'valueA'], 'param-B' => '', - ) - ), + ], + ], // With Xpression - array( + [ 'query{{valueA}}', 'query{{valueA}}', - array( + [ 'query{{valueA}}' => '', - ) - ), - array( + ], + ], + [ 'query={price{{test}}&price=6}', 'query=price%7B%7Btest%7D%7D%26price%3D6', - array( + [ 'query' => 'price{{test}}&price=6', - ) - ), - array( + ], + ], + [ 'query={name{{test 2}}}', 'query=name%7B%7Btest+2%7D%7D', - array( + [ 'query' => 'name{{test 2}}', - ) - ), - array( + ], + ], + [ 'query={valueA}', 'query=valueA', - array( + [ 'query' => 'valueA', - ) - ), - array( + ], + ], + [ 'query[]={valueA}', 'query[]=valueA', - array( - 'query' => array('valueA'), - ) - ), - array( + [ + 'query' => ['valueA'], + ], + ], + [ 'query[subA]={valueA}', 'query[subA]=valueA', - array( - 'query' => array('subA' => 'valueA'), - ) - ), - array( + [ + 'query' => ['subA' => 'valueA'], + ], + ], + [ 'query-A={valueA}&query-B={valueB}', 'query-A=valueA&query-B=valueB', - array( + [ 'query-A' => 'valueA', 'query-B' => 'valueB', - ) - ), - array( + ], + ], + [ 'query-A[]={valueA1}&query-A[]={valueA2}&query-B={valueB}', 'query-A[]=valueA1&query-A[]=valueA2&query-B=valueB', - array( - 'query-A' => array('valueA1', 'valueA2'), + [ + 'query-A' => ['valueA1', 'valueA2'], 'query-B' => 'valueB', - ) - ), - array( + ], + ], + [ 'query-A[subA]={valueA}&query-B={valueB}', 'query-A[subA]=valueA&query-B=valueB', - array( - 'query-A' => array('subA' => 'valueA'), + [ + 'query-A' => ['subA' => 'valueA'], 'query-B' => 'valueB', - ) - ), + ], + ], // Fail - array( + [ 'query-A=valueA}', 'query-A=valueA}', - array( + [ 'query-A' => 'valueA}', - ) - ), - array( + ], + ], + [ 'query-A={valueA', 'query-A={valueA', - array( + [ 'query-A' => '{valueA', - ) - ), - array( + ], + ], + [ 'query-A={}valueA', 'query-A={}valueA', - array( + [ 'query-A' => '{}valueA', - ) - ), - array( + ], + ], + [ 'query-A={{valueA}}', 'query-A={{valueA}}', - array( + [ 'query-A' => '{{valueA}}', - ) - ), - ); + ], + ], + ]; } /** * @dataProvider parseDataProvider - * - * @param $queryString - * @param $expectedQueryString - * @param $expectedGET */ - public function testParse($queryString, $expectedQueryString, $expectedGET) + public function testParse(string $queryString, string $expectedQueryString, array $expectedGET) { $_SERVER['QUERY_STRING'] = $queryString; QueryStringParser::correctServerQueryString();