Skip to content

Commit

Permalink
Fix psalm 5.x issues
Browse files Browse the repository at this point in the history
  • Loading branch information
timdev committed Aug 19, 2023
1 parent d5de0c8 commit 460ca94
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
16 changes: 10 additions & 6 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@ public function testLogsMessages(): void

public function testAddsContext(): void
{
$this->log->addContext(['some' => 'context']);
$this->log->info('An info.');
$this->log->addContext(['some' => 'context'])->info('An info.');
$this->assertEquals(['some'], $this->log->contextKeysAt(0));

$this->log->addContext(['more' => 'context']);
$this->log->info('I should have two bits of context.');
$this->log
->addContext(['more' => 'context'])
->info('I should have two bits of context.');
$this->assertEquals(2, $this->log->contextCountAt(1));
$this->assertEquals(['some', 'more'], $this->log->contextKeysAt(1));

$this->log->addContext(['even more' => 'context']);
$this->log->warning('This message should get four context elements.', ['foo' => 'bar']);
$this->log
->addContext(['even more' => 'context'])
->warning(
'This message should get four context elements.',
['foo' => 'bar']
);
$this->assertEquals(4, $this->log->contextCountAt(2));

$this->log->debug('Back to three!');
Expand Down
3 changes: 1 addition & 2 deletions tests/MonologTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ public function testWithnameConvoluted(): void

$first->debug('First now has one record with two context');
$this->assertCount(1, $first->getChannelRecords());
$this->assertCount(2, $first->getChannelRecords()[0]['context']);

$this->assertCount(2, $first->channelRecordAt(0)['context']);
$this->assertEquals(2, $first->contextCountAt(0));
}
}
2 changes: 2 additions & 0 deletions tests/Support/LaminasStackLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Laminas\Log\Logger;
use Laminas\Log\PsrLoggerAdapter;
use Laminas\Log\Writer\Mock;
use Psr\Log\LoggerInterface;
use TimDev\StackLogger\Psr3StackLogger;

/**
* Implements TestLoggerInterface by wrapping a \Laminas\Log\PsrLoggerAdapter.
* PsrLoggerAdapter decorates \Laminas\Log\Logger to make it PSR3 compatible.
*
* @psalm-import-type LogRecord from TestStackLogger
* @extends Psr3StackLogger<LoggerInterface>
*/
class LaminasStackLogger extends Psr3StackLogger implements TestStackLogger
{
Expand Down
12 changes: 8 additions & 4 deletions tests/Support/MonologStackLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* TestLoggerInterface implementation that extends WrappedMonolog.
*
* @psalm-type MonologRecord = array{message:string, context:array, channel:string}
* @psalm-type MonologRecord = array{message:string, context:array, channel:string, ...}
*/
class MonologStackLogger extends BaseMonologStackLogger implements TestStackLogger
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public function getRecords(): array
* loggers inherit handlers from their parent, but it's useful in some tests
* to inspect log messages written by a particular instance.
*
* @return array<MonologRecord>
* @return array<int,MonologRecord>
*/
public function getChannelRecords(): array
{
Expand All @@ -54,9 +54,13 @@ public function getChannelRecords(): array
);
}

public function channelRecordAt(int $index): ?array
/**
* @param int $index
* @return MonologRecord
*/
public function channelRecordAt(int $index): array
{
return $this->getChannelRecords()[$index] ?? null;
return $this->getChannelRecords()[$index] ?? throw new \OutOfBoundsException("No record at index {$index}");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/TestStackLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* TestLoggerTrait to get most of this. You may need to override a method or
* two, as we do in ExtendedWrappedMonolog.
*
* @psalm-type LogRecord = array{message:string, context:array, channel:string}
* @psalm-type LogRecord = array{message:string, context:array, channel:string, ...}
*/
interface TestStackLogger extends StackLogger
{
Expand Down

0 comments on commit 460ca94

Please sign in to comment.