| Conditions | 3 |
| Paths | 3 |
| Total Lines | 90 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 24 | public function testIncrementalDateFieldHandling() |
||
| 25 | { |
||
| 26 | // create the record |
||
| 27 | $object = (object) [ |
||
| 28 | 'id' => 'dude', |
||
| 29 | 'mightyDate' => '1984-05-02T07:00:01+0000' |
||
| 30 | ]; |
||
| 31 | |||
| 32 | $client = static::createRestClient(); |
||
| 33 | $client->put('/testcase/incremental-date-constraint/dude', $object); |
||
| 34 | $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
||
| 35 | $this->assertNull($client->getResults()); |
||
| 36 | |||
| 37 | // foolish attempts to beat the validation |
||
| 38 | $shouldNotWork = [ |
||
| 39 | 'same' => '1984-05-02T07:00:01+0000', |
||
| 40 | 'timezone' => '1984-05-02T07:00:01+2000', |
||
| 41 | '1sec' => '1984-05-02T07:00:00+0000', |
||
| 42 | 'string' => 'ss' |
||
| 43 | ]; |
||
| 44 | |||
| 45 | foreach ($shouldNotWork as $date) { |
||
| 46 | $object = (object) [ |
||
| 47 | 'id' => 'dude', |
||
| 48 | 'mightyDate' => $date |
||
| 49 | ]; |
||
| 50 | |||
| 51 | $client = static::createRestClient(); |
||
| 52 | $client->put('/testcase/incremental-date-constraint/dude', $object); |
||
| 53 | |||
| 54 | $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode()); |
||
| 55 | $this->assertEquals( |
||
| 56 | $client->getResults()[0], |
||
| 57 | (object) [ |
||
| 58 | 'propertyPath' => 'mightyDate', |
||
| 59 | 'message' => 'The date must be greater than the saved date 1984-05-02T07:00:01+0000' |
||
| 60 | ] |
||
| 61 | ); |
||
| 62 | |||
| 63 | // same with patch, no change made, same date if first validation goes through |
||
| 64 | $patchObject = json_encode( |
||
| 65 | [ |
||
| 66 | [ |
||
| 67 | 'op' => 'replace', |
||
| 68 | 'path' => '/mightyDate', |
||
| 69 | 'value' => $date |
||
| 70 | ] |
||
| 71 | ] |
||
| 72 | ); |
||
| 73 | $client->request('PATCH', '/testcase/incremental-date-constraint/dude', [], [], [], $patchObject); |
||
| 74 | |||
| 75 | if ($client->getResponse()->getStatusCode() == Response::HTTP_BAD_REQUEST) { |
||
| 76 | $this->assertEquals( |
||
| 77 | $client->getResults()[0], |
||
| 78 | (object) [ |
||
| 79 | 'propertyPath' => 'mightyDate', |
||
| 80 | 'message' => 'The date must be greater than the saved date 1984-05-02T07:00:01+0000' |
||
| 81 | ] |
||
| 82 | ); |
||
| 83 | } else { |
||
| 84 | $this->assertEquals(Response::HTTP_NOT_MODIFIED, $client->getResponse()->getStatusCode()); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | // this should work (+1 sec) |
||
| 89 | $object = (object) [ |
||
| 90 | 'id' => 'dude', |
||
| 91 | 'mightyDate' => '1984-05-02T07:00:02+0000' |
||
| 92 | ]; |
||
| 93 | |||
| 94 | $client = static::createRestClient(); |
||
| 95 | $client->put('/testcase/incremental-date-constraint/dude', $object); |
||
| 96 | $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
||
| 97 | $this->assertNull($client->getResults()); |
||
| 98 | |||
| 99 | // should work via PATCH |
||
| 100 | $patchObject = json_encode( |
||
| 101 | [ |
||
| 102 | [ |
||
| 103 | 'op' => 'replace', |
||
| 104 | 'path' => '/mightyDate', |
||
| 105 | 'value' => '1984-05-02T07:00:03+0000' |
||
| 106 | ] |
||
| 107 | ] |
||
| 108 | ); |
||
| 109 | $client = static::createRestClient(); |
||
| 110 | $client->request('PATCH', '/testcase/incremental-date-constraint/dude', [], [], [], $patchObject); |
||
| 111 | $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
||
| 112 | $this->assertNull($client->getResults()); |
||
| 113 | } |
||
| 114 | |||
| 154 |