Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into 4.5
Browse files Browse the repository at this point in the history
 Conflicts:
	tests/_support/Log/Handlers/TestHandler.php
  • Loading branch information
kenjis committed Mar 5, 2024
2 parents a663b36 + efb3816 commit 8b0752a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"phpunit/phpcov": "^8.2",
"phpunit/phpunit": "^9.1",
"predis/predis": "^1.1 || ^2.0",
"rector/rector": "1.0.1",
"rector/rector": "1.0.2",
"vimeo/psalm": "^5.0"
},
"replace": {
Expand Down
5 changes: 3 additions & 2 deletions tests/_support/Log/Handlers/TestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Tests\Support\Log\Handlers;

use CodeIgniter\I18n\Time;
use CodeIgniter\Log\Handlers\FileHandler;

/**
Expand Down Expand Up @@ -40,7 +41,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 @@ -56,7 +57,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 @@ -14,6 +14,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 @@ -34,6 +35,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 @@ -69,8 +78,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 @@ -82,8 +93,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 @@ -14,6 +14,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 @@ -28,6 +29,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 @@ -66,7 +75,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 @@ -96,7 +107,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 @@ -112,8 +125,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 @@ -129,8 +144,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 @@ -146,8 +163,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 @@ -163,7 +182,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 @@ -179,9 +200,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 @@ -213,7 +236,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 @@ -232,7 +257,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 @@ -247,7 +274,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 @@ -262,7 +291,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 @@ -277,7 +308,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 @@ -292,7 +325,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 @@ -307,7 +342,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 @@ -322,7 +359,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 @@ -337,7 +376,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 @@ -352,7 +393,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 8b0752a

Please sign in to comment.