Skip to content

Commit

Permalink
process with carbon immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Mar 9, 2025
1 parent fc7a18b commit 4135d02
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
13 changes: 1 addition & 12 deletions src/Illuminate/Log/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,8 @@ protected function createMonologDriver(array $config)
$config['handler_with'] ?? []
);

// ???
$handler = $this->prepareHandler(
$this->makeMonologDriver($config['handler'], $with), $config // @todo move make handler to its own protected method so it can be overridden by LogFake
$this->app->make($config['handler'], $with), $config
);

$processors = (new Collection($config['processors'] ?? []))
Expand All @@ -429,16 +428,6 @@ protected function createMonologDriver(array $config)
);
}

/**
* @param class-string<\Monolog\Handler\HandlerInterface> $handler
* @param array<string, mixed> $with
* @return \Monolog\Handler\HandlerInterface
*/
protected function makeMonologDriver($handler, $with)
{
return $this->app->make($handler, $with);
}

/**
* Prepare the handlers for usage by Monolog.
*
Expand Down
25 changes: 17 additions & 8 deletions src/Illuminate/Support/Testing/Fakes/LogFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
use Illuminate\Log\LogManager;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Date;
use Illuminate\Tests\Integration\Foundation\FakeHandler;
use Monolog\Handler\TestHandler;
use Monolog\LogRecord;

class LogFake extends LogManager implements Fake
{
/**
* Create a new LogFake instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\LogManager $logManager The original LogManager instance
* @return void
*/
public function __construct($app, public LogManager $logManager)
{
parent::__construct($app);
Expand All @@ -27,7 +35,11 @@ protected function getTestingConfig(): array
#[\Override]
protected function resolve($name, ?array $config = null)
{
return parent::resolve($name, [...$this->getTestingConfig(), ...['name' => $name]]);
return parent::resolve($name, [...$this->getTestingConfig(), ...['name' => $name]])
->pushProcessor(function(LogRecord $logRecord): LogRecord {
return $logRecord->with(datetime: Date::now()->toImmutable());
}
);
}

/**
Expand All @@ -47,19 +59,16 @@ public function logged(?\Closure $callback = null, ?string $channel = null): Col
->map($this->mapLogRecordToArray(...));
}

protected function makeMonologDriver($handler, $with)
{
return parent::makeMonologDriver($handler, $with); // TODO: Change the autogenerated stub
}

/**
* @param LogRecord $logRecord
* @return array{"message": string, "context": array<array-key, mixed>, "level": 100|200|250|300|400|500|550|600, "level_name": "emergency"|"alert"|"critical"|"error"|"warning"|"notice"|"info"|"debug", "channel": string, "datetime": \DateTimeInterface, "extra": array<array-key, mixed>}
* @return array{"message": string, "context": array<array-key, mixed>, "level_int": 100|200|250|300|400|500|550|600, "level": "emergency"|"alert"|"critical"|"error"|"warning"|"notice"|"info"|"debug", "channel": string, "datetime": \DateTimeInterface, "extra": array<array-key, mixed>}
*/
protected function mapLogRecordToArray(LogRecord $logRecord): array
{
$arr = $logRecord->toArray();
$arr['level_name'] = strtolower($arr['level_name']);
$arr['level_int'] = $arr['level'];
$arr['level'] = strtolower($arr['level_name']);
unset($arr['level_name']);
$arr['datetime'] = Date::make($arr['datetime']);

return $arr;
Expand Down
33 changes: 26 additions & 7 deletions tests/Support/LogFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Support;

use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\Log;
use Orchestra\Testbench\TestCase;
Expand All @@ -11,51 +12,66 @@ class LogFakeTest extends TestCase
public function test_all_channels_write_to_test_handler()
{
Log::fake();

$this->travelTo('2025-03-09 11:11:00Z');
Log::debug('hello');

$this->travelTo('2025-03-09 11:11:10Z');
Log::channel('single')->warning('Danger Will Robinson');
$this->travelTo('2025-03-09 11:12:10Z');
Log::channel('single')->info('all clear', ['value' => 'foo']);

Log::channel('not-in-config')->alert('hi', ['contextual' => true]);
Log::channel('slack')->debug('some slack message', ['album' => 'Marquee Moon']);

$logsWrittenToDefault = Log::logged();
$this->assertCount(1, $logsWrittenToDefault);
$this->assertSame('hello', $logsWrittenToDefault[0]['message']);
$this->assertSame('debug', $logsWrittenToDefault[0]['level_name']);
$this->assertLogRecordArrayMatches([
'message' => 'hello',
'level' => 'debug',
'channel' => 'stack',
'datetime' => CarbonImmutable::parse('2025-03-09 11:11:00Z'),
], $logsWrittenToDefault[0]);

$logsWrittenToSingle = Log::logged(channel: 'single');
$this->assertCount(2, $logsWrittenToSingle);

$this->assertLogRecordArrayMatches([
'message' => 'Danger Will Robinson',
'channel' => 'single',
'level_name' => 'warning',
'level' => 'warning',
'context' => [],
'extra' => [],
'datetime' => CarbonImmutable::parse('2025-03-09 11:11:10Z'),
], $logsWrittenToSingle[0]);

$this->assertLogRecordArrayMatches([
'message' => 'all clear',
'channel' => 'single',
'level_name' => 'info',
'level' => 'info',
'context' => ['value' => 'foo'],
'extra' => [],
'datetime' => CarbonImmutable::parse('2025-03-09 11:12:10Z'),
], $logsWrittenToSingle[1]);

$logsWrittenToNotInConfig = Log::logged(channel: 'not-in-config');
$this->assertCount(1, $logsWrittenToNotInConfig);
$this->assertLogRecordArrayMatches([
'message' => 'hi',
'channel' => 'not-in-config',
'level_name' => 'alert',
'level' => 'alert',
'context' => ['contextual' => true],
'extra' => [],
'datetime' => CarbonImmutable::parse('2025-03-09 11:12:10Z'),
], $logsWrittenToNotInConfig[0]);

$logsWrittenToSlack = Log::logged(channel: 'slack');
$this->assertCount(1, $logsWrittenToSlack);
$this->assertLogRecordArrayMatches([
'message' => 'some slack message',
'context' => ['album' => 'Marquee Moon'],
'level' => 'debug',
'datetime' => CarbonImmutable::parse('2025-03-09 11:12:10Z'),
], $logsWrittenToSlack[0]);
}

Expand All @@ -69,12 +85,15 @@ public function test_it_respects_context_processor()
$this->assertLogRecordArrayMatches([
'channel' => 'ondemand',
'extra' => ['artist' => 'Television'],
'level_name' => 'critical',
'level' => 'critical',
], $logs[0]);
}

private function assertLogRecordArrayMatches(array $expected, array $actual)
{
$this->assertEqualsCanonicalizing($expected, collect($actual)->only(array_keys($expected))->all());
$this->assertEqualsCanonicalizing(
$expected,
collect($actual)->only(array_keys($expected))->all()
);
}
}

0 comments on commit 4135d02

Please sign in to comment.