diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..0619b4e --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,32 @@ + + + + + ./tests + + + + + ./src/ZM/Logger + + + + + + + + + + diff --git a/tests/ConsoleLoggerTest.php b/tests/ConsoleLoggerTest.php new file mode 100644 index 0000000..60316bc --- /dev/null +++ b/tests/ConsoleLoggerTest.php @@ -0,0 +1,95 @@ +getLogger(); + $logger->{$level}($message); + $logger->log($level, $message); + + $this->assertSame($expected, $this->getLogs()); + } + + public function provideCanLogAtAllLevels(): array + { + return [ + LogLevel::EMERGENCY => [LogLevel::EMERGENCY, 'this is a emergency message for testing'], + LogLevel::ALERT => [LogLevel::ALERT, 'this is a alert message for testing'], + LogLevel::CRITICAL => [LogLevel::CRITICAL, 'this is a critical message for testing'], + LogLevel::ERROR => [LogLevel::ERROR, 'this is a error message for testing'], + LogLevel::WARNING => [LogLevel::WARNING, 'this is a warning message for testing'], + LogLevel::NOTICE => [LogLevel::NOTICE, 'this is a notice message for testing'], + LogLevel::INFO => [LogLevel::INFO, 'this is a info message for testing'], + LogLevel::DEBUG => [LogLevel::DEBUG, 'this is a debug message for testing'], + ]; + } + + public function testWillThrowsOnInvalidLevel(): void + { + $this->expectException(InvalidArgumentException::class); + + $logger = $this->getLogger(); + $logger->log('invalid', 'this is a message for testing'); + } + + public function testCanReplaceContext(): void + { + $logger = $this->getLogger(); + $logger->info('this is a {message} with {nothing}', ['message' => 'info message for testing']); + $this->assertSame(['this is a info message for testing with {nothing}'], $this->getLogs()); + } + + public function testCanCastObjectToString(): void + { + $string = uniqid('DUMMY', true); + $dummy = $this->createMock(\Stringable::class); + $dummy->expects($this->once())->method('__toString')->willReturn($string); + + $this->getLogger()->info($dummy); + $this->assertSame([$string], $this->getLogs()); + } + + /** + * @dataProvider provideTestCanContainAnythingInContext + */ + public function testCanContainAnythingInContext($context, $expected): void + { + $logger = $this->getLogger(); + $logger->info('{context}', ['context' => $context]); + $this->assertSame([$expected], $this->getLogs()); + } + + public function provideTestCanContainAnythingInContext(): array + { + return [ + 'callable' => [[new ConsoleLoggerTest(), 'testCanContainAnythingInContext'], self::class . '@testCanContainAnythingInContext'], + 'closure' => [Closure::fromCallable([$this, 'testCanContainAnythingInContext']), 'closure'], + 'string' => ['string', 'string'], + 'array' => [['123', '42', 'hello', 122], 'array["123","42","hello",122]'], + 'object' => [new \stdClass(), 'stdClass'], + 'resource' => [fopen('php://memory', 'rb'), 'resource(stream)'], + 'null' => [null, 'null'], + 'boolean 1' => [true, 'true'], + 'boolean 2' => [false, 'false'], + 'float' => [123.456, '123.456'], + 'integer' => [123, '123'], + ]; + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..c5b6beb --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,30 @@ +addLogCallback(function ($level, $output, $message, $context) { + $this->logs[] = $output; + return false; + }); + return $logger; + } + + protected function getLogs(): array + { + return $this->logs; + } +}