Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tests/_support/Encryption/WithoutEncryptionEnvTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* 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');
}
}
}
19 changes: 5 additions & 14 deletions tests/system/Encryption/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,29 @@

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
*/
#[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
Expand All @@ -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();
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/system/Encryption/Handlers/OpenSSLHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,32 @@
use CodeIgniter\Test\CIUnitTestCase;
use Config\Encryption as EncryptionConfig;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Encryption\WithoutEncryptionEnvTrait;

/**
* @internal
*/
#[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')) {
Expand Down
17 changes: 17 additions & 0 deletions tests/system/Encryption/Handlers/SodiumHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,33 @@
use CodeIgniter\Test\CIUnitTestCase;
use Config\Encryption as EncryptionConfig;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Encryption\WithoutEncryptionEnvTrait;

/**
* @internal
*/
#[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')) {
Expand Down
Loading