Basic coverage tests of logger classes for 100% unit tests coverage
parent
6ca0e8e81a
commit
b9cb7d57fd
@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Engelsystem\Test\Unit\Logger;
|
||||||
|
|
||||||
|
use Engelsystem\Logger\EngelsystemLogger;
|
||||||
|
use Engelsystem\Models\LogEntry;
|
||||||
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
|
use Psr\Log\LogLevel;
|
||||||
|
|
||||||
|
class EngelsystemLoggerTest extends ServiceProviderTest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers \Engelsystem\Logger\EngelsystemLogger::__construct
|
||||||
|
* @covers \Engelsystem\Logger\EngelsystemLogger::log
|
||||||
|
*/
|
||||||
|
public function testLog()
|
||||||
|
{
|
||||||
|
/** @var LogEntry|MockObject $logEntry */
|
||||||
|
$logEntry = $this->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'; }
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Engelsystem\Test\Unit\Models;
|
||||||
|
|
||||||
|
use Engelsystem\Models\LogEntry;
|
||||||
|
use Engelsystem\Test\Unit\HasDatabase;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Psr\Log\LogLevel;
|
||||||
|
|
||||||
|
class LogEntryTest extends TestCase
|
||||||
|
{
|
||||||
|
use HasDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \Engelsystem\Models\LogEntry::filter
|
||||||
|
*/
|
||||||
|
public function testFilter()
|
||||||
|
{
|
||||||
|
foreach ([
|
||||||
|
'I\'m an info' => 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue