Skip to content

Commit

Permalink
Merge pull request #8471 from ping-yee/240128_loggerTest
Browse files Browse the repository at this point in the history
test: LoggerTest depends on time
  • Loading branch information
kenjis authored Mar 4, 2024
2 parents 9894b92 + a820445 commit f70786e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 21 deletions.
6 changes: 4 additions & 2 deletions tests/_support/Log/Handlers/TestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Tests\Support\Log\Handlers;

use CodeIgniter\I18n\Time;

/**
* Class TestHandler
*
Expand All @@ -36,7 +38,7 @@ public function __construct(array $config)
parent::__construct($config);

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

self::$logs = [];
}
Expand All @@ -52,7 +54,7 @@ public function __construct(array $config)
*/
public function handle($level, $message): bool
{
$date = date($this->dateFormat);
$date = Time::now()->format($this->dateFormat);

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

Expand Down
17 changes: 15 additions & 2 deletions tests/system/HTTP/RedirectExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\HTTP;

use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Log\Logger;
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
use Config\Services;
Expand All @@ -32,6 +33,14 @@ protected function setUp(): void
Services::injectMock('logger', new Logger(new LoggerConfig()));
}

protected function tearDown(): void
{
parent::tearDown();

// Reset the current time.
Time::setTestNow();
}

public function testResponse(): void
{
$response = (new RedirectException(
Expand Down Expand Up @@ -67,8 +76,10 @@ public function testResponseWithoutStatusCode(): void

public function testLoggingLocationHeader(): void
{
Time::setTestNow('2023-11-25 12:00:00');

$uri = 'http://location';
$expected = 'INFO - ' . date('Y-m-d') . ' --> REDIRECTED ROUTE at ' . $uri;
$expected = 'INFO - ' . Time::now()->format('Y-m-d') . ' --> REDIRECTED ROUTE at ' . $uri;
$response = (new RedirectException(Services::response()->redirect($uri)))->getResponse();

$logs = TestHandler::getLogs();
Expand All @@ -80,8 +91,10 @@ public function testLoggingLocationHeader(): void

public function testLoggingRefreshHeader(): void
{
Time::setTestNow('2023-11-25 12:00:00');

$uri = 'http://location';
$expected = 'INFO - ' . date('Y-m-d') . ' --> REDIRECTED ROUTE at ' . $uri;
$expected = 'INFO - ' . Time::now()->format('Y-m-d') . ' --> REDIRECTED ROUTE at ' . $uri;
$response = (new RedirectException(Services::response()->redirect($uri, 'refresh')))->getResponse();

$logs = TestHandler::getLogs();
Expand Down
77 changes: 60 additions & 17 deletions tests/system/Log/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Log;

use CodeIgniter\Exceptions\FrameworkException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Log\Exceptions\LogException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
Expand All @@ -25,6 +26,14 @@
*/
final class LoggerTest extends CIUnitTestCase
{
protected function tearDown(): void
{
parent::tearDown();

// Reset the current time.
Time::setTestNow();
}

public function testThrowsExceptionWithBadHandlerSettings(): void
{
$config = new LoggerConfig();
Expand Down Expand Up @@ -63,7 +72,9 @@ public function testLogActuallyLogs(): void
$config = new LoggerConfig();
$logger = new Logger($config);

$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message';
$logger->log('debug', 'Test message');

$logs = TestHandler::getLogs();
Expand Down Expand Up @@ -93,7 +104,9 @@ public function testLogInterpolatesMessage(): void

$logger = new Logger($config);

$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message bar baz';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message bar baz';

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

Expand All @@ -109,8 +122,10 @@ public function testLogInterpolatesPost(): void

$logger = new Logger($config);

Time::setTestNow('2023-11-25 12:00:00');

$_POST = ['foo' => 'bar'];
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message $_POST: ' . print_r($_POST, true);
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message $_POST: ' . print_r($_POST, true);

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

Expand All @@ -126,8 +141,10 @@ public function testLogInterpolatesGet(): void

$logger = new Logger($config);

Time::setTestNow('2023-11-25 12:00:00');

$_GET = ['bar' => 'baz'];
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message $_GET: ' . print_r($_GET, true);
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message $_GET: ' . print_r($_GET, true);

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

Expand All @@ -143,8 +160,10 @@ public function testLogInterpolatesSession(): void

$logger = new Logger($config);

Time::setTestNow('2023-11-25 12:00:00');

$_SESSION = ['xxx' => 'yyy'];
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message $_SESSION: ' . print_r($_SESSION, true);
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message $_SESSION: ' . print_r($_SESSION, true);

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

Expand All @@ -160,7 +179,9 @@ public function testLogInterpolatesCurrentEnvironment(): void

$logger = new Logger($config);

$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message ' . ENVIRONMENT;
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message ' . ENVIRONMENT;

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

Expand All @@ -176,9 +197,11 @@ public function testLogInterpolatesEnvironmentVars(): void

$logger = new Logger($config);

Time::setTestNow('2023-11-25 12:00:00');

$_ENV['foo'] = 'bar';

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

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

Expand Down Expand Up @@ -210,7 +233,9 @@ public function testLogInterpolatesExceptions(): void
$config = new LoggerConfig();
$logger = new Logger($config);

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

$expected = 'ERROR - ' . Time::now()->format('Y-m-d') . ' --> [ERROR] These are not the droids you are looking for';

try {
throw new Exception('These are not the droids you are looking for');
Expand All @@ -229,7 +254,9 @@ public function testEmergencyLogsCorrectly(): void
$config = new LoggerConfig();
$logger = new Logger($config);

$expected = 'EMERGENCY - ' . date('Y-m-d') . ' --> Test message';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'EMERGENCY - ' . Time::now()->format('Y-m-d') . ' --> Test message';

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

Expand All @@ -244,7 +271,9 @@ public function testAlertLogsCorrectly(): void
$config = new LoggerConfig();
$logger = new Logger($config);

$expected = 'ALERT - ' . date('Y-m-d') . ' --> Test message';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'ALERT - ' . Time::now()->format('Y-m-d') . ' --> Test message';

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

Expand All @@ -259,7 +288,9 @@ public function testCriticalLogsCorrectly(): void
$config = new LoggerConfig();
$logger = new Logger($config);

$expected = 'CRITICAL - ' . date('Y-m-d') . ' --> Test message';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'CRITICAL - ' . Time::now()->format('Y-m-d') . ' --> Test message';

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

Expand All @@ -274,7 +305,9 @@ public function testErrorLogsCorrectly(): void
$config = new LoggerConfig();
$logger = new Logger($config);

$expected = 'ERROR - ' . date('Y-m-d') . ' --> Test message';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'ERROR - ' . Time::now()->format('Y-m-d') . ' --> Test message';

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

Expand All @@ -289,7 +322,9 @@ public function testWarningLogsCorrectly(): void
$config = new LoggerConfig();
$logger = new Logger($config);

$expected = 'WARNING - ' . date('Y-m-d') . ' --> Test message';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'WARNING - ' . Time::now()->format('Y-m-d') . ' --> Test message';

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

Expand All @@ -304,7 +339,9 @@ public function testNoticeLogsCorrectly(): void
$config = new LoggerConfig();
$logger = new Logger($config);

$expected = 'NOTICE - ' . date('Y-m-d') . ' --> Test message';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'NOTICE - ' . Time::now()->format('Y-m-d') . ' --> Test message';

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

Expand All @@ -319,7 +356,9 @@ public function testInfoLogsCorrectly(): void
$config = new LoggerConfig();
$logger = new Logger($config);

$expected = 'INFO - ' . date('Y-m-d') . ' --> Test message';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'INFO - ' . Time::now()->format('Y-m-d') . ' --> Test message';

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

Expand All @@ -334,7 +373,9 @@ public function testDebugLogsCorrectly(): void
$config = new LoggerConfig();
$logger = new Logger($config);

$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message';

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

Expand All @@ -349,7 +390,9 @@ public function testLogLevels(): void
$config = new LoggerConfig();
$logger = new Logger($config);

$expected = 'WARNING - ' . date('Y-m-d') . ' --> Test message';
Time::setTestNow('2023-11-25 12:00:00');

$expected = 'WARNING - ' . Time::now()->format('Y-m-d') . ' --> Test message';

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

Expand Down

0 comments on commit f70786e

Please sign in to comment.