From c14514aee548b38be6c1ca8d481f5e8581d4a6a3 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Wed, 18 Nov 2026 21:29:53 +0800 Subject: [PATCH] test: hide the local `.env` and unset encryption settings --- .../Encryption/WithoutEncryptionEnvTrait.php | 54 +++++++++++++++++++ tests/system/Encryption/EncryptionTest.php | 19 ++----- .../Handlers/OpenSSLHandlerTest.php | 17 ++++++ .../Encryption/Handlers/SodiumHandlerTest.php | 17 ++++++ 4 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 tests/_support/Encryption/WithoutEncryptionEnvTrait.php diff --git a/tests/_support/Encryption/WithoutEncryptionEnvTrait.php b/tests/_support/Encryption/WithoutEncryptionEnvTrait.php new file mode 100644 index 000000000000..2c99291b5e5f --- /dev/null +++ b/tests/_support/Encryption/WithoutEncryptionEnvTrait.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace Tests\Support\Encryption; + +use CodeIgniter\Superglobals; +use Config\Services; + +/** + * Hides the local ".env" and the "encryption.*" values loaded from it. + */ +trait WithoutEncryptionEnvTrait +{ + public static function setUpBeforeClassWithoutEncryptionEnv(): void + { + $superglobals = new Superglobals(); + Services::injectMock('superglobals', $superglobals); + + if (! is_file(ROOTPATH . '.env')) { + return; + } + + rename(ROOTPATH . '.env', ROOTPATH . '.env.bak'); + + foreach (array_keys($_ENV) as $key) { + if (! str_starts_with($key, 'encryption.')) { + continue; + } + + putenv($key); + unset($_ENV[$key]); + $superglobals->unsetServer($key); + } + } + + public static function tearDownAfterClassWithoutEncryptionEnv(): void + { + Services::resetSingle('superglobals'); + + if (is_file(ROOTPATH . '.env.bak')) { + rename(ROOTPATH . '.env.bak', ROOTPATH . '.env'); + } + } +} diff --git a/tests/system/Encryption/EncryptionTest.php b/tests/system/Encryption/EncryptionTest.php index 81c4554336e0..abea05da717d 100644 --- a/tests/system/Encryption/EncryptionTest.php +++ b/tests/system/Encryption/EncryptionTest.php @@ -13,14 +13,13 @@ namespace CodeIgniter\Encryption; -use CodeIgniter\Config\Services as CodeIgniterServices; use CodeIgniter\Encryption\Exceptions\EncryptionException; use CodeIgniter\Encryption\Handlers\OpenSSLHandler; -use CodeIgniter\Superglobals; use CodeIgniter\Test\CIUnitTestCase; use Config\Encryption as EncryptionConfig; use Config\Services; use PHPUnit\Framework\Attributes\Group; +use Tests\Support\Encryption\WithoutEncryptionEnvTrait; /** * @internal @@ -28,21 +27,15 @@ #[Group('Others')] final class EncryptionTest extends CIUnitTestCase { + use WithoutEncryptionEnvTrait; + private Encryption $encryption; public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - CodeIgniterServices::injectMock('superglobals', new Superglobals()); - - if (is_file(ROOTPATH . '.env')) { - rename(ROOTPATH . '.env', ROOTPATH . '.env.bak'); - - putenv('encryption.key'); - unset($_ENV['encryption.key']); - service('superglobals')->unsetServer('encryption.key'); - } + self::setUpBeforeClassWithoutEncryptionEnv(); } protected function setUp(): void @@ -56,9 +49,7 @@ public static function tearDownAfterClass(): void { parent::tearDownAfterClass(); - if (is_file(ROOTPATH . '.env.bak')) { - rename(ROOTPATH . '.env.bak', ROOTPATH . '.env'); - } + self::tearDownAfterClassWithoutEncryptionEnv(); } /** diff --git a/tests/system/Encryption/Handlers/OpenSSLHandlerTest.php b/tests/system/Encryption/Handlers/OpenSSLHandlerTest.php index 01254f31b810..78ca72b9d00b 100644 --- a/tests/system/Encryption/Handlers/OpenSSLHandlerTest.php +++ b/tests/system/Encryption/Handlers/OpenSSLHandlerTest.php @@ -18,6 +18,7 @@ use CodeIgniter\Test\CIUnitTestCase; use Config\Encryption as EncryptionConfig; use PHPUnit\Framework\Attributes\Group; +use Tests\Support\Encryption\WithoutEncryptionEnvTrait; /** * @internal @@ -25,8 +26,24 @@ #[Group('Others')] final class OpenSSLHandlerTest extends CIUnitTestCase { + use WithoutEncryptionEnvTrait; + private Encryption $encryption; + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + + self::setUpBeforeClassWithoutEncryptionEnv(); + } + + public static function tearDownAfterClass(): void + { + parent::tearDownAfterClass(); + + self::tearDownAfterClassWithoutEncryptionEnv(); + } + protected function setUp(): void { if (! extension_loaded('openssl')) { diff --git a/tests/system/Encryption/Handlers/SodiumHandlerTest.php b/tests/system/Encryption/Handlers/SodiumHandlerTest.php index fe63cec36fb9..114c8c9f7e32 100644 --- a/tests/system/Encryption/Handlers/SodiumHandlerTest.php +++ b/tests/system/Encryption/Handlers/SodiumHandlerTest.php @@ -18,6 +18,7 @@ use CodeIgniter\Test\CIUnitTestCase; use Config\Encryption as EncryptionConfig; use PHPUnit\Framework\Attributes\Group; +use Tests\Support\Encryption\WithoutEncryptionEnvTrait; /** * @internal @@ -25,9 +26,25 @@ #[Group('Others')] final class SodiumHandlerTest extends CIUnitTestCase { + use WithoutEncryptionEnvTrait; + private Encryption $encryption; private EncryptionConfig $config; + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + + self::setUpBeforeClassWithoutEncryptionEnv(); + } + + public static function tearDownAfterClass(): void + { + parent::tearDownAfterClass(); + + self::tearDownAfterClassWithoutEncryptionEnv(); + } + protected function setUp(): void { if (! extension_loaded('sodium')) {