Passed
Pull Request — master (#217)
by
unknown
02:58 queued 17s
created

HtmlRendererTest::createTestTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 6
rs 10
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);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        fwrite(/** @scrutinizer ignore-type */ $file, $templateContent);
Loading history...
77
        fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
78
    }
79
80
    private function removeTestTemplate(string $templatePath, string $template): void
81
    {
82
        unlink($templatePath . $template . '.php');
83
    }
84
}
85