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 Yiisoft\Yii\Web\ErrorHandler\HtmlRenderer; |
9
|
|
|
|
10
|
|
|
class HtmlRendererTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
private $renderer; |
13
|
|
|
|
14
|
|
|
private $request; |
15
|
|
|
|
16
|
|
|
public function setUp(): void |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
$this->renderer = new HtmlRenderer(); |
20
|
|
|
$this->request = new ServerRequest('GET', '/', ['Accept' => ['text/html']]); |
21
|
|
|
$this->renderer->setRequest($this->request); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testNonVerboseOutput(): void |
25
|
|
|
{ |
26
|
|
|
$exceptionMessage = 'exception-test-message'; |
27
|
|
|
$exception = new \RuntimeException($exceptionMessage); |
28
|
|
|
$renderedOutput = $this->renderer->render($exception); |
29
|
|
|
$this->assertStringContainsString('<html', $renderedOutput); |
30
|
|
|
$this->assertStringNotContainsString(RuntimeException::class, $renderedOutput); |
31
|
|
|
$this->assertStringNotContainsString($exceptionMessage, $renderedOutput); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testVerboseOutput(): void |
35
|
|
|
{ |
36
|
|
|
$exceptionMessage = 'exception-test-message'; |
37
|
|
|
$exception = new \RuntimeException($exceptionMessage); |
38
|
|
|
$renderedOutput = $this->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
|
|
|
$exceptionMessage = 'exception-test-message'; |
47
|
|
|
$exception = new \RuntimeException($exceptionMessage); |
48
|
|
|
$templatePath = __DIR__ . '/'; |
49
|
|
|
$template = 'testTemplate'; |
50
|
|
|
$templateContent = '<?php echo $throwable ?>'; |
51
|
|
|
$this->createTestTemplate($templatePath, $template, $templateContent); |
52
|
|
|
|
53
|
|
|
$renderedOutput = $this->renderer->render($exception, $template, $templatePath); |
54
|
|
|
$this->removeTestTemplate($templatePath, $template); |
55
|
|
|
$this->assertStringContainsString($exceptionMessage, $renderedOutput); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testVerboseOutputWithCustomTemplate(): void |
59
|
|
|
{ |
60
|
|
|
$exceptionMessage = 'exception-test-message'; |
61
|
|
|
$exception = new \RuntimeException($exceptionMessage); |
62
|
|
|
$templatePath = __DIR__ . '/'; |
63
|
|
|
$template = 'testTemplate'; |
64
|
|
|
$templateContent = '<?php echo $throwable ?>'; |
65
|
|
|
$this->createTestTemplate($templatePath, $template, $templateContent); |
66
|
|
|
|
67
|
|
|
$renderedOutput = $this->renderer->renderVerbose($exception, $template, $templatePath); |
68
|
|
|
$this->removeTestTemplate($templatePath, $template); |
69
|
|
|
$this->assertStringContainsString($exceptionMessage, $renderedOutput); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function createTestTemplate(string $templatePath, string $template, $templateContent): void |
73
|
|
|
{ |
74
|
|
|
$fullPath = $templatePath . $template . '.php'; |
75
|
|
|
$file = fopen($fullPath, 'w'); |
76
|
|
|
fwrite($file, $templateContent); |
|
|
|
|
77
|
|
|
fclose($file); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function removeTestTemplate(string $templatePath, string $template): void |
81
|
|
|
{ |
82
|
|
|
unlink($templatePath . $template . '.php'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|