gamez/psr-testlogger

This package is abandoned and no longer maintained. The author suggests using the beste/psr-testlogger package instead.

PSR-3 compliant test logger for developers who like tests and want to check if their application logs messages as they expect.

Maintainers

πŸ‘ jeromegamez

Package info

github.com/jeromegamez/php-psr-testlogger

pkg:composer/gamez/psr-testlogger

Fund package maintenance!

jeromegamez

Statistics

Installs: 199 092

Dependents: 12

Suggesters: 0

Stars: 8

Open Issues: 0

3.0.0 2018-06-11 00:22 UTC

Requires

Requires (Dev)

Suggests

None

Provides

Conflicts

None

Replaces

None

MIT 14656b9bd5c6df39b2a6f85fe2955cf2b8f459e2

  • JΓ©rΓ΄me Gamez <jerome.woop@gamez.name>

logphpunittesttestspsrpsr-3

This package is auto-updated.

Last update: 2022-09-24 15:59:20 UTC


README

PSR-3 compliant test logger for developers who like tests and want to check if their application logs messages as they expect.

This package was superseded by beste/psr-testlogger.

πŸ‘ Packagist
πŸ‘ Supported PHP version
πŸ‘ Build Status
πŸ‘ GitHub license
πŸ‘ Total Downloads

Installation

composer require --dev gamez/psr-testlogger

Usage

Inject an instance of Gamez\Psr\Log\TestLogger into your Subject Under Test instead of your regular logger.

use Psr\Log\LoggerInterface;

class SubjectUnderTest
{
 public function __construct(LoggerInterface $logger)
 {
 $this->logger = $logger;
 }

 public function execute()
 {
 $this->logger->info('Message with a {placeholder}', ['placeholder' => 'value']);
 $this->logger->emergency('This {placeholder} will not be replaced.');
 }
}
use Gamez\Psr\Log\TestLogger;
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
 /**
 * @var TestLogger
 */
 private $logger;

 /**
 * @var SubjectUnderTest
 */
 private $sut;

 protected function setUp()
 {
 $this->logger = new TestLogger();
 $this->sut = new SubjectUnderTest($this->logger);
 }
 
 public function testLogging()
 {
 $this->sut->execute();
 
 $log = $this->logger->log;
 
 $this->assertTrue($log->has('Message with a value'));
 $this->assertTrue($log->hasRecordsWithContextKey('foo'));
 $this->assertFalse($log->hasRecordsWithContextKeyAndValue('foo', 'unwanted'));
 // This will break
 $this->assertFalse($log->hasRecordsWithUnreplacedPlaceholders());
 }
}

You can find all available helper methods in the Gamez\Psr\Log\Log class. If it doesn't provide a method you need, you can use your own filters:

use Gamez\Psr\Log\Record;
use Gamez\Psr\Log\TestLogger;
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
 /**
 * @var TestLogger
 */
 private $logger;

 /**
 * @var SubjectUnderTest
 */
 private $sut;

 protected function setUp()
 {
 $this->logger = new TestLogger();
 $this->sut = new SubjectUnderTest($this->logger);
 }
 
 public function testSomethingSpecial()
 {
 $filteredLog = $this->logger->log->filter(function (Record $record) {
 // Matches messages with only numbers
 return ctype_digit($record->message);
 });
 
 $this->assertCount(0, $filteredLog);
 }
}