2017-11-24 15:08:43 +01:00
|
|
|
<?php
|
|
|
|
|
2020-04-19 20:41:38 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Exceptions\Handlers;
|
2017-11-24 15:08:43 +01:00
|
|
|
|
|
|
|
use Engelsystem\Application;
|
|
|
|
use Engelsystem\Exceptions\Handlers\Whoops;
|
2018-10-08 19:30:37 +02:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
2017-11-24 15:08:43 +01:00
|
|
|
use Engelsystem\Http\Request;
|
2020-06-05 10:48:48 +02:00
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
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 Whoops\Handler\JsonResponseHandler;
|
|
|
|
use Whoops\Handler\PrettyPageHandler;
|
|
|
|
use Whoops\Run as WhoopsRunner;
|
|
|
|
use Whoops\RunInterface as WhoopsRunnerInterface;
|
|
|
|
|
|
|
|
class WhoopsTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2020-01-02 15:08:08 +01:00
|
|
|
* @covers \Engelsystem\Exceptions\Handlers\Whoops::__construct
|
|
|
|
* @covers \Engelsystem\Exceptions\Handlers\Whoops::render
|
|
|
|
* @covers \Engelsystem\Exceptions\Handlers\Whoops::getPrettyPageHandler
|
|
|
|
* @covers \Engelsystem\Exceptions\Handlers\Whoops::getJsonResponseHandler
|
|
|
|
* @covers \Engelsystem\Exceptions\Handlers\Whoops::getData
|
2017-11-24 15:08:43 +01:00
|
|
|
*/
|
|
|
|
public function testRender(): void
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Application|MockObject $app */
|
2017-11-24 15:08:43 +01:00
|
|
|
$app = $this->createMock(Application::class);
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
2018-10-08 19:30:37 +02:00
|
|
|
$auth = $this->createMock(Authenticator::class);
|
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 WhoopsRunnerInterface|MockObject $whoopsRunner */
|
2017-11-24 15:08:43 +01:00
|
|
|
$whoopsRunner = $this->getMockForAbstractClass(WhoopsRunnerInterface::class);
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var PrettyPageHandler|MockObject $prettyPageHandler */
|
2017-11-24 15:08:43 +01:00
|
|
|
$prettyPageHandler = $this->createMock(PrettyPageHandler::class);
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var JsonResponseHandler|MockObject $jsonResponseHandler */
|
2018-10-08 19:30:37 +02:00
|
|
|
$jsonResponseHandler = $this->createMock(JsonResponseHandler::class);
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Exception|MockObject $exception */
|
2018-10-08 19:30:37 +02:00
|
|
|
$exception = $this->createMock(Exception::class);
|
|
|
|
|
2020-06-05 10:48:48 +02:00
|
|
|
$this->setExpects($request, 'isXmlHttpRequest', null, true);
|
2018-10-08 19:30:37 +02:00
|
|
|
|
2020-06-05 10:48:48 +02:00
|
|
|
$this->setExpects($prettyPageHandler, 'setApplicationPaths');
|
|
|
|
$this->setExpects($prettyPageHandler, 'addDataTable');
|
2018-10-08 19:30:37 +02:00
|
|
|
|
2020-06-05 10:48:48 +02:00
|
|
|
$this->setExpects($jsonResponseHandler, 'setJsonApi', [true]);
|
|
|
|
$this->setExpects($jsonResponseHandler, 'addTraceToOutput', [true]);
|
2017-11-24 15:08:43 +01:00
|
|
|
|
|
|
|
$app->expects($this->exactly(3))
|
|
|
|
->method('make')
|
|
|
|
->withConsecutive(
|
|
|
|
[WhoopsRunner::class],
|
|
|
|
[PrettyPageHandler::class],
|
|
|
|
[JsonResponseHandler::class]
|
|
|
|
)
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
$whoopsRunner,
|
|
|
|
$prettyPageHandler,
|
|
|
|
$jsonResponseHandler
|
|
|
|
);
|
2018-10-08 19:30:37 +02:00
|
|
|
$app->expects($this->once())
|
|
|
|
->method('has')
|
|
|
|
->with('authenticator')
|
|
|
|
->willReturn(true);
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('authenticator')
|
|
|
|
->willReturn($auth);
|
|
|
|
|
|
|
|
$auth->expects($this->once())
|
|
|
|
->method('user')
|
|
|
|
->willReturn(null);
|
2017-11-24 15:08:43 +01:00
|
|
|
|
|
|
|
$whoopsRunner
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('pushHandler')
|
|
|
|
->withConsecutive(
|
|
|
|
[$prettyPageHandler],
|
|
|
|
[$jsonResponseHandler]
|
|
|
|
);
|
2020-06-05 10:48:48 +02:00
|
|
|
$this->setExpects($whoopsRunner, 'writeToOutput', [false]);
|
|
|
|
$this->setExpects($whoopsRunner, 'allowQuit', [false]);
|
|
|
|
$this->setExpects($whoopsRunner, 'handleException', [$exception]);
|
2017-11-24 15:08:43 +01:00
|
|
|
|
|
|
|
$handler = new Whoops($app);
|
|
|
|
$handler->render($request, $exception);
|
|
|
|
}
|
|
|
|
}
|