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  | 
            ||
| 3 | class ShortlistTest extends FunctionalTest  | 
            ||
| 4 | { | 
            ||
| 5 | protected static $fixture_file = 'fixtures.yml';  | 
            ||
| 6 | protected $fixtureFactory = null;  | 
            ||
| 7 | |||
| 8 | public function setUpOnce()  | 
            ||
| 9 |     { | 
            ||
| 10 | parent::setUpOnce();  | 
            ||
| 11 | |||
| 12 |         $this->fixtureFactory = Injector::inst()->create('FixtureFactory'); | 
            ||
| 13 | }  | 
            ||
| 14 | |||
| 15 | public function setup()  | 
            ||
| 16 |     { | 
            ||
| 17 | parent::setup();  | 
            ||
| 18 | |||
| 19 | SecurityToken::enable();  | 
            ||
| 20 | }  | 
            ||
| 21 | |||
| 22 | public function teardown()  | 
            ||
| 23 |     { | 
            ||
| 24 | parent::teardown();  | 
            ||
| 25 | |||
| 26 | SecurityToken::disable();  | 
            ||
| 27 | }  | 
            ||
| 28 | |||
| 29 | public function testShortlistCreation()  | 
            ||
| 30 |     { | 
            ||
| 31 | $shortlists = ShortList::get();  | 
            ||
| 32 | |||
| 33 | $this->assertNotEquals($shortlists->Count(), 0);  | 
            ||
| 34 | }  | 
            ||
| 35 | |||
| 36 | /*  | 
            ||
| 37 | |--------------------------------------------------------------------------  | 
            ||
| 38 | | Add item to short list DBO.  | 
            ||
| 39 | |--------------------------------------------------------------------------  | 
            ||
| 40 | */  | 
            ||
| 41 | public function testAddItemtoShortList()  | 
            ||
| 42 |     { | 
            ||
| 43 |         $shortlist = $this->fixtureFactory->get('ShortList', 'test'); | 
            ||
| 44 |         $item = $this->fixtureFactory->get('ShortListItem', 'item1'); | 
            ||
| 45 | |||
| 46 | $this->assertEquals($shortlist->ShortListItems()->Count(), 0);  | 
            ||
| 47 | |||
| 48 | // add to the shortlist.  | 
            ||
| 49 | $shortlist->ShortListItems()->add($item);  | 
            ||
| 50 | |||
| 51 |         $retrievedItem = $shortlist->ShortListItems()->filter('ID', $item->ID)->first()->getActualRecord(); | 
            ||
| 52 | |||
| 53 | // Check the item has been saved correctly  | 
            ||
| 54 | $this->assertEquals($retrievedItem->ClassName, 'Page');  | 
            ||
| 55 | $this->assertEquals($retrievedItem->ID, $item->ID);  | 
            ||
| 56 | |||
| 57 | // do we have an item added?  | 
            ||
| 58 | $this->assertNotEquals($shortlist->ShortListItems()->Count(), 0);  | 
            ||
| 59 | // what about the reverse side?  | 
            ||
| 60 | $this->assertEquals($item->ShortList()->ID, $shortlist->ID);  | 
            ||
| 61 | }  | 
            ||
| 62 | |||
| 63 | /*  | 
            ||
| 64 | |--------------------------------------------------------------------------  | 
            ||
| 65 | | Remove item from shortlist DBO.  | 
            ||
| 66 | |--------------------------------------------------------------------------  | 
            ||
| 67 | */  | 
            ||
| 68 | public function testRemoveItemFromShortList()  | 
            ||
| 69 |     { | 
            ||
| 70 |         $testpage = $this->objFromFixture('Page', 'page1'); | 
            ||
| 71 | |||
| 72 | // create the shortlist  | 
            ||
| 73 | $shortlist = new ShortList();  | 
            ||
| 74 | $shortlist->write();  | 
            ||
| 75 | |||
| 76 | $this->assertEquals($shortlist->ShortListItems()->Count(), 0);  | 
            ||
| 77 | |||
| 78 | // create the item.  | 
            ||
| 79 | $item = new ShortListItem();  | 
            ||
| 80 | $item->ItemType = $testpage->ClassName;  | 
            ||
| 81 | $item->ItemID = $testpage->ID;  | 
            ||
| 82 | $item->write();  | 
            ||
| 83 | |||
| 84 | // add to the shortlist.  | 
            ||
| 85 | $shortlist->ShortListItems()->add($item);  | 
            ||
| 86 | $shortlist->write();  | 
            ||
| 87 | |||
| 88 | // do we have an item added?  | 
            ||
| 89 | $this->assertNotEquals($shortlist->ShortListItems()->Count(), 0);  | 
            ||
| 90 | // what about the reverse side?  | 
            ||
| 91 | $this->assertNotEquals($item->ShortList()->ID, 0);  | 
            ||
| 92 | |||
| 93 | $shortlist->ShortListItems()->remove($item);  | 
            ||
| 94 | |||
| 95 | // do we have an item removed?  | 
            ||
| 96 | $this->assertEquals($shortlist->ShortListItems()->Count(), 0);  | 
            ||
| 97 | }  | 
            ||
| 98 | |||
| 99 | /*  | 
            ||
| 100 | |--------------------------------------------------------------------------  | 
            ||
| 101 | | Check whether we are or are not a browser.  | 
            ||
| 102 | |--------------------------------------------------------------------------  | 
            ||
| 103 | */  | 
            ||
| 104 | public function testisBrowser()  | 
            ||
| 105 |     { | 
            ||
| 106 |         $list = $this->objFromFixture('ShortList', 'test'); | 
            ||
| 107 |         $ua = Controller::curr()->getRequest()->getHeader('User-Agent'); | 
            ||
| 108 | |||
| 109 |         if (empty(strtolower($ua))) { | 
            ||
| 110 | $this->assertFalse($list->isBrowser());  | 
            ||
| 111 |         } else { | 
            ||
| 112 | $this->assertTrue($list->isBrowser());  | 
            ||
| 113 | }  | 
            ||
| 114 | }  | 
            ||
| 115 | |||
| 116 | /*  | 
            ||
| 117 | |--------------------------------------------------------------------------  | 
            ||
| 118 | | Check to see that the generated URL contains the config URL segment  | 
            ||
| 119 | | and the genertated random URL.  | 
            ||
| 120 | |--------------------------------------------------------------------------  | 
            ||
| 121 | */  | 
            ||
| 122 | public function testShortListLink()  | 
            ||
| 123 |     { | 
            ||
| 124 |         $list = $this->objFromFixture('ShortList', 'test'); | 
            ||
| 125 | $list->write();  | 
            ||
| 126 | |||
| 127 |         $configURL = Config::inst()->get('ShortList', 'URLSegment'); | 
            ||
| 128 | $link = $list->Link();  | 
            ||
| 129 | |||
| 130 | $configPos = strpos($link, $configURL);  | 
            ||
| 131 | $linkPos = strpos($link, $list->URL);  | 
            ||
| 132 | |||
| 133 | $this->assertGreaterThan($configPos, $linkPos);  | 
            ||
| 134 | }  | 
            ||
| 135 | |||
| 136 | /*  | 
            ||
| 137 | |--------------------------------------------------------------------------  | 
            ||
| 138 | | Test opening the shortlist controller url.  | 
            ||
| 139 | |--------------------------------------------------------------------------  | 
            ||
| 140 | */  | 
            ||
| 141 | public function testShortListController()  | 
            ||
| 142 |     { | 
            ||
| 143 |         $url = Config::inst()->get('ShortList', 'URLSegment'); | 
            ||
| 144 | $response = $this->get($url);  | 
            ||
| 145 | |||
| 146 | // Legit response?  | 
            ||
| 147 | $this->assertEquals($response->getStatusCode(), 200);  | 
            ||
| 148 | }  | 
            ||
| 149 | |||
| 150 | /*  | 
            ||
| 151 | |--------------------------------------------------------------------------  | 
            ||
| 152 | | Add via the controller.  | 
            ||
| 153 | |--------------------------------------------------------------------------  | 
            ||
| 154 | */  | 
            ||
| 155 | public function testShortListControllerAdd()  | 
            ||
| 156 |     { | 
            ||
| 157 |         $url = Config::inst()->get('ShortList', 'URLSegment'); | 
            ||
| 158 | $list = ShortListController::getShortListSession();  | 
            ||
| 159 | |||
| 160 | /*  | 
            ||
| 161 | |--------------------------------------------------------------------------  | 
            ||
| 162 | | There should not be anything in the shortlist  | 
            ||
| 163 | |--------------------------------------------------------------------------  | 
            ||
| 164 | */  | 
            ||
| 165 | $this->assertEmpty($list);  | 
            ||
| 166 | |||
| 167 |         $id = $this->fixtureFactory->getId('Page', 'page1'); | 
            ||
| 168 | $response = $this->get($url.'add/?id='.$id.'&type=Page&s='.Utilities::getSecurityToken());  | 
            ||
| 169 | |||
| 170 | $list = ShortListController::getShortListSession();  | 
            ||
| 171 | |||
| 172 | $this->assertNotEmpty($list);  | 
            ||
| 173 | $this->assertEquals($list->ShortListItems()->count(), 1);  | 
            ||
| 174 | }  | 
            ||
| 175 | |||
| 176 | /*  | 
            ||
| 177 | |--------------------------------------------------------------------------  | 
            ||
| 178 | | Remove an item from the list.  | 
            ||
| 179 | |--------------------------------------------------------------------------  | 
            ||
| 180 | */  | 
            ||
| 181 | public function testShortListControllerRemove()  | 
            ||
| 182 |     { | 
            ||
| 183 | /*  | 
            ||
| 184 | |--------------------------------------------------------------------------  | 
            ||
| 185 | | First add to the shortlist.  | 
            ||
| 186 | |--------------------------------------------------------------------------  | 
            ||
| 187 | */  | 
            ||
| 188 |         $url = Config::inst()->get('ShortList', 'URLSegment'); | 
            ||
| 189 | $list = ShortListController::getShortListSession();  | 
            ||
| 190 | |||
| 191 | $this->assertEmpty($list);  | 
            ||
| 192 | |||
| 193 |         $id = $this->fixtureFactory->getId('Page', 'page1'); | 
            ||
| 194 | $response = $this->get($url.'add/?id='.$id.'&type=Page&s='.Utilities::getSecurityToken());  | 
            ||
| 195 | |||
| 196 | $list = ShortListController::getShortListSession();  | 
            ||
| 197 | |||
| 198 | $this->assertNotEmpty($list);  | 
            ||
| 199 | $this->assertEquals($list->ShortListItems()->count(), 1);  | 
            ||
| 200 | |||
| 201 | $this->get($url.'remove/?id='.$id.'&type=Page&s='.Utilities::getSecurityToken());  | 
            ||
| 202 | $this->assertEquals($list->ShortListItems()->count(), 0);  | 
            ||
| 203 | }  | 
            ||
| 204 | |||
| 205 | /*  | 
            ||
| 206 | |--------------------------------------------------------------------------  | 
            ||
| 207 | | Paginate the page.  | 
            ||
| 208 | |--------------------------------------------------------------------------  | 
            ||
| 209 | */  | 
            ||
| 210 | public function testShortListControllerPagination()  | 
            ||
| 211 |     { | 
            ||
| 212 |         $url = Config::inst()->get('ShortList', 'URLSegment'); | 
            ||
| 213 | $response = $this->get($url.'?page=1');  | 
            ||
| 214 | |||
| 215 | $this->assertEquals($response->getStatusCode(), 200);  | 
            ||
| 216 | }  | 
            ||
| 217 | |||
| 218 | /*  | 
            ||
| 219 | |--------------------------------------------------------------------------  | 
            ||
| 220 | | Visit the index after the shortlist has been created. We should be  | 
            ||
| 221 | | redirected to the appropriate url - e.g. /shortlist/<uniqueurl>  | 
            ||
| 222 | |--------------------------------------------------------------------------  | 
            ||
| 223 | */  | 
            ||
| 224 | public function testShortListControllerReIndex()  | 
            ||
| 225 |     { | 
            ||
| 226 | /*  | 
            ||
| 227 | |--------------------------------------------------------------------------  | 
            ||
| 228 | | First add to the shortlist.  | 
            ||
| 229 | |--------------------------------------------------------------------------  | 
            ||
| 230 | */  | 
            ||
| 231 |         $url = Config::inst()->get('ShortList', 'URLSegment'); | 
            ||
| 232 | $list = ShortListController::getShortListSession();  | 
            ||
| 233 | |||
| 234 | $this->assertEmpty($list);  | 
            ||
| 235 | |||
| 236 |         $id = $this->fixtureFactory->getId('Page', 'page1'); | 
            ||
| 237 | $response = $this->get($url.'add/?id='.$id.'&type=Page&s='.Utilities::getSecurityToken());  | 
            ||
| 238 | |||
| 239 | $response = $this->get($url);  | 
            ||
| 240 | }  | 
            ||
| 241 | |||
| 242 | public function testShortListControllerInvalidAdd()  | 
            ||
| 243 |     { | 
            ||
| 244 |         $url = Config::inst()->get('ShortList', 'URLSegment'); | 
            ||
| 245 | $list = ShortListController::getShortListSession();  | 
            ||
| 246 | |||
| 247 | /*  | 
            ||
| 248 | |--------------------------------------------------------------------------  | 
            ||
| 249 | | There should not be anything in the shortlist  | 
            ||
| 250 | |--------------------------------------------------------------------------  | 
            ||
| 251 | */  | 
            ||
| 252 | $this->assertEmpty($list);  | 
            ||
| 253 | |||
| 254 |         $id = $this->fixtureFactory->getId('Page', 'page1'); | 
            ||
| 255 | $response = $this->get($url.'add/?id='.$id.'&type=Page');  | 
            ||
| 256 | |||
| 257 | $this->assertEquals($response->getStatusCode(), 404);  | 
            ||
| 258 | }  | 
            ||
| 259 | }  | 
            ||
| 260 |