1. Go to this page and download the library: Download timacdonald/log-fake library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
timacdonald / log-fake example snippets
use Illuminate\Support\Facades\Log;
use TiMacDonald\Log\LogEntry;
use TiMacDonald\Log\LogFake;
public function testItLogsWhenAUserAuthenticates()
{
/*
* Test setup.
*
* In the setup of your tests, you can call the following `bind` helper,
* which will switch out the underlying log driver with the fake.
*/
LogFake::bind();
/*
* Application implementation.
*
* In your application's implementation, you then utilise the logger, as you
* normally would.
*/
Log::info('User logged in.', ['user_id' => $user->id]);
/*
* Test assertions.
*
* Finally you can make assertions against the log channels, stacks, etc. to
* ensure the expected logging occurred in your implementation.
*/
Log::assertLogged(fn (LogEntry $log) =>
$log->level === 'info'
&& $log->message === 'User logged in.'
&& $log->context === ['user_id' => 5]
);
}
/*
* implementation...
*/
Log::channel('single')->info('User logged in.');
/*
* assertions...
*/
Log::channel('stderr')->assertNothingLogged(); // ✅
Log::channel('single')->assertNothingLogged(); // ❌ as a log was created in the `single` channel.
/*
* implementation...
*/
Log::channel('single')->info('User logged in.');
Log::forgetChannel('single');
/*
* assertions...
*/
Log::channel('single')->assertWasForgotten(); // ✅
Log::channel('stderr')->assertWasForgotten(); // ❌ as it was the `single` not the `stderr` channel that was not forgotten.
/*
* implementation...
*/
Log::channel('single')->info('User logged in.');
Log::forgetChannel('single');
Log::channel('single')->info('User logged in.');
Log::forgetChannel('single');
/*
* assertions...
*/
Log::channel('single')->assertWasForgottenTimes(2); // ✅
Log::channel('single')->assertWasForgottenTimes(99); // ❌ as the channel was forgotten twice, not 99 times.