From 5e0a37a2959f7c139c9a5d716632e1605a8f1c39 Mon Sep 17 00:00:00 2001 From: Bas Date: Tue, 19 Oct 2021 19:52:53 +0200 Subject: [PATCH] - removed duplicate !array check in for clause - fixed array of predicates falsely being recognized as a single predicate instead of a group. - SupportCommandsTest correctly covers bindArrayValues - added tests for unbounded and time based aggregation window queries. --- src/AQL/HasQueryClauses.php | 2 -- src/Clauses/ForClause.php | 7 ++---- src/Traits/ValidatesPredicates.php | 7 +++++- tests/Unit/AQL/QueryClausesTest.php | 25 +++++++++++++++++++ tests/Unit/AQL/SupportCommandsTest.php | 1 + .../Expressions/PredicateExpressionTest.php | 24 ++++++++++++++++++ 6 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/AQL/HasQueryClauses.php b/src/AQL/HasQueryClauses.php index 71867da..9e18f9d 100644 --- a/src/AQL/HasQueryClauses.php +++ b/src/AQL/HasQueryClauses.php @@ -19,7 +19,6 @@ use LaravelFreelancerNL\FluentAQL\Clauses\WithCountClause; use LaravelFreelancerNL\FluentAQL\Expressions\Expression; use LaravelFreelancerNL\FluentAQL\QueryBuilder; -use phpDocumentor\Reflection\Types\ArrayKey; /** * Trait hasQueryClauses @@ -192,7 +191,6 @@ public function aggregate( * @link https://www.arangodb.com/docs/stable/aql/operations-sort.html * * @param mixed ...$references - * @return QueryBuilder */ public function sort(...$references): self { diff --git a/src/Clauses/ForClause.php b/src/Clauses/ForClause.php index e8351dc..2239e15 100644 --- a/src/Clauses/ForClause.php +++ b/src/Clauses/ForClause.php @@ -21,18 +21,15 @@ class ForClause extends Clause protected array|Expression|ExpressionInterface|QueryBuilder|string|null $in; /** - * @param array|string|Expression $variables + * @param array $variables * @param array|string|QueryBuilder|Expression|null $in */ public function __construct( - array|string|Expression $variables, + array $variables, array|string|QueryBuilder|Expression $in = null ) { parent::__construct(); - if (!is_array($variables)) { - $variables = [$variables]; - } $this->variables = $variables; $this->in = $in; diff --git a/src/Traits/ValidatesPredicates.php b/src/Traits/ValidatesPredicates.php index b9a62ff..be7d33b 100644 --- a/src/Traits/ValidatesPredicates.php +++ b/src/Traits/ValidatesPredicates.php @@ -14,7 +14,12 @@ public function isPredicate(mixed $value): bool return true; } - if (is_array($value) && isset($value[0]) && ! is_array($value[0])) { + if ( + is_array($value) + && isset($value[0]) + && ! is_array($value[0]) + && ! $value[0] instanceof PredicateExpression + ) { return true; } diff --git a/tests/Unit/AQL/QueryClausesTest.php b/tests/Unit/AQL/QueryClausesTest.php index 9f1818e..bc10432 100644 --- a/tests/Unit/AQL/QueryClausesTest.php +++ b/tests/Unit/AQL/QueryClausesTest.php @@ -439,4 +439,29 @@ public function testWindowClauseWithRange() $result->query ); } + + public function testWindowClauseUnbounded() + { + $result = (new QueryBuilder()) + ->for('t', 'observations') + ->window(['preceding' => 'unbounded', 'following' => 10], 't.time') + ->get(); + + self::assertEquals( + 'FOR t IN observations WINDOW t.time WITH {"preceding":"unbounded","following":10}', + $result->query + ); + } + public function testWindowClauseDurationBasedAggregation() + { + $qb = new QueryBuilder(); + $qb->for('t', 'observations') + ->window(['preceding' => 'PT30M'], $qb->dateTimestamp('t.time')) + ->get(); + + self::assertEquals( + 'FOR t IN observations WINDOW DATE_TIMESTAMP(t.time) WITH {"preceding":"PT30M"}', + $qb->get()->query + ); + } } diff --git a/tests/Unit/AQL/SupportCommandsTest.php b/tests/Unit/AQL/SupportCommandsTest.php index e4a5893..8753664 100644 --- a/tests/Unit/AQL/SupportCommandsTest.php +++ b/tests/Unit/AQL/SupportCommandsTest.php @@ -6,6 +6,7 @@ use LaravelFreelancerNL\FluentAQL\Tests\TestCase; /** + * @covers \LaravelFreelancerNL\FluentAQL\QueryBuilder * @covers \LaravelFreelancerNL\FluentAQL\AQL\HasSupportCommands * @covers \LaravelFreelancerNL\FluentAQL\Clauses\RawClause */ diff --git a/tests/Unit/Expressions/PredicateExpressionTest.php b/tests/Unit/Expressions/PredicateExpressionTest.php index 99a6231..b040d86 100644 --- a/tests/Unit/Expressions/PredicateExpressionTest.php +++ b/tests/Unit/Expressions/PredicateExpressionTest.php @@ -62,4 +62,28 @@ public function testPredicateWithoutRightOperand() self::assertEquals('x == null', $result); } + + public function testGroupOfPredicateExpressions() + { + $qb = new QueryBuilder(); + $predicate1 = new PredicateExpression( + (new LiteralExpression('u.name')), + '==', + 'Cookie Monster' + ); + $predicate2 = new PredicateExpression( + (new LiteralExpression('u.age')), + '==', + 27 + ); + + $result = $qb->for('u', 'users') + ->filter([$predicate1, $predicate2]) + ->return('u'); + + self::assertEquals( + 'FOR u IN users FILTER u.name == @' . $qb->getQueryId() . '_1 AND u.age == 27 RETURN u', + $result->get()->query + ); + } }