Skip to content

Commit fc44bb3

Browse files
authored
refactor: make random-order API tests deterministic (#9983)
1 parent c38abf9 commit fc44bb3

File tree

6 files changed

+64
-62
lines changed

6 files changed

+64
-62
lines changed

tests/system/API/ResponseTraitTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ protected function setUp(): void
5353
{
5454
parent::setUp();
5555

56+
Services::superglobals()->setGetArray([]);
5657
$this->formatter = new JSONFormatter();
5758
}
5859

60+
protected function tearDown(): void
61+
{
62+
Services::superglobals()->setGetArray([]);
63+
64+
parent::tearDown();
65+
}
66+
5967
private function createAppConfig(): App
6068
{
6169
$config = new App();
@@ -826,7 +834,7 @@ public function testPaginateWithPageParameter(): void
826834

827835
// Create controller with page=2 in query string
828836
$controller = $this->makeController('/api/items?page=2');
829-
Services::superglobals()->setGet('page', '2');
837+
$this->request->setGlobal('get', ['page' => '2']);
830838

831839
$this->invoke($controller, 'paginate', [$model, 20]);
832840

@@ -847,8 +855,8 @@ public function testPaginateLinksStructure(): void
847855

848856
$model = $this->createMockModelWithPager($data, 2, 20, 100, 5);
849857

850-
Services::superglobals()->setGet('page', '2');
851858
$controller = $this->makeController('/api/items?page=2');
859+
$this->request->setGlobal('get', ['page' => '2']);
852860

853861
$this->invoke($controller, 'paginate', [$model, 20]);
854862

@@ -893,8 +901,8 @@ public function testPaginateLastPageNoNextLink(): void
893901

894902
$model = $this->createMockModelWithPager($data, 3, 20, 50, 3);
895903

896-
Services::superglobals()->setGet('page', '3');
897904
$controller = $this->makeController('/api/items?page=3');
905+
$this->request->setGlobal('get', ['page' => '3']);
898906

899907
$this->invoke($controller, 'paginate', [$model, 20]);
900908

@@ -912,8 +920,8 @@ public function testPaginateLinkHeader(): void
912920

913921
$model = $this->createMockModelWithPager($data, 2, 20, 100, 5);
914922

915-
Services::superglobals()->setGet('page', '2');
916923
$controller = $this->makeController('/api/items?page=2');
924+
$this->request->setGlobal('get', ['page' => '2']);
917925

918926
$this->invoke($controller, 'paginate', [$model, 20]);
919927

@@ -1018,9 +1026,8 @@ public function testPaginatePreservesOtherQueryParameters(): void
10181026

10191027
$model = $this->createMockModelWithPager($data, 1, 20, 50, 3);
10201028

1021-
Services::superglobals()->setGet('filter', 'active');
1022-
Services::superglobals()->setGet('sort', 'name');
10231029
$controller = $this->makeController('/api/items?filter=active&sort=name');
1030+
$this->request->setGlobal('get', ['filter' => 'active', 'sort' => 'name']);
10241031

10251032
$this->invoke($controller, 'paginate', [$model, 20]);
10261033

tests/system/API/TransformerTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use CodeIgniter\HTTP\UserAgent;
2020
use CodeIgniter\Test\CIUnitTestCase;
2121
use CodeIgniter\Test\Mock\MockAppConfig;
22+
use Config\Services;
2223
use PHPUnit\Framework\Attributes\Group;
2324
use stdClass;
2425

@@ -28,6 +29,20 @@
2829
#[Group('Others')]
2930
final class TransformerTest extends CIUnitTestCase
3031
{
32+
protected function setUp(): void
33+
{
34+
parent::setUp();
35+
36+
Services::superglobals()->setGetArray([]);
37+
}
38+
39+
protected function tearDown(): void
40+
{
41+
Services::superglobals()->setGetArray([]);
42+
43+
parent::tearDown();
44+
}
45+
3146
private function createMockRequest(string $query = ''): IncomingRequest
3247
{
3348
$config = new MockAppConfig();
@@ -318,6 +333,9 @@ public function toArray(mixed $resource): array
318333
return $resource;
319334
}
320335

336+
/**
337+
* @return list<array{id: int, title: string}>
338+
*/
321339
protected function includePosts(): array
322340
{
323341
return [['id' => 1, 'title' => 'Post 1']];
@@ -341,6 +359,9 @@ public function toArray(mixed $resource): array
341359
return $resource;
342360
}
343361

362+
/**
363+
* @return list<array{id: int, title: string}>
364+
*/
344365
protected function includePosts(): array
345366
{
346367
return [['id' => 1, 'title' => 'Post 1']];
@@ -367,11 +388,17 @@ public function toArray(mixed $resource): array
367388
return $resource;
368389
}
369390

391+
/**
392+
* @return list<array{id: int, title: string}>
393+
*/
370394
protected function includePosts(): array
371395
{
372396
return [['id' => 1, 'title' => 'Post 1']];
373397
}
374398

399+
/**
400+
* @return list<array{id: int, text: string}>
401+
*/
375402
protected function includeComments(): array
376403
{
377404
return [['id' => 1, 'text' => 'Comment 1']];
@@ -399,6 +426,9 @@ public function toArray(mixed $resource): array
399426
return $resource;
400427
}
401428

429+
/**
430+
* @return list<array{id: int, title: string}>
431+
*/
402432
protected function includePosts(): array
403433
{
404434
return [['id' => 1, 'title' => 'Post 1']];
@@ -427,6 +457,9 @@ protected function getAllowedIncludes(): array
427457
return [];
428458
}
429459

460+
/**
461+
* @return list<array{id: int, title: string}>
462+
*/
430463
protected function includePosts(): array
431464
{
432465
return [['id' => 1, 'title' => 'Post 1']];
@@ -450,6 +483,9 @@ public function toArray(mixed $resource): array
450483
return $resource;
451484
}
452485

486+
/**
487+
* @return list<array{id: int, title: string}>
488+
*/
453489
protected function includePosts(): array
454490
{
455491
return [['id' => 1, 'title' => 'Post 1']];
@@ -502,6 +538,9 @@ public function toArray(mixed $resource): array
502538
return $resource;
503539
}
504540

541+
/**
542+
* @return list<array{id: int, title: string}>
543+
*/
505544
protected function includePosts(): array
506545
{
507546
return [['id' => 1, 'title' => 'Post 1']];
@@ -561,6 +600,9 @@ public function toArray(mixed $resource): array
561600
return ['id' => $resource['id'], 'name' => $resource['name']];
562601
}
563602

603+
/**
604+
* @return list<array{id: int, title: string}>
605+
*/
564606
protected function includePosts(): array
565607
{
566608
return [['id' => 1, 'title' => 'Post 1']];
@@ -584,6 +626,9 @@ public function toArray(mixed $resource): array
584626
return ['id' => $resource['id'], 'name' => $resource['name']];
585627
}
586628

629+
/**
630+
* @return list<array{id: int, title: string}>
631+
*/
587632
protected function includePosts(): array
588633
{
589634
return [['id' => 1, 'title' => 'Post 1']];

utils/phpstan-baseline/function.alreadyNarrowedType.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
parameters:
44
ignoreErrors:
55
-
6-
message: '#^Call to function property_exists\(\) with \$this\(class@anonymous/tests/system/API/ResponseTraitTest\.php\:190\) and ''stringAsHtml'' will always evaluate to true\.$#'
6+
message: '#^Call to function property_exists\(\) with \$this\(class@anonymous/tests/system/API/ResponseTraitTest\.php\:198\) and ''stringAsHtml'' will always evaluate to true\.$#'
77
count: 1
88
path: ../../tests/system/API/ResponseTraitTest.php
99

1010
-
11-
message: '#^Call to function property_exists\(\) with \$this\(class@anonymous/tests/system/API/ResponseTraitTest\.php\:310\) and ''stringAsHtml'' will always evaluate to true\.$#'
11+
message: '#^Call to function property_exists\(\) with \$this\(class@anonymous/tests/system/API/ResponseTraitTest\.php\:318\) and ''stringAsHtml'' will always evaluate to true\.$#'
1212
count: 1
1313
path: ../../tests/system/API/ResponseTraitTest.php

utils/phpstan-baseline/function.impossibleType.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ parameters:
88
path: ../../system/Debug/ExceptionHandler.php
99

1010
-
11-
message: '#^Call to function property_exists\(\) with \$this\(class@anonymous/tests/system/API/ResponseTraitTest\.php\:132\) and ''stringAsHtml'' will always evaluate to false\.$#'
11+
message: '#^Call to function property_exists\(\) with \$this\(class@anonymous/tests/system/API/ResponseTraitTest\.php\:140\) and ''stringAsHtml'' will always evaluate to false\.$#'
1212
count: 1
1313
path: ../../tests/system/API/ResponseTraitTest.php
1414

1515
-
16-
message: '#^Call to function property_exists\(\) with \$this\(class@anonymous/tests/system/API/ResponseTraitTest\.php\:630\) and ''stringAsHtml'' will always evaluate to false\.$#'
16+
message: '#^Call to function property_exists\(\) with \$this\(class@anonymous/tests/system/API/ResponseTraitTest\.php\:638\) and ''stringAsHtml'' will always evaluate to false\.$#'
1717
count: 1
1818
path: ../../tests/system/API/ResponseTraitTest.php

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 2126 errors
1+
# total 2116 errors
22

33
includes:
44
- argument.type.neon

utils/phpstan-baseline/missingType.iterableValue.neon

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 1269 errors
1+
# total 1259 errors
22

33
parameters:
44
ignoreErrors:
@@ -4742,56 +4742,6 @@ parameters:
47424742
count: 1
47434743
path: ../../system/View/Parser.php
47444744

4745-
-
4746-
message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:315\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#'
4747-
count: 1
4748-
path: ../../tests/system/API/TransformerTest.php
4749-
4750-
-
4751-
message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:338\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#'
4752-
count: 1
4753-
path: ../../tests/system/API/TransformerTest.php
4754-
4755-
-
4756-
message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:364\:\:includeComments\(\) return type has no value type specified in iterable type array\.$#'
4757-
count: 1
4758-
path: ../../tests/system/API/TransformerTest.php
4759-
4760-
-
4761-
message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:364\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#'
4762-
count: 1
4763-
path: ../../tests/system/API/TransformerTest.php
4764-
4765-
-
4766-
message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:396\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#'
4767-
count: 1
4768-
path: ../../tests/system/API/TransformerTest.php
4769-
4770-
-
4771-
message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:419\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#'
4772-
count: 1
4773-
path: ../../tests/system/API/TransformerTest.php
4774-
4775-
-
4776-
message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:447\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#'
4777-
count: 1
4778-
path: ../../tests/system/API/TransformerTest.php
4779-
4780-
-
4781-
message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:499\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#'
4782-
count: 1
4783-
path: ../../tests/system/API/TransformerTest.php
4784-
4785-
-
4786-
message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:558\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#'
4787-
count: 1
4788-
path: ../../tests/system/API/TransformerTest.php
4789-
4790-
-
4791-
message: '#^Method CodeIgniter\\API\\BaseTransformer@anonymous/tests/system/API/TransformerTest\.php\:581\:\:includePosts\(\) return type has no value type specified in iterable type array\.$#'
4792-
count: 1
4793-
path: ../../tests/system/API/TransformerTest.php
4794-
47954745
-
47964746
message: '#^Method CodeIgniter\\AutoReview\\ComposerJsonTest\:\:checkConfig\(\) has parameter \$fromComponent with no value type specified in iterable type array\.$#'
47974747
count: 1

0 commit comments

Comments
 (0)