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.

MyAwesomeApplicationSmokeTest::customize()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 99
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 44
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 99
rs 9.216

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
use JDolba\SlimHttpSmokeTesting\Configuration;
4
use JDolba\SlimHttpSmokeTesting\DataSetCustomizer;
5
use JDolba\SlimHttpSmokeTesting\RequestDataSet;
6
use JDolba\SlimHttpSmokeTesting\RouterAdapter\RouterAdapterInterface;
7
use JDolba\SlimHttpSmokeTesting\RouterAdapter\SlimRouterAdapter;
8
use JDolba\SlimHttpSmokeTesting\SlimApplicationHttpSmokeTestCase;
9
use Slim\App;
10
use Slim\Http\Environment;
11
use Slim\Http\Request;
12
use Slim\Http\Response;
13
14
class MyAwesomeApplicationSmokeTest extends SlimApplicationHttpSmokeTestCase
15
{
16
    public static function setUpSmokeTestAndCallConfigure(): void
17
    {
18
        // $app = new App(); // retrieve your App with configured routes, container etc.
19
        // TODO: be sure that App is configured for testing environment(database)
20
21
        $app = self::configureAppRoutesForExamplePurpose();
22
23
        self::configure(
24
            new Configuration(
25
                $app,
26
                new SlimRouterAdapter($app->getContainer()->get('router'))
27
            )
28
        );
29
    }
30
31
    protected function customize(DataSetCustomizer $customizer): void
32
    {
33
        // it is important to realize, that RequestDataSet = route + method
34
        // example: [ GET /api/user, POST /api/user ] are 2 instance of RequestDataSet
35
        // each DataSet may have additional DataSet, which will use same Route, but different Request
36
37
        $customizer
38
            // customize callback will be applied on ALL data sets (i.e. all routes with all methods)
39
            ->customize(
40
                function (RequestDataSet $dataSet) {
41
                    if ($dataSet->getMethod() !== 'GET') {
42
                        $dataSet->skip('We are lazy and we will smoke-test only routes which are using GET');
43
                    }
44
45
                    $dataSet->setAuthenticationCallback(function (Request $request) {
46
                        // here you can inject some authentication credentials into Request headers etc.
47
                        // authenticationCallback is invoked AFTER creating Request from requestPromise method
48
                        return $request;
49
                    });
50
                }
51
            )
52
            // example of modifying route with named parameters (URI params)
53
            // it will use Router implementation by your Configuration to generate uri
54
            ->customizeByRouteName(
55
                '/api/user/find-by-id/{userId}',
56
                function (RequestDataSet $dataSet) {
57
                    $dataSet->setUriParamsForRouter(
58
                        [
59
                            'userId' => 42, // will create /api/user/find-by-id/42
60
                        ]
61
                    );
62
                }
63
            )
64
            // also you can use query params and add more RequestDataSet to test same route with more data-examples
65
            ->customizeByRouteNameAndMethod(
66
                '/api/user',
67
                'GET',
68
                function (RequestDataSet $dataSet) {
69
                    $dataSet->setQueryParamsForRouter(
70
                        [
71
                            'userId' => 42, // will create /api/user?userId=42
72
                        ]
73
                    );
74
75
                    $newRequestDataSet = $dataSet->addNewExtraRequestDataSet();
76
                    // method addNewExtraRequestDataSet returns new instance of RequestDataSet so you can modify it here
77
                    // additional data sets are NOT matched by all customize* functions
78
                    $newRequestDataSet->setQueryParamsForRouter(
79
                        [
80
                            'userId' => 96, // will create another request on same route /api/user?userId=96
81
                        ]
82
                    );
83
                    $newRequestDataSet->setExpectedHttpCode(404);
84
                }
85
            )
86
            // more complicated example with custom request and custom condition to match RequestDataSet
87
            ->customizeByConditionCallback(
88
                function (RequestDataSet $dataSet) {
89
                    // here you can put any condition you like to find your desired data set
90
                    return $dataSet->getMethod() === 'POST' // route is defined to accept only post
91
                        && $dataSet->getRouteConfiguration()->getRouteName() === '/api/user';
92
                },
93
                function (RequestDataSet $dataSet) {
94
                    $dataSet->asNotSkipped(); // all non-GET routes were skipped by previous customization rule
95
96
                    // RequestDataSet is using callback to create Request and you can use your own RequestPromise
97
                    // there is only simple RequestPromise callback by default, so for complicated requests you must
98
                    // create your own Request using setRequestPromise method, for example as below:
99
                    $dataSet->setRequestPromise(
100
                        function (RouterAdapterInterface $routerAdapter, RequestDataSet $dataSet) {
101
                            $uri = $routerAdapter->generateRelativePath(
102
                                $dataSet->getRouteConfiguration(),
103
                                $dataSet->getUriParamsForRouter(),
104
                                $dataSet->getQueryParamsForRouter()
105
                            );
106
107
                            $env = Environment::mock(
108
                                [
109
                                    'REQUEST_METHOD' => $dataSet->getMethod(),
110
                                    'REQUEST_URI' => $uri,
111
                                ]
112
                            );
113
                            $request = Request::createFromEnvironment($env);
114
                            // this is standard way how to mock Requests for Slim
115
116
                            // lets say we are POSTing data to create new user
117
                            // don't forget that Request implementation is IMMUTABLE
118
                            return $request->withParsedBody(
119
                                [
120
                                    'name' => 'Johny Walker',
121
                                    'email' => '[email protected]',
122
                                ]
123
                            );
124
                        }
125
                    );
126
127
                    $newRequest = $dataSet->addNewExtraRequestDataSet();
128
                    $newRequest->setRequestPromise($dataSet->createDefaultRequestPromise());
129
                    $newRequest->setExpectedHttpCode(500);
130
                }
131
            );
132
    }
133
134
    /**
135
     * will configure base routes for example application in this test
136
     *
137
     * @return  \Slim\App $app
138
     */
139
    private static function configureAppRoutesForExamplePurpose(): App
140
    {
141
        $app = new App([
142
            'settings' => [
143
                'displayErrorDetails' => true,
144
            ]
145
        ]);
146
147
        $app->any(
148
            '/',
149
            function (Request $request, Response $response) {
150
                return $response;
151
            }
152
        );
153
        $app->get(
154
            '/api/user/find-by-id/{userId}',
155
            function (Request $request, Response $response, $args) {
156
                if ($args['userId'] == 42) {
157
                    return $response->withJson(['ok']);
158
                }
159
160
                return $response->withJson(['not found'])->withStatus(404);
161
            }
162
        );
163
        $app->get(
164
            '/api/user',
165
            function (Request $request, Response $response) {
166
167
                if ($request->getQueryParam('userId') == 42) {
168
                    return $response->withJson(['ok']);
169
                } else {
170
                    return $response->withJson(['not found'])->withStatus(404);
171
                }
172
173
            }
174
        );
175
        $app->post(
176
            '/api/user',
177
            function (Request $request, Response $response) {
178
                if (
179
                    $request->getParam('name') === 'Johny Walker'
180
                    && $request->getParam('email') === '[email protected]'
181
                ) {
182
                    return $response->withJson('ok');
183
                }
184
185
                throw new \RuntimeException('MyAwesomeApplication has runtime exception');
186
            }
187
        );
188
189
        return $app;
190
    }
191
}
192