1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\ErrorHandler; |
4
|
|
|
|
5
|
|
|
use Nyholm\Psr7\ServerRequest; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use Spiral\Files\Exception\WriteErrorException; |
|
|
|
|
9
|
|
|
use Yiisoft\Yii\Web\ErrorHandler\HtmlRenderer; |
10
|
|
|
|
11
|
|
|
class HtmlRendererTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
private const CUSTOM_TEMPLATES = [ |
14
|
|
|
'exception' => __DIR__ . '/test-template-verbose.php', |
15
|
|
|
'error' => __DIR__ . '/test-template-non-verbose.php', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
public function testNonVerboseOutput(): void |
19
|
|
|
{ |
20
|
|
|
$renderer = new HtmlRenderer(); |
21
|
|
|
$request = new ServerRequest('GET', '/', ['Accept' => ['text/html']]); |
22
|
|
|
$renderer->setRequest($request); |
23
|
|
|
$exceptionMessage = 'exception-test-message'; |
24
|
|
|
$exception = new \RuntimeException($exceptionMessage); |
25
|
|
|
$renderedOutput = $renderer->render($exception); |
26
|
|
|
$this->assertStringContainsString('<html', $renderedOutput); |
27
|
|
|
$this->assertStringNotContainsString(RuntimeException::class, $renderedOutput); |
28
|
|
|
$this->assertStringNotContainsString($exceptionMessage, $renderedOutput); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testVerboseOutput(): void |
32
|
|
|
{ |
33
|
|
|
$renderer = new HtmlRenderer(); |
34
|
|
|
$request = new ServerRequest('GET', '/', ['Accept' => ['text/html']]); |
35
|
|
|
$renderer->setRequest($request); |
36
|
|
|
$exceptionMessage = 'exception-test-message'; |
37
|
|
|
$exception = new \RuntimeException($exceptionMessage); |
38
|
|
|
$renderedOutput = $renderer->renderVerbose($exception); |
39
|
|
|
$this->assertStringContainsString('<html', $renderedOutput); |
40
|
|
|
$this->assertStringContainsString(RuntimeException::class, $renderedOutput); |
41
|
|
|
$this->assertStringContainsString($exceptionMessage, $renderedOutput); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testNonVerboseOutputWithCustomTemplate(): void |
45
|
|
|
{ |
46
|
|
|
$templateFileContents = '<html><?php echo $throwable->getMessage();?></html>'; |
47
|
|
|
$this->createTestTemplate(self::CUSTOM_TEMPLATES['error'], $templateFileContents); |
48
|
|
|
|
49
|
|
|
$renderer = new HtmlRenderer(self::CUSTOM_TEMPLATES); |
50
|
|
|
$request = new ServerRequest('GET', '/', ['Accept' => ['text/html']]); |
51
|
|
|
$renderer->setRequest($request); |
52
|
|
|
|
53
|
|
|
$exceptionMessage = 'exception-test-message'; |
54
|
|
|
$exception = new \RuntimeException($exceptionMessage); |
55
|
|
|
|
56
|
|
|
$renderedOutput = $renderer->render($exception); |
57
|
|
|
$this->assertStringContainsString($exceptionMessage, $renderedOutput); |
58
|
|
|
$this->assertStringContainsString('<html>', $renderedOutput); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testVerboseOutputWithCustomTemplate(): void |
62
|
|
|
{ |
63
|
|
|
$templateFileContents = '<html><?php echo $throwable->getMessage();?></html>'; |
64
|
|
|
$this->createTestTemplate(self::CUSTOM_TEMPLATES['exception'], $templateFileContents); |
65
|
|
|
|
66
|
|
|
$renderer = new HtmlRenderer(self::CUSTOM_TEMPLATES); |
67
|
|
|
$request = new ServerRequest('GET', '/', ['Accept' => ['text/html']]); |
68
|
|
|
$renderer->setRequest($request); |
69
|
|
|
|
70
|
|
|
$exceptionMessage = 'exception-test-message'; |
71
|
|
|
$exception = new \RuntimeException($exceptionMessage); |
72
|
|
|
|
73
|
|
|
$renderedOutput = $renderer->renderVerbose($exception); |
74
|
|
|
$this->assertStringContainsString($exceptionMessage, $renderedOutput); |
75
|
|
|
$this->assertStringContainsString('<html>', $renderedOutput); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testRenderTemplateThrowsExceptionWhenTemplateFileNotExists(): void |
79
|
|
|
{ |
80
|
|
|
$exampleNonExistingFile = sprintf('%s.php', bin2hex(random_bytes(16))); |
81
|
|
|
$templates = [ |
82
|
|
|
'error' => $exampleNonExistingFile |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$renderer = new HtmlRenderer($templates); |
86
|
|
|
$exceptionMessage = 'exception-test-message'; |
87
|
|
|
$exception = new \Exception($exceptionMessage); |
88
|
|
|
|
89
|
|
|
$this->expectException(RuntimeException::class); |
90
|
|
|
$renderer->render($exception); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function tearDown(): void |
94
|
|
|
{ |
95
|
|
|
parent::tearDown(); |
96
|
|
|
foreach (self::CUSTOM_TEMPLATES as $template) { |
97
|
|
|
if (file_exists($template)) { |
98
|
|
|
$this->removeTestTemplate($template); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function createTestTemplate(string $path, string $templateContents): void |
104
|
|
|
{ |
105
|
|
|
if (!file_put_contents($path, $templateContents)) { |
106
|
|
|
throw new WriteErrorException(sprintf('Unable to create file at path %s', $path)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function removeTestTemplate(string $path): void |
111
|
|
|
{ |
112
|
|
|
unlink($path); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths