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 |
||
| 9 | class ProductMapperTest extends PHPUnit_Framework_TestCase |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * If all required fields are given and the validation passes a product should be returned |
||
| 13 | * |
||
| 14 | * @test |
||
| 15 | */ |
||
| 16 | public function getProduct_AllRequiredFieldsPresent_ValidationPasses_ProductIsReturned() |
||
| 17 | { |
||
| 18 | // arrange |
||
| 19 | $contentMapperMock = $this->createMock(ContentMapper::class); |
||
| 20 | $contentMapperMock->method("getContent")->willReturn(new Content("Title", "Summary", "...")); |
||
| 21 | |||
| 22 | /** @var ContentMapper $contentMapperMock */ |
||
| 23 | $productMapper = new ProductMapper($contentMapperMock); |
||
| 24 | |||
| 25 | $productData = array( |
||
| 26 | "sku" => "a-product", |
||
| 27 | "slug" => "A-Product", |
||
| 28 | "title" => "Super fancy product", |
||
| 29 | "summary" => "A super fancy product", |
||
| 30 | ); |
||
| 31 | |||
| 32 | // act |
||
| 33 | $product = $productMapper->getProduct($productData); |
||
| 34 | |||
| 35 | // assert |
||
| 36 | $this->assertNotNull($product, "getProduct() should have returned a product"); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * If given product data is missing one of the mandatory fields and exception should be thrown. |
||
| 41 | * |
||
| 42 | * @param array $productData Unstructured product data |
||
| 43 | * |
||
| 44 | * @test |
||
| 45 | * |
||
| 46 | * @expectedException \Wambo\Catalog\Error\ProductException |
||
| 47 | * @expectedExceptionMessageRegExp /The field '\w+' is missing in the given product data/ |
||
| 48 | * @dataProvider getProductDataWithMissingAttribute |
||
| 49 | */ |
||
| 50 | public function getProduct_RequiredFieldMissing_ProductExceptionIsThrown(array $productData) |
||
| 51 | { |
||
| 52 | // arrange |
||
| 53 | $contentMapperMock = $this->createMock(ContentMapper::class); |
||
| 54 | $contentMapperMock->method("getContent")->willReturn(new Content("Title", "Summary", "Description")); |
||
| 55 | |||
| 56 | /** @var ContentMapper $contentMapperMock */ |
||
| 57 | $productMapper = new ProductMapper($contentMapperMock); |
||
| 58 | |||
| 59 | // act |
||
| 60 | $productMapper->getProduct($productData); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * If the SKU validation fails and exception should be thrown |
||
| 65 | * |
||
| 66 | * @test |
||
| 67 | * @expectedException Wambo\Catalog\Error\ProductException |
||
| 68 | */ |
||
| 69 | public function getProduct_AllRequiredFieldsPresent_SkuValidationFails_ProductIsReturned() |
||
| 70 | { |
||
| 71 | // arrange |
||
| 72 | $contentMapperMock = $this->createMock(ContentMapper::class); |
||
| 73 | $contentMapperMock->method("getContent")->willReturn(new Content("Title", "Summary", "...")); |
||
| 74 | |||
| 75 | /** @var ContentMapper $contentMapperMock */ |
||
| 76 | $productMapper = new ProductMapper($contentMapperMock); |
||
| 77 | |||
| 78 | $productData = array( |
||
| 79 | "sku" => "a", |
||
| 80 | "slug" => "A-Product", |
||
| 81 | "title" => "Super fancy product", |
||
| 82 | "summary" => "A super fancy product", |
||
| 83 | ); |
||
| 84 | |||
| 85 | // act |
||
| 86 | $productMapper->getProduct($productData); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * If the Slug validation fails and exception should be thrown |
||
| 91 | * |
||
| 92 | * @test |
||
| 93 | * @expectedException Wambo\Catalog\Error\ProductException |
||
| 94 | */ |
||
| 95 | public function getProduct_AllRequiredFieldsPresent_SlugValidationFails_ProductIsReturned() |
||
| 96 | { |
||
| 97 | // arrange |
||
| 98 | $contentMapperMock = $this->createMock(ContentMapper::class); |
||
| 99 | $contentMapperMock->method("getContent")->willReturn(new Content("Title", "A super fancy product", "...")); |
||
| 100 | |||
| 101 | /** @var ContentMapper $contentMapperMock */ |
||
| 102 | $productMapper = new ProductMapper($contentMapperMock); |
||
| 103 | |||
| 104 | $productData = array( |
||
| 105 | "sku" => "a-product", |
||
| 106 | "slug" => "A/Product", |
||
| 107 | "title" => "Super fancy product", |
||
| 108 | "summary" => "A super fancy product", |
||
| 109 | ); |
||
| 110 | |||
| 111 | // act |
||
| 112 | $productMapper->getProduct($productData); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get product data with one or more missing attributes. |
||
| 117 | */ |
||
| 118 | public static function getProductDataWithMissingAttribute() |
||
| 119 | { |
||
| 120 | return [ |
||
| 121 | |||
| 122 | // empty |
||
| 123 | [ |
||
| 124 | [] |
||
| 125 | ], |
||
| 126 | |||
| 127 | // wrong casing |
||
| 128 | [ |
||
| 129 | [ |
||
| 130 | "SKU" => "a-product", |
||
| 131 | "SLUG" => "A-Product", |
||
| 132 | ] |
||
| 133 | ], |
||
| 134 | |||
| 135 | // slug missing |
||
| 136 | [ |
||
| 137 | [ |
||
| 138 | "sku" => "a-product", |
||
| 139 | ] |
||
| 140 | ], |
||
| 141 | |||
| 142 | // sku missing |
||
| 143 | [ |
||
| 144 | [ |
||
| 145 | "slug" => "A-Product", |
||
| 146 | ] |
||
| 147 | ] |
||
| 148 | |||
| 149 | ]; |
||
| 150 | } |
||
| 151 | } |
||
| 152 |