Passed
Pull Request — master (#257)
by Wilmer
10:49
created

ErrorCatcherTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 57
dl 0
loc 98
rs 10
c 4
b 1
f 0
wmc 9
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\Middleware;
4
5
use Nyholm\Psr7\Factory\Psr17Factory;
6
use Nyholm\Psr7\ServerRequest;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Yiisoft\Di\Container;
10
use Yiisoft\Log\Logger;
11
use Yiisoft\Yii\Web\ErrorHandler\ErrorHandler;
12
use Yiisoft\Yii\Web\ErrorHandler\ErrorCatcher;
13
use Yiisoft\Yii\Web\Tests\Middleware\Mock\MockRequestHandler;
14
use Yiisoft\Yii\Web\Tests\Middleware\Mock\MockThrowableRenderer;
15
16
class ErrorCatcherTest extends TestCase
17
{
18
    private const DEFAULT_RENDERER_RESPONSE = 'default-renderer-test';
19
20
    public function testAddedRenderer(): void
21
    {
22
        $expectedRendererOutput = 'expectedRendererOutput';
23
        $containerId = 'testRenderer';
24
        $container = $this->getContainerWithThrowableRenderer($containerId, $expectedRendererOutput);
25
        $mimeType = 'test/test';
26
        $catcher = $this->getErrorCatcher($container)->withAddedRenderer($mimeType, $containerId);
27
        $requestHandler = (new MockRequestHandler())->setHandleExcaption(new \RuntimeException());
28
        $response = $catcher->process(new ServerRequest('GET', '/', ['Accept' => [$mimeType]]), $requestHandler);
29
        $response->getBody()->rewind();
30
        $content = $response->getBody()->getContents();
31
        $this->assertNotSame(self::DEFAULT_RENDERER_RESPONSE, $content);
32
        $this->assertSame($expectedRendererOutput, $content);
33
    }
34
35
    public function testWithoutRenderers(): void
36
    {
37
        $container = new Container();
38
        $catcher = $this->getErrorCatcher($container)->withoutRenderers();
39
        $requestHandler = (new MockRequestHandler())->setHandleExcaption(new \RuntimeException());
40
        $response = $catcher->process(new ServerRequest('GET', '/', ['Accept' => ['test/html']]), $requestHandler);
41
        $response->getBody()->rewind();
42
        $content = $response->getBody()->getContents();
43
        $this->assertSame(self::DEFAULT_RENDERER_RESPONSE, $content);
44
    }
45
46
    public function testWithoutRenderer(): void
47
    {
48
        $container = new Container();
49
        $catcher = $this->getErrorCatcher($container)->withoutRenderers('*/*');
50
        $requestHandler = (new MockRequestHandler())->setHandleExcaption(new \RuntimeException());
51
        $response = $catcher->process(new ServerRequest('GET', '/', ['Accept' => ['test/html']]), $requestHandler);
52
        $response->getBody()->rewind();
53
        $content = $response->getBody()->getContents();
54
        $this->assertSame(self::DEFAULT_RENDERER_RESPONSE, $content);
55
    }
56
57
    public function testAdvancedAcceptHeader(): void
58
    {
59
        $containerId = 'testRenderer';
60
        $expectedRendererOutput = 'expectedRendererOutput';
61
        $container = $this->getContainerWithThrowableRenderer($containerId, $expectedRendererOutput);
62
        $mimeType = 'text/html;version=2';
63
        $catcher = $this->getErrorCatcher($container)->withAddedRenderer($mimeType, $containerId);
64
        $requestHandler = (new MockRequestHandler())->setHandleExcaption(new \RuntimeException());
65
        $response = $catcher->process(
66
            new ServerRequest('GET', '/', ['Accept' => ['text/html', $mimeType]]),
67
            $requestHandler
68
        );
69
        $response->getBody()->rewind();
70
        $content = $response->getBody()->getContents();
71
        $this->assertNotSame(self::DEFAULT_RENDERER_RESPONSE, $content);
72
    }
73
74
    public function testDefaultContentType(): void
75
    {
76
        $expectedRendererOutput = 'expectedRendererOutput';
77
        $containerId = 'testRenderer';
78
        $container = $this->getContainerWithThrowableRenderer($containerId, $expectedRendererOutput);
79
        $catcher = $this->getErrorCatcher($container)
80
            ->withAddedRenderer('*/*', $containerId);
81
        $requestHandler = (new MockRequestHandler())->setHandleExcaption(new \RuntimeException());
82
        $response = $catcher->process(
83
            new ServerRequest('GET', '/', ['Accept' => ['test/test']]),
84
            $requestHandler
85
        );
86
        $response->getBody()->rewind();
87
        $content = $response->getBody()->getContents();
88
        $this->assertNotSame(self::DEFAULT_RENDERER_RESPONSE, $content);
89
        $this->assertSame($expectedRendererOutput, $content);
90
    }
91
92
    private function getContainerWithThrowableRenderer(string $id, string $expectedOutput): Container
93
    {
94
        return new Container(
95
            [
96
                $id => new MockThrowableRenderer($expectedOutput)
97
            ]
98
        );
99
    }
100
101
    private function getErrorHandler(): ErrorHandler
102
    {
103
        return new ErrorHandler(new Logger(), new MockThrowableRenderer(self::DEFAULT_RENDERER_RESPONSE));
104
    }
105
106
    private function getFactory(): ResponseFactoryInterface
107
    {
108
        return new Psr17Factory();
109
    }
110
111
    private function getErrorCatcher(Container $container): ErrorCatcher
112
    {
113
        return new ErrorCatcher($this->getFactory(), $this->getErrorHandler(), $container);
114
    }
115
}
116