You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.4 KiB
PHTML
80 lines
2.4 KiB
PHTML
6 years ago
|
<?php
|
||
|
|
||
|
namespace Engelsystem\Test\Unit\Logger;
|
||
|
|
||
5 years ago
|
use Engelsystem\Logger\Logger;
|
||
6 years ago
|
use Engelsystem\Models\LogEntry;
|
||
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||
|
use InvalidArgumentException;
|
||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||
|
use Psr\Log\LogLevel;
|
||
|
|
||
5 years ago
|
class LoggerTest extends ServiceProviderTest
|
||
6 years ago
|
{
|
||
|
/**
|
||
5 years ago
|
* @covers \Engelsystem\Logger\Logger::__construct
|
||
|
* @covers \Engelsystem\Logger\Logger::log
|
||
6 years ago
|
*/
|
||
|
public function testLog()
|
||
|
{
|
||
|
/** @var LogEntry|MockObject $logEntry */
|
||
5 years ago
|
$logEntry = $this->getMockBuilder(LogEntry::class)
|
||
|
->addMethods(['create'])
|
||
|
->getMock();
|
||
6 years ago
|
$logEntry->expects($this->once())
|
||
|
->method('create')
|
||
|
->with(['level' => LogLevel::INFO, 'message' => 'I\'m an information!']);
|
||
|
|
||
5 years ago
|
$logger = new Logger($logEntry);
|
||
6 years ago
|
|
||
|
$logger->log(LogLevel::INFO, 'I\'m an information!');
|
||
|
}
|
||
|
|
||
|
/**
|
||
5 years ago
|
* @covers \Engelsystem\Logger\Logger::log
|
||
|
* @covers \Engelsystem\Logger\Logger::checkLevel
|
||
6 years ago
|
*/
|
||
|
public function testCheckLevel()
|
||
|
{
|
||
|
/** @var LogEntry|MockObject $logEntry */
|
||
|
$logEntry = $this->createMock(LogEntry::class);
|
||
5 years ago
|
$logger = new Logger($logEntry);
|
||
6 years ago
|
|
||
|
$this->expectException(InvalidArgumentException::class);
|
||
|
$logger->log('FooBar', 'Random Stuff');
|
||
|
}
|
||
|
|
||
|
/**
|
||
5 years ago
|
* @covers \Engelsystem\Logger\Logger::interpolate
|
||
6 years ago
|
*/
|
||
|
public function testInterpolate()
|
||
|
{
|
||
|
/** @var LogEntry|MockObject $logEntry */
|
||
5 years ago
|
$logEntry = $this->getMockBuilder(LogEntry::class)
|
||
|
->addMethods(['create'])
|
||
|
->getMock();
|
||
6 years ago
|
$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']]
|
||
|
);
|
||
|
|
||
5 years ago
|
$logger = new Logger($logEntry);
|
||
6 years ago
|
|
||
|
$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
|
||
|
{
|
||
5 years ago
|
public function __toString()
|
||
|
{
|
||
|
return 'Bar';
|
||
|
}
|
||
6 years ago
|
}
|
||
|
]);
|
||
|
}
|
||
|
}
|