From ccbddae5b46c6c694c4a4da1a360ecdeb697c62c Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 26 Oct 2023 07:13:09 +0900 Subject: [PATCH 1/4] test: reset state after testing Mocks should be removed. --- tests/system/Config/BaseConfigTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index 958387cc6b6d..c9780454b497 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -276,11 +276,14 @@ public function testDiscoveryNotEnabledWillNotPopulateRegistrarsArray(): void /** @var MockObject&Modules $modules */ $modules = $this->createMock(Modules::class); $modules->method('shouldDiscover')->with('registrars')->willReturn(false); - RegistrarConfig::setModules($modules); + $config = new RegistrarConfig(); $this->assertSame([], $config::$registrars); + + // Reset Modules Config. + RegistrarConfig::setModules(new Modules()); } public function testRedoingDiscoveryWillStillSetDidDiscoveryPropertyToTrue(): void @@ -295,5 +298,8 @@ public function testRedoingDiscoveryWillStillSetDidDiscoveryPropertyToTrue(): vo $config = new RegistrarConfig(); $this->assertTrue($this->getPrivateProperty($config, 'didDiscovery')); + + // Reset locator. + $this->resetServices(); } } From bfecd50ae0327292f5253d23a354e70483e48181 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 26 Oct 2023 07:23:57 +0900 Subject: [PATCH 2/4] docs: add @psalm-suppress --- tests/system/Config/BaseConfigTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index c9780454b497..94d60577e44a 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -271,6 +271,9 @@ public function testBadRegistrar(): void $this->assertSame('bar', $config->foo); } + /** + * @psalm-suppress UndefinedClass + */ public function testDiscoveryNotEnabledWillNotPopulateRegistrarsArray(): void { /** @var MockObject&Modules $modules */ @@ -286,6 +289,9 @@ public function testDiscoveryNotEnabledWillNotPopulateRegistrarsArray(): void RegistrarConfig::setModules(new Modules()); } + /** + * @psalm-suppress UndefinedClass + */ public function testRedoingDiscoveryWillStillSetDidDiscoveryPropertyToTrue(): void { /** @var FileLocator&MockObject $locator */ From 469eef4e6fb11de778088d10d76e7de3ea87a305 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 26 Oct 2023 14:40:18 +0900 Subject: [PATCH 3/4] feat: add reset() for testing --- system/Config/BaseConfig.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index 7daa6e1544c0..5dd6cde2ad02 100644 --- a/system/Config/BaseConfig.php +++ b/system/Config/BaseConfig.php @@ -83,6 +83,18 @@ public static function setModules(Modules $modules): void static::$moduleConfig = $modules; } + /** + * @internal For testing purposes only. + * @testTag + */ + public static function reset(): void + { + static::$registrars = []; + static::$override = true; + static::$didDiscovery = false; + static::$moduleConfig = null; + } + /** * Will attempt to get environment variables with names * that match the properties of the child class. From f42244fc64a93581cf6e9b90e35e2751378deaa4 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 26 Oct 2023 14:43:52 +0900 Subject: [PATCH 4/4] test: add tearDown() and tweak setUp() --- tests/system/Config/BaseConfigTest.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index 94d60577e44a..7c05af3a390e 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -49,8 +49,17 @@ protected function setUp(): void require $this->fixturesFolder . '/Encryption.php'; } - BaseConfig::$registrars = []; - BaseConfig::setModules(new Modules()); // reset to clean copy of Modules + BaseConfig::reset(); + } + + protected function tearDown(): void + { + parent::tearDown(); + + // This test modifies BaseConfig::$modules, so should reset. + BaseConfig::reset(); + // This test modifies Services locator, so should reset. + $this->resetServices(); } public function testBasicValues(): void @@ -284,9 +293,6 @@ public function testDiscoveryNotEnabledWillNotPopulateRegistrarsArray(): void $config = new RegistrarConfig(); $this->assertSame([], $config::$registrars); - - // Reset Modules Config. - RegistrarConfig::setModules(new Modules()); } /** @@ -304,8 +310,5 @@ public function testRedoingDiscoveryWillStillSetDidDiscoveryPropertyToTrue(): vo $config = new RegistrarConfig(); $this->assertTrue($this->getPrivateProperty($config, 'didDiscovery')); - - // Reset locator. - $this->resetServices(); } }