Skip to content

Commit 3eac5ab

Browse files
committed
add IniTestTrait to easy backup ini values
1 parent 0cf729d commit 3eac5ab

4 files changed

Lines changed: 84 additions & 2 deletions

File tree

system/Test/IniTestTrait.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Test;
13+
14+
trait IniTestTrait
15+
{
16+
private array $iniSettings = [];
17+
18+
private function backupIniValues(array $keys): void
19+
{
20+
foreach ($keys as $key) {
21+
$this->iniSettings[$key] = ini_get($key);
22+
}
23+
}
24+
25+
private function restoreIniValues(): void
26+
{
27+
foreach ($this->iniSettings as $key => $value) {
28+
ini_set($key, $value);
29+
}
30+
31+
$this->iniSettings = [];
32+
}
33+
}
File renamed without changes.

tests/system/Debug/ExceptionHandlerTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use App\Controllers\Home;
1515
use CodeIgniter\Exceptions\PageNotFoundException;
1616
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\IniTestTrait;
1718
use CodeIgniter\Test\StreamFilterTrait;
1819
use Config\Exceptions as ExceptionsConfig;
1920
use Config\Services;
@@ -27,6 +28,7 @@
2728
final class ExceptionHandlerTest extends CIUnitTestCase
2829
{
2930
use StreamFilterTrait;
31+
use IniTestTrait;
3032

3133
private ExceptionHandler $handler;
3234

@@ -240,14 +242,20 @@ public function testMaskSensitiveDataTraceDataKey(): void
240242

241243
public function testHighlightFile(): void
242244
{
245+
$this->backupIniValues([
246+
'highlight.comment', 'highlight.default', 'highlight.html', 'highlight.keyword', 'highlight.string',
247+
]);
248+
243249
$highlightFile = $this->getPrivateMethodInvoker($this->handler, 'highlightFile');
244250
$result = $highlightFile(SUPPORTPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Hello.php', 16);
245-
$resultFile = version_compare(PHP_VERSION, '8.3.0', '<') ?
246-
'highlightFile_pre_8_3_0.html' :
251+
$resultFile = PHP_VERSION_ID < 80300 ?
252+
'highlightFile_pre_80300.html' :
247253
'highlightFile.html';
248254

249255
$expected = file_get_contents(SUPPORTPATH . 'Debug' . DIRECTORY_SEPARATOR . $resultFile);
250256

251257
$this->assertSame($expected, $result);
258+
259+
$this->restoreIniValues();
252260
}
253261
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Test;
13+
14+
use ReflectionException;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class IniTestTraitTest extends CIUnitTestCase
20+
{
21+
use IniTestTrait;
22+
23+
/**
24+
* @throws ReflectionException
25+
*/
26+
public function testBackupAndRestoreIniValues(): void
27+
{
28+
$this->backupIniValues(['highlight.default']);
29+
$backup = $this->getPrivateProperty($this, 'iniSettings');
30+
$this->assertSame('#0000BB', $backup['highlight.default']);
31+
32+
ini_set('highlight.default', '#FFFFFF');
33+
$this->assertSame('#FFFFFF', ini_get('highlight.default'));
34+
35+
$this->restoreIniValues();
36+
$this->assertSame('#0000BB', ini_get('highlight.default'));
37+
38+
$backup = $this->getPrivateProperty($this, 'iniSettings');
39+
$this->assertSame([], $backup);
40+
}
41+
}

0 commit comments

Comments
 (0)