Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.2.0 release #2

Merged
merged 1 commit into from Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -60,6 +60,15 @@ $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`
Expand Down
19 changes: 19 additions & 0 deletions src/LogCall.php
Expand Up @@ -4,6 +4,8 @@

namespace WMDE\PsrLogTestDoubles;

use Psr\Log\LogLevel;

/**
* Value object representing a call to the logger
*
Expand All @@ -12,6 +14,8 @@
*/
class LogCall {

private const ERROR_LEVELS = [ LogLevel::ERROR, LogLevel::CRITICAL, LogLevel::ALERT, LogLevel::EMERGENCY ];

private mixed $level;
private string $message;
private array $context;
Expand All @@ -34,4 +38,19 @@ public function getContext(): array {
return $this->context;
}

/**
* Returns if the log level is error or above.
* @since 3.2
*/
public function isError(): bool {
return in_array( $this->level, self::ERROR_LEVELS );
}

/**
* @since 3.2
*/
public function withoutContext(): self {
return new self( $this->level, $this->message, [] );
}

}
42 changes: 42 additions & 0 deletions src/LogCalls.php
Expand Up @@ -4,6 +4,8 @@

namespace WMDE\PsrLogTestDoubles;

use Psr\Log\LogLevel;

/**
* Immutable and ordered collection of LogCall objects
*
Expand Down Expand Up @@ -58,4 +60,44 @@ public function count(): int {
return count( $this->calls );
}

/**
* @since 3.2
* @param callable(LogCall):bool $filter
* @return self
*/
public function filter( callable $filter ): self {
return new self( ...array_filter( $this->calls, $filter ) );
}

/**
* Returns log calls with log level ERROR and above.
* @since 3.2
*/
public function getErrors(): self {
return $this->filter( fn( LogCall $call ) => $call->isError() );
}

/**
* @since 3.2
* @param callable(LogCall):LogCall $map
* @return self
*/
public function map( callable $map ): self {
$calls = [];

foreach ( $this->calls as $call ) {
$calls[] = $map( $call );
}

return new self( ...$calls );
}

/**
* Returns a copy of the log calls with their contexts removed.
* @since 3.2
*/
public function withoutContexts(): self {
return $this->map( fn( LogCall $call ) => $call->withoutContext() );
}

}
45 changes: 45 additions & 0 deletions tests/LogCallTest.php
@@ -0,0 +1,45 @@
<?php

declare( strict_types = 1 );

namespace WMDE\PsrLogTestDoubles\Tests;

use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;
use WMDE\PsrLogTestDoubles\LogCall;

/**
* @covers \WMDE\PsrLogTestDoubles\LogCall
*
* @license GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class LogCallTest extends TestCase {

public function testIsNotError(): void {
$this->assertFalse( ( new LogCall( LogLevel::DEBUG, '' ) )->isError() );
$this->assertFalse( ( new LogCall( LogLevel::NOTICE, '' ) )->isError() );
}

public function testIsError(): void {
$this->assertTrue( ( new LogCall( LogLevel::ERROR, '' ) )->isError() );
$this->assertTrue( ( new LogCall( LogLevel::EMERGENCY, '' ) )->isError() );
$this->assertTrue( ( new LogCall( LogLevel::ALERT, '' ) )->isError() );
$this->assertTrue( ( new LogCall( LogLevel::CRITICAL, '' ) )->isError() );
}

public function testWithoutContextRemovesContext(): void {
$call = new LogCall( LogLevel::DEBUG, 'whatever', [ 'foo' => 'bar' ] );

$this->assertEquals(
new LogCall( LogLevel::DEBUG, 'whatever', [] ),
$call->withoutContext()
);

$this->assertSame(
[ 'foo' => 'bar' ],
$call->getContext()
);
}

}
64 changes: 64 additions & 0 deletions tests/LogCallsTest.php
Expand Up @@ -91,4 +91,68 @@ public function testWhenThereAreNoLogCalls_getLastCallReturnsNull(): void {
$this->assertNull( ( new LogCalls() )->getLastCall() );
}

public function testFilter(): void {
$logCalls = new LogCalls(
new LogCall( LogLevel::INFO, 'And so it begins', [ 'year' => 2258 ] ),
new LogCall( LogLevel::ALERT, "There's a hole in your mind" ),
new LogCall( LogLevel::INFO, 'And so it begins' ),
new LogCall( LogLevel::CRITICAL, 'Enemy sighted' ),
);

$this->assertEquals(
new LogCalls(
new LogCall( LogLevel::INFO, 'And so it begins', [ 'year' => 2258 ] ),
new LogCall( LogLevel::INFO, 'And so it begins' ),
),
$logCalls->filter( fn( LogCall $call ) => $call->getLevel() === LogLevel::INFO )
);
}

public function testGetErrors(): void {
$logCalls = new LogCalls(
new LogCall( LogLevel::INFO, 'And so it begins', [ 'year' => 2258 ] ),
new LogCall( LogLevel::ALERT, "There's a hole in your mind" ),
new LogCall( LogLevel::INFO, 'And so it begins' ),
new LogCall( LogLevel::CRITICAL, 'Enemy sighted' ),
);

$this->assertEquals(
new LogCalls(
new LogCall( LogLevel::ALERT, "There's a hole in your mind" ),
new LogCall( LogLevel::CRITICAL, 'Enemy sighted' ),
),
$logCalls->getErrors()
);
}

public function testMap(): void {
$logCalls = new LogCalls(
new LogCall( LogLevel::INFO, 'And so it begins', [ 'year' => 2258 ] ),
new LogCall( LogLevel::ALERT, "There's a hole in your mind", [ 'year' => 2260 ] ),
);

$this->assertEquals(
new LogCalls(
new LogCall( LogLevel::INFO, 'And so it begins' ),
new LogCall( LogLevel::ALERT, "There's a hole in your mind" ),
),
$logCalls->map( fn( LogCall $call ) => $call->withoutContext() )
);
}

public function testWithoutContexts(): void {
$logCalls = new LogCalls(
new LogCall( LogLevel::INFO, 'And so it begins', [ 'year' => 2258 ] ),
new LogCall( LogLevel::ALERT, "There's a hole in your mind", [ 'year' => 2260 ] ),
);

$this->assertEquals(
new LogCalls(
new LogCall( LogLevel::INFO, 'And so it begins' ),
new LogCall( LogLevel::ALERT, "There's a hole in your mind" ),
),
$logCalls->withoutContexts()
);
}

}