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 |
||
| 6 | class DMSDocumentCartTest extends SapphireTest |
||
|
|
|||
| 7 | { |
||
| 8 | protected static $fixture_file = 'DMSDocumentCartTest.yml'; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var DMSDocumentCart |
||
| 12 | */ |
||
| 13 | protected $cart; |
||
| 14 | |||
| 15 | public function setUp() |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @expectedException DMSDocumentCartException |
||
| 23 | * @expectedExceptionMessage Backend must implement DMSCartBackendInterface! |
||
| 24 | */ |
||
| 25 | public function testConstructorThrowsExceptionWhenProvidedBackendDoesNotImplementInterface() |
||
| 26 | { |
||
| 27 | DMSDocumentCart::create(new DMSDocument); |
||
| 28 | } |
||
| 29 | |||
| 30 | View Code Duplication | public function testAddItem() |
|
| 31 | { |
||
| 32 | $doc = $this->objFromFixture('DMSDocument', 'doc1'); |
||
| 33 | |||
| 34 | $item = DMSRequestItem::create(); |
||
| 35 | $item->setDocument($doc)->setQuantity(2); |
||
| 36 | $this->cart->addItem($item); |
||
| 37 | $this->assertFalse($this->cart->isCartEmpty()); |
||
| 38 | } |
||
| 39 | |||
| 40 | View Code Duplication | public function testEmptyCart() |
|
| 41 | { |
||
| 42 | $doc = $this->objFromFixture('DMSDocument', 'doc1'); |
||
| 43 | |||
| 44 | $item = DMSRequestItem::create(); |
||
| 45 | $item->setDocument($doc)->setQuantity(18); |
||
| 46 | $this->cart->addItem($item); |
||
| 47 | $this->assertFalse($this->cart->isCartEmpty()); |
||
| 48 | |||
| 49 | $this->cart->emptyCart(); |
||
| 50 | $this->assertTrue($this->cart->isCartEmpty()); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function testGetBackend() |
||
| 54 | { |
||
| 55 | $this->assertInstanceOf('DMSSessionBackend', $this->cart->getBackend()); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testGetBackURL() |
||
| 59 | { |
||
| 60 | $url = 'TestURL'; |
||
| 61 | $this->cart->setBackUrl($url); |
||
| 62 | $this->assertEquals($url, $this->cart->getBackUrl()); |
||
| 63 | } |
||
| 64 | |||
| 65 | View Code Duplication | public function testGetItem() |
|
| 66 | { |
||
| 67 | $doc = $this->objFromFixture('DMSDocument', 'doc1'); |
||
| 68 | |||
| 69 | $item = DMSRequestItem::create(); |
||
| 70 | $item->setDocument($doc)->setQuantity(2); |
||
| 71 | $this->cart->addItem($item); |
||
| 72 | |||
| 73 | $this->assertEquals($doc, $this->cart->getItem($doc->ID)->getDocument()); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testGetItems() |
||
| 77 | { |
||
| 78 | $item = DMSRequestItem::create(); |
||
| 79 | $item->setDocument($this->objFromFixture('DMSDocument', 'doc1'))->setQuantity(2); |
||
| 80 | $this->cart->addItem($item); |
||
| 81 | $this->assertCount(1, $this->cart->getItems()); |
||
| 82 | |||
| 83 | $item2 = DMSRequestItem::create(); |
||
| 84 | $item2->setDocument($this->objFromFixture('DMSDocument', 'limited_supply'))->setQuantity(2); |
||
| 85 | $this->cart->addItem($item2); |
||
| 86 | $this->assertCount(2, $this->cart->getItems()); |
||
| 87 | |||
| 88 | // Edge case handling for when a document is deleted in the background |
||
| 89 | $item2->getDocument()->delete(); |
||
| 90 | $this->assertCount(1, $this->cart->getItems()); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testGetReceiverInfo() |
||
| 94 | { |
||
| 95 | $info = array('Name' => 'Jane'); |
||
| 96 | $this->cart->setReceiverInfo($info); |
||
| 97 | $this->assertEquals($info, $this->cart->getReceiverInfo()); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testIsCartEmpty() |
||
| 101 | { |
||
| 102 | $this->assertTrue($this->cart->isCartEmpty()); |
||
| 103 | $doc = $this->objFromFixture('DMSDocument', 'doc1'); |
||
| 104 | $item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2); |
||
| 105 | $this->cart->addItem($item); |
||
| 106 | $this->assertFalse($this->cart->isCartEmpty()); |
||
| 107 | } |
||
| 108 | |||
| 109 | View Code Duplication | public function testRemoveItem() |
|
| 122 | |||
| 123 | View Code Duplication | public function testRemoveItemByID() |
|
| 136 | |||
| 137 | public function testUpdateItemQuantity() |
||
| 138 | { |
||
| 139 | $doc = $this->objFromFixture('DMSDocument', 'doc1'); |
||
| 140 | |||
| 141 | // Test Update |
||
| 142 | $item = DMSRequestItem::create(); |
||
| 143 | $item->setDocument($doc)->setQuantity(2); |
||
| 144 | $this->cart->addItem($item); |
||
| 145 | $this->assertEquals(2, $this->cart->getItem($doc->ID)->getQuantity()); |
||
| 162 | |||
| 163 | public function testSaveSubmission() |
||
| 179 | |||
| 180 | |||
| 181 | View Code Duplication | public function testIsInCart() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Ensure that the cart contents are hashed an used for the cart summary cache key |
||
| 193 | */ |
||
| 194 | public function testGetCartSummaryCacheKey() |
||
| 204 | } |
||
| 205 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.