| Conditions | 1 |
| Paths | 1 |
| Total Lines | 76 |
| Code Lines | 42 |
| 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 |
||
| 155 | public function testPatching() |
||
| 156 | { |
||
| 157 | // create a record, specify no version |
||
| 158 | $record = (object) [ |
||
| 159 | 'id' => 'patch-id-test', |
||
| 160 | 'data' => 'mydata' |
||
| 161 | ]; |
||
| 162 | |||
| 163 | $client = static::createRestClient(); |
||
| 164 | $client->put('/testcase/versioning-entity/'.($record->id), $record); |
||
| 165 | $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
||
| 166 | |||
| 167 | // PATCH to fail, no version field |
||
| 168 | $client = static::createRestClient(); |
||
| 169 | $patch = json_encode( |
||
| 170 | [ |
||
| 171 | [ |
||
| 172 | 'op' => 'replace', |
||
| 173 | 'path' => '/data', |
||
| 174 | 'value' => 'fail here' |
||
| 175 | ] |
||
| 176 | ] |
||
| 177 | ); |
||
| 178 | $client->request('PATCH', '/testcase/versioning-entity/' . ($record->id), array(), array(), array(), $patch); |
||
| 179 | $response = $client->getResponse(); |
||
| 180 | $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
||
| 181 | |||
| 182 | // PATCH to fail, wrong version number |
||
| 183 | $client = static::createRestClient(); |
||
| 184 | $patch = json_encode( |
||
| 185 | [ |
||
| 186 | [ |
||
| 187 | 'op' => 'replace', |
||
| 188 | 'path' => '/data', |
||
| 189 | 'value' => 'should be KO' |
||
| 190 | ], |
||
| 191 | [ |
||
| 192 | 'op' => 'replace', |
||
| 193 | 'path' => '/version', |
||
| 194 | 'value' => 7 |
||
| 195 | ] |
||
| 196 | ] |
||
| 197 | ); |
||
| 198 | $client->request('PATCH', '/testcase/versioning-entity/' . ($record->id), array(), array(), array(), $patch); |
||
| 199 | $response = $client->getResponse(); |
||
| 200 | |||
| 201 | $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
||
| 202 | $this->assertEquals(1, $response->headers->get(VersionServiceConstraint::HEADER_NAME)); |
||
| 203 | |||
| 204 | // PATCH, checking header from failed and use it to patch version. |
||
| 205 | $patch = json_encode( |
||
| 206 | [ |
||
| 207 | [ |
||
| 208 | 'op' => 'replace', |
||
| 209 | 'path' => '/data', |
||
| 210 | 'value' => 'should be OK' |
||
| 211 | ], |
||
| 212 | [ |
||
| 213 | 'op' => 'replace', |
||
| 214 | 'path' => '/version', |
||
| 215 | 'value' => $response->headers->get(VersionServiceConstraint::HEADER_NAME) |
||
| 216 | ] |
||
| 217 | ] |
||
| 218 | ); |
||
| 219 | |||
| 220 | $client->request('PATCH', '/testcase/versioning-entity/' . ($record->id), array(), array(), array(), $patch); |
||
| 221 | $response = $client->getResponse(); |
||
| 222 | $this->assertEquals(200, $response->getStatusCode()); |
||
| 223 | |||
| 224 | // Let's check that the patch updated counter version. |
||
| 225 | $client->request('GET', '/testcase/versioning-entity/' . ($record->id)); |
||
| 226 | $response = $client->getResponse(); |
||
| 227 | $current = json_decode($response->getContent()); |
||
| 228 | $this->assertEquals(2, $current->version); |
||
| 229 | $this->assertEquals('should be OK', $current->data); |
||
| 230 | } |
||
| 231 | } |
||
| 232 |