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 |
||
13 | class CatalogMapperTest extends \PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | /** |
||
16 | * @test |
||
17 | */ |
||
18 | public function getCatalog_EmptyArrayIsGiven_EmptyCatalogIsReturned() |
||
19 | { |
||
20 | // arrange |
||
21 | $catalogData = array(); |
||
22 | $productMapperMock = $this->getMockBuilder(ProductMapper::class)->disableOriginalConstructor()->getMock(); |
||
23 | /** @var ProductMapper $productMapperMock A product mapper instance */ |
||
24 | |||
25 | $productMapper = new CatalogMapper($productMapperMock); |
||
26 | |||
27 | |||
28 | // act |
||
29 | $result = $productMapper->getCatalog($catalogData); |
||
30 | |||
31 | // assert |
||
32 | $this->assertNotNull($result, "getCatalog should have returned an empty catalog"); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * When the product mapper throws an exception the getCatalog function should throw a CatalogException |
||
37 | * |
||
38 | * @test |
||
39 | * @expectedException Wambo\Catalog\Error\CatalogException |
||
40 | */ |
||
41 | public function getCatalog_ArrayOfTwoProductsGiven_ProductMapperThrowsException_CatalogExceptionIsThrown() |
||
42 | { |
||
43 | // arrange |
||
44 | $catalogData = [ |
||
45 | ["sku" => "0001"], |
||
46 | ["sku" => "0002"], |
||
47 | ]; |
||
48 | $productMapperMock = $this->createMock(ProductMapper::class); |
||
49 | $productMapperMock->method("getProduct")->willThrowException(new Exception("Some product mapper exception")); |
||
50 | |||
51 | /** @var ProductMapper $productMapperMock A product mapper instance */ |
||
52 | |||
53 | $productMapper = new CatalogMapper($productMapperMock); |
||
54 | |||
55 | |||
56 | // act |
||
57 | $productMapper->getCatalog($catalogData); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * If the the given product catalog data contains products with duplicate SKUs a |
||
62 | * Wambo\Catalog\Error\CatalogException should be thrown. |
||
63 | * |
||
64 | * @test |
||
65 | * |
||
66 | * @expectedException Wambo\Catalog\Error\CatalogException |
||
67 | * @expectedExceptionMessageRegExp /Cannot add a second product with the SKU '.+'/ |
||
68 | */ |
||
69 | public function getCatalog_ArrayWithDuplicateSKUGiven_CatalogExceptionIsThrown() |
||
70 | { |
||
71 | // arrange |
||
72 | $catalogData = [ |
||
73 | ["sku" => "0001"], |
||
74 | ["sku" => "0002"], |
||
75 | ["sku" => "0001"], |
||
76 | ]; |
||
77 | |||
78 | $productMapperMock = $this->getMockBuilder(ProductMapper::class)->disableOriginalConstructor()->getMock(); |
||
79 | $productMapperMock->method("getProduct")->will($this->onConsecutiveCalls( |
||
80 | new Product(new SKU("0001"), new Slug("product-1"), new Content("Title", "Summary")), |
||
81 | new Product(new SKU("0002"), new Slug("product-2"), new Content("Title", "Summary")), |
||
82 | new Product(new SKU("0001"), new Slug("product-1a"), new Content("Title", "Summary"))) |
||
83 | ); |
||
84 | |||
85 | /** @var ProductMapper $productMapperMock A product mapper instance */ |
||
86 | |||
87 | $productMapper = new CatalogMapper($productMapperMock); |
||
88 | |||
89 | // act |
||
90 | $productMapper->getCatalog($catalogData); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * If the the given product catalog data contains products with duplicate Slugs a |
||
95 | * Wambo\Catalog\Error\CatalogException should be thrown. |
||
96 | * |
||
97 | * @test |
||
98 | * |
||
99 | * @expectedException Wambo\Catalog\Error\CatalogException |
||
100 | * @expectedExceptionMessageRegExp /Cannot add a second product with the Slug 'product-1'/ |
||
101 | */ |
||
102 | public function getCatalog_ArrayWithDuplicateSlugsGiven_CatalogExceptionIsThrown() |
||
103 | { |
||
104 | // arrange |
||
105 | $catalogData = [ |
||
106 | ["sku" => "0001"], |
||
107 | ["sku" => "0002"], |
||
108 | ["sku" => "0003"], |
||
109 | ]; |
||
110 | |||
111 | $productMapperMock = $this->getMockBuilder(ProductMapper::class)->disableOriginalConstructor()->getMock(); |
||
112 | $productMapperMock->method("getProduct")->will($this->onConsecutiveCalls( |
||
113 | new Product(new SKU("0001"), new Slug("product-1"), new Content("Title", "Summary")), |
||
114 | new Product(new SKU("0002"), new Slug("product-2"), new Content("Title", "Summary")), |
||
115 | new Product(new SKU("0003"), new Slug("product-1"), new Content("Title", "Summary"))) |
||
116 | ); |
||
117 | |||
118 | /** @var ProductMapper $productMapperMock A product mapper instance */ |
||
119 | |||
120 | $productMapper = new CatalogMapper($productMapperMock); |
||
121 | |||
122 | // act |
||
123 | $productMapper->getCatalog($catalogData); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * If the the given product catalog data contains products with similar SKUs a |
||
128 | * Wambo\Catalog\Error\CatalogException should be thrown. |
||
129 | * |
||
130 | * @test |
||
131 | * |
||
132 | * @expectedException Wambo\Catalog\Error\CatalogException |
||
133 | * @expectedExceptionMessageRegExp /Cannot add a second product with the SKU 'product'/ |
||
134 | */ |
||
135 | public function getCatalog_ArrayWithSimilarSKUsGiven_CatalogExceptionIsThrown() |
||
136 | { |
||
137 | // arrange |
||
138 | $catalogData = [ |
||
139 | ["sku" => "0001"], |
||
140 | ["sku" => "0002"], |
||
141 | ["sku" => "0003"], |
||
142 | ]; |
||
143 | |||
144 | $productMapperMock = $this->createMock(ProductMapper::class); |
||
145 | $productMapperMock->method("getProduct")->will($this->onConsecutiveCalls( |
||
146 | new Product(new SKU("product"), new Slug("product-a"), new Content("Title", "Summary")), |
||
147 | new Product(new SKU("product"), new Slug("product-b"), new Content("Title", "Summary"))) |
||
148 | ); |
||
149 | |||
150 | /** @var ProductMapper $productMapperMock A product mapper instance */ |
||
151 | |||
152 | $productMapper = new CatalogMapper($productMapperMock); |
||
153 | |||
154 | // act |
||
155 | $productMapper->getCatalog($catalogData); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * If the the given product catalog data contains products with similar slugs a |
||
160 | * Wambo\Catalog\Error\CatalogException should be thrown. |
||
161 | * |
||
162 | * @test |
||
163 | * |
||
164 | * @expectedException Wambo\Catalog\Error\CatalogException |
||
165 | * @expectedExceptionMessageRegExp /Cannot add a second product with the Slug 'a-product'/ |
||
166 | */ |
||
167 | public function getCatalog_ArrayWithSimilarSlugsGiven_CatalogExceptionIsThrown() |
||
168 | { |
||
169 | // arrange |
||
170 | $catalogData = [ |
||
171 | ["sku" => "0001"], |
||
172 | ["sku" => "0002"], |
||
173 | ["sku" => "0003"], |
||
174 | ]; |
||
175 | |||
176 | $productMapperMock = $this->getMockBuilder(ProductMapper::class)->disableOriginalConstructor()->getMock(); |
||
177 | $productMapperMock->method("getProduct")->will($this->onConsecutiveCalls( |
||
178 | new Product(new SKU("0001"), new Slug("a-product"), new Content("Title", "Summary")), |
||
179 | new Product(new SKU("0002"), new Slug("A-Product"), new Content("Title", "Summary"))) |
||
180 | ); |
||
181 | |||
182 | /** @var ProductMapper $productMapperMock A product mapper instance */ |
||
183 | |||
184 | $productMapper = new CatalogMapper($productMapperMock); |
||
185 | |||
186 | // act |
||
187 | $productMapper->getCatalog($catalogData); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @test |
||
192 | */ |
||
193 | public function getCatalog_ArrayOfTwoProductsGiven_ProductMapperReturnsProducts_CatalogWithTwoProductsIsReturned() |
||
194 | { |
||
195 | // arrange |
||
196 | $catalogData = [ |
||
197 | ["sku" => "0001"], |
||
198 | ["sku" => "0002"], |
||
199 | ]; |
||
200 | |||
201 | $productMapperMock = $this->createMock(ProductMapper::class); |
||
202 | $productMapperMock->method("getProduct")->will($this->onConsecutiveCalls( |
||
203 | new Product(new SKU("0001"), new Slug("product-1"), new Content("Title", "Summary")), |
||
204 | new Product(new SKU("0002"), new Slug("product-2"), new Content("Title", "Summary"))) |
||
205 | ); |
||
206 | |||
207 | /** @var ProductMapper $productMapperMock A product mapper instance */ |
||
208 | |||
209 | $productMapper = new CatalogMapper($productMapperMock); |
||
210 | |||
211 | |||
212 | // act |
||
213 | $catalog = $productMapper->getCatalog($catalogData); |
||
214 | |||
215 | // assert |
||
216 | $this->assertCount(2, $catalog, "The catalog should contain two products"); |
||
217 | } |
||
218 | } |