Total Complexity | 43 |
Total Lines | 352 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like StandardTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StandardTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class StandardTest extends \PHPUnit\Framework\TestCase |
||
13 | { |
||
14 | private $context; |
||
15 | private $object; |
||
16 | |||
17 | |||
18 | protected function setUp() : void |
||
19 | { |
||
20 | $this->context = \TestHelper::context(); |
||
21 | $this->object = new \Aimeos\Controller\Frontend\Product\Standard( $this->context ); |
||
22 | } |
||
23 | |||
24 | |||
25 | protected function tearDown() : void |
||
26 | { |
||
27 | unset( $this->object, $this->context ); |
||
28 | } |
||
29 | |||
30 | |||
31 | public function testAggregate() |
||
32 | { |
||
33 | $list = $this->object->aggregate( 'index.attribute.id' ); |
||
34 | |||
35 | $this->assertGreaterThan( 0, count( $list ) ); |
||
36 | } |
||
37 | |||
38 | |||
39 | public function testAllOf() |
||
40 | { |
||
41 | $manager = \Aimeos\MShop::create( $this->context, 'attribute' ); |
||
42 | |||
43 | $length = $manager->find( '30', [], 'product', 'length' )->getId(); |
||
44 | $width = $manager->find( '29', [], 'product', 'width' )->getId(); |
||
45 | |||
46 | $this->assertEquals( 2, count( $this->object->allOf( [$length, $width] )->search() ) ); |
||
47 | } |
||
48 | |||
49 | |||
50 | public function testCategory() |
||
51 | { |
||
52 | $manager = \Aimeos\MShop::create( $this->context, 'catalog' ); |
||
53 | $catId = $manager->find( 'cafe' )->getId(); |
||
54 | |||
55 | $this->assertEquals( 2, count( $this->object->category( $catId, 'promotion' )->search() ) ); |
||
56 | } |
||
57 | |||
58 | |||
59 | public function testCategoryTree() |
||
60 | { |
||
61 | $manager = \Aimeos\MShop::create( $this->context, 'catalog' ); |
||
62 | |||
63 | $catId = $manager->find( 'categories' )->getId(); |
||
64 | $grpId = $manager->find( 'group' )->getId(); |
||
65 | |||
66 | $this->object->category( [$catId, $grpId], 'promotion', \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE ); |
||
67 | $this->assertEquals( 3, count( $this->object->search() ) ); |
||
68 | } |
||
69 | |||
70 | |||
71 | public function testCompare() |
||
72 | { |
||
73 | $this->assertEquals( 1, count( $this->object->compare( '==', 'product.type', 'bundle' )->search() ) ); |
||
74 | } |
||
75 | |||
76 | |||
77 | public function testFind() |
||
78 | { |
||
79 | $item = $this->object->uses( ['product'] )->find( 'U:BUNDLE' ); |
||
80 | |||
81 | $this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $item ); |
||
82 | $this->assertEquals( 2, count( $item->getRefItems( 'product' ) ) ); |
||
83 | } |
||
84 | |||
85 | |||
86 | public function testFunction() |
||
87 | { |
||
88 | $str = $this->object->function( 'product:has', ['domain', 'type', 'refid'] ); |
||
89 | $this->assertEquals( 'product:has("domain","type","refid")', $str ); |
||
90 | } |
||
91 | |||
92 | |||
93 | public function testGet() |
||
94 | { |
||
95 | $item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'U:BUNDLE' ); |
||
96 | $item = $this->object->uses( ['product'] )->get( $item->getId() ); |
||
97 | |||
98 | $this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $item ); |
||
99 | $this->assertEquals( 2, count( $item->getRefItems( 'product' ) ) ); |
||
100 | } |
||
101 | |||
102 | |||
103 | public function testHas() |
||
104 | { |
||
105 | $manager = \Aimeos\MShop::create( $this->context, 'attribute' ); |
||
106 | $attrId = $manager->find( '30', [], 'product', 'length' )->getId(); |
||
107 | |||
108 | $this->assertEquals( 1, count( $this->object->has( 'attribute', 'variant', $attrId )->search() ) ); |
||
109 | } |
||
110 | |||
111 | |||
112 | public function testOneOf() |
||
113 | { |
||
114 | $manager = \Aimeos\MShop::create( $this->context, 'attribute' ); |
||
115 | |||
116 | $length = $manager->find( '30', [], 'product', 'length' )->getId(); |
||
117 | $width = $manager->find( '29', [], 'product', 'width' )->getId(); |
||
118 | |||
119 | $this->assertEquals( 4, count( $this->object->oneOf( [$length, $width] )->search() ) ); |
||
120 | } |
||
121 | |||
122 | |||
123 | public function testOneOfList() |
||
124 | { |
||
125 | $manager = \Aimeos\MShop::create( $this->context, 'attribute' ); |
||
126 | |||
127 | $length = $manager->find( '30', [], 'product', 'length' )->getId(); |
||
128 | $width = $manager->find( '30', [], 'product', 'width' )->getId(); |
||
129 | |||
130 | $this->assertEquals( 1, count( $this->object->oneOf( [[$length], [$width]] )->search() ) ); |
||
131 | } |
||
132 | |||
133 | |||
134 | public function testParse() |
||
138 | } |
||
139 | |||
140 | |||
141 | public function testPrice() |
||
142 | { |
||
143 | $this->assertEquals( 5, count( $this->object->price( 20 )->search() ) ); |
||
144 | } |
||
145 | |||
146 | |||
147 | public function testPriceArray() |
||
150 | } |
||
151 | |||
152 | |||
153 | public function testPriceBoth() |
||
154 | { |
||
155 | $this->assertEquals( 4, count( $this->object->price( [15, 20] )->search() ) ); |
||
156 | } |
||
157 | |||
158 | |||
159 | public function testProduct() |
||
160 | { |
||
161 | $manager = \Aimeos\MShop::create( $this->context, 'product' ); |
||
162 | |||
163 | $cncId = $manager->find( 'CNC' )->getId(); |
||
164 | $cneId = $manager->find( 'CNE' )->getId(); |
||
165 | |||
166 | $this->assertEquals( 2, count( $this->object->product( [$cncId, $cneId] )->search() ) ); |
||
167 | } |
||
168 | |||
169 | |||
170 | public function testProperty() |
||
173 | } |
||
174 | |||
175 | |||
176 | public function testRadius() |
||
177 | { |
||
178 | $this->assertEquals( 2, count( $this->object->radius( [52.5, 10], 115 )->search() ) ); |
||
179 | } |
||
180 | |||
181 | |||
182 | public function testResolve() |
||
183 | { |
||
184 | $item = $this->object->resolve( 'cafe-noire-cappuccino' ); |
||
185 | |||
186 | $this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $item ); |
||
187 | $this->assertEquals( 'Cafe Noire Cappuccino', $item->getLabel() ); |
||
188 | } |
||
189 | |||
190 | |||
191 | public function testResolveLanguage() |
||
192 | { |
||
193 | $this->context->locale()->setLanguageId( 'en' ); |
||
194 | $item = $this->object->resolve( 'cafe_noire_cappuccino' ); |
||
195 | |||
196 | $this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $item ); |
||
197 | $this->assertEquals( 'Cafe Noire Cappuccino', $item->getLabel() ); |
||
198 | } |
||
199 | |||
200 | |||
201 | public function testSearch() |
||
202 | { |
||
203 | $total = 0; |
||
204 | $items = $this->object->uses( ['price'] )->sort( 'code' )->search( $total ); |
||
205 | |||
206 | $this->assertEquals( 8, count( $items ) ); |
||
207 | $this->assertEquals( 8, $total ); |
||
208 | $this->assertEquals( 2, count( $items->first()->getRefItems( 'price' ) ) ); |
||
209 | } |
||
210 | |||
211 | |||
212 | public function testSearchAll() |
||
213 | { |
||
214 | $total = 0; |
||
215 | $this->context->config()->set( 'controller/frontend/product/show-all', true ); |
||
216 | $object = new \Aimeos\Controller\Frontend\Product\Standard( $this->context ); |
||
217 | |||
218 | $items = $object->search( $total ); |
||
219 | |||
220 | $this->assertEquals( 22, count( $items ) ); |
||
221 | $this->assertEquals( 22, $total ); |
||
222 | } |
||
223 | |||
224 | |||
225 | public function testSlice() |
||
226 | { |
||
227 | $this->assertEquals( 2, count( $this->object->slice( 0, 2 )->search() ) ); |
||
228 | } |
||
229 | |||
230 | |||
231 | public function testSort() |
||
232 | { |
||
233 | $this->assertEquals( 8, count( $this->object->sort( '+relevance' )->search() ) ); |
||
234 | } |
||
235 | |||
236 | |||
237 | public function testSortGeneric() |
||
238 | { |
||
239 | $this->assertEquals( 8, count( $this->object->sort( '-product.status' )->search() ) ); |
||
240 | } |
||
241 | |||
242 | |||
243 | public function testSortMultiple() |
||
244 | { |
||
245 | $this->assertEquals( 8, count( $this->object->sort( 'sort:index.text:relevance("de","test(\"\")"),product.id' )->search() ) ); |
||
246 | } |
||
247 | |||
248 | |||
249 | public function testSortCode() |
||
250 | { |
||
251 | $result = $this->object->sort( 'code' )->search(); |
||
252 | $this->assertEquals( 'ABCD', $result->first()->getCode() ); |
||
253 | } |
||
254 | |||
255 | |||
256 | public function testSortCodeDesc() |
||
257 | { |
||
258 | $result = $this->object->sort( '-code' )->search(); |
||
259 | $this->assertStringStartsWith( 'U:', $result->first()->getCode() ); |
||
260 | } |
||
261 | |||
262 | |||
263 | public function testSortCtime() |
||
264 | { |
||
265 | $this->assertEquals( 8, count( $this->object->sort( 'ctime' )->search() ) ); |
||
266 | } |
||
267 | |||
268 | |||
269 | public function testSortCtimeDesc() |
||
270 | { |
||
271 | $this->assertEquals( 8, count( $this->object->sort( '-ctime' )->search() ) ); |
||
272 | } |
||
273 | |||
274 | |||
275 | public function testSortName() |
||
276 | { |
||
277 | $result = $this->object->uses( ['text'] )->sort( 'name' )->search(); |
||
278 | $this->assertEquals( 'Cafe Noire Cappuccino', $result->first()->getName() ); |
||
279 | } |
||
280 | |||
281 | |||
282 | public function testSortNameDesc() |
||
283 | { |
||
284 | $result = $this->object->uses( ['text'] )->sort( '-name' )->search(); |
||
285 | $this->assertEquals( 'Unterproduct 1', $result->first()->getName() ); |
||
286 | } |
||
287 | |||
288 | |||
289 | public function testSortPrice() |
||
290 | { |
||
291 | $result = $this->object->uses( ['price'] )->sort( 'price' )->search(); |
||
292 | $prices = $result->first()->getRefItems( 'price', 'default', 'default' ); |
||
293 | |||
294 | $this->assertEquals( '12.00', $prices->first()->getValue() ); |
||
295 | } |
||
296 | |||
297 | |||
298 | public function testSortPriceDesc() |
||
299 | { |
||
300 | $result = $this->object->uses( ['price'] )->sort( '-price' )->search(); |
||
301 | $prices = $result->first()->getRefItems( 'price', 'default', 'default' ); |
||
302 | |||
303 | $this->assertEquals( '600.00', $prices->first()->getValue() ); |
||
304 | } |
||
305 | |||
306 | |||
307 | public function testSortRating() |
||
308 | { |
||
309 | $result = $this->object->sort( '-rating' )->search(); |
||
310 | $this->assertGreaterThan( 0, $result->count() ); |
||
311 | } |
||
312 | |||
313 | |||
314 | public function testSortRelevanceCategory() |
||
315 | { |
||
316 | $manager = \Aimeos\MShop::create( $this->context, 'catalog' ); |
||
317 | $catId = $manager->find( 'new' )->getId(); |
||
318 | |||
319 | $result = $this->object->category( $catId )->sort( 'relevance' )->search(); |
||
320 | |||
321 | $this->assertEquals( 3, count( $result ) ); |
||
322 | $this->assertEquals( 'CNE', $result->first()->getCode() ); |
||
323 | $this->assertEquals( 'U:BUNDLE', $result->last()->getCode() ); |
||
324 | } |
||
325 | |||
326 | |||
327 | public function testSortRelevanceSupplier() |
||
328 | { |
||
329 | $manager = \Aimeos\MShop::create( $this->context, 'supplier' ); |
||
330 | $supId = $manager->find( 'unitSupplier001' )->getId(); |
||
331 | |||
332 | $result = $this->object->supplier( $supId )->sort( 'relevance' )->search(); |
||
333 | |||
334 | $this->assertEquals( 2, count( $result ) ); |
||
335 | $this->assertEquals( 'CNC', $result->first()->getCode() ); |
||
336 | $this->assertEquals( 'CNE', $result->last()->getCode() ); |
||
337 | } |
||
338 | |||
339 | |||
340 | public function testSupplier() |
||
341 | { |
||
342 | $manager = \Aimeos\MShop::create( $this->context, 'supplier' ); |
||
343 | $supId = $manager->find( 'unitSupplier001' )->getId(); |
||
344 | |||
345 | $this->assertEquals( 2, count( $this->object->supplier( $supId )->search() ) ); |
||
346 | } |
||
347 | |||
348 | |||
349 | public function testText() |
||
352 | } |
||
353 | |||
354 | |||
355 | public function testTextCode() |
||
356 | { |
||
357 | $this->assertEquals( 2, count( $this->object->text( 'CNE' )->search() ) ); |
||
358 | } |
||
359 | |||
360 | |||
361 | public function testUses() |
||
364 | } |
||
365 | } |
||
366 |