Test Doubles for the PSR-3 Logger Interface

Overview

PSR Log Test Doubles

Build Status Type Coverage codecov Latest Stable Version Download count

Test Doubles for the PSR-3 Logger Interface

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 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

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() );

Release notes

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
You might also like...
Laravel logger is an activity event logger for your Laravel or Lumen application
Laravel logger is an activity event logger for your Laravel or Lumen application

Laravel logger is an activity event logger for your Laravel or Lumen application. It comes out the box with ready to use with dashboard to view your activity. Laravel logger can be added as a middleware or called through a trait. Easily have an Activity Log. This package is easily configurable and customizable. Supports Laravel 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 6, and 7+

Slim-Logger - A stand-alone logger class for use with the Slim Framework

Slim-Logger - A stand-alone logger class for use with the Slim Framework

A simple and flexible PHP middleware dispatcher based on PSR-7, PSR-11, and PSR-15

Woohoo Labs. Harmony Woohoo Labs. Harmony is a PSR-15 compatible middleware dispatcher. Harmony was born to be a totally flexible and almost invisible

A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Allows you to process logs using any PSR-3 compatible logger such as Monolog

Yii 2 PSR Log Target Allows you to process logs using any PSR-3 compatible logger such as Monolog

Robust, composite logger with filtering, formatting, and PSR-3 support

laminas-log πŸ‡·πŸ‡Ί Русским Π³Ρ€Π°ΠΆΠ΄Π°Π½Π°ΠΌ ΠœΡ‹, участники Laminas, Ρ€ΠΎΠ΄ΠΈΠ»ΠΈΡΡŒ ΠΈ ΠΆΠΈΠ²Π΅ΠΌ Π² Ρ€Π°Π·Π½Ρ‹Ρ… странах. Π£ ΠΌΠ½ΠΎΠ³ΠΈΡ… ΠΈΠ· нас Π΅ΡΡ‚ΡŒ Π΄Ρ€ΡƒΠ·ΡŒΡ, родствСнники ΠΈ ΠΊΠΎΠ»Π»Π΅Π³ΠΈ ΠΊΠ°ΠΊ Π²

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

This is a port of the VCR Ruby library to PHP. Record your test suite's HTTP interactions and replay them during future test runs for fast, determinis

this package can help you to test race condition in Laravel Feature Test
this package can help you to test race condition in Laravel Feature Test

Laravel Async Testing this package can help you to test race condition in Laravel Feature Test Requirements Laravel versions 5.7, 6.x, 7.x and 8.x PHP

SNIA SSS PTS test suite based on SNIA's Solid State Storage Performance Test Specification for Transcend products
SNIA SSS PTS test suite based on SNIA's Solid State Storage Performance Test Specification for Transcend products

SNIA-SSS-PTS ABSTRACT SNIA SSS PTS describes a solid state storage device-level performance test methodology, test suite and reporting format intended

Middleware for PHP built on top of PSR-7 and PSR-15

zend-stratigility Repository abandoned 2019-12-31 This repository has moved to laminas/laminas-stratigility. From "Strata", Latin for "layer", and "ag

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

Route This package is compliant with PSR-1, PSR-2, PSR-4, PSR-7, PSR-11 and PSR-15. If you notice compliance oversights, please send a patch via pull

:tada: Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)

HTTP router for PHP 7.1+ (incl. PHP 8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger) Installation compo

PSR-6 cache implementation adapting a given PSR-16 instance

PSR-6 cache implementation adapting PSR-16 This package provides a PSR-6 cache instance when you only have a PSR-16 cache at hand. As PSR-6 is more fe

PSR Log - This repository holds all interfaces/classes/traits related to PSR-3.

PSR Log This repository holds all interfaces/classes/traits related to PSR-3. Note that this is not a logger of its own. It is merely an interface tha

PSR-7 and PSR-15 JWT Authentication Middleware

PSR-7 and PSR-15 JWT Authentication Middleware This middleware implements JSON Web Token Authentication. It was originally developed for Slim but can

PSR-7 and PSR-15 HTTP Basic Authentication Middleware

PSR-7 and PSR-15 Basic Auth Middleware This middleware implements HTTP Basic Authentication. It was originally developed for Slim but can be used with

Testing utilities for the psr/log package that backs the PSR-3 specification.

FIG - Log Test Testing utilities for the psr/log package that backs the PSR-3 specification. Psr\Log\Test\LoggerInterfaceTest provides a base test cla

A thin PSR-6 cache wrapper with a generic interface to various caching backends emphasising cache tagging and indexing.

Apix Cache, cache-tagging for PHP Apix Cache is a generic and thin cache wrapper with a PSR-6 interface to various caching backends and emphasising ca

Comments
  • 3.2.0 release

    3.2.0 release

    • Added LogCall::isError
    • Added LogCall::withoutContext
    • Added LogCalls::filter
    • Added LogCalls::getErrors
    • Added LogCalls::map
    • Added LogCalls::withoutContexts
    opened by JeroenDeDauw 1
Owner
Jeroen De Dauw
Professional Wiki Services: https://professional.wiki
Jeroen De Dauw
Slim-Logger - A stand-alone logger class for use with the Slim Framework

Slim-Logger - A stand-alone logger class for use with the Slim Framework

Josh Lockhart 41 Mar 12, 2022
Allows you to process logs using any PSR-3 compatible logger such as Monolog

Yii 2 PSR Log Target Allows you to process logs using any PSR-3 compatible logger such as Monolog

Alexander Makarov 67 Dec 14, 2022
Robust, composite logger with filtering, formatting, and PSR-3 support

laminas-log ???? Русским Π³Ρ€Π°ΠΆΠ΄Π°Π½Π°ΠΌ ΠœΡ‹, участники Laminas, Ρ€ΠΎΠ΄ΠΈΠ»ΠΈΡΡŒ ΠΈ ΠΆΠΈΠ²Π΅ΠΌ Π² Ρ€Π°Π·Π½Ρ‹Ρ… странах. Π£ ΠΌΠ½ΠΎΠ³ΠΈΡ… ΠΈΠ· нас Π΅ΡΡ‚ΡŒ Π΄Ρ€ΡƒΠ·ΡŒΡ, родствСнники ΠΈ ΠΊΠΎΠ»Π»Π΅Π³ΠΈ ΠΊΠ°ΠΊ Π²

Laminas Project 27 Nov 24, 2022
123Solar is a set of PHP/JS files that make a web logger to monitor your photovoltaic inverter(s)

123Solar is a set of PHP/JS files that make a web logger to monitor your photovoltaic inverter(s). It just need a web server and PHP, no databases are even needed. The philosophy is: To keep it simple, fast, with a low foot print to run on cheap and low powered devices.

null 30 Jan 6, 2023
A simple database logger for all outgoing emails sent by Laravel website.

Laravel Email Database Log A simple database logger for all outgoing emails sent by Laravel website.

Shvets Group 136 Jan 4, 2023
Simple PHP Logger. Composer package

Basic PHP Logger ?? Simple logger. Composer package.

null 1 Jan 28, 2022
temperature-pi: a simple Raspberry Pi based temperature logger using a DS18B20 1-Wire digital temperature sensor, & a local sqlite database

temperature-pi temperature-pi is a simple Raspberry Pi based temperature logger using a DS18B20 1-Wire digital temperature sensor, & a local sqlite da

Ronan Guilloux 23 Dec 27, 2020
πŸ“‘ Laravel HTTP Logger

Logging incoming HTTP requests

The Dragon Code 28 Nov 29, 2022
ChromePhp - a PHP library for the Chrome Logger Google Chrome extension

ChromePhp - a PHP library for the Chrome Logger Google Chrome extension

Craig Campbell 1.4k Dec 30, 2022
QaraTMS is open source test case, test suites, test plans and test runs management tool.

QaraTMS - Open Source Test Management System QaraTMS is open source test management software for managing test suites, test cases, test plans, test ru

Alex H 29 Dec 22, 2022