Skip to content

Commit 8b0752a

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.5
Conflicts: tests/_support/Log/Handlers/TestHandler.php
2 parents a663b36 + efb3816 commit 8b0752a

File tree

4 files changed

+79
-22
lines changed

4 files changed

+79
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"phpunit/phpcov": "^8.2",
3333
"phpunit/phpunit": "^9.1",
3434
"predis/predis": "^1.1 || ^2.0",
35-
"rector/rector": "1.0.1",
35+
"rector/rector": "1.0.2",
3636
"vimeo/psalm": "^5.0"
3737
},
3838
"replace": {

tests/_support/Log/Handlers/TestHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Tests\Support\Log\Handlers;
1515

16+
use CodeIgniter\I18n\Time;
1617
use CodeIgniter\Log\Handlers\FileHandler;
1718

1819
/**
@@ -40,7 +41,7 @@ public function __construct(array $config)
4041
parent::__construct($config);
4142

4243
$this->handles = $config['handles'] ?? [];
43-
$this->destination = $this->path . 'log-' . date('Y-m-d') . '.' . $this->fileExtension;
44+
$this->destination = $this->path . 'log-' . Time::now()->format('Y-m-d') . '.' . $this->fileExtension;
4445

4546
self::$logs = [];
4647
}
@@ -56,7 +57,7 @@ public function __construct(array $config)
5657
*/
5758
public function handle($level, $message): bool
5859
{
59-
$date = date($this->dateFormat);
60+
$date = Time::now()->format($this->dateFormat);
6061

6162
self::$logs[] = strtoupper($level) . ' - ' . $date . ' --> ' . $message;
6263

tests/system/HTTP/RedirectExceptionTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\HTTP;
1515

1616
use CodeIgniter\HTTP\Exceptions\RedirectException;
17+
use CodeIgniter\I18n\Time;
1718
use CodeIgniter\Log\Logger;
1819
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
1920
use Config\Services;
@@ -34,6 +35,14 @@ protected function setUp(): void
3435
Services::injectMock('logger', new Logger(new LoggerConfig()));
3536
}
3637

38+
protected function tearDown(): void
39+
{
40+
parent::tearDown();
41+
42+
// Reset the current time.
43+
Time::setTestNow();
44+
}
45+
3746
public function testResponse(): void
3847
{
3948
$response = (new RedirectException(
@@ -69,8 +78,10 @@ public function testResponseWithoutStatusCode(): void
6978

7079
public function testLoggingLocationHeader(): void
7180
{
81+
Time::setTestNow('2023-11-25 12:00:00');
82+
7283
$uri = 'http://location';
73-
$expected = 'INFO - ' . date('Y-m-d') . ' --> REDIRECTED ROUTE at ' . $uri;
84+
$expected = 'INFO - ' . Time::now()->format('Y-m-d') . ' --> REDIRECTED ROUTE at ' . $uri;
7485
$response = (new RedirectException(Services::response()->redirect($uri)))->getResponse();
7586

7687
$logs = TestHandler::getLogs();
@@ -82,8 +93,10 @@ public function testLoggingLocationHeader(): void
8293

8394
public function testLoggingRefreshHeader(): void
8495
{
96+
Time::setTestNow('2023-11-25 12:00:00');
97+
8598
$uri = 'http://location';
86-
$expected = 'INFO - ' . date('Y-m-d') . ' --> REDIRECTED ROUTE at ' . $uri;
99+
$expected = 'INFO - ' . Time::now()->format('Y-m-d') . ' --> REDIRECTED ROUTE at ' . $uri;
87100
$response = (new RedirectException(Services::response()->redirect($uri, 'refresh')))->getResponse();
88101

89102
$logs = TestHandler::getLogs();

tests/system/Log/LoggerTest.php

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Log;
1515

1616
use CodeIgniter\Exceptions\FrameworkException;
17+
use CodeIgniter\I18n\Time;
1718
use CodeIgniter\Log\Exceptions\LogException;
1819
use CodeIgniter\Test\CIUnitTestCase;
1920
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
@@ -28,6 +29,14 @@
2829
*/
2930
final class LoggerTest extends CIUnitTestCase
3031
{
32+
protected function tearDown(): void
33+
{
34+
parent::tearDown();
35+
36+
// Reset the current time.
37+
Time::setTestNow();
38+
}
39+
3140
public function testThrowsExceptionWithBadHandlerSettings(): void
3241
{
3342
$config = new LoggerConfig();
@@ -66,7 +75,9 @@ public function testLogActuallyLogs(): void
6675
$config = new LoggerConfig();
6776
$logger = new Logger($config);
6877

69-
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message';
78+
Time::setTestNow('2023-11-25 12:00:00');
79+
80+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message';
7081
$logger->log('debug', 'Test message');
7182

7283
$logs = TestHandler::getLogs();
@@ -96,7 +107,9 @@ public function testLogInterpolatesMessage(): void
96107

97108
$logger = new Logger($config);
98109

99-
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message bar baz';
110+
Time::setTestNow('2023-11-25 12:00:00');
111+
112+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message bar baz';
100113

101114
$logger->log('debug', 'Test message {foo} {bar}', ['foo' => 'bar', 'bar' => 'baz']);
102115

@@ -112,8 +125,10 @@ public function testLogInterpolatesPost(): void
112125

113126
$logger = new Logger($config);
114127

128+
Time::setTestNow('2023-11-25 12:00:00');
129+
115130
$_POST = ['foo' => 'bar'];
116-
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message $_POST: ' . print_r($_POST, true);
131+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message $_POST: ' . print_r($_POST, true);
117132

118133
$logger->log('debug', 'Test message {post_vars}');
119134

@@ -129,8 +144,10 @@ public function testLogInterpolatesGet(): void
129144

130145
$logger = new Logger($config);
131146

147+
Time::setTestNow('2023-11-25 12:00:00');
148+
132149
$_GET = ['bar' => 'baz'];
133-
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message $_GET: ' . print_r($_GET, true);
150+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message $_GET: ' . print_r($_GET, true);
134151

135152
$logger->log('debug', 'Test message {get_vars}');
136153

@@ -146,8 +163,10 @@ public function testLogInterpolatesSession(): void
146163

147164
$logger = new Logger($config);
148165

166+
Time::setTestNow('2023-11-25 12:00:00');
167+
149168
$_SESSION = ['xxx' => 'yyy'];
150-
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message $_SESSION: ' . print_r($_SESSION, true);
169+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message $_SESSION: ' . print_r($_SESSION, true);
151170

152171
$logger->log('debug', 'Test message {session_vars}');
153172

@@ -163,7 +182,9 @@ public function testLogInterpolatesCurrentEnvironment(): void
163182

164183
$logger = new Logger($config);
165184

166-
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message ' . ENVIRONMENT;
185+
Time::setTestNow('2023-11-25 12:00:00');
186+
187+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message ' . ENVIRONMENT;
167188

168189
$logger->log('debug', 'Test message {env}');
169190

@@ -179,9 +200,11 @@ public function testLogInterpolatesEnvironmentVars(): void
179200

180201
$logger = new Logger($config);
181202

203+
Time::setTestNow('2023-11-25 12:00:00');
204+
182205
$_ENV['foo'] = 'bar';
183206

184-
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message bar';
207+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message bar';
185208

186209
$logger->log('debug', 'Test message {env:foo}');
187210

@@ -213,7 +236,9 @@ public function testLogInterpolatesExceptions(): void
213236
$config = new LoggerConfig();
214237
$logger = new Logger($config);
215238

216-
$expected = 'ERROR - ' . date('Y-m-d') . ' --> [ERROR] These are not the droids you are looking for';
239+
Time::setTestNow('2023-11-25 12:00:00');
240+
241+
$expected = 'ERROR - ' . Time::now()->format('Y-m-d') . ' --> [ERROR] These are not the droids you are looking for';
217242

218243
try {
219244
throw new Exception('These are not the droids you are looking for');
@@ -232,7 +257,9 @@ public function testEmergencyLogsCorrectly(): void
232257
$config = new LoggerConfig();
233258
$logger = new Logger($config);
234259

235-
$expected = 'EMERGENCY - ' . date('Y-m-d') . ' --> Test message';
260+
Time::setTestNow('2023-11-25 12:00:00');
261+
262+
$expected = 'EMERGENCY - ' . Time::now()->format('Y-m-d') . ' --> Test message';
236263

237264
$logger->emergency('Test message');
238265

@@ -247,7 +274,9 @@ public function testAlertLogsCorrectly(): void
247274
$config = new LoggerConfig();
248275
$logger = new Logger($config);
249276

250-
$expected = 'ALERT - ' . date('Y-m-d') . ' --> Test message';
277+
Time::setTestNow('2023-11-25 12:00:00');
278+
279+
$expected = 'ALERT - ' . Time::now()->format('Y-m-d') . ' --> Test message';
251280

252281
$logger->alert('Test message');
253282

@@ -262,7 +291,9 @@ public function testCriticalLogsCorrectly(): void
262291
$config = new LoggerConfig();
263292
$logger = new Logger($config);
264293

265-
$expected = 'CRITICAL - ' . date('Y-m-d') . ' --> Test message';
294+
Time::setTestNow('2023-11-25 12:00:00');
295+
296+
$expected = 'CRITICAL - ' . Time::now()->format('Y-m-d') . ' --> Test message';
266297

267298
$logger->critical('Test message');
268299

@@ -277,7 +308,9 @@ public function testErrorLogsCorrectly(): void
277308
$config = new LoggerConfig();
278309
$logger = new Logger($config);
279310

280-
$expected = 'ERROR - ' . date('Y-m-d') . ' --> Test message';
311+
Time::setTestNow('2023-11-25 12:00:00');
312+
313+
$expected = 'ERROR - ' . Time::now()->format('Y-m-d') . ' --> Test message';
281314

282315
$logger->error('Test message');
283316

@@ -292,7 +325,9 @@ public function testWarningLogsCorrectly(): void
292325
$config = new LoggerConfig();
293326
$logger = new Logger($config);
294327

295-
$expected = 'WARNING - ' . date('Y-m-d') . ' --> Test message';
328+
Time::setTestNow('2023-11-25 12:00:00');
329+
330+
$expected = 'WARNING - ' . Time::now()->format('Y-m-d') . ' --> Test message';
296331

297332
$logger->warning('Test message');
298333

@@ -307,7 +342,9 @@ public function testNoticeLogsCorrectly(): void
307342
$config = new LoggerConfig();
308343
$logger = new Logger($config);
309344

310-
$expected = 'NOTICE - ' . date('Y-m-d') . ' --> Test message';
345+
Time::setTestNow('2023-11-25 12:00:00');
346+
347+
$expected = 'NOTICE - ' . Time::now()->format('Y-m-d') . ' --> Test message';
311348

312349
$logger->notice('Test message');
313350

@@ -322,7 +359,9 @@ public function testInfoLogsCorrectly(): void
322359
$config = new LoggerConfig();
323360
$logger = new Logger($config);
324361

325-
$expected = 'INFO - ' . date('Y-m-d') . ' --> Test message';
362+
Time::setTestNow('2023-11-25 12:00:00');
363+
364+
$expected = 'INFO - ' . Time::now()->format('Y-m-d') . ' --> Test message';
326365

327366
$logger->info('Test message');
328367

@@ -337,7 +376,9 @@ public function testDebugLogsCorrectly(): void
337376
$config = new LoggerConfig();
338377
$logger = new Logger($config);
339378

340-
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message';
379+
Time::setTestNow('2023-11-25 12:00:00');
380+
381+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message';
341382

342383
$logger->debug('Test message');
343384

@@ -352,7 +393,9 @@ public function testLogLevels(): void
352393
$config = new LoggerConfig();
353394
$logger = new Logger($config);
354395

355-
$expected = 'WARNING - ' . date('Y-m-d') . ' --> Test message';
396+
Time::setTestNow('2023-11-25 12:00:00');
397+
398+
$expected = 'WARNING - ' . Time::now()->format('Y-m-d') . ' --> Test message';
356399

357400
$logger->log(5, 'Test message');
358401

0 commit comments

Comments
 (0)