diff --git a/.github/workflows/code-checks.yml b/.github/workflows/code-checks.yml index 49d0c0e..d57ddf2 100644 --- a/.github/workflows/code-checks.yml +++ b/.github/workflows/code-checks.yml @@ -11,9 +11,6 @@ jobs: strategy: matrix: php: - - '7.3' - - '7.4' - - '8.0' - '8.1' - '8.2' - '8.3' diff --git a/.gitignore b/.gitignore index 87230d1..ecbf85c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/vendor +/vendor/ +/.phpunit.cache/ /composer.lock /phpunit.xml -/.phpunit.result.cache diff --git a/.runConfigurations/All unit tests.run.xml b/.runConfigurations/All unit tests.run.xml index a42ab5a..5d27a5b 100644 --- a/.runConfigurations/All unit tests.run.xml +++ b/.runConfigurations/All unit tests.run.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 4480eb9..c5226c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +- Type declarations for parameters and return types. +### Removed +- **BC break**: Removed support for PHP versions <= v8.0 as they are no longer + [actively supported](https://php.net/supported-versions.php) by the PHP project. ## [4.1.2] - 2025-01-30 ### Changed diff --git a/composer.json b/composer.json index aeb0347..e6e3252 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,11 @@ } ], "require": { - "php": "^7.3|^8", + "php": "^8.1", "ext-json": "*", - "psr/log": "^1 || ^2 || ^3", + "psr/log": "^2 || ^3", "graylog2/gelf-php": "^1.3", - "symfony/console": "^2.5.12|^3|^4|^5 || ^6 || ^7" + "symfony/console": "^6 || ^7" }, "autoload": { "psr-4": { @@ -27,10 +27,10 @@ } }, "provide": { - "psr/log-implementation": "1.0 || 2.0 || 3.0" + "psr/log-implementation": "2.0 || 3.0" }, "require-dev": { - "phpunit/phpunit": "^9", + "phpunit/phpunit": "^10", "symplify/easy-coding-standard": "^12" }, "autoload-dev": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6c17a6a..d7805a3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,9 +1,10 @@ @@ -11,9 +12,9 @@ tests - + src - + diff --git a/src/Config.php b/src/Config.php index 8e41486..cf158e9 100644 --- a/src/Config.php +++ b/src/Config.php @@ -9,14 +9,9 @@ */ class Config implements ConfigInterface { - /** - * @var array - */ - protected $config; - - public function __construct(array $config) - { - $this->config = $config; + public function __construct( + protected array $config, + ) { } public function getLoggerConfig(string $name): array diff --git a/src/Decorator/AbstractDecorator.php b/src/Decorator/AbstractDecorator.php index 3b38ffe..63224e3 100644 --- a/src/Decorator/AbstractDecorator.php +++ b/src/Decorator/AbstractDecorator.php @@ -12,22 +12,16 @@ */ abstract class AbstractDecorator extends AbstractLogger { - /** - * @var LoggerInterface - */ - private $logger; - /** * Stores the Logger for re-use in the concrete via getInnerLogger() * * If the concrete requires use of the config value, override the constructor * to add validation and store for re-use - * - * @param mixed $config */ - public function __construct(LoggerInterface $logger, $config) - { - $this->logger = $logger; + public function __construct( + private readonly LoggerInterface $logger, + mixed $config, + ) { } protected function getInnerLogger(): LoggerInterface diff --git a/src/Decorator/DefaultContext.php b/src/Decorator/DefaultContext.php index 85d3646..5f13e7d 100644 --- a/src/Decorator/DefaultContext.php +++ b/src/Decorator/DefaultContext.php @@ -15,10 +15,7 @@ */ class DefaultContext extends AbstractDecorator { - /** - * @var array - */ - private $decorations; + private readonly array $decorations; public function __construct(LoggerInterface $logger, array $decorations) { diff --git a/src/Decorator/LevelFilter.php b/src/Decorator/LevelFilter.php index 369b333..1df677a 100644 --- a/src/Decorator/LevelFilter.php +++ b/src/Decorator/LevelFilter.php @@ -18,7 +18,7 @@ class LevelFilter extends AbstractDecorator * * @var string[] Logging levels */ - private static $levels = [ + private static array $levels = [ LogLevel::EMERGENCY, // 0 LogLevel::ALERT, // 1 LogLevel::CRITICAL, // 2 @@ -29,17 +29,14 @@ class LevelFilter extends AbstractDecorator LogLevel::DEBUG, // 7 ]; - /** - * @var int - */ - private $logLevel; + private readonly int $logLevel; public function __construct(LoggerInterface $logger, string $level) { parent::__construct($logger, $level); - $this->logLevel = array_search($level, self::$levels, true); - if ($this->logLevel === false) { + $logLevel = array_search($level, self::$levels, true); + if ($logLevel === false) { throw new InvalidArgumentException( sprintf( 'Cannot use logging level "%s"', @@ -47,6 +44,7 @@ public function __construct(LoggerInterface $logger, string $level) ) ); } + $this->logLevel = $logLevel; } /** diff --git a/src/Factory.php b/src/Factory.php index d37e56e..b349df1 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -11,10 +11,7 @@ */ class Factory { - /** - * @var array - */ - private $decorators = [ + private array $decorators = [ 'defaultContext' => \Phlib\Logger\Decorator\DefaultContext::class, 'level' => \Phlib\Logger\Decorator\LevelFilter::class, ]; @@ -58,9 +55,7 @@ public function createLogger(string $name, array $config): LoggerInterface } $logger = $this->{$methodName}($name, $config); - $logger = $this->applyDecorators($logger, $config); - - return $logger; + return $this->applyDecorators($logger, $config); } private function applyDecorators(LoggerInterface $logger, array $config): LoggerInterface diff --git a/src/LoggerType/CliColor.php b/src/LoggerType/CliColor.php index 1b77272..7c3d184 100644 --- a/src/LoggerType/CliColor.php +++ b/src/LoggerType/CliColor.php @@ -14,10 +14,7 @@ */ class CliColor extends Stream { - /** - * @var OutputFormatter - */ - private $formatter; + private readonly OutputFormatter $formatter; /** * @see Stream::__construct() @@ -39,11 +36,7 @@ public function __construct(string $name, $stream = STDERR) ]); } - /** - * @see Stream::getMessageFormat() - * @param mixed $level - */ - protected function getMessageFormat($level, array $context = []): string + protected function getMessageFormat(mixed $level, array $context = []): string { $parentFormat = parent::getMessageFormat($level, $context); $consoleFormat = "<{$level}>{$parentFormat}"; diff --git a/src/LoggerType/Collection.php b/src/LoggerType/Collection.php index e70c4f0..43673b9 100644 --- a/src/LoggerType/Collection.php +++ b/src/LoggerType/Collection.php @@ -12,10 +12,7 @@ */ class Collection extends AbstractLogger { - /** - * @var \SplObjectStorage - */ - protected $loggerInstances; + protected \SplObjectStorage $loggerInstances; public function __construct() { diff --git a/src/LoggerType/Stream.php b/src/LoggerType/Stream.php index 0adc721..487cd7d 100644 --- a/src/LoggerType/Stream.php +++ b/src/LoggerType/Stream.php @@ -11,25 +11,16 @@ */ class Stream extends AbstractLogger { - /** - * @var string - */ - private $name; + private readonly string $name; /** * @var resource */ private $stream; - /** - * @var string - */ - private $messageFormat = '[{datetime}] {name}.{level}: {message} {context}'; + private string $messageFormat = '[{datetime}] {name}.{level}: {message} {context}'; - /** - * @var string - */ - private $dateFormat = 'Y-m-d H:i:s'; + private string $dateFormat = 'Y-m-d H:i:s'; /** * @param resource|string $stream @@ -58,10 +49,8 @@ public function setMessageFormat(string $format): self * Get current message format * * This method can be overridden by extending classes to modify the behaviour - * - * @param mixed $level */ - protected function getMessageFormat($level, array $context = []): string + protected function getMessageFormat(mixed $level, array $context = []): string { return $this->messageFormat; } @@ -73,11 +62,7 @@ public function setDateFormat(string $format): self return $this; } - /** - * @param mixed $level - * @param string|\Stringable $message - */ - public function log($level, $message, array $context = []): void + public function log(mixed $level, string|\Stringable $message, array $context = []): void { $meta = [ 'datetime' => date($this->dateFormat), @@ -95,9 +80,8 @@ public function log($level, $message, array $context = []): void protected function formatMessage(string $message, array $context): string { $message = static::interpolate($message, $context); - $message = trim(str_replace(["\r", "\n"], ' ', $message)); - return $message; + return trim(str_replace(["\r", "\n"], ' ', $message)); } protected function formatContext(array $context): string @@ -117,10 +101,8 @@ protected function formatContext(array $context): string * * Expanded on reference implementation to include handling for complex types, * trying to ensure no error, warning or notice is happening - * - * @param mixed $message */ - private static function interpolate($message, array $context): string + private static function interpolate(string $message, array $context): string { // build a replacement array with braces around the context keys $replace = []; @@ -151,7 +133,7 @@ private static function contextValueToString($val): string if (is_callable([$val, '__toString'])) { return (string)$val; } - return get_class($val); + return $val::class; } return gettype($val); } diff --git a/src/Pool.php b/src/Pool.php index 5b35178..04bf5bb 100644 --- a/src/Pool.php +++ b/src/Pool.php @@ -12,30 +12,17 @@ */ class Pool { - /** - * @var ConfigInterface - */ - protected $config; - - /** - * @var string - */ - protected $prefix = ''; + protected string $prefix = ''; /** * @var LoggerInterface[] */ - protected $loggerInstances = []; + protected array $loggerInstances = []; - /** - * @var Factory - */ - protected $loggerFactory; - - public function __construct(ConfigInterface $config, Factory $loggerFactory) - { - $this->config = $config; - $this->loggerFactory = $loggerFactory; + public function __construct( + protected ConfigInterface $config, + protected Factory $loggerFactory, + ) { } public function setPrefix(string $prefix): self diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index a01c70f..3918a59 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -10,7 +10,7 @@ class ConfigTest extends TestCase { - public function testEmptyConfig() + public function testEmptyConfig(): void { $configArray = []; $config = new Config($configArray); @@ -23,7 +23,7 @@ public function testEmptyConfig() static::assertEquals($expected, $config->getLoggerConfig('test')); } - public function testStream() + public function testStream(): void { $streamConfig = [ 'type' => 'stream', @@ -39,7 +39,7 @@ public function testStream() static::assertEquals($streamConfig, $config->getLoggerConfig('test')); } - public function testCollectionCoerce() + public function testCollectionCoerce(): void { $gelfConfig = [ 'type' => 'gelf', @@ -63,7 +63,7 @@ public function testCollectionCoerce() static::assertEquals($expected, $config->getLoggerConfig('test')); } - public function testAlias() + public function testAlias(): void { $streamConfig = [ 'type' => 'stream', @@ -82,7 +82,7 @@ public function testAlias() static::assertEquals($streamConfig, $config->getLoggerConfig('test')); } - public function testInvalidLoggerConfigType() + public function testInvalidLoggerConfigType(): void { $configArray = [ 'test' => false, diff --git a/tests/Decorator/DefaultContextTest.php b/tests/Decorator/DefaultContextTest.php index 632b8ee..5b85247 100644 --- a/tests/Decorator/DefaultContextTest.php +++ b/tests/Decorator/DefaultContextTest.php @@ -5,22 +5,21 @@ namespace Phlib\Logger\Test\Decorator; use Phlib\Logger\Decorator\DefaultContext; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; class DefaultContextTest extends TestCase { - public function testIsPsrLog() + public function testIsPsrLog(): void { $decorator = new DefaultContext($this->getMockLogger(), []); - static::assertInstanceOf(\Psr\Log\LoggerInterface::class, $decorator); + static::assertInstanceOf(LoggerInterface::class, $decorator); } - /** - * @dataProvider providerAddDefaultContext - */ - public function testAddDefaultContext($defaultContext, $logContext, $expected) + #[DataProvider('providerAddDefaultContext')] + public function testAddDefaultContext(array $defaultContext, array $logContext, array $expected): void { $loggerInterface = $this->getMockLogger(); @@ -36,7 +35,7 @@ public function testAddDefaultContext($defaultContext, $logContext, $expected) $decorator->info('message', $logContext); } - public function providerAddDefaultContext() + public static function providerAddDefaultContext(): array { return [ // Defaults, no log context @@ -83,12 +82,8 @@ public function providerAddDefaultContext() ]; } - /** - * @return LoggerInterface|MockObject - */ - protected function getMockLogger() + private function getMockLogger(): LoggerInterface&MockObject { - $loggerInterface = $this->createMock(\Psr\Log\LoggerInterface::class); - return $loggerInterface; + return $this->createMock(LoggerInterface::class); } } diff --git a/tests/Decorator/LevelFilterTest.php b/tests/Decorator/LevelFilterTest.php index bd2ccd9..b48d912 100644 --- a/tests/Decorator/LevelFilterTest.php +++ b/tests/Decorator/LevelFilterTest.php @@ -5,19 +5,20 @@ namespace Phlib\Logger\Test\Decorator; use Phlib\Logger\Decorator\LevelFilter; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; class LevelFilterTest extends TestCase { - public function testIsPsrLog() + public function testIsPsrLog(): void { $levelFilter = new LevelFilter($this->getMockLoggerInterface(), LogLevel::DEBUG); - static::assertInstanceOf(\Psr\Log\LoggerInterface::class, $levelFilter); + static::assertInstanceOf(LoggerInterface::class, $levelFilter); } - public function testLog() + public function testLog(): void { $loggerInterface = $this->getMockLoggerInterface(); @@ -25,10 +26,19 @@ public function testLog() // and to ignore the WARNING log message $loggerInterface->expects(static::exactly(2)) ->method('log') - ->withConsecutive( - [LogLevel::ERROR], - [LogLevel::CRITICAL] - ); + ->with(static::callback(function (string $level): bool { + static $invocationCount = 0; + $invocationCount++; + + match ($invocationCount) { + 1 => static::assertSame(LogLevel::ERROR, $level), + 2 => static::assertSame(LogLevel::CRITICAL, $level), + default => static::fail('Method called more times than expected'), + }; + + return true; + })); + $levelFilter = new LevelFilter($loggerInterface, LogLevel::ERROR); @@ -42,15 +52,15 @@ public function testLog() $levelFilter->log(LogLevel::WARNING, 'TEST WARNING MESSAGE'); } - public function testInvalidConstructorLogLevel() + public function testInvalidConstructorLogLevel(): void { // We expect an exception to be thrown when specifying an invalid logging level in the constructor $this->expectException(\Psr\Log\InvalidArgumentException::class); - $levelFilter = new LevelFilter($this->getMockLoggerInterface(), 'InvalidLogLevel'); + new LevelFilter($this->getMockLoggerInterface(), 'InvalidLogLevel'); } - public function testInvalidLogLogLevel() + public function testInvalidLogLogLevel(): void { // We expect an exception to be thrown when specifying an invalid logging level in the log method parameter $this->expectException(\Psr\Log\InvalidArgumentException::class); @@ -59,12 +69,8 @@ public function testInvalidLogLogLevel() $levelFilter->log('InvalidLogLevel', 'TEST LOG MESSAGE'); } - /** - * @return LoggerInterface - */ - protected function getMockLoggerInterface() + private function getMockLoggerInterface(): LoggerInterface&MockObject { - $loggerInterface = $this->createMock(\Psr\Log\LoggerInterface::class); - return $loggerInterface; + return $this->createMock(LoggerInterface::class); } } diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 2233157..5b3a4f2 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -10,7 +10,7 @@ class FactoryTest extends TestCase { - public function testCreateStreamLogger() + public function testCreateStreamLogger(): void { $factory = new Factory(); @@ -22,7 +22,7 @@ public function testCreateStreamLogger() static::assertInstanceOf(\Phlib\Logger\LoggerType\Stream::class, $logger); } - public function testCreateGelfLogger() + public function testCreateGelfLogger(): void { $factory = new Factory(); @@ -33,7 +33,7 @@ public function testCreateGelfLogger() static::assertInstanceOf(\Gelf\Logger::class, $logger); } - public function testCreateCollectionLoggerEmpty() + public function testCreateCollectionLoggerEmpty(): void { $factory = new Factory(); $logger = $factory->createCollectionLogger('test', [ @@ -43,7 +43,7 @@ public function testCreateCollectionLoggerEmpty() static::assertInstanceOf(\Phlib\Logger\LoggerType\Collection::class, $logger); } - public function testCreateCollectionLoggerExistingLogger() + public function testCreateCollectionLoggerExistingLogger(): void { $existingLogger = $this->createMock(\Psr\Log\LoggerInterface::class); @@ -55,7 +55,7 @@ public function testCreateCollectionLoggerExistingLogger() static::assertInstanceOf(\Phlib\Logger\LoggerType\Collection::class, $logger); } - public function testCreateCollectionLoggerWithConfig() + public function testCreateCollectionLoggerWithConfig(): void { $gelfConfig = [ 'type' => 'gelf', @@ -70,7 +70,7 @@ public function testCreateCollectionLoggerWithConfig() static::assertInstanceOf(\Phlib\Logger\LoggerType\Collection::class, $logger); } - public function testCreateCollectionLoggerWithInvalidConfig() + public function testCreateCollectionLoggerWithInvalidConfig(): void { $this->expectException(\DomainException::class); $this->expectExceptionMessageMatches('/at index 0$/'); @@ -85,7 +85,7 @@ public function testCreateCollectionLoggerWithInvalidConfig() ]); } - public function testCreateLoggerStreamUnfiltered() + public function testCreateLoggerStreamUnfiltered(): void { $fh = fopen('php://memory', 'a'); @@ -98,7 +98,7 @@ public function testCreateLoggerStreamUnfiltered() static::assertInstanceOf(\Phlib\Logger\LoggerType\Stream::class, $logger); } - public function testCreateLoggerGelfUnfiltered() + public function testCreateLoggerGelfUnfiltered(): void { $factory = new Factory(); $logger = $factory->createLogger('test', [ @@ -109,7 +109,7 @@ public function testCreateLoggerGelfUnfiltered() static::assertInstanceOf(\Gelf\Logger::class, $logger); } - public function testCreateLoggerCollectionUnfiltered() + public function testCreateLoggerCollectionUnfiltered(): void { $factory = new Factory(); $logger = $factory->createLogger('test', [ @@ -120,7 +120,7 @@ public function testCreateLoggerCollectionUnfiltered() static::assertInstanceOf(\Phlib\Logger\LoggerType\Collection::class, $logger); } - public function testDecoratorLevelIsRegisteredKey() + public function testDecoratorLevelIsRegisteredKey(): void { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('key already in use'); @@ -131,7 +131,7 @@ public function testDecoratorLevelIsRegisteredKey() $factory->registerDecorator($configKey, 'dummy'); } - public function testDecoratorLevelIsRegisteredClass() + public function testDecoratorLevelIsRegisteredClass(): void { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('class already registered'); @@ -142,7 +142,7 @@ public function testDecoratorLevelIsRegisteredClass() $factory->registerDecorator('dummy', $className); } - public function testUnregisterDecorator() + public function testUnregisterDecorator(): void { $configKey = 'level'; @@ -156,7 +156,7 @@ public function testUnregisterDecorator() $factory->unregisterDecorator($configKey); } - public function testUnregisterDecoratorNotSet() + public function testUnregisterDecoratorNotSet(): void { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('key not registered'); @@ -167,7 +167,7 @@ public function testUnregisterDecoratorNotSet() $factory->unregisterDecorator($configKey); } - public function testCreateLoggerDecoratorNotRegistered() + public function testCreateLoggerDecoratorNotRegistered(): void { $fh = fopen('php://memory', 'a'); @@ -183,7 +183,7 @@ public function testCreateLoggerDecoratorNotRegistered() static::assertInstanceOf(\Phlib\Logger\LoggerType\Stream::class, $logger); } - public function testCreateLoggerDecorator() + public function testCreateLoggerDecorator(): void { $fh = fopen('php://memory', 'a'); @@ -199,7 +199,7 @@ public function testCreateLoggerDecorator() static::assertInstanceOf(\Phlib\Logger\Decorator\LevelFilter::class, $logger); } - public function testRegisterDecoratorMissing() + public function testRegisterDecoratorMissing(): void { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('class not found'); @@ -215,7 +215,7 @@ public function testRegisterDecoratorMissing() ]); } - public function testRegisterDecoratorInvalid() + public function testRegisterDecoratorInvalid(): void { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('class invalid'); @@ -224,14 +224,14 @@ public function testRegisterDecoratorInvalid() $factory = new Factory(); $factory->registerDecorator('dummy', \Phlib\Logger\Config::class); - $logger = $factory->createLogger('test', [ + $factory->createLogger('test', [ 'type' => 'stream', 'dummy' => true, 'path' => $fh, ]); } - public function testCreateLoggerMissingLoggerType() + public function testCreateLoggerMissingLoggerType(): void { $this->expectException(\DomainException::class); $this->expectExceptionMessage('Logger config missing logger type'); @@ -242,7 +242,7 @@ public function testCreateLoggerMissingLoggerType() ]); } - public function testCreateLoggerInvalidLogger() + public function testCreateLoggerInvalidLogger(): void { $this->expectException(\DomainException::class); $this->expectExceptionMessage('Cannot find a logger type named'); diff --git a/tests/LoggerType/CliColorTest.php b/tests/LoggerType/CliColorTest.php index bebac91..b9790cd 100644 --- a/tests/LoggerType/CliColorTest.php +++ b/tests/LoggerType/CliColorTest.php @@ -10,7 +10,7 @@ class CliColorTest extends TestCase { - public function testLogDebug() + public function testLogDebug(): void { $resource = fopen('php://memory', 'a'); $logger = new CliColor('name', $resource); @@ -24,7 +24,7 @@ public function testLogDebug() static::assertStringContainsString($message, $logMessage); } - public function testLogInfo() + public function testLogInfo(): void { $resource = fopen('php://memory', 'a'); $logger = new CliColor('name', $resource); @@ -39,7 +39,7 @@ public function testLogInfo() static::assertStringEndsWith("\033[39m\n", $logMessage); } - public function testLogNotice() + public function testLogNotice(): void { $resource = fopen('php://memory', 'a'); $logger = new CliColor('name', $resource); @@ -54,7 +54,7 @@ public function testLogNotice() static::assertStringEndsWith("\033[39m" . PHP_EOL, $logMessage); } - public function testLogWarning() + public function testLogWarning(): void { $resource = fopen('php://memory', 'a'); $logger = new CliColor('name', $resource); @@ -69,7 +69,7 @@ public function testLogWarning() static::assertStringEndsWith("\033[39m" . PHP_EOL, $logMessage); } - public function testLogError() + public function testLogError(): void { $resource = fopen('php://memory', 'a'); $logger = new CliColor('name', $resource); @@ -84,7 +84,7 @@ public function testLogError() static::assertStringEndsWith("\033[39m" . PHP_EOL, $logMessage); } - public function testLogCritical() + public function testLogCritical(): void { $resource = fopen('php://memory', 'a'); $logger = new CliColor('name', $resource); @@ -99,7 +99,7 @@ public function testLogCritical() static::assertStringEndsWith("\033[39;49m" . PHP_EOL, $logMessage); } - public function testLogAlert() + public function testLogAlert(): void { $resource = fopen('php://memory', 'a'); $logger = new CliColor('name', $resource); @@ -114,7 +114,7 @@ public function testLogAlert() static::assertStringEndsWith("\033[39;49;22m" . PHP_EOL, $logMessage); } - public function testLogEmergency() + public function testLogEmergency(): void { $resource = fopen('php://memory', 'a'); $logger = new CliColor('name', $resource); diff --git a/tests/LoggerType/CollectionTest.php b/tests/LoggerType/CollectionTest.php index ca345cc..f01894d 100644 --- a/tests/LoggerType/CollectionTest.php +++ b/tests/LoggerType/CollectionTest.php @@ -5,19 +5,20 @@ namespace Phlib\Logger\Test\LoggerType; use Phlib\Logger\LoggerType\Collection; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; class CollectionTest extends TestCase { - public function testIsPsrLog() + public function testIsPsrLog(): void { $logger = new Collection(); - static::assertInstanceOf(\Psr\Log\LoggerInterface::class, $logger); + static::assertInstanceOf(LoggerInterface::class, $logger); } - public function testLog() + public function testLog(): void { $logger = new Collection(); @@ -44,12 +45,8 @@ public function testLog() $logger->log(LogLevel::ERROR, 'Test Log Message'); } - /** - * @return LoggerInterface - */ - protected function getMockLoggerInterface() + protected function getMockLoggerInterface(): LoggerInterface&MockObject { - $loggerInterface = $this->createMock(\Psr\Log\LoggerInterface::class); - return $loggerInterface; + return $this->createMock(LoggerInterface::class); } } diff --git a/tests/LoggerType/StreamTest.php b/tests/LoggerType/StreamTest.php index d9d0785..5880cde 100644 --- a/tests/LoggerType/StreamTest.php +++ b/tests/LoggerType/StreamTest.php @@ -10,7 +10,7 @@ class StreamTest extends TestCase { - public function testIsPsrLog() + public function testIsPsrLog(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); @@ -18,7 +18,7 @@ public function testIsPsrLog() static::assertInstanceOf(\Psr\Log\LoggerInterface::class, $stream); } - public function testLog() + public function testLog(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); @@ -31,7 +31,7 @@ public function testLog() static::assertStringContainsString($message, $logMessage); } - public function testMessageFormatName() + public function testMessageFormatName(): void { $streamName = 'myTestStreamLogger'; $resource = fopen('php://memory', 'a'); @@ -46,7 +46,7 @@ public function testMessageFormatName() static::assertEquals($streamName . PHP_EOL, $logMessage); } - public function testMessageFormatLevel() + public function testMessageFormatLevel(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); @@ -60,7 +60,7 @@ public function testMessageFormatLevel() static::assertEquals($level . PHP_EOL, $logMessage); } - public function testMessageFormatMessage() + public function testMessageFormatMessage(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); @@ -74,7 +74,7 @@ public function testMessageFormatMessage() static::assertEquals($message . PHP_EOL, $logMessage); } - public function testMessageFormatContext() + public function testMessageFormatContext(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); @@ -99,7 +99,7 @@ public function testMessageFormatContext() static::assertEquals($contextString . PHP_EOL, $logMessage); } - public function testNewDateFormat() + public function testNewDateFormat(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); @@ -115,7 +115,7 @@ public function testNewDateFormat() static::assertStringMatchesFormat('%d/%d/%d' . PHP_EOL, $logMessage); } - public function testStringCannotOpenException() + public function testStringCannotOpenException(): void { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Unable to open'); @@ -123,7 +123,7 @@ public function testStringCannotOpenException() new Stream('name', __DIR__ . '/path/what/does/not/exist'); } - public function testFormatContextBoolean() + public function testFormatContextBoolean(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); @@ -140,7 +140,7 @@ public function testFormatContextBoolean() static::assertEquals('true' . PHP_EOL, $logMessage); } - public function testFormatContextString() + public function testFormatContextString(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); @@ -157,7 +157,7 @@ public function testFormatContextString() static::assertEquals('value' . PHP_EOL, $logMessage); } - public function testFormatContextNull() + public function testFormatContextNull(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); @@ -174,7 +174,7 @@ public function testFormatContextNull() static::assertEquals('NULL' . PHP_EOL, $logMessage); } - public function testFormatContextClass() + public function testFormatContextClass(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); @@ -193,7 +193,7 @@ public function testFormatContextClass() static::assertEquals(\stdClass::class . PHP_EOL, $logMessage); } - public function testFormatContextRawType() + public function testFormatContextRawType(): void { $resource = fopen('php://memory', 'a'); $stream = new Stream('name', $resource); diff --git a/tests/PoolTest.php b/tests/PoolTest.php index f986c44..50870c7 100644 --- a/tests/PoolTest.php +++ b/tests/PoolTest.php @@ -10,7 +10,7 @@ class PoolTest extends TestCase { - public function testGetLogger() + public function testGetLogger(): void { $loggerConfig = [ 'type' => 'collection', @@ -35,7 +35,7 @@ public function testGetLogger() static::assertSame($logger, $pool->test); } - public function testGetLoggerAgain() + public function testGetLoggerAgain(): void { $loggerConfig = [ 'type' => 'stream', @@ -64,7 +64,7 @@ public function testGetLoggerAgain() static::assertSame($actualLogger, $pool->test); } - public function testPrefix() + public function testPrefix(): void { $prefix = 'logger-prefix-'; @@ -93,7 +93,7 @@ public function testPrefix() static::assertSame($logger, $pool->test); } - public function testGetLoggerCollection() + public function testGetLoggerCollection(): void { $loggerConfig = [ 'type' => 'stream',