Example blog

Laravel Log Fake 2.0 | Laravel News

Laravel Log Fake has released v2.0 with Laravel 9 support and a fully closure-based assertion API:

The fake Log packet is a great way to ensure that critical logging occurs by testing assertions. As a basic example of the readme file, you must first link the LogFake instance, then you can assert logging:

1LogFake::bind();

2 

3// Run test code

4 

5// Logging assertions

6Log::assertLogged(fn (LogEntry $log) =>

7 $log->level === 'info'

8 && $log->message === 'User logged in.'

9 && $log->context === ['user_id' => 5]

10);

Another novelty of v2.0, the dd() and dump() helpers to debug log messages during a test:

1Log::dump();

2// array:1 [

3// 0 => array:4 [

4// "level" => "info"

5// "message" => "User logged in."

6// "context" => []

7// "channel" => "stack"

8// ]

9// ]

10 

11Log::channel('slack')->dump();

12 

13Log::dumpAll();

14 

15Log::dd();

16Log::ddAll();

You can also inspect logging and write assertions around them using various logs() aids:

1$logs = Log::allLogs();

2 

3assert($logs->count() === 2);

4 

5$logs = Log::allLogs();

6assert($logs->count() === 2);

Finally, with Laravel 9.12’s per-channel logging context API, the Log Fake package can also assert the context:

1Log::assertCurrentContext(

2 fn (array $context) => $context['app'] === 'Acme CRM')

3);

Be sure to check available assertions to see the full Log Assertion API available. You can learn more about this package, get full installation instructions, and view the source code on GitHub.



Source link