Coder Social home page Coder Social logo

beste / psr-testlogger Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 15 KB

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

License: MIT License

PHP 100.00%
log logging php psr-3 testing

psr-testlogger's Introduction

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.

Current version Packagist PHP Version Support Monthly Downloads Total Downloads Tests Sponsor

Installation

composer require --dev beste/psr-testlogger

Usage

In your unit tests, inject the Beste\Psr\Log\TestLogger class into tested subjects that expect a Prs\Log\LoggerInterface.

The test logger records all log messages and exposes them via the records property which is an instance of Beste\Psr\Log\Records.

use Beste\Psr\Log\Record;
use Beste\Psr\Log\TestLogger;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

final class Subject
{
    public function __construct(
        public readonly LoggerInterface $logger
    ) {}
    
    public function doSomething(): void
    {
        $this->logger->info('Doing something');
        $this->logger->warning('1st problem');
        $this->logger->warning('2nd problem');
        $this->logger->critical('Uh oh!');
    }
}

final class SubjectTest extends \PHPUnit\Framework\TestCase
{
    private TestLogger $logger;
    private Subject $subject;
    
    protected function setUp() : void{
        $this->logger = TestLogger::create();
    }
    
    /** @test */
    public function it_does_something(): void
    {
        $this->subject->doSomething();
        
        self::assertCount(4, $this->logger->records);
        
        self::assertEqualsCanonicalizing(
            [LogLevel::INFO, LogLevel::WARNING, LogLevel::CRITICAL],
            $this->logger->records->levels()
        );
        
        self::assertTrue($this->logger->records->includeMessagesWithLevel('info'));
        self::assertCount(1, $this->logger->records->filteredByLevel('info'));
        self::assertCount(3, $this->logger->records->filteredByLevel('info', 'warning'));
        
        self::assertTrue($this->logger->records->includeMessagesContaining('problem'));
        self::assertCount(2, $this->logger->records->filteredByMessageContaining('problem'));
        
        self::assertTrue($this->logger->records->includeMessagesMatching('/^\d{1,}(st|nd)/i'));
        self::assertCount(2, $this->logger->records->filteredByMessageMatching('/^\d{1,}(st|nd)/i'));
        
        // You can filter by your own criteria. If you discover criteria not natively
        // covered by the test logger, please consider a pull request.
        self::assertTrue($this->logger->records->includeMessagesBy(fn (Record $r) => str_contains($r->level, 'n')));
        self::assertCount(3, $this->logger->records->filteredBy(fn (Record $r) => str_contains($r->level, 'n')));  
    }
}

License

This project is published under the MIT License.

psr-testlogger's People

Contributors

jeromegamez avatar

Stargazers

Syndesi avatar  avatar

Watchers

 avatar

Forkers

jan-sti

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.