Skip to content

Commit 5eb97a5

Browse files
committed
Fix unit test
1 parent 0902764 commit 5eb97a5

3 files changed

Lines changed: 174 additions & 30 deletions

File tree

src/Database/Adapter/Pool.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,9 @@ protected function execute(mixed $stmt): bool
489489
{
490490
return $this->delegate(__FUNCTION__, \func_get_args());
491491
}
492+
493+
public function getIdAttributeType(): string
494+
{
495+
return $this->delegate(__FUNCTION__, \func_get_args());
496+
}
492497
}

src/Database/Validator/Sequence.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function isArray(): bool
3333

3434
public function getType(): string
3535
{
36-
if($this->idAttributeType === 'string'){
36+
if ($this->idAttributeType === 'string') {
3737
return self::TYPE_STRING;
3838
}
3939

@@ -48,25 +48,22 @@ public function isValid($value): bool
4848

4949
$type = gettype($value);
5050

51-
if ($type !== 'string'){
51+
if ($type !== 'string') {
5252
return false;
5353
}
5454

5555
if ($this->idAttributeType === 'string') {
5656
return preg_match('/^[a-f0-9]{24}$/i', $value) === 1;
57-
}
58-
else if ($this->idAttributeType === 'int') {
59-
$value = (int)$value;
60-
61-
$validator = new Integer();
62-
if (!$validator->isValid($value)){
57+
} elseif ($this->idAttributeType === 'int') {
58+
$validator = new Integer(loose: true);
59+
if (!$validator->isValid($value)) {
6360
return false;
6461
}
6562

66-
$start = ($this->primary) ? 1:0;
63+
$start = ($this->primary) ? 1 : 0;
6764

6865
$validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER);
69-
if (!$validator->isValid($value)){
66+
if (!$validator->isValid($value)) {
7067
return false;
7168
}
7269

tests/unit/Validator/StructureTest.php

Lines changed: 162 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ class StructureTest extends TestCase
9090
'array' => true,
9191
'filters' => [],
9292
],
93+
[
94+
'$id' => 'id',
95+
'type' => Database::VAR_ID,
96+
'format' => '',
97+
'size' => 0,
98+
'required' => false,
99+
'signed' => false,
100+
'array' => false,
101+
'filters' => [],
102+
],
93103
],
94104
'indexes' => [],
95105
];
@@ -121,7 +131,10 @@ public function tearDown(): void
121131

122132
public function testDocumentInstance(): void
123133
{
124-
$validator = new Structure(new Document($this->collection));
134+
$validator = new Structure(
135+
new Document($this->collection),
136+
'int'
137+
);
125138

126139
$this->assertEquals(false, $validator->isValid('string'));
127140
$this->assertEquals(false, $validator->isValid(null));
@@ -133,7 +146,10 @@ public function testDocumentInstance(): void
133146

134147
public function testCollectionAttribute(): void
135148
{
136-
$validator = new Structure(new Document());
149+
$validator = new Structure(
150+
new Document($this->collection),
151+
'int'
152+
);
137153

138154
$this->assertEquals(false, $validator->isValid(new Document()));
139155

@@ -142,7 +158,10 @@ public function testCollectionAttribute(): void
142158

143159
public function testCollection(): void
144160
{
145-
$validator = new Structure(new Document());
161+
$validator = new Structure(
162+
new Document(),
163+
'int'
164+
);
146165

147166
$this->assertEquals(false, $validator->isValid(new Document([
148167
'$collection' => ID::custom('posts'),
@@ -160,7 +179,10 @@ public function testCollection(): void
160179

161180
public function testRequiredKeys(): void
162181
{
163-
$validator = new Structure(new Document($this->collection));
182+
$validator = new Structure(
183+
new Document($this->collection),
184+
'int'
185+
);
164186

165187
$this->assertEquals(false, $validator->isValid(new Document([
166188
'$collection' => ID::custom('posts'),
@@ -177,7 +199,10 @@ public function testRequiredKeys(): void
177199

178200
public function testNullValues(): void
179201
{
180-
$validator = new Structure(new Document($this->collection));
202+
$validator = new Structure(
203+
new Document($this->collection),
204+
'int'
205+
);
181206

182207
$this->assertEquals(true, $validator->isValid(new Document([
183208
'$collection' => ID::custom('posts'),
@@ -188,6 +213,7 @@ public function testNullValues(): void
188213
'published' => true,
189214
'tags' => ['dog', 'cat', 'mouse'],
190215
'feedback' => 'team@appwrite.io',
216+
'id' => '1000',
191217
])));
192218

193219
$this->assertEquals(true, $validator->isValid(new Document([
@@ -204,7 +230,10 @@ public function testNullValues(): void
204230

205231
public function testUnknownKeys(): void
206232
{
207-
$validator = new Structure(new Document($this->collection));
233+
$validator = new Structure(
234+
new Document($this->collection),
235+
'int'
236+
);
208237

209238
$this->assertEquals(false, $validator->isValid(new Document([
210239
'$collection' => ID::custom('posts'),
@@ -223,7 +252,10 @@ public function testUnknownKeys(): void
223252

224253
public function testIntegerAsString(): void
225254
{
226-
$validator = new Structure(new Document($this->collection));
255+
$validator = new Structure(
256+
new Document($this->collection),
257+
'int'
258+
);
227259

228260
$this->assertEquals(false, $validator->isValid(new Document([
229261
'$collection' => ID::custom('posts'),
@@ -241,7 +273,10 @@ public function testIntegerAsString(): void
241273

242274
public function testValidDocument(): void
243275
{
244-
$validator = new Structure(new Document($this->collection));
276+
$validator = new Structure(
277+
new Document($this->collection),
278+
'int'
279+
);
245280

246281
$this->assertEquals(true, $validator->isValid(new Document([
247282
'$collection' => ID::custom('posts'),
@@ -257,7 +292,10 @@ public function testValidDocument(): void
257292

258293
public function testStringValidation(): void
259294
{
260-
$validator = new Structure(new Document($this->collection));
295+
$validator = new Structure(
296+
new Document($this->collection),
297+
'int'
298+
);
261299

262300
$this->assertEquals(false, $validator->isValid(new Document([
263301
'$collection' => ID::custom('posts'),
@@ -275,7 +313,10 @@ public function testStringValidation(): void
275313

276314
public function testArrayOfStringsValidation(): void
277315
{
278-
$validator = new Structure(new Document($this->collection));
316+
$validator = new Structure(
317+
new Document($this->collection),
318+
'int'
319+
);
279320

280321
$this->assertEquals(false, $validator->isValid(new Document([
281322
'$collection' => ID::custom('posts'),
@@ -333,7 +374,10 @@ public function testArrayOfStringsValidation(): void
333374
*/
334375
public function testArrayAsObjectValidation(): void
335376
{
336-
$validator = new Structure(new Document($this->collection));
377+
$validator = new Structure(
378+
new Document($this->collection),
379+
'int'
380+
);
337381

338382
$this->assertEquals(false, $validator->isValid(new Document([
339383
'$collection' => ID::custom('posts'),
@@ -349,7 +393,10 @@ public function testArrayAsObjectValidation(): void
349393

350394
public function testArrayOfObjectsValidation(): void
351395
{
352-
$validator = new Structure(new Document($this->collection));
396+
$validator = new Structure(
397+
new Document($this->collection),
398+
'int'
399+
);
353400

354401
$this->assertEquals(false, $validator->isValid(new Document([
355402
'$collection' => ID::custom('posts'),
@@ -365,7 +412,10 @@ public function testArrayOfObjectsValidation(): void
365412

366413
public function testIntegerValidation(): void
367414
{
368-
$validator = new Structure(new Document($this->collection));
415+
$validator = new Structure(
416+
new Document($this->collection),
417+
'int'
418+
);
369419

370420
$this->assertEquals(false, $validator->isValid(new Document([
371421
'$collection' => ID::custom('posts'),
@@ -396,7 +446,10 @@ public function testIntegerValidation(): void
396446

397447
public function testArrayOfIntegersValidation(): void
398448
{
399-
$validator = new Structure(new Document($this->collection));
449+
$validator = new Structure(
450+
new Document($this->collection),
451+
'int'
452+
);
400453

401454
$this->assertEquals(true, $validator->isValid(new Document([
402455
'$collection' => ID::custom('posts'),
@@ -451,7 +504,10 @@ public function testArrayOfIntegersValidation(): void
451504

452505
public function testFloatValidation(): void
453506
{
454-
$validator = new Structure(new Document($this->collection));
507+
$validator = new Structure(
508+
new Document($this->collection),
509+
'int'
510+
);
455511

456512
$this->assertEquals(false, $validator->isValid(new Document([
457513
'$collection' => ID::custom('posts'),
@@ -482,7 +538,10 @@ public function testFloatValidation(): void
482538

483539
public function testBooleanValidation(): void
484540
{
485-
$validator = new Structure(new Document($this->collection));
541+
$validator = new Structure(
542+
new Document($this->collection),
543+
'int'
544+
);
486545

487546
$this->assertEquals(false, $validator->isValid(new Document([
488547
'$collection' => ID::custom('posts'),
@@ -513,7 +572,10 @@ public function testBooleanValidation(): void
513572

514573
public function testFormatValidation(): void
515574
{
516-
$validator = new Structure(new Document($this->collection));
575+
$validator = new Structure(
576+
new Document($this->collection),
577+
'int'
578+
);
517579

518580
$this->assertEquals(false, $validator->isValid(new Document([
519581
'$collection' => ID::custom('posts'),
@@ -531,7 +593,10 @@ public function testFormatValidation(): void
531593

532594
public function testIntegerMaxRange(): void
533595
{
534-
$validator = new Structure(new Document($this->collection));
596+
$validator = new Structure(
597+
new Document($this->collection),
598+
'int'
599+
);
535600

536601
$this->assertEquals(false, $validator->isValid(new Document([
537602
'$collection' => ID::custom('posts'),
@@ -549,7 +614,10 @@ public function testIntegerMaxRange(): void
549614

550615
public function testDoubleUnsigned(): void
551616
{
552-
$validator = new Structure(new Document($this->collection));
617+
$validator = new Structure(
618+
new Document($this->collection),
619+
'int'
620+
);
553621

554622
$this->assertEquals(false, $validator->isValid(new Document([
555623
'$collection' => ID::custom('posts'),
@@ -567,7 +635,10 @@ public function testDoubleUnsigned(): void
567635

568636
public function testDoubleMaxRange(): void
569637
{
570-
$validator = new Structure(new Document($this->collection));
638+
$validator = new Structure(
639+
new Document($this->collection),
640+
'int'
641+
);
571642

572643
$this->assertEquals(false, $validator->isValid(new Document([
573644
'$collection' => ID::custom('posts'),
@@ -581,4 +652,75 @@ public function testDoubleMaxRange(): void
581652
])));
582653
}
583654

655+
public function testId(): void
656+
{
657+
$validator = new Structure(
658+
new Document($this->collection),
659+
'int'
660+
);
661+
662+
$id = '1000';
663+
$mongoid = '507f1f77bcf86cd799439011';
664+
665+
/**
666+
* Sql
667+
*/
668+
669+
$this->assertEquals(true, $validator->isValid(new Document([
670+
'$collection' => ID::custom('posts'),
671+
'title' => 'My Title',
672+
'description' => null,
673+
'rating' => 5,
674+
'price' => 1.99,
675+
'published' => true,
676+
'tags' => ['dog', 'cat', 'mouse'],
677+
'feedback' => 'team@appwrite.io',
678+
'id' => $id,
679+
])));
680+
681+
$this->assertEquals(false, $validator->isValid(new Document([
682+
'$collection' => ID::custom('posts'),
683+
'title' => 'My Title',
684+
'description' => null,
685+
'rating' => 5,
686+
'price' => 1.99,
687+
'published' => true,
688+
'tags' => ['dog', 'cat', 'mouse'],
689+
'feedback' => 'team@appwrite.io',
690+
'id' => $mongoid,
691+
])));
692+
693+
/**
694+
* Mongo
695+
*/
696+
$validator = new Structure(
697+
new Document($this->collection),
698+
'string'
699+
);
700+
701+
$this->assertEquals(true, $validator->isValid(new Document([
702+
'$collection' => ID::custom('posts'),
703+
'title' => 'My Title',
704+
'description' => null,
705+
'rating' => 5,
706+
'price' => 1.99,
707+
'published' => true,
708+
'tags' => ['dog', 'cat', 'mouse'],
709+
'feedback' => 'team@appwrite.io',
710+
'id' => $mongoid,
711+
])));
712+
713+
$this->assertEquals(true, $validator->isValid(new Document([
714+
'$collection' => ID::custom('posts'),
715+
'title' => 'My Title',
716+
'description' => null,
717+
'rating' => 5,
718+
'price' => 1.99,
719+
'published' => true,
720+
'tags' => ['dog', 'cat', 'mouse'],
721+
'feedback' => 'team@appwrite.io',
722+
'id' => $mongoid,
723+
])));
724+
}
725+
584726
}

0 commit comments

Comments
 (0)