Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 18 | class AppControllerTest extends RestTestCase |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @const complete content type string expected on a resouce |
||
| 22 | */ |
||
| 23 | const CONTENT_TYPE = 'application/json; charset=UTF-8; profile=http://localhost/schema/core/app/item'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @const corresponding vendorized schema mime type |
||
| 27 | */ |
||
| 28 | const COLLECTION_TYPE = 'application/json; charset=UTF-8; profile=http://localhost/schema/core/app/collection'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * setup client and load fixtures |
||
| 32 | * |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | public function setUp() |
||
| 36 | { |
||
| 37 | $this->loadFixtures( |
||
| 38 | array( |
||
| 39 | 'Graviton\CoreBundle\DataFixtures\MongoDB\LoadAppData', |
||
| 40 | 'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData', |
||
| 41 | 'Graviton\I18nBundle\DataFixtures\MongoDB\LoadMultiLanguageData', |
||
| 42 | 'Graviton\I18nBundle\DataFixtures\MongoDB\LoadTranslatableData', |
||
| 43 | 'Graviton\I18nBundle\DataFixtures\MongoDB\LoadTranslatablesApp' |
||
| 44 | ), |
||
| 45 | null, |
||
| 46 | 'doctrine_mongodb' |
||
| 47 | ); |
||
| 48 | } |
||
| 49 | /** |
||
| 50 | * check if all fixtures are returned on GET |
||
| 51 | * |
||
| 52 | * @return void |
||
| 53 | */ |
||
| 54 | public function testFindAll() |
||
| 55 | { |
||
| 56 | $client = static::createRestClient(); |
||
| 57 | $client->request('GET', '/core/app/'); |
||
| 58 | |||
| 59 | $response = $client->getResponse(); |
||
| 60 | $results = $client->getResults(); |
||
| 61 | |||
| 62 | $this->assertResponseContentType(self::COLLECTION_TYPE, $response); |
||
| 63 | $this->assertEquals(2, count($results)); |
||
| 64 | |||
| 65 | $this->assertEquals('admin', $results[0]->id); |
||
| 66 | $this->assertEquals('Administration', $results[0]->name->en); |
||
| 67 | $this->assertEquals(true, $results[0]->showInMenu); |
||
| 68 | $this->assertEquals(2, $results[0]->order); |
||
| 69 | |||
| 70 | $this->assertEquals('tablet', $results[1]->id); |
||
| 71 | $this->assertEquals('Tablet', $results[1]->name->en); |
||
| 72 | $this->assertEquals(true, $results[1]->showInMenu); |
||
| 73 | $this->assertEquals(1, $results[1]->order); |
||
| 74 | |||
| 75 | $this->assertContains( |
||
| 76 | '<http://localhost/core/app/>; rel="self"', |
||
| 77 | $response->headers->get('Link') |
||
| 78 | ); |
||
| 79 | $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin')); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * test if we can get list of apps, paged and with filters.. |
||
| 84 | * |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | public function testGetAppWithFilteringAndPaging() |
||
| 88 | { |
||
| 89 | $client = static::createRestClient(); |
||
| 90 | $_SERVER['QUERY_STRING'] = 'eq(showInMenu,true)&limit(1)'; |
||
| 91 | $client->request('GET', '/core/app/?eq(showInMenu,true)&limit(1)'); |
||
| 92 | unset($_SERVER['QUERY_STRING']); |
||
| 93 | $response = $client->getResponse(); |
||
| 94 | |||
| 95 | $this->assertEquals(1, count($client->getResults())); |
||
| 96 | |||
| 97 | $this->assertContains( |
||
| 98 | '<http://localhost/core/app/?eq(showInMenu%2Ctrue)&limit(1)>; rel="self"', |
||
| 99 | $response->headers->get('Link') |
||
| 100 | ); |
||
| 101 | |||
| 102 | $this->assertContains( |
||
| 103 | '<http://localhost/core/app/?eq(showInMenu%2Ctrue)&limit(1%2C1)>; rel="next"', |
||
| 104 | $response->headers->get('Link') |
||
| 105 | ); |
||
| 106 | |||
| 107 | $this->assertContains( |
||
| 108 | '<http://localhost/core/app/?eq(showInMenu%2Ctrue)&limit(1%2C1)>; rel="last"', |
||
| 109 | $response->headers->get('Link') |
||
| 110 | ); |
||
| 111 | |||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * rql limit() should *never* be overwritten by default value |
||
| 116 | * |
||
| 117 | * @return void |
||
| 118 | */ |
||
| 119 | public function testGetAppPagingWithRql() |
||
| 120 | { |
||
| 121 | // does limit work? |
||
| 122 | $client = static::createRestClient(); |
||
| 123 | $client->request('GET', '/core/app/?limit(1)'); |
||
| 124 | $this->assertEquals(1, count($client->getResults())); |
||
| 125 | |||
| 126 | $response = $client->getResponse(); |
||
| 127 | |||
| 128 | $this->assertContains( |
||
| 129 | '<http://localhost/core/app/?limit(1)>; rel="self"', |
||
| 130 | $response->headers->get('Link') |
||
| 131 | ); |
||
| 132 | |||
| 133 | $this->assertContains( |
||
| 134 | '<http://localhost/core/app/?limit(1%2C1)>; rel="next"', |
||
| 135 | $response->headers->get('Link') |
||
| 136 | ); |
||
| 137 | |||
| 138 | $this->assertContains( |
||
| 139 | '<http://localhost/core/app/?limit(1%2C1)>; rel="last"', |
||
| 140 | $response->headers->get('Link') |
||
| 141 | ); |
||
| 142 | |||
| 143 | $this->assertSame('2', $response->headers->get('X-Total-Count')); |
||
| 144 | |||
| 145 | /*** pagination tests **/ |
||
| 146 | $client = static::createRestClient(); |
||
| 147 | $client->request('GET', '/core/app/?limit(1,1)'); |
||
| 148 | $this->assertEquals(1, count($client->getResults())); |
||
| 149 | |||
| 150 | $response = $client->getResponse(); |
||
| 151 | |||
| 152 | $this->assertContains( |
||
| 153 | '<http://localhost/core/app/?limit(1%2C1)>; rel="self"', |
||
| 154 | $response->headers->get('Link') |
||
| 155 | ); |
||
| 156 | |||
| 157 | $this->assertContains( |
||
| 158 | '<http://localhost/core/app/?limit(1%2C0)>; rel="prev"', |
||
| 159 | $response->headers->get('Link') |
||
| 160 | ); |
||
| 161 | |||
| 162 | // we're on the 'last' page - so 'last' should not be in in Link header |
||
| 163 | $this->assertNotContains( |
||
| 164 | 'rel="last"', |
||
| 165 | $response->headers->get('Link') |
||
| 166 | ); |
||
| 167 | |||
| 168 | $this->assertSame('2', $response->headers->get('X-Total-Count')); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * check for a client error if invalid limit value is provided |
||
| 173 | * |
||
| 174 | * @dataProvider invalidPagingPageSizeProvider |
||
| 175 | * |
||
| 176 | * @param integer $limit limit value that should fail |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | public function testInvalidPagingPageSize($limit) |
||
| 180 | { |
||
| 181 | $client = static::createRestClient(); |
||
| 182 | $client->request('GET', sprintf('/core/app/?limit(%s)', $limit)); |
||
| 183 | |||
| 184 | $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode()); |
||
| 185 | $this->assertContains('negative or null limit in rql', $client->getResults()->message); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * page size test provides |
||
| 190 | * |
||
| 191 | * @return array[] |
||
|
|
|||
| 192 | */ |
||
| 193 | public function invalidPagingPageSizeProvider() |
||
| 194 | { |
||
| 195 | return [ |
||
| 196 | [0], |
||
| 197 | [-1], |
||
| 198 | ]; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * RQL is parsed only when we get apps |
||
| 203 | * |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | public function testRqlIsParsedOnlyOnGetRequest() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Test only RQL select() operator is allowed for GET one |
||
| 260 | * |
||
| 261 | * @return void |
||
| 262 | * @group tmp |
||
| 263 | */ |
||
| 264 | public function testOnlyRqlSelectIsAllowedOnGetOne() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * check for empty collections when no fixtures are loaded |
||
| 295 | * |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | View Code Duplication | public function testFindAllEmptyCollection() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * test if we can get an app by id |
||
| 315 | * |
||
| 316 | * @return void |
||
| 317 | */ |
||
| 318 | public function testGetApp() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * test if we can create an app through POST |
||
| 340 | * |
||
| 341 | * @return void |
||
| 342 | */ |
||
| 343 | public function testPostApp() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * test if we get a correct return if we post empty. |
||
| 375 | * |
||
| 376 | * @return void |
||
| 377 | */ |
||
| 378 | public function testPostEmptyApp() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * test if we get a correct return if we post empty. |
||
| 397 | * |
||
| 398 | * @return void |
||
| 399 | */ |
||
| 400 | public function testPostNonObjectApp() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * test if 500 error is reported when posting an malformed input |
||
| 412 | * |
||
| 413 | * @return void |
||
| 414 | */ |
||
| 415 | public function testPostMalformedApp() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Tests if an error is returned when an id is send in a post |
||
| 447 | * |
||
| 448 | * @return void |
||
| 449 | */ |
||
| 450 | public function testPostWithId() |
||
| 464 | /** |
||
| 465 | * test updating apps |
||
| 466 | * |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | public function testPutApp() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Test for PATCH Request |
||
| 500 | * |
||
| 501 | * @return void |
||
| 502 | */ |
||
| 503 | public function testPatchAppRequestApplyChanges() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Test for Malformed PATCH Request |
||
| 543 | * |
||
| 544 | * @return void |
||
| 545 | */ |
||
| 546 | public function testMalformedPatchAppRequest() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Try to update an app with a non matching ID in GET and req body |
||
| 578 | * |
||
| 579 | * @return void |
||
| 580 | */ |
||
| 581 | public function testNonMatchingIdPutApp() |
||
| 601 | |||
| 602 | /** |
||
| 603 | * We had an issue when PUTing without ID would create a new record. |
||
| 604 | * This test ensures that we don't do that, instead we should apply the ID from the GET req. |
||
| 605 | * |
||
| 606 | * @return void |
||
| 607 | */ |
||
| 608 | public function testPutAppNoIdInPayload() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * test updating an inexistant document (upsert) |
||
| 632 | * |
||
| 633 | * @return void |
||
| 634 | */ |
||
| 635 | public function testUpsertApp() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * test deleting an app |
||
| 651 | * |
||
| 652 | * @return void |
||
| 653 | */ |
||
| 654 | public function testDeleteApp() |
||
| 674 | |||
| 675 | /** |
||
| 676 | * test failing validation on boolean field |
||
| 677 | * |
||
| 678 | * @return void |
||
| 679 | */ |
||
| 680 | public function testFailingBooleanValidationOnAppUpdate() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * test getting schema information |
||
| 701 | * |
||
| 702 | * @return void |
||
| 703 | */ |
||
| 704 | View Code Duplication | public function testGetAppSchemaInformation() |
|
| 713 | |||
| 714 | /** |
||
| 715 | * requests on OPTIONS and HEAD shall not lead graviton to get any data from mongodb. |
||
| 716 | * if we page limit(1) this will lead to presence of the x-total-count header if |
||
| 717 | * data is generated (asserted by testGetAppPagingWithRql()). thus, if we don't |
||
| 718 | * have this header, we can safely assume that no data has been processed in RestController. |
||
| 719 | * |
||
| 720 | * @return void |
||
| 721 | */ |
||
| 722 | View Code Duplication | public function testNoRecordsAreGeneratedOnPreRequests() |
|
| 734 | |||
| 735 | /** |
||
| 736 | * test getting schema information from canonical url |
||
| 737 | * |
||
| 738 | * @return void |
||
| 739 | */ |
||
| 740 | View Code Duplication | public function testGetAppSchemaInformationCanonical() |
|
| 748 | |||
| 749 | /** |
||
| 750 | * test getting collection schema |
||
| 751 | * |
||
| 752 | * @return void |
||
| 753 | */ |
||
| 754 | public function testGetAppCollectionSchemaInformation() |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Test for searchable translations |
||
| 785 | * |
||
| 786 | * @dataProvider searchableTranslationDataProvider |
||
| 787 | * |
||
| 788 | * @param string $expr expression |
||
| 789 | * @param int $expCount count |
||
| 790 | * |
||
| 791 | * @return void |
||
| 792 | */ |
||
| 793 | public function testSearchableTranslations($expr, $expCount) |
||
| 807 | |||
| 808 | /** |
||
| 809 | * data provider for searchable translations |
||
| 810 | * |
||
| 811 | * @return array data |
||
| 812 | */ |
||
| 813 | public function searchableTranslationDataProvider() |
||
| 824 | |||
| 825 | /** |
||
| 826 | * ensure we have nice parse error output in rql parse failure |
||
| 827 | * |
||
| 828 | * @return void |
||
| 829 | */ |
||
| 830 | View Code Duplication | public function testRqlSyntaxError() |
|
| 844 | |||
| 845 | /** |
||
| 846 | * check if response looks like schema |
||
| 847 | * |
||
| 848 | * @param object $response response |
||
| 849 | * |
||
| 850 | * @return void |
||
| 851 | */ |
||
| 852 | private function assertIsSchemaResponse($response) |
||
| 857 | |||
| 858 | /** |
||
| 859 | * check if a schema is of the app type |
||
| 860 | * |
||
| 861 | * @param \stdClass $schema schema from service to validate |
||
| 862 | * |
||
| 863 | * @return void |
||
| 864 | */ |
||
| 865 | private function assertIsAppSchema(\stdClass $schema) |
||
| 890 | } |
||
| 891 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.