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 |
||
14 | class AuctionArrayMapDecoratorTest extends TestCase |
||
15 | { |
||
16 | |||
17 | const CORRECT_CATEGORY = 6092; |
||
18 | |||
19 | const INCORRECT_CATEGORY = 42; |
||
20 | |||
21 | |||
22 | protected function getTestPhotoArray() |
||
23 | { |
||
24 | return [ |
||
25 | __DIR__ . '/i.am.a.photo.file.txt' |
||
26 | ]; |
||
27 | } |
||
28 | |||
29 | |||
30 | protected function getTestArray() |
||
31 | { |
||
32 | return [ |
||
33 | 'title' => 'Allegro test auction', |
||
34 | 'description' => 'Test auction description', |
||
35 | 'category' => self::CORRECT_CATEGORY, |
||
36 | 'timespan' => AuctionTimespans::TIMESPAN_3_DAYS, |
||
37 | 'quantity' => 100, |
||
38 | 'country' => 1, |
||
39 | 'region' => 15, |
||
40 | 'city' => 'SomeCity', |
||
41 | 'postcode' => '12-345', |
||
42 | 'condition' => Conditions::CONDITION_NEW, |
||
43 | 'sale_format' => SaleFormats::SALE_FORMAT_SHOP, |
||
44 | 'buy_now_price' => 43, |
||
45 | 'shipping_paid_by' => ShippingPaidBy::SHIPPING_PAID_BY_BUYER, |
||
46 | 'post_package_priority_price' => 12, |
||
47 | ]; |
||
48 | } |
||
49 | |||
50 | |||
51 | protected function getTestFidArray() |
||
52 | { |
||
53 | return [ |
||
54 | 1 => 'Allegro test auction', |
||
55 | 24 => 'Test auction description', |
||
56 | 2 => self::CORRECT_CATEGORY, |
||
57 | 4 => 0, |
||
58 | 5 => 100, |
||
59 | 9 => 1, |
||
60 | 10 => 15, |
||
61 | 11 => 'SomeCity', |
||
62 | 32 => '12-345', |
||
63 | 20626 => 1, |
||
64 | 29 => 1, |
||
65 | 8 => 43, |
||
66 | 12 => 1, |
||
67 | 38 => 12, |
||
68 | ]; |
||
69 | } |
||
70 | |||
71 | |||
72 | protected function getTestMap() |
||
73 | { |
||
74 | return [ |
||
75 | 'title' => 1, |
||
76 | 'description' => 24, |
||
77 | 'category' => 2, |
||
78 | 'timespan' => 4, |
||
79 | 'quantity' => 5, |
||
80 | 'country' => 9, |
||
81 | 'region' => 10, |
||
82 | 'city' => 11, |
||
83 | 'postcode' => 32, |
||
84 | 'condition' => 20626, |
||
85 | 'sale_format' => 29, |
||
86 | 'buy_now_price' => 8, |
||
87 | 'shipping_paid_by' => 12, |
||
88 | 'post_package_priority_price' => 38, |
||
89 | ]; |
||
90 | } |
||
91 | |||
92 | |||
93 | protected function getDecorator(Auction $auction) |
||
94 | { |
||
95 | $decorator = $this->getMockForAbstractClass(AuctionArrayMapDecorator::class, [ |
||
96 | $auction |
||
97 | ]); |
||
98 | |||
99 | $decorator->expects($this->any()) |
||
100 | ->method('getMap') |
||
101 | ->willReturn( |
||
102 | $this->getTestMap() |
||
103 | ); |
||
104 | |||
105 | return $decorator; |
||
106 | } |
||
107 | |||
108 | |||
109 | public function testFromArray() |
||
110 | { |
||
111 | $auction = $this->getMockBuilder(Auction::class) |
||
112 | ->setMethods(['fromArray']) |
||
113 | ->getMock(); |
||
114 | |||
115 | $auction->expects($this->once()) |
||
116 | ->method('fromArray') |
||
117 | ->with($this->getTestFidArray()) |
||
118 | ->willReturn(null); |
||
119 | |||
120 | $decorator = $this->getDecorator($auction); |
||
121 | |||
122 | $decorator->fromArray($this->getTestArray()); |
||
123 | } |
||
124 | |||
125 | |||
126 | public function testToArray() |
||
127 | { |
||
128 | $auction = $this->getMockBuilder(Auction::class) |
||
129 | ->setMethods(['toArray']) |
||
130 | ->getMock(); |
||
131 | |||
132 | $auction->expects($this->once()) |
||
133 | ->method('toArray') |
||
134 | ->willReturn($this->getTestFidArray()); |
||
135 | |||
136 | $decorator = $this->getDecorator($auction); |
||
137 | |||
138 | $this->assertSame($this->getTestArray(), $decorator->toArray()); |
||
139 | } |
||
140 | |||
141 | |||
142 | |||
143 | public function testSetPhotos() |
||
144 | { |
||
145 | $auction = $this->getMockBuilder(Auction::class) |
||
146 | ->setMethods(['setPhotos']) |
||
147 | ->getMock(); |
||
148 | |||
149 | $auction->expects($this->once()) |
||
150 | ->method('setPhotos') |
||
151 | ->with($this->getTestPhotoArray()) |
||
152 | ->willReturn(null); |
||
153 | |||
154 | $decorator = $this->getDecorator($auction); |
||
155 | $decorator->setPhotos($this->getTestPhotoArray()); |
||
156 | } |
||
157 | |||
158 | |||
159 | public function testToApiRepresentation() |
||
160 | { |
||
161 | $expectedResult = [ |
||
162 | 'whatever' => 'just testing if it is forwarded correctly' |
||
163 | ]; |
||
164 | |||
165 | $auction = $this->getMockBuilder(Auction::class) |
||
166 | ->setMethods(['toApiRepresentation']) |
||
167 | ->getMock(); |
||
168 | |||
169 | $auction->expects($this->once()) |
||
170 | ->method('toApiRepresentation') |
||
171 | ->willReturn($expectedResult); |
||
172 | |||
173 | $decorator = $this->getDecorator($auction); |
||
174 | $result = $decorator->toApiRepresentation(); |
||
175 | $this->assertSame($expectedResult, $result); |
||
176 | } |
||
177 | |||
178 | |||
179 | public function testFromApiRepresentation() |
||
180 | { |
||
181 | $expectedArgument = [ |
||
182 | 'whatever' => 'just testing if it is forwarded correctly' |
||
183 | ]; |
||
184 | |||
185 | $auction = $this->getMockBuilder(Auction::class) |
||
186 | ->setMethods(['fromApiRepresentation']) |
||
187 | ->getMock(); |
||
188 | |||
189 | $auction->expects($this->once()) |
||
190 | ->method('fromApiRepresentation') |
||
191 | ->with($this->equalTo($expectedArgument)); |
||
192 | |||
193 | |||
194 | $decorator = $this->getDecorator($auction); |
||
195 | $decorator->fromApiRepresentation($expectedArgument); |
||
196 | } |
||
197 | |||
198 | |||
199 | public function testDefaultCategoryFromArray() |
||
200 | { |
||
201 | $auction = $this->getMockBuilder(Auction::class) |
||
202 | ->setMethods(['fromArray']) |
||
203 | ->getMock(); |
||
204 | |||
205 | $auction->expects($this->once()) |
||
206 | ->method('fromArray') |
||
207 | ->with([AuctionFids::FID_CATEGORY => self::CORRECT_CATEGORY]) |
||
208 | ->willReturn(null); |
||
209 | |||
210 | $decorator = $this->getDecorator($auction); |
||
211 | $decorator->expects($this->any()) |
||
212 | ->method('getIdCategory') |
||
213 | ->willReturn(self::CORRECT_CATEGORY); |
||
214 | |||
215 | $decorator->fromArray([]); |
||
216 | } |
||
217 | |||
218 | |||
219 | public function testDefaultCategoryToArray() |
||
220 | { |
||
221 | $auction = $this->getMockBuilder(Auction::class) |
||
222 | ->setMethods(['toArray']) |
||
223 | ->getMock(); |
||
224 | |||
225 | $auction->expects($this->once()) |
||
226 | ->method('toArray') |
||
227 | ->with() |
||
228 | ->willReturn([]); |
||
229 | |||
230 | $decorator = $this->getDecorator($auction); |
||
231 | $decorator->expects($this->any()) |
||
232 | ->method('getIdCategory') |
||
233 | ->willReturn(self::CORRECT_CATEGORY); |
||
234 | |||
235 | $this->assertEquals(['category' => self::CORRECT_CATEGORY], $decorator->toArray()); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @expectedException Radowoj\Yaah\Exception |
||
240 | * @expectedExceptionMessage Invalid category. |
||
241 | */ |
||
242 | public function testExceptionOnIncorrectCategoryFromArray() |
||
243 | { |
||
244 | $auction = $this->getMockBuilder(Auction::class) |
||
245 | ->getMock(); |
||
246 | |||
247 | $decorator = $this->getDecorator($auction); |
||
248 | $decorator->expects($this->any()) |
||
249 | ->method('getIdCategory') |
||
250 | ->willReturn(self::CORRECT_CATEGORY); |
||
251 | |||
252 | $decorator->fromArray([ |
||
253 | 'category' => self::INCORRECT_CATEGORY |
||
254 | ]); |
||
255 | } |
||
256 | |||
257 | |||
258 | /** |
||
259 | * @expectedException Radowoj\Yaah\Exception |
||
260 | * @expectedExceptionMessage Invalid category. |
||
261 | */ |
||
262 | public function testExceptionOnIncorrectCategoryToArray() |
||
263 | { |
||
264 | $auction = $this->getMockBuilder(Auction::class) |
||
265 | ->setMethods(['toArray']) |
||
266 | ->getMock(); |
||
267 | |||
268 | $auction->expects($this->once()) |
||
269 | ->method('toArray') |
||
270 | ->willReturn([ |
||
271 | AuctionFids::FID_CATEGORY => self::INCORRECT_CATEGORY |
||
272 | ]); |
||
273 | |||
274 | $decorator = $this->getDecorator($auction); |
||
275 | $decorator->expects($this->any()) |
||
276 | ->method('getIdCategory') |
||
277 | ->willReturn(self::CORRECT_CATEGORY); |
||
278 | |||
279 | $decorator->toArray(); |
||
280 | } |
||
281 | |||
282 | } |
||
283 |