2019-07-16 02:59:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Logger;
|
|
|
|
|
2020-02-29 20:53:25 +01:00
|
|
|
use Engelsystem\Logger\Logger;
|
2019-07-16 02:59:33 +02:00
|
|
|
use Engelsystem\Models\LogEntry;
|
|
|
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use Psr\Log\LogLevel;
|
|
|
|
|
2020-02-29 20:53:25 +01:00
|
|
|
class LoggerTest extends ServiceProviderTest
|
2019-07-16 02:59:33 +02:00
|
|
|
{
|
|
|
|
/**
|
2020-02-29 20:53:25 +01:00
|
|
|
* @covers \Engelsystem\Logger\Logger::__construct
|
|
|
|
* @covers \Engelsystem\Logger\Logger::log
|
2019-07-16 02:59:33 +02:00
|
|
|
*/
|
|
|
|
public function testLog(): void
|
|
|
|
{
|
|
|
|
/** @var LogEntry|MockObject $logEntry */
|
2019-11-06 12:15:14 +01:00
|
|
|
$logEntry = $this->getMockBuilder(LogEntry::class)
|
|
|
|
->addMethods(['create'])
|
|
|
|
->getMock();
|
2019-07-16 02:59:33 +02:00
|
|
|
$logEntry->expects($this->once())
|
|
|
|
->method('create')
|
|
|
|
->with(['level' => LogLevel::INFO, 'message' => 'I\'m an information!']);
|
|
|
|
|
2020-02-29 20:53:25 +01:00
|
|
|
$logger = new Logger($logEntry);
|
2019-07-16 02:59:33 +02:00
|
|
|
|
|
|
|
$logger->log(LogLevel::INFO, 'I\'m an information!');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-29 20:53:25 +01:00
|
|
|
* @covers \Engelsystem\Logger\Logger::log
|
|
|
|
* @covers \Engelsystem\Logger\Logger::checkLevel
|
2019-07-16 02:59:33 +02:00
|
|
|
*/
|
|
|
|
public function testCheckLevel(): void
|
|
|
|
{
|
|
|
|
/** @var LogEntry|MockObject $logEntry */
|
|
|
|
$logEntry = $this->createMock(LogEntry::class);
|
2020-02-29 20:53:25 +01:00
|
|
|
$logger = new Logger($logEntry);
|
2019-07-16 02:59:33 +02:00
|
|
|
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$logger->log('FooBar', 'Random Stuff');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-29 20:53:25 +01:00
|
|
|
* @covers \Engelsystem\Logger\Logger::interpolate
|
2019-07-16 02:59:33 +02:00
|
|
|
*/
|
|
|
|
public function testInterpolate(): void
|
|
|
|
{
|
|
|
|
/** @var LogEntry|MockObject $logEntry */
|
2019-11-06 12:15:14 +01:00
|
|
|
$logEntry = $this->getMockBuilder(LogEntry::class)
|
|
|
|
->addMethods(['create'])
|
|
|
|
->getMock();
|
2019-07-16 02:59:33 +02:00
|
|
|
$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']]
|
|
|
|
);
|
|
|
|
|
2020-02-29 20:53:25 +01:00
|
|
|
$logger = new Logger($logEntry);
|
2019-07-16 02:59:33 +02:00
|
|
|
|
|
|
|
$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
|
|
|
|
{
|
2019-11-10 23:26:23 +01:00
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return 'Bar';
|
|
|
|
}
|
2019-07-16 02:59:33 +02:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|