Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jeroendedauw/psrlogtestdoubles
Test Doubles for the PSR-3 Logger Interface
https://github.com/jeroendedauw/psrlogtestdoubles
Last synced: about 1 month ago
JSON representation
Test Doubles for the PSR-3 Logger Interface
- Host: GitHub
- URL: https://github.com/jeroendedauw/psrlogtestdoubles
- Owner: JeroenDeDauw
- License: other
- Created: 2022-01-25T22:18:30.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-01-23T17:12:34.000Z (10 months ago)
- Last Synced: 2024-04-24T18:41:08.949Z (7 months ago)
- Language: PHP
- Homepage: https://entropywins.wtf
- Size: 89.8 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: COPYING.txt
Awesome Lists containing this project
README
# PSR Log Test Doubles
[![Build Status](https://img.shields.io/github/actions/workflow/status/JeroenDeDauw/PsrLogTestDoubles/ci.yml?branch=master)](https://github.com/JeroenDeDauw/PsrLogTestDoubles/actions?query=workflow%3ACI)
[![Type Coverage](https://shepherd.dev/github/JeroenDeDauw/PsrLogTestDoubles/coverage.svg)](https://shepherd.dev/github/JeroenDeDauw/PsrLogTestDoubles)
[![Psalm level](https://shepherd.dev/github/JeroenDeDauw/PsrLogTestDoubles/level.svg)](psalm.xml)
[![codecov](https://codecov.io/gh/JeroenDeDauw/PsrLogTestDoubles/branch/master/graph/badge.svg?token=GnOG3FF16Z)](https://codecov.io/gh/JeroenDeDauw/PsrLogTestDoubles)
[![Latest Stable Version](https://poser.pugx.org/jeroen/psr-log-test-doubles/v/stable)](https://packagist.org/packages/jeroen/psr-log-test-doubles)
[![Download count](https://poser.pugx.org/jeroen/psr-log-test-doubles/downloads)](https://packagist.org/packages/jeroen/psr-log-test-doubles)[Test Doubles][doubles] for the [PSR-3 Logger Interface][psr-3]
Composer/Packagist: [`jeroen/psr-log-test-doubles`](https://packagist.org/packages/jeroen/psr-log-test-doubles)
## Motivation
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][better-mocks] and not necessarily a mock.)
While this framework is often helpful, using it instead of creating your own Test Doubles comes with
some cost:* Tools do not understand the PHPUnit magic. You will not be able to use automated refactorings such
as method rename without your Test Doubles breaking. You will also get incorrect type warnings.
* When using `LoggerInterface`, your test will bind to an implementation detail: if you use `log` and
provide a log level, or call a shortcut method such as `error`.
* Developers need to be familiar with the mocking tool.
* Simple things such as asserting the logger got called with two messages become difficult.## Usage
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**
```php
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**
```php
$this->assertCount( 2, $logger->getLogCalls() );
```**Assert the message and log level of the first logger call**
```php
$firstLogCall = $logger->getFirstLogCall();$this->assertSame( 'First message', $firstLogCall->getMessage() );
$this->assertSame( LogLevel::ERROR, $firstLogCall->getLevel() );
```## Release notes
### 3.2.0 (2022-03-28)
* Added `LogCall::isError`
* Added `LogCall::withoutContext`
* Added `LogCalls::filter`
* Added `LogCalls::getErrors`
* Added `LogCalls::map`
* Added `LogCalls::withoutContexts`### 3.1.0 (2022-01-26)
* Added `LogCalls::getLastCall`
### 3.0.0 (2022-01-26)
* Added support for `psr/log` 2.x and 3.x
* `LoggerSpy` only supports `psr/log` 2.x and 3.x. Added `LegacyLoggerSpy` for `psr/log` 1.x
* Changed minimum PHP version from PHP 7.1 to 8.0
* Added several property, parameter and return types
* Added Psalm and PHPStan CI and compliance with level 1 checks### 2.2.0 (2017-05-23)
* Added `LoggerSpy::getFirstLogCall` convenience method
* Changed minimum PHP version from PHP 7.0 to 7.1### 2.1.0 (2017-01-17)
* `LogCalls` now implements `Countable`
### 2.0.0 (2017-01-16)
* `LoggerSpy::getLogCalls` now returns an instance of `LogCalls`, which is a collection of `LogCall`
* Added `LogCalls::getMessages`
* Added `LogCalls::getFirstCall`### 1.1.0 (2016-11-11)
* Added `LoggerSpy::assertNoLoggingCallsWhereMade`
### 1.0.0 (2016-10-18)
* Initial release with minimal `LoggerSpy`
[doubles]: https://en.wikipedia.org/wiki/Test_double
[psr-3]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
[better-mocks]: https://www.entropywins.wtf/blog/2016/05/13/5-ways-to-write-better-mocks/