GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

testHttpResponse()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 2
dl 0
loc 24
ccs 0
cts 21
cp 0
crap 20
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JDolba\SlimHttpSmokeTesting;
6
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
abstract class SlimApplicationHttpSmokeTestCase extends TestCase
12
{
13
    /**
14
     * @var \JDolba\SlimHttpSmokeTesting\Configuration
15
     */
16
    private static $configuration;
17
18
    /**
19
     * init your application, routes, router, etc, you MUST call here configure()
20
     * @see \JDolba\SlimHttpSmokeTesting\SlimApplicationHttpSmokeTestCase::configure()
21
     */
22
    abstract public static function setUpSmokeTestAndCallConfigure(): void;
23
24
    /**
25
     * here you should customize your RequestDataSets
26
     *
27
     * @param \JDolba\SlimHttpSmokeTesting\DataSetCustomizer $requestCustomizer
28
     */
29
    abstract protected function customize(DataSetCustomizer $requestCustomizer): void;
30
31
    /**
32
     * @param \JDolba\SlimHttpSmokeTesting\Configuration $smokeConfiguration
33
     */
34
    final public static function configure(Configuration $smokeConfiguration)
35
    {
36
        self::$configuration = $smokeConfiguration;
37
    }
38
39
    private function throwNotConfiguredException()
40
    {
41
        throw new \LogicException(
42
            sprintf(
43
                'You must call %s::configure before any test is executed',
44
                __CLASS__
45
            )
46
        );
47
    }
48
49
    /**
50
     * @return \JDolba\SlimHttpSmokeTesting\RouteConfiguration\RouteConfigurationInterface[][]
51
     */
52
    final public function httpResponseDataProvider()
53
    {
54
        static::setUpSmokeTestAndCallConfigure();
55
56
        if (self::$configuration === null) {
57
            $this->throwNotConfiguredException();
58
        }
59
60
        /** @var \JDolba\SlimHttpSmokeTesting\RequestDataSet[] $dataSets */
61
        $dataSets = [];
62
        foreach (self::$configuration->getRouterAdapter()->getRouteConfigurations() as $routeConfiguration) {
63
            foreach ($routeConfiguration->getMethods() as $method) {
64
                $dataSets[] = new RequestDataSet($routeConfiguration, $method);
65
            }
66
        }
67
68
        $this->customize(
69
            new DataSetCustomizer($dataSets)
70
        );
71
72
        $allRequestDataSets = [];
73
        foreach ($dataSets as $dataSet) {
74
            $allRequestDataSets[] = $dataSet;
75
            foreach ($dataSet->getAdditionalRequestDataSet() as $additionalRequestDataSet) {
76
                $allRequestDataSets[] = $additionalRequestDataSet;
77
            }
78
        }
79
        unset($dataSets);
80
81
        return array_map(
82
            function (RequestDataSet $dataSet) {
83
                $requestPromise = $dataSet->getRequestPromise();
84
                if (!is_callable($requestPromise)) {
85
                    throw new \LogicException(
86
                        sprintf(
87
                            'RequestPromise in RequestDataSet must be callable and must return %s (on route %s)',
88
                            RequestInterface::class,
89
                            $dataSet->getRouteConfiguration()->getRoute()->getPattern()
90
                        )
91
                    );
92
                }
93
94
                $request = $requestPromise(self::$configuration->getRouterAdapter(), $dataSet);
95
                if (!$request instanceof RequestInterface) {
96
                    throw new \LogicException(sprintf(
97
                        '%s expected, %s given from DataSet request promise for route %s',
98
                        RequestInterface::class,
99
                        get_class($request),
100
                        $dataSet->getRouteConfiguration()->getRoute()->getPattern()
101
                    ));
102
                }
103
104
                $authenticationCallback = $dataSet->getAuthenticationCallback();
105
                if ($authenticationCallback !== null) {
106
                    $request = $authenticationCallback($request);
107
108
                    if (!$request instanceof RequestInterface) {
109
                        throw new \LogicException(
110
                            sprintf(
111
                                '%s expected, %s given from DataSet authentication callback for route %s',
112
                                RequestInterface::class,
113
                                get_class($request),
114
                                $dataSet->getRouteConfiguration()->getRoute()->getPattern()
115
                            )
116
                        );
117
                    }
118
                }
119
120
                return [
121
                    $request,
122
                    $dataSet
123
                ];
124
            },
125
            $allRequestDataSets
126
        );
127
    }
128
129
    /**
130
     * @dataProvider httpResponseDataProvider
131
     *
132
     * @param \Psr\Http\Message\RequestInterface $request
133
     * @param \JDolba\SlimHttpSmokeTesting\RequestDataSet $requestDataSet
134
     */
135
    final public function testHttpResponse(RequestInterface $request, RequestDataSet $requestDataSet)
136
    {
137
        if (self::$configuration === null) {
138
            $this->throwNotConfiguredException();
139
        }
140
141
        if ($requestDataSet->isSkipped()) {
142
            $this->markTestSkipped(
143
                sprintf(
144
                    'Route %s (%s) is skipped. %s',
145
                    $requestDataSet->getRouteConfiguration()->getRoute()->getPattern(),
146
                    $requestDataSet->getMethod(),
147
                    ($requestDataSet->getSkippedReason() ?: 'Reason not provided')
148
                )
149
            );
150
            return;
151
        }
152
153
        $response = $this->handleRequest($request);
154
155
        $this->assertResponse(
156
            $response,
157
            $request,
158
            $requestDataSet
159
        );
160
    }
161
162
    /**
163
     * @param \Psr\Http\Message\RequestInterface $request
164
     * @return \Psr\Http\Message\ResponseInterface
165
     */
166
    protected function handleRequest(RequestInterface $request)
167
    {
168
        $application = self::$configuration->getApp();
169
        $application->getContainer()['request'] = $request;
170
        return $application->run(true);
171
    }
172
173
    /**
174
     * @param \Psr\Http\Message\ResponseInterface $response
175
     * @param \Psr\Http\Message\RequestInterface $request
176
     * @param \JDolba\SlimHttpSmokeTesting\RequestDataSet $requestDataSet
177
     */
178
    protected function assertResponse(
179
        ResponseInterface $response,
180
        RequestInterface $request,
181
        RequestDataSet $requestDataSet
182
    ) {
183
        $failureMessage = sprintf(
184
            'Response code %d for route %s is not identical to expected %d',
185
            $response->getStatusCode(),
186
            $request->getRequestTarget(),
187
            $requestDataSet->getExpectedHttpCode()
188
        );
189
190
        $this->assertSame(
191
            $response->getStatusCode(),
192
            $requestDataSet->getExpectedHttpCode(),
193
            $failureMessage
194
        );
195
    }
196
}
197