Skip to content

Commit 1f868a5

Browse files
authored
test: hide the local .env and unset encryption settings (#10783)
1 parent aa00085 commit 1f868a5

4 files changed

Lines changed: 93 additions & 14 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Encryption;
15+
16+
use CodeIgniter\Superglobals;
17+
use Config\Services;
18+
19+
/**
20+
* Hides the local ".env" and the "encryption.*" values loaded from it.
21+
*/
22+
trait WithoutEncryptionEnvTrait
23+
{
24+
public static function setUpBeforeClassWithoutEncryptionEnv(): void
25+
{
26+
$superglobals = new Superglobals();
27+
Services::injectMock('superglobals', $superglobals);
28+
29+
if (! is_file(ROOTPATH . '.env')) {
30+
return;
31+
}
32+
33+
rename(ROOTPATH . '.env', ROOTPATH . '.env.bak');
34+
35+
foreach (array_keys($_ENV) as $key) {
36+
if (! str_starts_with($key, 'encryption.')) {
37+
continue;
38+
}
39+
40+
putenv($key);
41+
unset($_ENV[$key]);
42+
$superglobals->unsetServer($key);
43+
}
44+
}
45+
46+
public static function tearDownAfterClassWithoutEncryptionEnv(): void
47+
{
48+
Services::resetSingle('superglobals');
49+
50+
if (is_file(ROOTPATH . '.env.bak')) {
51+
rename(ROOTPATH . '.env.bak', ROOTPATH . '.env');
52+
}
53+
}
54+
}

‎tests/system/Encryption/EncryptionTest.php‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,29 @@
1313

1414
namespace CodeIgniter\Encryption;
1515

16-
use CodeIgniter\Config\Services as CodeIgniterServices;
1716
use CodeIgniter\Encryption\Exceptions\EncryptionException;
1817
use CodeIgniter\Encryption\Handlers\OpenSSLHandler;
19-
use CodeIgniter\Superglobals;
2018
use CodeIgniter\Test\CIUnitTestCase;
2119
use Config\Encryption as EncryptionConfig;
2220
use Config\Services;
2321
use PHPUnit\Framework\Attributes\Group;
22+
use Tests\Support\Encryption\WithoutEncryptionEnvTrait;
2423

2524
/**
2625
* @internal
2726
*/
2827
#[Group('Others')]
2928
final class EncryptionTest extends CIUnitTestCase
3029
{
30+
use WithoutEncryptionEnvTrait;
31+
3132
private Encryption $encryption;
3233

3334
public static function setUpBeforeClass(): void
3435
{
3536
parent::setUpBeforeClass();
3637

37-
CodeIgniterServices::injectMock('superglobals', new Superglobals());
38-
39-
if (is_file(ROOTPATH . '.env')) {
40-
rename(ROOTPATH . '.env', ROOTPATH . '.env.bak');
41-
42-
putenv('encryption.key');
43-
unset($_ENV['encryption.key']);
44-
service('superglobals')->unsetServer('encryption.key');
45-
}
38+
self::setUpBeforeClassWithoutEncryptionEnv();
4639
}
4740

4841
protected function setUp(): void
@@ -56,9 +49,7 @@ public static function tearDownAfterClass(): void
5649
{
5750
parent::tearDownAfterClass();
5851

59-
if (is_file(ROOTPATH . '.env.bak')) {
60-
rename(ROOTPATH . '.env.bak', ROOTPATH . '.env');
61-
}
52+
self::tearDownAfterClassWithoutEncryptionEnv();
6253
}
6354

6455
/**

‎tests/system/Encryption/Handlers/OpenSSLHandlerTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,32 @@
1818
use CodeIgniter\Test\CIUnitTestCase;
1919
use Config\Encryption as EncryptionConfig;
2020
use PHPUnit\Framework\Attributes\Group;
21+
use Tests\Support\Encryption\WithoutEncryptionEnvTrait;
2122

2223
/**
2324
* @internal
2425
*/
2526
#[Group('Others')]
2627
final class OpenSSLHandlerTest extends CIUnitTestCase
2728
{
29+
use WithoutEncryptionEnvTrait;
30+
2831
private Encryption $encryption;
2932

33+
public static function setUpBeforeClass(): void
34+
{
35+
parent::setUpBeforeClass();
36+
37+
self::setUpBeforeClassWithoutEncryptionEnv();
38+
}
39+
40+
public static function tearDownAfterClass(): void
41+
{
42+
parent::tearDownAfterClass();
43+
44+
self::tearDownAfterClassWithoutEncryptionEnv();
45+
}
46+
3047
protected function setUp(): void
3148
{
3249
if (! extension_loaded('openssl')) {

‎tests/system/Encryption/Handlers/SodiumHandlerTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,33 @@
1818
use CodeIgniter\Test\CIUnitTestCase;
1919
use Config\Encryption as EncryptionConfig;
2020
use PHPUnit\Framework\Attributes\Group;
21+
use Tests\Support\Encryption\WithoutEncryptionEnvTrait;
2122

2223
/**
2324
* @internal
2425
*/
2526
#[Group('Others')]
2627
final class SodiumHandlerTest extends CIUnitTestCase
2728
{
29+
use WithoutEncryptionEnvTrait;
30+
2831
private Encryption $encryption;
2932
private EncryptionConfig $config;
3033

34+
public static function setUpBeforeClass(): void
35+
{
36+
parent::setUpBeforeClass();
37+
38+
self::setUpBeforeClassWithoutEncryptionEnv();
39+
}
40+
41+
public static function tearDownAfterClass(): void
42+
{
43+
parent::tearDownAfterClass();
44+
45+
self::tearDownAfterClassWithoutEncryptionEnv();
46+
}
47+
3148
protected function setUp(): void
3249
{
3350
if (! extension_loaded('sodium')) {

0 commit comments

Comments
 (0)