Passed
Pull Request — master (#217)
by
unknown
01:42
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
    ];
28
29
    public function testNonVerboseOutput(): void
30
    {
31
        $renderer = new HtmlRenderer(self::DEFAULT_TEMPLATES);
32
        $request = $this->getServerRequestMock();
33
        $renderer->setRequest($request);
34
        $exceptionMessage = 'exception-test-message';
35
        $exception = new \RuntimeException($exceptionMessage);
36
        $renderedOutput = $renderer->render($exception);
37
        $this->assertStringContainsString('<html', $renderedOutput);
38
        $this->assertStringNotContainsString(RuntimeException::class, $renderedOutput);
39
        $this->assertStringNotContainsString($exceptionMessage, $renderedOutput);
40
    }
41
42
    public function testVerboseOutput(): void
43
    {
44
        $renderer = new HtmlRenderer(self::DEFAULT_TEMPLATES);
45
        $request = $this->getServerRequestMock();
46
        $renderer->setRequest($request);
47
        $exceptionMessage = 'exception-test-message';
48
        $exception = new \RuntimeException($exceptionMessage);
49
        $renderedOutput = $renderer->renderVerbose($exception);
50
        $this->assertStringContainsString('<html', $renderedOutput);
51
        $this->assertStringContainsString(RuntimeException::class, $renderedOutput);
52
        $this->assertStringContainsString($exceptionMessage, $renderedOutput);
53
    }
54
55
    public function testNonVerboseOutputWithCustomTemplate(): void
56
    {
57
        $templateFileContents = '<html><?php echo $throwable->getMessage();?></html>';
58
        $this->createTestTemplate(self::CUSTOM_TEMPLATES['error'], $templateFileContents);
59
60
        $templates = $this->getTemplateConfigParamsForCustomTemplates();
61
        $renderer = new HtmlRenderer($templates);
62
        $request = $this->getServerRequestMock();
63
        $renderer->setRequest($request);
64
65
        $exceptionMessage = 'exception-test-message';
66
        $exception = new \RuntimeException($exceptionMessage);
67
68
        $renderedOutput = $renderer->render($exception);
69
        $this->assertStringContainsString("<html>$exceptionMessage</html>", $renderedOutput);
70
    }
71
72
    public function testVerboseOutputWithCustomTemplate(): void
73
    {
74
        $templateFileContents = '<html><?php echo $throwable->getMessage();?></html>';
75
        $this->createTestTemplate(self::CUSTOM_TEMPLATES['exception'], $templateFileContents);
76
77
        $templates = $this->getTemplateConfigParamsForCustomTemplates();
78
        $renderer = new HtmlRenderer($templates);
79
        $request = $this->getServerRequestMock();
80
        $renderer->setRequest($request);
81
82
        $exceptionMessage = 'exception-test-message';
83
        $exception = new \RuntimeException($exceptionMessage);
84
85
        $renderedOutput = $renderer->renderVerbose($exception);
86
        $this->assertStringContainsString("<html>$exceptionMessage</html>", $renderedOutput);
87
    }
88
89
    public function testRenderTemplateThrowsExceptionWhenTemplateFileNotExists(): void
90
    {
91
        $exampleNonExistingFile = '_not_found_.php';
92
93
        $templates = [
94
            'error' => $exampleNonExistingFile
95
        ];
96
        $templates = array_merge(self::DEFAULT_TEMPLATES, $templates);
97
98
        $renderer = new HtmlRenderer($templates);
99
        $request = $this->getServerRequestMock();
100
        $renderer->setRequest($request);
101
        $exception = new \Exception();
102
        $this->expectException(RuntimeException::class);
103
        $renderer->render($exception);
104
    }
105
106
    public function tearDown(): void
107
    {
108
        parent::tearDown();
109
        foreach (self::CUSTOM_TEMPLATES as $template) {
110
            if (file_exists($template)) {
111
                $this->removeTestTemplate($template);
112
            }
113
        }
114
    }
115
116
    private function getServerRequestMock(): ServerRequestInterface
117
    {
118
        $acceptHeader = [
119
            'text/html'
120
        ];
121
        $serverRequestMock = $this->createMock(ServerRequestInterface::class);
122
        $serverRequestMock
123
            ->method('getHeader')
124
            ->with('Accept')
125
            ->willReturn($acceptHeader);
126
127
        $serverRequestMock
128
            ->method('getHeaders')
129
            ->willReturn(
130
                [
131
                    'Accept' => $acceptHeader
132
                ]
133
            );
134
135
        $serverRequestMock
136
            ->method('getMethod')
137
            ->willReturn('GET');
138
139
        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...
140
    }
141
142
    private function getTemplateConfigParamsForCustomTemplates(): array
143
    {
144
        return array_merge(self::CUSTOM_TEMPLATES, self::DEFAULT_TEMPLATES);
145
    }
146
147
    private function createTestTemplate(string $path, string $templateContents): void
148
    {
149
        if (!file_put_contents($path, $templateContents)) {
150
            throw new WriteErrorException(sprintf('Unable to create file at path %s', $path));
151
        }
152
    }
153
154
    private function removeTestTemplate(string $path): void
155
    {
156
        unlink($path);
157
    }
158
}
159