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 |
||
| 4 | class ProductFinderModelTest extends UnitTestCase |
||
| 5 | { |
||
| 6 | protected static $products = array( |
||
| 7 | array( |
||
| 8 | 'id' => 8172401, |
||
| 9 | 'name' => 'TPS Report Cover Sheet', |
||
| 10 | 'price' => 0.89, |
||
| 11 | ), |
||
| 12 | array( |
||
| 13 | 'id' => 917246, |
||
| 14 | 'name' => 'Weighted Companion Cube', |
||
| 15 | 'price' => 129.99, |
||
| 16 | ), |
||
| 17 | array( |
||
| 18 | 'id' => 7856122, |
||
| 19 | 'name' => 'Longcat', |
||
| 20 | 'price' => 14599, |
||
| 21 | ), |
||
| 22 | array( |
||
| 23 | 'id' => 123456, |
||
| 24 | 'name' => 'Red Stapler', |
||
| 25 | 'price' => 3.14, |
||
| 26 | ), |
||
| 27 | array( |
||
| 28 | 'id' => 3165463, |
||
| 29 | 'name' => 'Sildenafil Citrate', |
||
| 30 | 'price' => 14.69, |
||
| 31 | ), |
||
| 32 | ); |
||
| 33 | |||
| 34 | public function testLol() { |
||
| 35 | $this->assertTrue(true); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @dataProvider productNamePrices |
||
| 40 | */ |
||
| 41 | public function testValidProductPricesByName($productName, $price) |
||
| 42 | { |
||
| 43 | $finder = $this->getContext()->getModel('ProductFinder'); |
||
| 44 | $this->assertEquals($price, $finder->retrieveByName($productName)->getPrice()); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function productNamePrices() |
||
| 48 | { |
||
| 49 | $retval = array(); |
||
| 50 | foreach(self::$products as $product) { |
||
| 51 | $retval[$product['name']] = array( |
||
| 52 | $product['name'], |
||
| 53 | $product['price'], |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | return $retval; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @dataProvider productIdPrices |
||
| 61 | */ |
||
| 62 | public function testValidProductPricesById($productId, $price) |
||
| 63 | { |
||
| 64 | $finder = $this->getContext()->getModel('ProductFinder'); |
||
| 65 | $this->assertEquals($price, $finder->retrieveById($productId)->getPrice()); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function productIdPrices() |
||
| 69 | { |
||
| 70 | $retval = array(); |
||
| 71 | foreach(self::$products as $product) { |
||
| 72 | $retval[$product['name']] = array( |
||
| 73 | $product['id'], |
||
| 74 | $product['price'], |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | return $retval; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @dataProvider productInfoPrices |
||
| 82 | */ |
||
| 83 | public function testValidProductPricesByInfo($productId, $productName, $price) |
||
| 84 | { |
||
| 85 | $finder = $this->getContext()->getModel('ProductFinder'); |
||
| 86 | $this->assertEquals($price, $finder->retrieveByIdAndName($productId, $productName)->getPrice()); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function productInfoPrices() |
||
| 90 | { |
||
| 91 | $retval = array(); |
||
| 92 | foreach(self::$products as $product) { |
||
| 93 | $retval[$product['name']] = array( |
||
| 94 | $product['id'], |
||
| 95 | $product['name'], |
||
| 96 | $product['price'], |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | return $retval; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function testNullForUnknownProductName() |
||
| 103 | { |
||
| 104 | $this->assertNull($this->getContext()->getModel('ProductFinder')->retrieveByName('unknown product')); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function testNullForUnknownProductId() |
||
| 108 | { |
||
| 109 | $this->assertNull($this->getContext()->getModel('ProductFinder')->retrieveById(-1)); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function testNullForUnknownProductInfo() |
||
| 113 | { |
||
| 114 | $this->assertNull($this->getContext()->getModel('ProductFinder')->retrieveByIdAndName(-1, 'unknown product')); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function testNullForPartiallyValidProductInfo() |
||
| 118 | { |
||
| 119 | $this->assertNull($this->getContext()->getModel('ProductFinder')->retrieveByIdAndName(123456, 'Red StaplerZOMG')); |
||
| 120 | $this->assertNull($this->getContext()->getModel('ProductFinder')->retrieveByIdAndName(1234567, 'Red Stapler')); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | ?> |