Passed
Pull Request — master (#217)
by
unknown
01:45
created

getTemplateConfigParamsForCustomTemplates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\ErrorHandler;
4
5
use Nyholm\Psr7\ServerRequest;
6
use PHPUnit\Framework\MockObject\MockObject;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\ServerRequestInterface;
9
use RuntimeException;
10
use Spiral\Files\Exception\WriteErrorException;
0 ignored issues
show
Bug introduced by
The type Spiral\Files\Exception\WriteErrorException was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Yiisoft\Yii\Web\ErrorHandler\HtmlRenderer;
12
13
class HtmlRendererTest extends TestCase
14
{
15
    private const CUSTOM_TEMPLATES = [
16
        'exception' => __DIR__ . '/test-template-verbose.php',
17
        'error' => __DIR__ . '/test-template-non-verbose.php',
18
    ];
19
20
    private const DEFAULT_TEMPLATES = [
21
        'default' => [
22
            'callStackItem',
23
            'error',
24
            'exception',
25
            'previousException'
26
        ],
27
        'path' => __DIR__ . '/../../src/ErrorHandler/templates',
28
    ];
29
30
    public function testNonVerboseOutput(): void
31
    {
32
        $renderer = new HtmlRenderer(self::DEFAULT_TEMPLATES);
33
        $request = $this->getServerRequestMock();
34
        $renderer->setRequest($request);
35
        $exceptionMessage = 'exception-test-message';
36
        $exception = new \RuntimeException($exceptionMessage);
37
        $renderedOutput = $renderer->render($exception);
38
        $this->assertStringContainsString('<html', $renderedOutput);
39
        $this->assertStringNotContainsString(RuntimeException::class, $renderedOutput);
40
        $this->assertStringNotContainsString($exceptionMessage, $renderedOutput);
41
    }
42
43
    public function testVerboseOutput(): void
44
    {
45
        $renderer = new HtmlRenderer(self::DEFAULT_TEMPLATES);
46
        $request = $this->getServerRequestMock();
47
        $renderer->setRequest($request);
48
        $exceptionMessage = 'exception-test-message';
49
        $exception = new \RuntimeException($exceptionMessage);
50
        $renderedOutput = $renderer->renderVerbose($exception);
51
        $this->assertStringContainsString('<html', $renderedOutput);
52
        $this->assertStringContainsString(RuntimeException::class, $renderedOutput);
53
        $this->assertStringContainsString($exceptionMessage, $renderedOutput);
54
    }
55
56
    public function testNonVerboseOutputWithCustomTemplate(): void
57
    {
58
        $templateFileContents = '<html><?php echo $throwable->getMessage();?></html>';
59
        $this->createTestTemplate(self::CUSTOM_TEMPLATES['error'], $templateFileContents);
60
61
        $templates = $this->getTemplateConfigParamsForCustomTemplates();
62
        $renderer = new HtmlRenderer($templates);
63
        $request = $this->getServerRequestMock();
64
        $renderer->setRequest($request);
65
66
        $exceptionMessage = 'exception-test-message';
67
        $exception = new \RuntimeException($exceptionMessage);
68
69
        $renderedOutput = $renderer->render($exception);
70
        $this->assertStringContainsString("<html>$exceptionMessage</html>", $renderedOutput);
71
    }
72
73
    public function testVerboseOutputWithCustomTemplate(): void
74
    {
75
        $templateFileContents = '<html><?php echo $throwable->getMessage();?></html>';
76
        $this->createTestTemplate(self::CUSTOM_TEMPLATES['exception'], $templateFileContents);
77
78
        $templates = $this->getTemplateConfigParamsForCustomTemplates();
79
        $renderer = new HtmlRenderer($templates);
80
        $request = $this->getServerRequestMock();
81
        $renderer->setRequest($request);
82
83
        $exceptionMessage = 'exception-test-message';
84
        $exception = new \RuntimeException($exceptionMessage);
85
86
        $renderedOutput = $renderer->renderVerbose($exception);
87
        $this->assertStringContainsString("<html>$exceptionMessage</html>", $renderedOutput);
88
    }
89
90
    public function testRenderTemplateThrowsExceptionWhenTemplateFileNotExists(): void
91
    {
92
        $exampleNonExistingFile = '_not_found_.php';
93
94
        $templates = [
95
            'error' => $exampleNonExistingFile
96
        ];
97
        $templates = array_merge(self::DEFAULT_TEMPLATES, $templates);
98
99
        $renderer = new HtmlRenderer($templates);
100
        $request = $this->getServerRequestMock();
101
        $renderer->setRequest($request);
102
        $exception = new \Exception();
103
        $this->expectException(RuntimeException::class);
104
        $renderer->render($exception);
105
    }
106
107
    public function tearDown(): void
108
    {
109
        parent::tearDown();
110
        foreach (self::CUSTOM_TEMPLATES as $template) {
111
            if (file_exists($template)) {
112
                $this->removeTestTemplate($template);
113
            }
114
        }
115
    }
116
117
    private function getServerRequestMock(): ServerRequestInterface
118
    {
119
        $acceptHeader = [
120
            'text/html'
121
        ];
122
        $serverRequestMock = $this->createMock(ServerRequestInterface::class);
123
        $serverRequestMock
124
            ->method('getHeader')
125
            ->with('Accept')
126
            ->willReturn($acceptHeader);
127
128
        $serverRequestMock
129
            ->method('getHeaders')
130
            ->willReturn(
131
                [
132
                    'Accept' => $acceptHeader
133
                ]
134
            );
135
136
        $serverRequestMock
137
            ->method('getMethod')
138
            ->willReturn('GET');
139
140
        return $serverRequestMock;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $serverRequestMock returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Psr\Http\Message\ServerRequestInterface.
Loading history...
141
    }
142
143
    private function getTemplateConfigParamsForCustomTemplates(): array
144
    {
145
        return array_merge(self::CUSTOM_TEMPLATES, self::DEFAULT_TEMPLATES);
146
    }
147
148
    private function createTestTemplate(string $path, string $templateContents): void
149
    {
150
        if (!file_put_contents($path, $templateContents)) {
151
            throw new WriteErrorException(sprintf('Unable to create file at path %s', $path));
152
        }
153
    }
154
155
    private function removeTestTemplate(string $path): void
156
    {
157
        unlink($path);
158
    }
159
}
160