Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions doc/TestLogger.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,33 @@ Context value "response_time" does not match:
Response too fast: 500 ms
```

### `contextValueEquals(string $key, mixed $expected): callable`

Matches log records where a specific context key exists and its value is strictly equal (`===`) to `$expected`.

This is a convenience shorthand for `contextValueMatches($key, Val::eq($expected))`.

```php
// Check if status_code equals 200
TestLogger::contextValueEquals('status_code', 200)

// Check if environment equals "production"
TestLogger::contextValueEquals('environment', 'production')
```

**Example failure messages:**

When key doesn't exist:
```
Context has no key "status_code".
```

When value doesn't match:
```
Context value "status_code" does not match:
Expected value 200, but got 500.
```

### `exceptionMatches(callable $exceptionMatcher): callable`

Matches log records where the context contains an `exception` key with a `Throwable` value that satisfies the provided matcher.
Expand Down
13 changes: 13 additions & 0 deletions src/TestLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Eventjet\TestDouble;

use Eventjet\TestDouble\Matcher\Val;
use Override;
use Psr\Log\AbstractLogger;
use Stringable;
Expand Down Expand Up @@ -111,6 +112,18 @@ public static function contextValueMatches(string $key, callable $valueMatcher):
};
}

/**
* Matches log records where a specific context key exists and its value is strictly equal to $expected.
*
* This is a convenience wrapper around {@see contextValueMatches()} and {@see Val::eq()}.
*
* @return Matcher
*/
public static function contextValueEquals(string $key, mixed $expected): callable
{
return self::contextValueMatches($key, Val::eq($expected));
}

/**
* @param ExceptionMatcher $exceptionMatcher
* @return Matcher
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/TestLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,27 @@ public static function onceIssueCases(): iterable
Expected an instance of Throwable, got DateTime.
EOF,
];
yield 'contextValueEquals: key does not exist' => [
[new LogRecord(LogLevel::INFO, 'Foo', ['bar' => 'baz'])],
TestLogger::contextValueEquals('foo', 'baz'),
<<<'EOF'
None of the records matched:

Record 0:
Context has no key "foo".
EOF,
];
yield 'contextValueEquals: value does not match' => [
[new LogRecord(LogLevel::INFO, 'Foo', ['foo' => 'actual'])],
TestLogger::contextValueEquals('foo', 'expected'),
<<<'EOF'
None of the records matched:

Record 0:
Context value "foo" does not match:
Expected value "expected", but got "actual".
EOF,
];
}

/**
Expand Down Expand Up @@ -199,6 +220,14 @@ public function __toString(): string
static fn(Throwable $error) => $error instanceof CustomError ? true : 'Wrong',
),
];
yield 'contextValueEquals: value matches' => [
[new LogRecord(LogLevel::INFO, 'Foo', ['foo' => 'bar'])],
TestLogger::contextValueEquals('foo', 'bar'),
];
yield 'contextValueEquals: integer value matches' => [
[new LogRecord(LogLevel::INFO, 'Foo', ['count' => 42])],
TestLogger::contextValueEquals('count', 42),
];
}

/**
Expand Down
Loading