| Conditions | 3 |
| Paths | 1 |
| Total Lines | 99 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 192 |