Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jeromegamez/php-psr-testlogger
PSR-3 compliant test logger for developers who like tests and want to check if their application logs messages as they expect.
https://github.com/jeromegamez/php-psr-testlogger
logger php php-7 php7 phpunit psr-3 testing tests
Last synced: 17 days ago
JSON representation
PSR-3 compliant test logger for developers who like tests and want to check if their application logs messages as they expect.
- Host: GitHub
- URL: https://github.com/jeromegamez/php-psr-testlogger
- Owner: jeromegamez
- License: mit
- Archived: true
- Created: 2015-08-13T08:29:25.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-09-24T15:58:27.000Z (over 2 years ago)
- Last Synced: 2025-01-17T20:48:56.783Z (22 days ago)
- Topics: logger, php, php-7, php7, phpunit, psr-3, testing, tests
- Language: PHP
- Homepage:
- Size: 48.8 KB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PSR Test Logger
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](https://github.com/beste/psr-testlogger).**
[![Packagist](https://img.shields.io/packagist/v/gamez/psr-testlogger.svg)](https://packagist.org/packages/gamez/psr-testlogger)
[![Supported PHP version](https://img.shields.io/packagist/php-v/gamez/psr-testlogger.svg)]()
[![Build Status](https://travis-ci.org/jeromegamez/php-psr-testlogger.svg?branch=master)](https://travis-ci.org/jeromegamez/php-psr-testlogger)
[![GitHub license](https://img.shields.io/github/license/jeromegamez/php-psr-testlogger.svg)](https://github.com/jeromegamez/php-psr-testlogger/blob/master/LICENSE)
[![Total Downloads](https://img.shields.io/packagist/dt/gamez/psr-testlogger.svg)]()## 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.```php
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.');
}
}
``````php
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](src/Log.php). If it
doesn't provide a method you need, you can use your own filters:```php
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);
}
}
```