Commit d2dcb4c4 authored by Tim Lieberman's avatar Tim Lieberman
Browse files

Fix covariant property type issue.

parent 806f06e9
......@@ -6,6 +6,8 @@ interface LoggerInterface extends \Psr\Log\LoggerInterface
{
/**
* Returns a new instance that has $context merged on top of any existing context.
*
* @return static
*/
public function child(array $context = []): self;
public function child(array $context = []);
}
......@@ -16,7 +16,11 @@ use TimDev\StackLogger\Test\Support\TestLoggerInterface;
*/
abstract class BaseTest extends TestCase
{
protected TestLoggerInterface $log;
/**
* This is private because property types are invariant (since they're r/w). Concrete subclasses can rely on
* makeTextSubject() directly in each test, or define their own typed private $log.
*/
private TestLoggerInterface $log;
abstract protected function makeTestSubject(): TestLoggerInterface;
......
......@@ -2,16 +2,16 @@
namespace TimDev\StackLogger\Test;
/**
* Runs the tests defined in BaseTest but with an instance based on
* PsrLoggerAdapter from laminas-log.
*/
class LaminasTest extends BaseTest
{
/* PHP 7.4 doesn't support covariant typed properties 😢 */
/** @var Support\ExtendedLaminasLogger */
protected Support\TestLoggerInterface $log;
private Support\TestLoggerInterface $log;
protected function makeTestSubject(): Support\ExtendedLaminasLogger
{
return new Support\ExtendedLaminasLogger();
}
}
......@@ -15,11 +15,6 @@ use TimDev\StackLogger\Test\Support\TestLoggerInterface;
*/
class MonologTest extends BaseTest
{
/* PHP 7.4 doesn't support covariant typed properties 😢 */
/** @var ExtendedMonologLogger
* @noinspection PhpDocFieldTypeMismatchInspection
*/
protected TestLoggerInterface $log;
protected function makeTestSubject(): ExtendedMonologLogger
{
......@@ -29,14 +24,14 @@ class MonologTest extends BaseTest
public function test_withName_cloning()
{
// push some context.
$log = $this->log->child(['basic' => 'context']);
$log = $this->makeTestSubject()->child(['basic' => 'context']);
// get a clone with a new monolog channel-name, and log to it.
$newChannel = $log->withName('other');
$newChannel->info('A message');
// ensure the handler has accumulated records with context attached.
$rec = $this->log->recordAt(0);
$rec = $log->recordAt(0);
$this->assertEquals('other', $rec['channel']);
$this->assertCount(1, $rec['context']);
......
......@@ -9,7 +9,7 @@ use Laminas\Log\Writer\Mock;
use TimDev\StackLogger\StackLoggerTrait;
/**
* An extension of the PsrLoggerAdapter from Laminas-Log.
* An extension of the PsrLoggerAdapter from laminas-Log.
*/
class ExtendedLaminasLogger extends PsrLoggerAdapter implements TestLoggerInterface
{
......@@ -29,9 +29,10 @@ class ExtendedLaminasLogger extends PsrLoggerAdapter implements TestLoggerInterf
public function getRecords(): array
{
/*
Laminas Log has a different internal record structure, but that's okay. We just transform them into something
that resembles PSR/Monolog style records here.
*/
* Laminas Log has a different internal record structure, but that's
* okay. We just transform them into something that resembles
* PSR/Monolog style records here.
*/
return array_map(function($record){
return [
'level' => $record['priorityName'],
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment