testHandleShouldRenderTheFileWithA500()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VM\ErrorHandler\Tests\Services;
6
7
use Fig\Http\Message\StatusCodeInterface;
8
use PHPUnit\Framework\TestCase;
9
use RuntimeException;
10
use TypeError;
11
use VM\ErrorHandler\Exceptions\Http\Forbidden;
12
use VM\ErrorHandler\Exceptions\Http\NotFound;
13
use VM\ErrorHandler\Services\StaticFileHandler;
14
use VM\Psr15Mocks\Middleware;
15
16
class StaticFileHandlerTest extends TestCase
17
{
18
    use Middleware;
19
20
    public function setUp(): void
21
    {
22
        $this->buildResponse();
23
        $this->buildBody();
24
    }
25
26
    public function tearDown(): void
27
    {
28
        $this->destroyBody();
29
        $this->destroyResponse();
30
    }
31
32
    public function testHandleShouldRenderTheFileWithA500(): void
33
    {
34
        $this->response->expects($this->once())->method('getBody')->willReturn($this->body);
35
        $this->response->expects($this->once())->method('withHeader')->with('Cache-Control', StaticFileHandler::NO_CACHE_HEADER)->willReturn($this->response);
36
        $this->response->expects($this->once())->method('withStatus')->with(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR)->willReturn($this->response);
37
38
        $this->body->expects($this->once())->method('write')->with("some static content\n");
39
40
        $filePath = __DIR__ . '/../Fixtures/static_file.txt';
41
        $handler = new StaticFileHandler($filePath, $this->response);
42
43
        $exception = new TypeError();
44
45
        $handler->handle($exception);
46
    }
47
48
    public function testHandleHttpExceptionShouldRenderTheFileWithADedicatedCode(): void
49
    {
50
        $this->response->expects($this->once())->method('getBody')->willReturn($this->body);
51
        $this->response->expects($this->once())->method('withHeader')->with('Cache-Control', StaticFileHandler::NO_CACHE_HEADER)->willReturn($this->response);
52
        $this->response->expects($this->once())->method('withStatus')->with(StatusCodeInterface::STATUS_NOT_FOUND)->willReturn($this->response);
53
54
        $this->body->expects($this->once())->method('write')->with("some static content\n");
55
56
        $filePath = __DIR__ . '/../Fixtures/static_file.txt';
57
        $handler = new StaticFileHandler($filePath, $this->response);
58
59
        $exception = new NotFound();
60
61
        $handler->handleHttpException($exception);
62
    }
63
64
    public function testHandleShouldThrowANewExceptionWhenThereIsNoFileConfigured(): void
65
    {
66
        $this->expectException(RuntimeException::class);
67
68
        $this->response->expects($this->never())->method('getBody');
69
        $this->response->expects($this->never())->method('withHeader');
70
        $this->response->expects($this->never())->method('withStatus');
71
72
        $handler = new StaticFileHandler('some-non-existing-file.txt', $this->response);
73
74
        $exception = new TypeError();
75
76
        $handler->handle($exception);
77
    }
78
79
    public function testHandleShouldUseOverrideFilesWhenPresent(): void
80
    {
81
        $this->response->expects($this->once())->method('getBody')->willReturn($this->body);
82
        $this->response->expects($this->once())->method('withHeader')->with('Cache-Control', StaticFileHandler::NO_CACHE_HEADER)->willReturn($this->response);
83
        $this->response->expects($this->once())->method('withStatus')->with(StatusCodeInterface::STATUS_FORBIDDEN)->willReturn($this->response);
84
85
        $this->body->expects($this->once())->method('write')->with("forbidden contents!\n");
86
87
        $filePath = __DIR__ . '/../Fixtures/static_file.txt';
88
        $forbiddenFilePath = __DIR__ . '/../Fixtures/forbidden_file.txt';
89
        $handler = new StaticFileHandler($filePath, $this->response, [StatusCodeInterface::STATUS_FORBIDDEN => $forbiddenFilePath]);
90
91
        $exception = new Forbidden();
92
93
        $handler->handleHttpException($exception);
94
    }
95
}
96