forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClearLogsTest.php
More file actions
157 lines (118 loc) · 4.77 KB
/
ClearLogsTest.php
File metadata and controls
157 lines (118 loc) · 4.77 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?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\CLI\CLI;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockInputOutput;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
/**
* @internal
*/
#[Group('Others')]
final class ClearLogsTest extends CIUnitTestCase
{
use StreamFilterTrait;
private string $date;
protected function setUp(): void
{
parent::setUp();
// test runs on other tests may log errors since default threshold
// is now 4, so set this to a safe distance
$this->date = date('Y-m-d', strtotime('+1 year'));
command('logs:clear --force');
$this->resetStreamFilterBuffer();
$this->createDummyLogFiles();
}
protected function tearDown(): void
{
command('logs:clear --force');
$this->resetStreamFilterBuffer();
CLI::reset();
parent::tearDown();
}
private function createDummyLogFiles(): void
{
$date = $this->date;
$path = WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$date}.log";
// create 10 dummy log files
for ($i = 0; $i < 10; $i++) {
$newDate = date('Y-m-d', strtotime("+1 year -{$i} day"));
$path = str_replace($date, $newDate, $path);
file_put_contents($path, 'Lorem ipsum');
$date = $newDate;
}
}
public function testClearLogsUsingForce(): void
{
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
command('logs:clear --force');
$this->assertFileDoesNotExist(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . 'index.html');
$this->assertSame("Logs cleared.\n", preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()));
}
public function testClearLogsAbortsClearWithoutForce(): void
{
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$io = new MockInputOutput();
$io->setInputs(['n']);
CLI::setInputOutput($io);
command('logs:clear');
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$this->assertSame(
<<<'EOT'
Deleting logs aborted.
If you want, use the "--force" option to force delete all log files.
EOT,
preg_replace('/\e\[[^m]+m/', '', $io->getOutput(2) . $io->getOutput(3)),
);
}
public function testClearLogsAbortsClearWithoutForceWithDefaultAnswer(): void
{
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$io = new MockInputOutput();
$io->setInputs(['']);
CLI::setInputOutput($io);
command('logs:clear');
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$this->assertSame(
<<<'EOT'
Deleting logs aborted.
If you want, use the "--force" option to force delete all log files.
EOT,
preg_replace('/\e\[[^m]+m/', '', $io->getOutput(2) . $io->getOutput(3)),
);
}
public function testClearLogsWithoutForceButWithConfirmation(): void
{
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$io = new MockInputOutput();
$io->setInputs(['y']);
CLI::setInputOutput($io);
command('logs:clear');
$this->assertFileDoesNotExist(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$this->assertSame("Logs cleared.\n", preg_replace('/\e\[[^m]+m/', '', $io->getOutput(2)));
}
#[RequiresOperatingSystem('Darwin|Linux')]
public function testClearLogsFailsOnChmodFailure(): void
{
$path = WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log";
file_put_contents($path, 'Lorem ipsum');
// Attempt to make the file itself undeletable
chmod(dirname($path), 0555);
command('logs:clear --force');
// Restore attributes so other tests are not affected.
chmod(dirname($path), 0755);
$this->assertFileExists($path);
$this->assertSame("Error in deleting the logs files.\n", preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()));
}
}