Test Doubles for the PSR-3 Logger Interface
OTHER License
Test Doubles for the PSR-3 Logger Interface
Composer/Packagist: jeroen/psr-log-test-doubles
In PHP world, most people create Test Doubles via PHPUnits mocking framework ($this->createMock
).
(Please beware that a PHPUnit mock really is a Test Double and not necessarily a mock.)
While this framework is often helpful, using it instead of creating your own Test Doubles comes with
some cost:
LoggerInterface
, your test will bind to an implementation detail: if you use log
anderror
.This library is unit testing tool agnostic. So while these examples use PHPUnit, any testing tool can be used.
Assert the logger is called twice with expected messages
public function testWhenStuffIsDone_loggerGetsCalled() {
$logger = new LoggerSpy();
$serviceToTest = new ServiceToTest( $logger /*, other dependencies */ );
$serviceToTest->doStuff( /**/ );
$this->assertSame(
[ 'First message', 'Second message' ],
$logger->getLogCalls()->getMessages()
);
}
Assert the logger is called twice
$this->assertCount( 2, $logger->getLogCalls() );
Assert the message and log level of the first logger call
$firstLogCall = $logger->getFirstLogCall();
$this->assertSame( 'First message', $firstLogCall->getMessage() );
$this->assertSame( LogLevel::ERROR, $firstLogCall->getLevel() );
LogCall::isError
LogCall::withoutContext
LogCalls::filter
LogCalls::getErrors
LogCalls::map
LogCalls::withoutContexts
LogCalls::getLastCall
psr/log
2.x and 3.xLoggerSpy
only supports psr/log
2.x and 3.x. Added LegacyLoggerSpy
for psr/log
1.xLoggerSpy::getFirstLogCall
convenience methodLogCalls
now implements Countable
LoggerSpy::getLogCalls
now returns an instance of LogCalls
, which is a collection of LogCall
LogCalls::getMessages
LogCalls::getFirstCall
LoggerSpy::assertNoLoggingCallsWhereMade
LoggerSpy