diff --git a/src/Logger/Log.php b/src/Logger/Log.php index 46f6abe..ca73045 100644 --- a/src/Logger/Log.php +++ b/src/Logger/Log.php @@ -30,27 +30,27 @@ class Log /** * @var string (required, for example 'Log::TYPE_INFO') */ - protected string $type; + protected string $type = ''; /** * @var string (required) */ - protected string $message; + protected string $message = ''; /** * @var string (required) */ - protected string $version; + protected string $version = ''; /** * @var string (required) */ - protected string $environment; + protected string $environment = ''; /** * @var string (required) */ - protected string $action; + protected string $action = ''; /** * @var array (optional) diff --git a/tests/unit/LoggerTest.php b/tests/unit/LoggerTest.php new file mode 100644 index 0000000..53e2820 --- /dev/null +++ b/tests/unit/LoggerTest.php @@ -0,0 +1,114 @@ +getAdapter()); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Log is not ready to be pushed.'); + + $logger->addLog(new Log()); + } + + /** + * A partially populated log (missing the action) must also be rejected + * with an Exception. + * + * @throws Exception + */ + public function testAddLogWithMissingActionThrowsException(): void + { + $logger = new Logger($this->getAdapter()); + + $log = new Log(); + $log->setType(Log::TYPE_ERROR); + $log->setMessage('Something went wrong'); + $log->setVersion('1.0.0'); + $log->setEnvironment(Log::ENVIRONMENT_PRODUCTION); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Log is not ready to be pushed.'); + + $logger->addLog($log); + } + + /** + * Reading a required field of a fresh log must not fatal. + */ + public function testUnsetRequiredFieldsAreEmptyStrings(): void + { + $log = new Log(); + + self::assertSame('', $log->getAction()); + self::assertSame('', $log->getType()); + self::assertSame('', $log->getMessage()); + self::assertSame('', $log->getVersion()); + self::assertSame('', $log->getEnvironment()); + } + + /** + * A fully populated log is pushed to the adapter. + * + * @throws Exception + */ + public function testAddLogWithCompleteLog(): void + { + $logger = new Logger($this->getAdapter()); + + $log = new Log(); + $log->setType(Log::TYPE_ERROR); + $log->setMessage('Something went wrong'); + $log->setVersion('1.0.0'); + $log->setEnvironment(Log::ENVIRONMENT_PRODUCTION); + $log->setAction('testAction'); + + self::assertEquals(200, $logger->addLog($log)); + } +}