Passed
Push — master ( 02721e...1546a1 )
by SignpostMarv
02:51
created

HttpHandlerTest::DataProviderHttpHandlerHandle()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 60
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 43
nc 6
nop 0
dl 0
loc 60
rs 8.6961
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftFramework\Tests;
8
9
use Generator;
10
use SignpostMarv\DaftFramework\HttpHandler;
11
use SignpostMarv\DaftRouter\DaftSource;
12
use SignpostMarv\DaftRouter\Tests\ImplementationTest as Base;
13
use Symfony\Component\HttpFoundation\Request;
14
15
class HttpHandlerTest extends Base
16
{
17
    public function DataProviderHttpHandlerInstances() : Generator
18
    {
19
        yield from [
20
            [
21
                HttpHandler::class,
22
                [
23
                    'ConfigureDatabaseConnection' => [
24
                        'sqlite::memory:',
25
                        null,
26
                        null,
27
                        [],
28
                    ],
29
                ],
30
                'https://example.com/',
31
                realpath(__DIR__ . '/fixtures'),
32
                [
33
                    DaftSource::class => [
34
                        'cacheFile' => (__DIR__ . '/fixtures/http-kernel.fast-route.cache'),
35
                    ],
36
                ],
37
            ],
38
        ];
39
    }
40
41
    public function DataProviderHttpHandlerHandle() : Generator
42
    {
43
        foreach ($this->DataProviderHttpHandlerInstances() as $args) {
44
            list($implementation, $postConstructionCalls, $baseUrl, $basePath, $config) = $args;
45
46
            foreach ($this->DataProviderVerifyHandlerGood() as $testArgs) {
47
                list(
48
                    $sources,
49
                    $prefix,
50
                    $expectedStatus,
51
                    $expectedContent,
52
                    $uri,
53
                    $method,
54
                    $parameters,
55
                    $cookies,
56
                    $files,
57
                    $server,
58
                    $content
59
                ) = $testArgs;
60
61
                $parsed = parse_url($uri);
62
63
                $baseUrl = $parsed['scheme'] . '://' . $parsed['host'];
64
65
                if (isset($parsed['port'])) {
66
                    $baseUrl .= ':' . $parsed['port'];
67
                }
68
69
                $baseUrl .= '/' . $prefix;
70
71
                $config[DaftSource::class]['sources'] = $sources;
72
                $config[DaftSource::class]['cacheFile'] = (
73
                    __DIR__ .
74
                    '/fixtures/http-kernel.fast-route.cache'
75
                );
76
77
                if (is_file($config[DaftSource::class]['cacheFile'])) {
78
                    unlink($config[DaftSource::class]['cacheFile']);
79
                }
80
81
                $instance = Utilities::ObtainHttpHandlerInstance(
82
                    $this,
83
                    $implementation,
84
                    $baseUrl,
85
                    $basePath,
86
                    $config
87
                );
88
                Utilities::ConfigureFrameworkInstance($this, $instance, $args[1]);
89
90
                $request = Request::create(
91
                    $uri,
92
                    $method,
93
                    $parameters,
94
                    $cookies,
95
                    $files,
96
                    $server,
97
                    $content
98
                );
99
100
                yield [$instance, $request, $expectedStatus, $expectedContent];
101
            }
102
        }
103
    }
104
105
    /**
106
    * @dataProvider DataProviderHttpHandlerHandle
107
    */
108
    public function testHandlerGoodOnHttpHandler(
109
        HttpHandler $instance,
110
        Request $request,
111
        int $expectedStatus,
112
        string $expectedContent
113
    ) : void {
114
        $response = $instance->handle($request);
115
116
        $this->assertSame($expectedStatus, $response->getStatusCode());
117
        $this->assertSame($expectedContent, $response->getContent());
118
    }
119
}
120