From fece50ca0993d2b52e09be8543cb796594681c9a Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 16 Jul 2019 02:59:33 +0200 Subject: [PATCH] Basic coverage tests of logger classes for 100% unit tests coverage --- tests/Unit/HasDatabase.php | 1 + tests/Unit/Logger/EngelsystemLoggerTest.php | 72 +++++++++++++++++++++ tests/Unit/Models/LogEntryTest.php | 43 ++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 tests/Unit/Logger/EngelsystemLoggerTest.php create mode 100644 tests/Unit/Models/LogEntryTest.php diff --git a/tests/Unit/HasDatabase.php b/tests/Unit/HasDatabase.php index 175c244b..7a58bb2b 100644 --- a/tests/Unit/HasDatabase.php +++ b/tests/Unit/HasDatabase.php @@ -44,6 +44,7 @@ trait HasDatabase ['migration' => '2018_01_01_000003_fix_old_tables'], ['migration' => '2018_01_01_000004_cleanup_group_privileges'], ['migration' => '2018_01_01_000005_add_angel_supporter_permissions'], + ['migration' => '2018_12_27_000000_fix_missing_arrival_dates'], ]); $migration->run(__DIR__ . '/../../db/migrations'); diff --git a/tests/Unit/Logger/EngelsystemLoggerTest.php b/tests/Unit/Logger/EngelsystemLoggerTest.php new file mode 100644 index 00000000..0f4c8c32 --- /dev/null +++ b/tests/Unit/Logger/EngelsystemLoggerTest.php @@ -0,0 +1,72 @@ +createMock(LogEntry::class); + $logEntry->expects($this->once()) + ->method('create') + ->with(['level' => LogLevel::INFO, 'message' => 'I\'m an information!']); + + $logger = new EngelsystemLogger($logEntry); + + $logger->log(LogLevel::INFO, 'I\'m an information!'); + } + + /** + * @covers \Engelsystem\Logger\EngelsystemLogger::log + * @covers \Engelsystem\Logger\EngelsystemLogger::checkLevel + */ + public function testCheckLevel() + { + /** @var LogEntry|MockObject $logEntry */ + $logEntry = $this->createMock(LogEntry::class); + $logger = new EngelsystemLogger($logEntry); + + $this->expectException(InvalidArgumentException::class); + $logger->log('FooBar', 'Random Stuff'); + } + + /** + * @covers \Engelsystem\Logger\EngelsystemLogger::interpolate + */ + public function testInterpolate() + { + /** @var LogEntry|MockObject $logEntry */ + $logEntry = $this->createMock(LogEntry::class); + $logEntry->expects($this->exactly(3)) + ->method('create') + ->withConsecutive( + [['level' => LogLevel::DEBUG, 'message' => 'User: Foo']], + [['level' => LogLevel::NOTICE, 'message' => 'User: {user}']], + [['level' => LogLevel::NOTICE, 'message' => 'User: Bar']] + ); + + $logger = new EngelsystemLogger($logEntry); + + $logger->log(LogLevel::DEBUG, 'User: {user}', ['user' => 'Foo']); + $logger->log(LogLevel::NOTICE, 'User: {user}', ['user' => ['name' => 'Lorem']]); + $logger->log(LogLevel::NOTICE, 'User: {user}', [ + 'user' => + new class + { + public function __toString() { return 'Bar'; } + } + ]); + } +} diff --git a/tests/Unit/Models/LogEntryTest.php b/tests/Unit/Models/LogEntryTest.php new file mode 100644 index 00000000..0a0efa3c --- /dev/null +++ b/tests/Unit/Models/LogEntryTest.php @@ -0,0 +1,43 @@ + LogLevel::INFO, + '*Insert explosion here*' => LogLevel::EMERGENCY, + 'Tracing along' => LogLevel::DEBUG, + 'Oops' => LogLevel::ERROR, + 'It\'s happening' => LogLevel::INFO, + 'Something is wrong' => LogLevel::ERROR, + 'Ohi' => LogLevel::INFO, + ] as $message => $level) { + (new LogEntry(['level' => $level, 'message' => $message]))->save(); + } + + $this->assertCount(7, LogEntry::filter()); + $this->assertCount(3, LogEntry::filter(LogLevel::INFO)); + $this->assertCount(1, LogEntry::filter('Oops')); + } + + /** + * Prepare test + */ + protected function setUp(): void + { + $this->initDatabase(); + } +}