forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClearDebugbarTest.php
More file actions
118 lines (94 loc) · 3.5 KB
/
ClearDebugbarTest.php
File metadata and controls
118 lines (94 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Commands\Housekeeping;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
/**
* @internal
*/
#[Group('Others')]
final class ClearDebugbarTest extends CIUnitTestCase
{
use StreamFilterTrait;
private int $time;
protected function setUp(): void
{
parent::setUp();
command('debugbar:clear');
$this->resetStreamFilterBuffer();
$this->time = time();
$this->createDummyDebugbarJson();
}
protected function tearDown(): void
{
command('debugbar:clear');
$this->resetStreamFilterBuffer();
parent::tearDown();
}
private function createDummyDebugbarJson(): void
{
$time = $this->time;
$path = WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$time}.json";
// create 10 dummy debugbar json files
for ($i = 0; $i < 10; $i++) {
$path = str_replace((string) $time, (string) ($time - $i), $path);
file_put_contents($path, "{}\n");
$time -= $i;
}
}
public function testClearDebugbarWorks(): void
{
$this->assertFileExists(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json");
command('debugbar:clear');
$this->assertFileDoesNotExist(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json");
$this->assertFileExists(WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . 'index.html');
$this->assertSame(
"Debugbar cleared.\n",
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
);
}
#[RequiresOperatingSystem('Darwin|Linux')]
public function testClearDebugbarWithError(): void
{
$path = WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$this->time}.json";
// Attempt to make the file itself undeletable by setting the
// immutable/uchg flag on supported platforms.
$immutableSet = false;
if (str_starts_with(PHP_OS, 'Darwin')) {
@exec(sprintf('chflags uchg %s', escapeshellarg($path)), $output, $rc);
$immutableSet = $rc === 0;
} else {
// Try chattr on Linux with sudo (for containerized environments)
@exec('which chattr', $whichOut, $whichRc);
if ($whichRc === 0) {
@exec(sprintf('sudo chattr +i %s', escapeshellarg($path)), $output, $rc);
$immutableSet = $rc === 0;
}
}
if (! $immutableSet) {
$this->markTestSkipped('Cannot set file immutability in this environment');
}
command('debugbar:clear');
// Restore attributes so other tests are not affected.
if (str_starts_with(PHP_OS, 'Darwin')) {
@exec(sprintf('chflags nouchg %s', escapeshellarg($path)));
} else {
@exec(sprintf('sudo chattr -i %s', escapeshellarg($path)));
}
$this->assertFileExists($path);
$this->assertSame(
"Error deleting the debugbar JSON files.\n",
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
);
}
}