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