Skip to content

Commit 90b2e54

Browse files
authored
fix: resolve global state pollution causing random-order test failures in HTTP suite (#10372)
* fix: resolve random order execution failures in HTTP test suite * fix: resolve encoding negotiation pollution in NegotiateTest * style: run composer cs-fix on HTTP classes * fix: remove redundant null check for string typed parameter in MessageTrait * fix: address reviewer feedback regarding HTTP test isolation * test: isolate CorsTest to prevent global state pollution * test: enable random order execution verification for HTTP component * test: move resetServices before parent::setUp in HTTP test suite to preserve parent mocks
1 parent 8b8c9e4 commit 90b2e54

10 files changed

Lines changed: 36 additions & 12 deletions

.github/scripts/random-tests-config.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Events
2727
Files
2828
# Filters
2929
Format
30-
# HTTP
30+
HTTP
3131
# Helpers
3232
Honeypot
3333
HotReloader

tests/system/HTTP/CURLRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class CURLRequestTest extends CIUnitTestCase
3939

4040
protected function setUp(): void
4141
{
42+
$this->resetServices();
4243
parent::setUp();
4344

44-
$this->resetServices();
4545
Services::injectMock('superglobals', new Superglobals());
4646
$this->request = $this->getRequest();
4747
}

tests/system/HTTP/CorsTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@
1414
namespace CodeIgniter\HTTP;
1515

1616
use CodeIgniter\Test\CIUnitTestCase;
17+
use PHPUnit\Framework\Attributes\BackupGlobals;
1718
use PHPUnit\Framework\Attributes\Group;
1819

1920
/**
2021
* @internal
2122
*/
23+
#[BackupGlobals(true)]
2224
#[Group('Others')]
2325
final class CorsTest extends CIUnitTestCase
2426
{
27+
protected function setUp(): void
28+
{
29+
$this->resetServices();
30+
parent::setUp();
31+
}
32+
2533
/**
2634
* @param array{
2735
* allowedOrigins?: list<string>,

tests/system/HTTP/DownloadResponseTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use CodeIgniter\Test\CIUnitTestCase;
2121
use DateTime;
2222
use DateTimeZone;
23+
use PHPUnit\Framework\Attributes\BackupGlobals;
2324
use PHPUnit\Framework\Attributes\Group;
2425
use PHPUnit\Framework\Attributes\PreserveGlobalState;
2526
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
@@ -28,6 +29,7 @@
2829
/**
2930
* @internal
3031
*/
32+
#[BackupGlobals(true)]
3133
#[Group('SeparateProcess')]
3234
final class DownloadResponseTest extends CIUnitTestCase
3335
{

tests/system/HTTP/IncomingRequestTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ final class IncomingRequestTest extends CIUnitTestCase
4343
#[WithoutErrorHandler]
4444
protected function setUp(): void
4545
{
46+
$this->resetServices();
4647
parent::setUp();
4748

4849
$_ENV = $_SESSION = [];
@@ -280,7 +281,7 @@ public function testSetValidLocales(): void
280281
*/
281282
public function testNegotiatesLocale(): void
282283
{
283-
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr-FR); q=1.0, en; q=0.5');
284+
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr-FR; q=1.0, en; q=0.5');
284285

285286
$config = new App();
286287
$config->negotiateLocale = true;
@@ -295,7 +296,7 @@ public function testNegotiatesLocale(): void
295296

296297
public function testNegotiatesLocaleOnlyBroad(): void
297298
{
298-
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr); q=1.0, en; q=0.5');
299+
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr; q=1.0, en; q=0.5');
299300

300301
$config = new App();
301302
$config->negotiateLocale = true;
@@ -349,7 +350,7 @@ public function testNegotiatesEncoding(): void
349350
public function testNegotiatesLanguage(): void
350351
{
351352
$this->request->setHeader('Accept-Language', 'da, en-gb;q=0.8, en;q=0.7');
352-
$this->assertSame('en', $this->request->negotiate('language', ['en', 'da']));
353+
$this->assertSame('da', $this->request->negotiate('language', ['en', 'da']));
353354
}
354355

355356
public function testCanGrabGetRawJSON(): void

tests/system/HTTP/MessageTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,11 @@ public function testPopulateHeadersWithoutContentType(): void
259259

260260
$this->assertNull($this->message->header('content-type'));
261261

262-
putenv("CONTENT_TYPE={$originalEnv}");
262+
if ($originalEnv !== false) {
263+
putenv("CONTENT_TYPE={$originalEnv}");
264+
} else {
265+
putenv('CONTENT_TYPE');
266+
}
263267
}
264268

265269
public function testPopulateHeadersWithoutHTTP(): void

tests/system/HTTP/NegotiateTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use Config\App;
1919
use Config\Feature;
20+
use PHPUnit\Framework\Attributes\BackupGlobals;
2021
use PHPUnit\Framework\Attributes\Group;
2122

2223
/**
2324
* @internal
2425
*/
26+
#[BackupGlobals(true)]
2527
#[Group('Others')]
2628
final class NegotiateTest extends CIUnitTestCase
2729
{
@@ -30,6 +32,7 @@ final class NegotiateTest extends CIUnitTestCase
3032

3133
protected function setUp(): void
3234
{
35+
$this->resetServices();
3336
parent::setUp();
3437

3538
$config = new App();

tests/system/HTTP/RedirectResponseTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Config\Modules;
2525
use Config\Routing;
2626
use Config\Services;
27+
use PHPUnit\Framework\Attributes\BackupGlobals;
2728
use PHPUnit\Framework\Attributes\Group;
2829
use PHPUnit\Framework\Attributes\PreserveGlobalState;
2930
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
@@ -32,6 +33,7 @@
3233
/**
3334
* @internal
3435
*/
36+
#[BackupGlobals(true)]
3537
#[Group('SeparateProcess')]
3638
final class RedirectResponseTest extends CIUnitTestCase
3739
{
@@ -46,9 +48,8 @@ final class RedirectResponseTest extends CIUnitTestCase
4648
#[WithoutErrorHandler]
4749
protected function setUp(): void
4850
{
49-
parent::setUp();
50-
5151
$this->resetServices();
52+
parent::setUp();
5253

5354
Services::injectMock('superglobals', new Superglobals());
5455
service('superglobals')->setServer('REQUEST_METHOD', 'GET');

tests/system/HTTP/ResponseTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,26 @@
2222
use Config\App;
2323
use DateTime;
2424
use DateTimeZone;
25+
use PHPUnit\Framework\Attributes\BackupGlobals;
2526
use PHPUnit\Framework\Attributes\DataProvider;
2627
use PHPUnit\Framework\Attributes\Group;
2728

2829
/**
2930
* @internal
3031
*/
32+
#[BackupGlobals(true)]
3133
#[Group('Others')]
3234
final class ResponseTest extends CIUnitTestCase
3335
{
3436
private array $server;
3537

3638
protected function setUp(): void
3739
{
38-
Services::injectMock('superglobals', new Superglobals());
39-
$this->server = service('superglobals')->getServerArray();
40-
40+
$this->resetServices();
4141
parent::setUp();
4242

43-
$this->resetServices();
43+
Services::injectMock('superglobals', new Superglobals());
44+
$this->server = service('superglobals')->getServerArray();
4445
}
4546

4647
protected function tearDown(): void
@@ -169,6 +170,7 @@ public function testSetLink(): void
169170
Factories::injectMock('config', 'App', $config);
170171

171172
$this->resetServices();
173+
Services::injectMock('superglobals', new Superglobals([], []));
172174

173175
$response = new Response($config);
174176
$pager = service('pager');

tests/system/HTTP/UserAgentTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
namespace CodeIgniter\HTTP;
1515

1616
use CodeIgniter\Test\CIUnitTestCase;
17+
use PHPUnit\Framework\Attributes\BackupGlobals;
1718
use PHPUnit\Framework\Attributes\Group;
1819

1920
/**
2021
* @internal
2122
*/
23+
#[BackupGlobals(true)]
2224
#[Group('Others')]
2325
final class UserAgentTest extends CIUnitTestCase
2426
{
@@ -28,6 +30,7 @@ final class UserAgentTest extends CIUnitTestCase
2830

2931
protected function setUp(): void
3032
{
33+
$this->resetServices();
3134
parent::setUp();
3235

3336
// set a baseline user agent

0 commit comments

Comments
 (0)