2017-11-24 15:08:43 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-19 20:41:38 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Exceptions\Handlers;
|
2017-11-24 15:08:43 +01:00
|
|
|
|
|
|
|
use Engelsystem\Exceptions\Handlers\Legacy;
|
|
|
|
use Engelsystem\Http\Request;
|
2020-04-19 20:41:38 +02:00
|
|
|
use ErrorException;
|
2017-11-24 15:08:43 +01:00
|
|
|
use Exception;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-11-24 15:08:43 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-04-19 20:41:38 +02:00
|
|
|
use Psr\Log\Test\TestLogger;
|
2017-11-24 15:08:43 +01:00
|
|
|
|
|
|
|
class LegacyTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2020-04-19 20:41:38 +02:00
|
|
|
* @covers \Engelsystem\Exceptions\Handlers\Legacy::render
|
2017-11-24 15:08:43 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRender(): void
|
2017-11-24 15:08:43 +01:00
|
|
|
{
|
2023-09-09 23:45:45 +02:00
|
|
|
/** @var Legacy|MockObject $handler */
|
|
|
|
$handler = $this->getMockBuilder(Legacy::class)
|
|
|
|
->onlyMethods(['isCli'])
|
|
|
|
->getMock();
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Request|MockObject $request */
|
2017-11-24 15:08:43 +01:00
|
|
|
$request = $this->createMock(Request::class);
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Exception|MockObject $exception */
|
2017-11-24 15:08:43 +01:00
|
|
|
$exception = $this->createMock(Exception::class);
|
|
|
|
|
2023-09-09 23:45:45 +02:00
|
|
|
$handler->expects($this->exactly(2))
|
|
|
|
->method('isCli')
|
|
|
|
->willReturnOnConsecutiveCalls(false, true);
|
|
|
|
|
2017-11-24 15:08:43 +01:00
|
|
|
$this->expectOutputRegex('/.*error occurred.*/i');
|
2023-09-09 23:45:45 +02:00
|
|
|
$handler->render($request, $exception);
|
2017-11-24 15:08:43 +01:00
|
|
|
|
2023-09-09 23:45:45 +02:00
|
|
|
// As CLI
|
2017-11-24 15:08:43 +01:00
|
|
|
$handler->render($request, $exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-19 20:41:38 +02:00
|
|
|
* @covers \Engelsystem\Exceptions\Handlers\Legacy::report
|
|
|
|
* @covers \Engelsystem\Exceptions\Handlers\Legacy::setLogger
|
|
|
|
* @covers \Engelsystem\Exceptions\Handlers\Legacy::stripBasePath
|
2017-11-24 15:08:43 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testReport(): void
|
2017-11-24 15:08:43 +01:00
|
|
|
{
|
|
|
|
$handler = new Legacy();
|
|
|
|
$exception = new Exception('Lorem Ipsum', 4242);
|
|
|
|
$line = __LINE__ - 1;
|
2020-04-19 20:41:38 +02:00
|
|
|
$exception2 = new Exception('Test Exception');
|
2020-10-17 16:23:31 +02:00
|
|
|
$exception3 = new Exception('Moar Exceptions!', 42, new Exception('Another Exception'));
|
2020-04-19 20:41:38 +02:00
|
|
|
$logger = new TestLogger();
|
|
|
|
$logger2 = $this->createMock(TestLogger::class);
|
|
|
|
$logger2->expects($this->once())
|
|
|
|
->method('critical')
|
2022-12-14 19:15:20 +01:00
|
|
|
->willReturnCallback(function (): void {
|
2020-04-19 20:41:38 +02:00
|
|
|
throw new ErrorException();
|
|
|
|
});
|
2017-11-24 15:08:43 +01:00
|
|
|
|
2020-04-19 20:41:38 +02:00
|
|
|
$logfile = tempnam(sys_get_temp_dir(), 'engelsystem-log');
|
2017-11-24 15:08:43 +01:00
|
|
|
$errorLog = ini_get('error_log');
|
2020-04-19 20:41:38 +02:00
|
|
|
ini_set('error_log', $logfile);
|
2017-11-24 15:08:43 +01:00
|
|
|
$handler->report($exception);
|
2020-04-19 20:41:38 +02:00
|
|
|
$handler->setLogger($logger);
|
|
|
|
$handler->report($exception2);
|
|
|
|
$handler->setLogger($logger2);
|
|
|
|
$handler->report($exception3);
|
2017-11-24 15:08:43 +01:00
|
|
|
ini_set('error_log', $errorLog);
|
2020-04-19 20:41:38 +02:00
|
|
|
$logContent = file_get_contents($logfile);
|
|
|
|
unset($logfile);
|
2017-11-24 15:08:43 +01:00
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
$this->assertStringContainsString('4242', $logContent);
|
|
|
|
$this->assertStringContainsString('Lorem Ipsum', $logContent);
|
|
|
|
$this->assertStringContainsString(basename(__FILE__), $logContent);
|
2022-12-25 11:59:45 +01:00
|
|
|
$this->assertStringContainsString((string) $line, $logContent);
|
2019-04-24 10:45:00 +02:00
|
|
|
$this->assertStringContainsString(__FUNCTION__, $logContent);
|
|
|
|
$this->assertStringContainsString(json_encode(__CLASS__), $logContent);
|
2020-10-17 16:23:31 +02:00
|
|
|
$this->assertStringContainsString('Test Exception', $logContent);
|
|
|
|
$this->assertStringContainsString('Moar Exceptions!', $logContent);
|
|
|
|
$this->assertStringContainsString('Another Exception', $logContent);
|
2020-04-19 20:41:38 +02:00
|
|
|
|
|
|
|
$this->assertTrue($logger->hasRecordThatPasses(function (array $record) use ($exception2) {
|
|
|
|
$context = $record['context'];
|
|
|
|
return isset($context['exception']) && $context['exception'] === $exception2;
|
|
|
|
}, 'critical'));
|
2017-11-24 15:08:43 +01:00
|
|
|
}
|
|
|
|
}
|