summaryrefslogtreecommitdiff
path: root/tests/Unit/Exceptions/Handlers/WhoopsTest.php
blob: f3e9cf32b06e4f967d1b098fa7cf722c52fa064a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace Engelsystem\Test\Unit\Exceptions\handlers;

use Engelsystem\Application;
use Engelsystem\Exceptions\Handlers\Whoops;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Request;
use Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Whoops\Handler\JsonResponseHandler;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run as WhoopsRunner;
use Whoops\RunInterface as WhoopsRunnerInterface;

class WhoopsTest extends TestCase
{
    /**
     * @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
     */
    public function testRender()
    {
        /** @var Application|MockObject $app */
        $app = $this->createMock(Application::class);
        /** @var Authenticator|MockObject $auth */
        $auth = $this->createMock(Authenticator::class);
        /** @var Request|MockObject $request */
        $request = $this->createMock(Request::class);
        /** @var WhoopsRunnerInterface|MockObject $whoopsRunner */
        $whoopsRunner = $this->getMockForAbstractClass(WhoopsRunnerInterface::class);
        /** @var PrettyPageHandler|MockObject $prettyPageHandler */
        $prettyPageHandler = $this->createMock(PrettyPageHandler::class);
        /** @var JsonResponseHandler|MockObject $jsonResponseHandler */
        $jsonResponseHandler = $this->createMock(JsonResponseHandler::class);
        /** @var Exception|MockObject $exception */
        $exception = $this->createMock(Exception::class);

        $request->expects($this->once())
            ->method('isXmlHttpRequest')
            ->willReturn(true);

        $prettyPageHandler
            ->expects($this->atLeastOnce())
            ->method('setApplicationPaths');
        $prettyPageHandler
            ->expects($this->once())
            ->method('setApplicationPaths');
        $prettyPageHandler
            ->expects($this->once())
            ->method('addDataTable');

        $jsonResponseHandler->expects($this->once())
            ->method('setJsonApi')
            ->with(true);
        $jsonResponseHandler->expects($this->once())
            ->method('addTraceToOutput')
            ->with(true);

        $app->expects($this->exactly(3))
            ->method('make')
            ->withConsecutive(
                [WhoopsRunner::class],
                [PrettyPageHandler::class],
                [JsonResponseHandler::class]
            )
            ->willReturnOnConsecutiveCalls(
                $whoopsRunner,
                $prettyPageHandler,
                $jsonResponseHandler
            );
        $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);

        $whoopsRunner
            ->expects($this->exactly(2))
            ->method('pushHandler')
            ->withConsecutive(
                [$prettyPageHandler],
                [$jsonResponseHandler]
            );
        $whoopsRunner
            ->expects($this->once())
            ->method('writeToOutput')
            ->with(false);
        $whoopsRunner
            ->expects($this->once())
            ->method('allowQuit')
            ->with(false);
        $whoopsRunner
            ->expects($this->once())
            ->method('handleException')
            ->with($exception);

        $handler = new Whoops($app);
        $handler->render($request, $exception);
    }
}