Skip to content

Commit

Permalink
Fix toArray + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fogelito committed Dec 14, 2023
1 parent 966194a commit 8f2a52a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,11 @@ public static function parseQueries(array $queries): array
*/
public function toArray(): array
{
$array = [
'method' => $this->method,
'attribute' => $this->attribute,
];
$array = ['method' => $this->method];

if(!empty($this->attribute)){
$array['attribute'] = $this->attribute;
}

if (\in_array($array['method'], self::LOGICAL_TYPES)) {
foreach ($this->values as $index => $value) {
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function testParse(): void
$this->assertEquals(Query::TYPE_OR, $query->getMethod());
$this->assertEquals(Query::TYPE_EQUAL, $queries[0]->getMethod());
$this->assertEquals('actors', $queries[0]->getAttribute());
$this->assertEquals($json, '{"method":"or","attribute":"","values":[{"method":"equal","attribute":"actors","values":["Brad Pitt"]},{"method":"equal","attribute":"actors","values":["Johnny Depp"]}]}');
$this->assertEquals($json, '{"method":"or","values":[{"method":"equal","attribute":"actors","values":["Brad Pitt"]},{"method":"equal","attribute":"actors","values":["Johnny Depp"]}]}');
}

public function testisMethod(): void
Expand All @@ -235,6 +235,7 @@ public function testisMethod(): void
$this->assertTrue(Query::isMethod('between'));
$this->assertTrue(Query::isMethod('select'));
$this->assertTrue(Query::isMethod('or'));
$this->assertTrue(Query::isMethod('and'));

$this->assertTrue(Query::isMethod(Query::TYPE_EQUAL));
$this->assertTrue(Query::isMethod(Query::TYPE_NOT_EQUAL));
Expand All @@ -255,6 +256,7 @@ public function testisMethod(): void
$this->assertTrue(Query::isMethod(QUERY::TYPE_BETWEEN));
$this->assertTrue(Query::isMethod(QUERY::TYPE_SELECT));
$this->assertTrue(Query::isMethod(QUERY::TYPE_OR));
$this->assertTrue(Query::isMethod(QUERY::TYPE_AND));

$this->assertFalse(Query::isMethod('invalid'));
$this->assertFalse(Query::isMethod('lte '));
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Validator/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,6 @@ public function testOrQuery(): void
)]
));

$this->assertEquals('Invalid query: Or queries requires only filters', $validator->getDescription());
$this->assertEquals('Invalid query: Or queries can only contain filter queries', $validator->getDescription());
}
}

0 comments on commit 8f2a52a

Please sign in to comment.