Skip to content

Commit f9b1836

Browse files
authored
Merge pull request #62 from utopia-php/refactor-accept-document-array-indexes-queries-validator
Accept array of index documents when constructing Queries validator
2 parents 7a7b2b9 + 23898ce commit f9b1836

2 files changed

Lines changed: 75 additions & 61 deletions

File tree

src/Database/Validator/Queries.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Utopia\Validator;
66
use Utopia\Database\Database;
7+
use Utopia\Database\Document;
78
use Utopia\Database\Validator\QueryValidator;
89
use Utopia\Database\Query;
910

@@ -38,15 +39,21 @@ class Queries extends Validator
3839
* Queries constructor
3940
*
4041
* @param QueryValidator $validator
41-
* @param array $indexes
42-
* @param array $indexesInQueue
42+
* @param Document[] $indexes
43+
* @param Document[] $indexesInQueue
4344
* @param bool $strict
4445
*/
4546
public function __construct($validator, $indexes, $indexesInQueue, $strict = true)
4647
{
4748
$this->validator = $validator;
48-
$this->indexes = $indexes;
49-
$this->indexesInQueue = $indexesInQueue;
49+
50+
foreach ($indexes as $index) {
51+
$this->indexes[] = $index->getArrayCopy(['attributes', 'type']);
52+
}
53+
foreach ($indexesInQueue as $index) {
54+
$this->indexesInQueue[] = $index->getArrayCopy(['attributes', 'type']);
55+
}
56+
5057
$this->strict = $strict;
5158
}
5259

tests/Database/Validator/QueriesTest.php

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Utopia\Database\Validator\QueryValidator;
66
use PHPUnit\Framework\TestCase;
77
use Utopia\Database\Database;
8+
use Utopia\Database\Document;
89
use Utopia\Database\Query;
910
use Utopia\Database\Validator\Queries;
1011

@@ -73,63 +74,8 @@ class QueriesTest extends TestCase
7374
'filters' => [],
7475
],
7576
],
76-
'indexes' => [
77-
[
78-
'$id' => 'testindex',
79-
'type' => 'key',
80-
'attributes' => [
81-
'title',
82-
'description'
83-
],
84-
'orders' => [
85-
'ASC',
86-
'DESC'
87-
],
88-
],
89-
[
90-
'$id' => 'testindex2',
91-
'type' => 'key',
92-
'attributes' => [
93-
'title',
94-
'description',
95-
'price'
96-
],
97-
'orders' => [
98-
'ASC',
99-
'DESC'
100-
],
101-
],
102-
[
103-
'$id' => 'testindex3',
104-
'type' => 'fulltext',
105-
'attributes' => [
106-
'title'
107-
],
108-
'orders' => []
109-
],
110-
[
111-
'$id' => 'testindex4',
112-
'type' => 'key',
113-
'attributes' => [
114-
'description'
115-
],
116-
'orders' => []
117-
],
118-
],
119-
'indexesInQueue' => [
120-
[
121-
'$id' => 'testindex4',
122-
'type' => 'key',
123-
'attributes' => [
124-
'price',
125-
'title'
126-
],
127-
'orders' => [
128-
'ASC',
129-
'DESC'
130-
]
131-
],
132-
]
77+
'indexes' => [],
78+
'indexesInQueue' => []
13379
];
13480

13581

@@ -151,6 +97,67 @@ public function setUp(): void
15197
$query2 = Query::parse('description.equal("Best movie ever")');
15298

15399
array_push($this->queries, $query1, $query2);
100+
101+
// Constructor expects Document[] $indexes
102+
// Object property declaration cannot initialize a Document object
103+
// Add Document[] $indexes separately
104+
$index1 = new Document([
105+
'$id' => 'testindex',
106+
'type' => 'key',
107+
'attributes' => [
108+
'title',
109+
'description'
110+
],
111+
'orders' => [
112+
'ASC',
113+
'DESC'
114+
],
115+
]);
116+
117+
$index2 = new Document([
118+
'$id' => 'testindex2',
119+
'type' => 'key',
120+
'attributes' => [
121+
'title',
122+
'description',
123+
'price'
124+
],
125+
'orders' => [
126+
'ASC',
127+
'DESC'
128+
],
129+
]);
130+
$index3 = new Document([
131+
'$id' => 'testindex3',
132+
'type' => 'fulltext',
133+
'attributes' => [
134+
'title'
135+
],
136+
'orders' => []
137+
]);
138+
$index4 = new Document([
139+
'$id' => 'testindex4',
140+
'type' => 'key',
141+
'attributes' => [
142+
'description'
143+
],
144+
'orders' => []
145+
]);
146+
$indexInQueue = new Document([
147+
'$id' => 'testindex4',
148+
'type' => 'key',
149+
'attributes' => [
150+
'price',
151+
'title'
152+
],
153+
'orders' => [
154+
'ASC',
155+
'DESC'
156+
]
157+
]);
158+
159+
$this->collection['indexes'] = [$index1, $index2, $index3, $index4];
160+
$this->collection['indexesInQueue'] = [$indexInQueue];
154161
}
155162

156163
public function tearDown(): void

0 commit comments

Comments
 (0)