Core\Test::logs
Core\Test::logs(): array<Core\Test\LogRecord>Core\Test::logs() returns every log record written since the current test started.
The list contains the records written by the test itself, by each Core\Test::request it sends and by each Core\Test::run it starts. They are in the order they were written. Each record is a Core\Test\LogRecord with three methods: level(), message() and fields(). Use it to check that your code logs the right thing, such as a warning when a login fails.
Each test starts with an empty list, so a record from one test is never in the list of the next test. Outside nvs test, the list is always empty.
Good to know: the setting [log] level is the lowest level that is written. The default is info. A debug record is below it, so it is not written anywhere and is not in the list. Each record in the list is also written to the [log] target, so you see it in the output of nvs test.
The examples below show a failed login that writes a warning, the fields of a record that a request wrote, and a debug record that is not kept.
Return value
Returns array<Core\Test\LogRecord>. One Core\Test\LogRecord for each call to Core\Log::write, in the order written. A record below [log] level is not written anywhere, so it is not in the list either. The list is empty when nothing was written.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Test::logs returns the log records written in the current test. The list includes the records of each request and each run the test started. Each record has level(), message() and fields(). |
Examples
A failed login writes a warning
<?nvs// Shows how to test that a failed login writes a warning to the log.// proof: test
use Core\Log;use Core\Test;use Core\Test\LogRecord;
class Login{ public static function attempt(string $user, string $password): bool { if ($password == 'correct horse') { return true; } Log::write(Log::Level::Warn, 'login failed', ['user' => $user]); return false; }}
class LoginTest{ #[Test] public function aWrongPasswordWritesAWarning(): void { Login::attempt('ada', 'wrong');
// The login wrote one record, so the list has one item. array<LogRecord> $logs = Test::logs(); Test::assertCount($logs, 1); Test::assertEquals($logs[0]->level(), 'warn'); Test::assertEquals($logs[0]->message(), 'login failed'); Test::assertEquals($logs[0]->fields()['user'], 'ada'); }
#[Test] public function aGoodPasswordWritesNothing(): void { Login::attempt('ada', 'correct horse');
// Each test starts with an empty list. The warning of the test above is not here. Test::assertCount(Test::logs(), 0); }} LoginTest ✓ aWrongPasswordWritesAWarning ✓ aGoodPasswordWritesNothing
0 failed, 2 passed, 0 skipped, 0 flakyThe fields of a record a request wrote
<?nvs// Shows how to test the fields of a log record that a request wrote.// proof: test
use Core\Http\Method;use Core\Log;use Core\Test;use Core\Test\LogRecord;
class OrderTest{ #[Test] public function anOrderIsLoggedWithItsNumberAndTotal(): void { Test::request(Method::Post, '/orders');
// The request wrote the record. The test reads it with `Core\Test::logs`. array<LogRecord> $logs = Test::logs(); Test::assertCount($logs, 1); Test::assertEquals($logs[0]->message(), 'order created');
// `fields()` returns the fields given to `Core\Log::write`. array<mixed> $fields = $logs[0]->fields(); Test::assertEquals($fields['order'], 42); Test::assertEquals($fields['total'], 19.5); }}
// `Core\Test::request` runs this file again for the request.// In that run, this part creates the order and writes the record.if (Core\Env::context() == Core\Env::Context::Request) { Log::write(Log::Level::Info, 'order created', ['order' => 42, 'total' => 19.5]); echo 'created';} OrderTest ✓ anOrderIsLoggedWithItsNumberAndTotal
0 failed, 1 passed, 0 skipped, 0 flakyA debug record is not kept
<?nvs// Shows that a record below `[log] level` is not in the list. The default level is `info`.// proof: test
use Core\Log;use Core\Test;use Core\Test\LogRecord;
class PriceList{ public static function price(string $sku): int { Log::write(Log::Level::Debug, 'price not in the cache', ['sku' => $sku]); Log::write(Log::Level::Info, 'price loaded', ['sku' => $sku]); return 1200; }}
class PriceListTest{ #[Test] public function onlyTheInfoRecordIsKept(): void { Test::assertEquals(PriceList::price('mug'), 1200);
// The code wrote two records. The `debug` record is below `info`, // so the list has only the `info` record. array<LogRecord> $logs = Test::logs(); Test::assertCount($logs, 1); Test::assertEquals($logs[0]->level(), 'info'); Test::assertEquals($logs[0]->message(), 'price loaded'); }} PriceListTest ✓ onlyTheInfoRecordIsKept
0 failed, 1 passed, 0 skipped, 0 flaky