1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Debug\Exceptions\Handlers\Whoops; |
6
|
|
|
|
7
|
|
|
use Opulence\Http\HttpException; |
8
|
|
|
use Opulence\Http\Requests\Request; |
9
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Whoops\RunInterface; |
12
|
|
|
|
13
|
|
|
class ExceptionRendererTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testGetRunReturnsRunSet(): void |
16
|
|
|
{ |
17
|
|
|
/** @var MockObject|RunInterface $runMock */ |
18
|
|
|
$runMock = $this->createMock(RunInterface::class); |
19
|
|
|
|
20
|
|
|
$sut = new ExceptionRenderer($runMock, true); |
21
|
|
|
|
22
|
|
|
$actualResult = $sut->getRun(); |
23
|
|
|
|
24
|
|
|
$this->assertSame($runMock, $actualResult); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testRenderCallsHandleException(): void |
28
|
|
|
{ |
29
|
|
|
$isDevelopmentEnvironment = true; |
30
|
|
|
|
31
|
|
|
$exceptionStub = new \Exception(); |
32
|
|
|
|
33
|
|
|
/** @var MockObject|RunInterface $runMock */ |
34
|
|
|
$runMock = $this->createMock(RunInterface::class); |
35
|
|
|
|
36
|
|
|
$runMock->expects($this->atLeastOnce())->method('handleException')->with($exceptionStub); |
37
|
|
|
|
38
|
|
|
$sut = new ExceptionRenderer($runMock, $isDevelopmentEnvironment); |
39
|
|
|
|
40
|
|
|
$sut->render($exceptionStub); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testRenderInProduction(): void |
44
|
|
|
{ |
45
|
|
|
$isDevelopmentEnvironment = false; |
46
|
|
|
|
47
|
|
|
$exceptionStub = new \Exception(); |
48
|
|
|
|
49
|
|
|
/** @var MockObject|RunInterface $runMock */ |
50
|
|
|
$runMock = $this->createMock(RunInterface::class); |
51
|
|
|
|
52
|
|
|
$runMock->expects($this->never())->method('handleException'); |
53
|
|
|
|
54
|
|
|
$sut = new ExceptionRenderer($runMock, $isDevelopmentEnvironment); |
55
|
|
|
|
56
|
|
|
$sut->render($exceptionStub); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testRenderHttpExceptionInDevelopmentWithJsonRequest(): void |
60
|
|
|
{ |
61
|
|
|
$expectedOutputString = 'foo'; |
62
|
|
|
|
63
|
|
|
$this->expectOutputString($expectedOutputString); |
64
|
|
|
|
65
|
|
|
$isDevelopmentEnvironment = false; |
66
|
|
|
|
67
|
|
|
$exceptionStub = new HttpException(505, $expectedOutputString, ['foo' => 'bar']); |
68
|
|
|
$jsonRequest = new Request([], [], [], [], [], []); |
69
|
|
|
$jsonRequest->getHeaders()->add('CONTENT_TYPE', 'application/json'); |
70
|
|
|
|
71
|
|
|
/** @var MockObject|RunInterface $runMock */ |
72
|
|
|
$runMock = $this->createMock(RunInterface::class); |
73
|
|
|
|
74
|
|
|
$runMock->expects($this->never())->method('handleException'); |
75
|
|
|
|
76
|
|
|
$sut = new ExceptionRenderer($runMock, $isDevelopmentEnvironment); |
77
|
|
|
$sut->setRequest($jsonRequest); |
78
|
|
|
|
79
|
|
|
$sut->render($exceptionStub); |
80
|
|
|
|
81
|
|
|
$headers = $sut->getHeaders(); |
82
|
|
|
$this->assertCount(3, $headers); |
83
|
|
|
$this->assertSame('foo:bar', $headers[1][0]); |
84
|
|
|
$this->assertSame('Content-Type:application/json', $headers[2][0]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testRenderHttpExceptionInDevelopmentWithHttpRequest(): void |
88
|
|
|
{ |
89
|
|
|
$expectedOutputString = 'foo'; |
90
|
|
|
|
91
|
|
|
$this->expectOutputString($expectedOutputString); |
92
|
|
|
|
93
|
|
|
$isDevelopmentEnvironment = false; |
94
|
|
|
|
95
|
|
|
$exceptionStub = new \Exception($expectedOutputString); |
96
|
|
|
$htmlRequest = new Request([], [], [], [], [], []); |
97
|
|
|
$htmlRequest->getHeaders()->add('CONTENT_TYPE', 'text/html'); |
98
|
|
|
|
99
|
|
|
/** @var MockObject|RunInterface $runMock */ |
100
|
|
|
$runMock = $this->createMock(RunInterface::class); |
101
|
|
|
|
102
|
|
|
$runMock->expects($this->never())->method('handleException'); |
103
|
|
|
|
104
|
|
|
$sut = new ExceptionRenderer($runMock, $isDevelopmentEnvironment); |
105
|
|
|
$sut->setRequest($htmlRequest); |
106
|
|
|
|
107
|
|
|
$sut->render($exceptionStub); |
108
|
|
|
|
109
|
|
|
$headers = $sut->getHeaders(); |
110
|
|
|
$this->assertCount(2, $headers); |
111
|
|
|
$this->assertSame('Content-Type:text/html', $headers[1][0]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|