Total Complexity | 44 |
Total Lines | 552 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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 |
||
13 | class StandardTest extends \PHPUnit\Framework\TestCase |
||
14 | { |
||
15 | private $object; |
||
16 | private $values; |
||
17 | private $price; |
||
18 | private $attribute = []; |
||
19 | |||
20 | |||
21 | protected function setUp() : void |
||
22 | { |
||
23 | $this->price = \Aimeos\MShop\Price\Manager\Factory::create( \TestHelperMShop::getContext() )->createItem(); |
||
24 | |||
25 | $attrValues = array( |
||
26 | 'order.base.service.attribute.id' => 3, |
||
27 | 'order.base.service.attribute.siteid' => 99, |
||
28 | 'order.base.service.attribute.parentid' => 42, |
||
29 | 'order.base.service.attribute.name' => 'UnitName', |
||
30 | 'order.base.service.attribute.type' => 'default', |
||
31 | 'order.base.service.attribute.code' => 'UnitCode', |
||
32 | 'order.base.service.attribute.value' => 'UnitValue', |
||
33 | 'order.base.service.attribute.mtime' => '2020-12-31 23:59:59', |
||
34 | 'order.base.service.attribute.ctime' => '2011-01-01 00:00:01', |
||
35 | 'order.base.service.attribute.editor' => 'unitTestUser' |
||
36 | ); |
||
37 | |||
38 | $this->attribute = array( new \Aimeos\MShop\Order\Item\Base\Service\Attribute\Standard( $attrValues ) ); |
||
39 | |||
40 | $this->values = array( |
||
41 | 'order.base.service.id' => 1, |
||
42 | 'order.base.service.siteid' => 99, |
||
43 | 'order.base.service.serviceid' => 'ServiceID', |
||
44 | 'order.base.service.baseid' => 42, |
||
45 | 'order.base.service.code' => 'UnitCode', |
||
46 | 'order.base.service.name' => 'UnitName', |
||
47 | 'order.base.service.mediaurl' => 'Url for test', |
||
48 | 'order.base.service.position' => 1, |
||
49 | 'order.base.service.type' => 'payment', |
||
50 | 'order.base.service.mtime' => '2012-01-01 00:00:01', |
||
51 | 'order.base.service.ctime' => '2011-01-01 00:00:01', |
||
52 | 'order.base.service.editor' => 'unitTestUser' |
||
53 | ); |
||
54 | |||
55 | $servItem = \Aimeos\MShop::create( \TestHelperMShop::getContext(), 'service' )->createItem(); |
||
56 | $this->object = new \Aimeos\MShop\Order\Item\Base\Service\Standard( |
||
57 | $this->price, $this->values, $this->attribute, $servItem |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | |||
62 | protected function tearDown() : void |
||
65 | } |
||
66 | |||
67 | |||
68 | public function testGetServiceItem() |
||
69 | { |
||
70 | $this->assertInstanceOf( \Aimeos\MShop\Service\Item\Iface::class, $this->object->getServiceItem() ); |
||
71 | $this->assertNull( ( new \Aimeos\MShop\Order\Item\Base\Service\Standard( $this->price ) )->getServiceItem() ); |
||
72 | } |
||
73 | |||
74 | |||
75 | public function testGetId() |
||
76 | { |
||
77 | $this->assertEquals( 1, $this->object->getId() ); |
||
78 | } |
||
79 | |||
80 | |||
81 | public function testSetId() |
||
82 | { |
||
83 | $return = $this->object->setId( null ); |
||
84 | |||
85 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
86 | $this->assertEquals( null, $this->object->getId() ); |
||
87 | $this->assertTrue( $this->object->isModified() ); |
||
88 | |||
89 | $return = $this->object->setId( 5 ); |
||
90 | |||
91 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
92 | $this->assertEquals( 5, $this->object->getId() ); |
||
93 | $this->assertFalse( $this->object->isModified() ); |
||
94 | } |
||
95 | |||
96 | |||
97 | public function testGetSiteId() |
||
98 | { |
||
99 | $this->assertEquals( 99, $this->object->getSiteId() ); |
||
100 | } |
||
101 | |||
102 | |||
103 | public function testSetSiteId() |
||
104 | { |
||
105 | $this->object->setSiteId( 100 ); |
||
106 | $this->assertEquals( 100, $this->object->getSiteId() ); |
||
107 | $this->assertTrue( $this->object->isModified() ); |
||
108 | } |
||
109 | |||
110 | |||
111 | public function testGetBaseId() |
||
112 | { |
||
113 | $this->assertEquals( 42, $this->object->getBaseId() ); |
||
114 | } |
||
115 | |||
116 | |||
117 | public function testSetBaseId() |
||
118 | { |
||
119 | $return = $this->object->setBaseId( 111 ); |
||
120 | |||
121 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
122 | $this->assertEquals( 111, $this->object->getBaseId() ); |
||
123 | $this->assertTrue( $this->object->isModified() ); |
||
124 | } |
||
125 | |||
126 | |||
127 | public function testSetBaseIdReset() |
||
128 | { |
||
129 | $return = $this->object->setBaseId( null ); |
||
130 | |||
131 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
132 | $this->assertEquals( null, $this->object->getBaseId() ); |
||
133 | $this->assertTrue( $this->object->isModified() ); |
||
134 | } |
||
135 | |||
136 | |||
137 | public function testGetServiceId() |
||
140 | } |
||
141 | |||
142 | |||
143 | public function testSetServiceId() |
||
144 | { |
||
145 | $return = $this->object->setServiceId( 'testServiceID' ); |
||
146 | |||
147 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
148 | $this->assertEquals( 'testServiceID', $this->object->getServiceId() ); |
||
149 | $this->assertTrue( $this->object->isModified() ); |
||
150 | } |
||
151 | |||
152 | |||
153 | public function testGetCode() |
||
156 | } |
||
157 | |||
158 | |||
159 | public function testSetCode() |
||
160 | { |
||
161 | $return = $this->object->setCode( 'testCode' ); |
||
162 | |||
163 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
164 | $this->assertEquals( 'testCode', $this->object->getCode() ); |
||
165 | $this->assertTrue( $this->object->isModified() ); |
||
166 | } |
||
167 | |||
168 | |||
169 | public function testGetName() |
||
170 | { |
||
171 | $this->assertEquals( 'UnitName', $this->object->getName() ); |
||
172 | } |
||
173 | |||
174 | |||
175 | public function testSetName() |
||
176 | { |
||
177 | $return = $this->object->setName( 'testName' ); |
||
178 | |||
179 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
180 | $this->assertEquals( 'testName', $this->object->getName() ); |
||
181 | $this->assertTrue( $this->object->isModified() ); |
||
182 | } |
||
183 | |||
184 | |||
185 | public function testGetMediaUrl() |
||
186 | { |
||
187 | $this->assertEquals( 'Url for test', $this->object->getMediaUrl() ); |
||
188 | } |
||
189 | |||
190 | |||
191 | public function testSetMediaUrl() |
||
192 | { |
||
193 | $return = $this->object->setMediaUrl( 'testUrl' ); |
||
194 | |||
195 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
196 | $this->assertEquals( 'testUrl', $this->object->getMediaUrl() ); |
||
197 | $this->assertTrue( $this->object->isModified() ); |
||
198 | } |
||
199 | |||
200 | |||
201 | public function testGetType() |
||
202 | { |
||
203 | $this->assertEquals( 'payment', $this->object->getType() ); |
||
204 | } |
||
205 | |||
206 | |||
207 | public function testSetType() |
||
208 | { |
||
209 | $return = $this->object->setType( 'delivery' ); |
||
210 | |||
211 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
212 | $this->assertEquals( 'delivery', $this->object->getType() ); |
||
213 | $this->assertTrue( $this->object->isModified() ); |
||
214 | } |
||
215 | |||
216 | |||
217 | public function testGetPrice() |
||
218 | { |
||
219 | $this->assertSame( $this->price, $this->object->getPrice() ); |
||
220 | } |
||
221 | |||
222 | |||
223 | public function testSetPrice() |
||
231 | } |
||
232 | |||
233 | |||
234 | public function testGetPosition() |
||
235 | { |
||
236 | $this->assertEquals( 1, $this->object->getPosition() ); |
||
237 | } |
||
238 | |||
239 | |||
240 | public function testSetPosition() |
||
241 | { |
||
242 | $return = $this->object->setPosition( 2 ); |
||
243 | |||
244 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
245 | $this->assertEquals( 2, $this->object->getPosition() ); |
||
246 | $this->assertTrue( $this->object->isModified() ); |
||
247 | } |
||
248 | |||
249 | |||
250 | public function testSetPositionReset() |
||
257 | } |
||
258 | |||
259 | |||
260 | public function testSetPositionInvalid() |
||
264 | } |
||
265 | |||
266 | |||
267 | public function testGetAttribute() |
||
268 | { |
||
269 | $manager = \Aimeos\MShop\Order\Manager\Factory::create( \TestHelperMShop::getContext() ); |
||
270 | $attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' ); |
||
271 | |||
304 | } |
||
305 | |||
306 | |||
307 | public function testGetAttributeList() |
||
308 | { |
||
309 | $manager = \Aimeos\MShop\Order\Manager\Factory::create( \TestHelperMShop::getContext() ); |
||
310 | $attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' ); |
||
311 | |||
312 | $attrItem001 = $attManager->createItem(); |
||
313 | $attrItem001->setAttributeId( '1' ); |
||
314 | $attrItem001->setCode( 'code_001' ); |
||
315 | $attrItem001->setType( 'test_001' ); |
||
316 | $attrItem001->setValue( 'value_001' ); |
||
317 | |||
318 | $attrItem002 = $attManager->createItem(); |
||
319 | $attrItem002->setAttributeId( '2' ); |
||
320 | $attrItem002->setCode( 'code_001' ); |
||
321 | $attrItem002->setType( 'test_001' ); |
||
322 | $attrItem002->setValue( 'value_002' ); |
||
323 | |||
324 | $this->object->setAttributeItems( array( $attrItem001, $attrItem002 ) ); |
||
325 | |||
326 | $result = $this->object->getAttribute( 'code_001', 'test_001' ); |
||
327 | $this->assertEquals( ['value_001', 'value_002'], $result ); |
||
328 | } |
||
329 | |||
330 | |||
331 | public function testGetAttributeItem() |
||
332 | { |
||
333 | $manager = \Aimeos\MShop\Order\Manager\Factory::create( \TestHelperMShop::getContext() ); |
||
334 | $attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' ); |
||
335 | |||
336 | $attrItem001 = $attManager->createItem(); |
||
337 | $attrItem001->setAttributeId( '1' ); |
||
338 | $attrItem001->setCode( 'code_001' ); |
||
339 | $attrItem001->setValue( 'value_001' ); |
||
340 | |||
341 | $attrItem002 = $attManager->createItem(); |
||
342 | $attrItem002->setAttributeId( '2' ); |
||
343 | $attrItem002->setCode( 'code_002' ); |
||
344 | $attrItem002->setType( 'test_002' ); |
||
345 | $attrItem002->setValue( 'value_002' ); |
||
346 | |||
347 | $this->object->setAttributeItems( array( $attrItem001, $attrItem002 ) ); |
||
348 | |||
349 | $result = $this->object->getAttributeItem( 'code_001' ); |
||
350 | $this->assertEquals( 'value_001', $result->getValue() ); |
||
351 | |||
352 | $result = $this->object->getAttributeItem( 'code_002', 'test_002' ); |
||
353 | $this->assertEquals( 'value_002', $result->getValue() ); |
||
354 | |||
355 | $result = $this->object->getAttributeItem( 'code_002' ); |
||
356 | $this->assertEquals( null, $result ); |
||
357 | |||
358 | $result = $this->object->getAttributeItem( 'code_003' ); |
||
359 | $this->assertEquals( null, $result ); |
||
360 | |||
361 | $this->object->setAttributeItems( [] ); |
||
362 | |||
363 | $result = $this->object->getAttributeItem( 'code_001' ); |
||
364 | $this->assertEquals( null, $result ); |
||
365 | } |
||
366 | |||
367 | |||
368 | public function testGetAttributeItemList() |
||
369 | { |
||
370 | $manager = \Aimeos\MShop\Order\Manager\Factory::create( \TestHelperMShop::getContext() ); |
||
371 | $attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' ); |
||
372 | |||
373 | $attrItem001 = $attManager->createItem(); |
||
374 | $attrItem001->setAttributeId( '1' ); |
||
375 | $attrItem001->setCode( 'code_001' ); |
||
376 | $attrItem001->setType( 'test_001' ); |
||
377 | $attrItem001->setValue( 'value_001' ); |
||
378 | |||
379 | $attrItem002 = $attManager->createItem(); |
||
380 | $attrItem002->setAttributeId( '2' ); |
||
381 | $attrItem002->setCode( 'code_001' ); |
||
382 | $attrItem002->setType( 'test_001' ); |
||
383 | $attrItem002->setValue( 'value_002' ); |
||
384 | |||
385 | $this->object->setAttributeItems( array( $attrItem001, $attrItem002 ) ); |
||
386 | |||
387 | $result = $this->object->getAttributeItem( 'code_001', 'test_001' ); |
||
388 | $this->assertEquals( 2, count( $result ) ); |
||
389 | } |
||
390 | |||
391 | |||
392 | public function testGetAttributeItems() |
||
393 | { |
||
394 | $this->assertEquals( $this->attribute, $this->object->getAttributeItems()->toArray() ); |
||
395 | } |
||
396 | |||
397 | |||
398 | public function testGetAttributeItemsByType() |
||
399 | { |
||
400 | $this->assertEquals( $this->attribute, $this->object->getAttributeItems( 'default' )->toArray() ); |
||
401 | } |
||
402 | |||
403 | |||
404 | public function testGetAttributeItemsInvalidType() |
||
405 | { |
||
406 | $this->assertEquals( [], $this->object->getAttributeItems( 'invalid' )->toArray() ); |
||
407 | } |
||
408 | |||
409 | |||
410 | public function testSetAttributeItem() |
||
411 | { |
||
412 | $manager = \Aimeos\MShop\Order\Manager\Factory::create( \TestHelperMShop::getContext() ); |
||
413 | $attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' ); |
||
414 | |||
415 | $item = $attManager->createItem(); |
||
416 | $item->setAttributeId( '1' ); |
||
417 | $item->setCode( 'test_code' ); |
||
418 | $item->setType( 'test_type' ); |
||
419 | $item->setValue( 'test_value' ); |
||
420 | |||
421 | $return = $this->object->setAttributeItem( $item ); |
||
422 | |||
423 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
424 | $this->assertEquals( 'test_value', $this->object->getAttributeItem( 'test_code', 'test_type' )->getValue() ); |
||
425 | $this->assertTrue( $this->object->isModified() ); |
||
426 | |||
427 | $item = $attManager->createItem(); |
||
428 | $item->setAttributeId( '1' ); |
||
429 | $item->setCode( 'test_code' ); |
||
430 | $item->setType( 'test_type' ); |
||
431 | $item->setValue( 'test_value2' ); |
||
432 | |||
433 | $return = $this->object->setAttributeItem( $item ); |
||
434 | |||
435 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return ); |
||
436 | $this->assertEquals( 'test_value2', $this->object->getAttributeItem( 'test_code', 'test_type' )->getValue() ); |
||
437 | $this->assertTrue( $this->object->isModified() ); |
||
438 | } |
||
439 | |||
440 | |||
441 | public function testSetAttributeItems() |
||
456 | } |
||
457 | |||
458 | |||
459 | public function testGetTimeModified() |
||
460 | { |
||
461 | $this->assertEquals( '2012-01-01 00:00:01', $this->object->getTimeModified() ); |
||
462 | } |
||
463 | |||
464 | |||
465 | public function testGetTimeCreated() |
||
466 | { |
||
467 | $this->assertEquals( '2011-01-01 00:00:01', $this->object->getTimeCreated() ); |
||
468 | } |
||
469 | |||
470 | |||
471 | public function testGetEditor() |
||
472 | { |
||
473 | $this->assertEquals( 'unitTestUser', $this->object->getEditor() ); |
||
474 | } |
||
475 | |||
476 | |||
477 | public function testFromArray() |
||
478 | { |
||
479 | $item = new \Aimeos\MShop\Order\Item\Base\Service\Standard( new \Aimeos\MShop\Price\Item\Standard() ); |
||
480 | |||
481 | $list = $entries = array( |
||
482 | 'order.base.service.id' => 1, |
||
483 | 'order.base.service.baseid' => 2, |
||
484 | 'order.base.service.serviceid' => 3, |
||
485 | 'order.base.service.position' => 4, |
||
486 | 'order.base.service.code' => 'test', |
||
487 | 'order.base.service.name' => 'test item', |
||
488 | 'order.base.service.type' => 'delivery', |
||
489 | ); |
||
490 | |||
491 | $item = $item->fromArray( $entries, true ); |
||
492 | |||
493 | $this->assertEquals( [], $entries ); |
||
494 | $this->assertEquals( $list['order.base.service.id'], $item->getId() ); |
||
495 | $this->assertEquals( $list['order.base.service.baseid'], $item->getBaseId() ); |
||
496 | $this->assertEquals( $list['order.base.service.serviceid'], $item->getServiceId() ); |
||
497 | $this->assertEquals( $list['order.base.service.position'], $item->getPosition() ); |
||
498 | $this->assertEquals( $list['order.base.service.code'], $item->getCode() ); |
||
499 | $this->assertEquals( $list['order.base.service.name'], $item->getName() ); |
||
500 | $this->assertEquals( $list['order.base.service.type'], $item->getType() ); |
||
501 | } |
||
502 | |||
503 | |||
504 | public function testToArray() |
||
505 | { |
||
506 | $arrayObject = $this->object->toArray( true ); |
||
507 | |||
508 | $this->assertEquals( count( $this->values ) + 5, count( $arrayObject ) ); |
||
509 | |||
510 | $this->assertEquals( $this->object->getId(), $arrayObject['order.base.service.id'] ); |
||
511 | $this->assertEquals( $this->object->getBaseId(), $arrayObject['order.base.service.baseid'] ); |
||
512 | $this->assertEquals( $this->object->getServiceId(), $arrayObject['order.base.service.serviceid'] ); |
||
513 | $this->assertEquals( $this->object->getPosition(), $arrayObject['order.base.service.position'] ); |
||
514 | $this->assertEquals( $this->object->getCode(), $arrayObject['order.base.service.code'] ); |
||
515 | $this->assertEquals( $this->object->getName(), $arrayObject['order.base.service.name'] ); |
||
516 | $this->assertEquals( $this->object->getType(), $arrayObject['order.base.service.type'] ); |
||
517 | $this->assertEquals( $this->object->getTimeModified(), $arrayObject['order.base.service.mtime'] ); |
||
518 | $this->assertEquals( $this->object->getTimeCreated(), $arrayObject['order.base.service.ctime'] ); |
||
519 | $this->assertEquals( $this->object->getTimeModified(), $arrayObject['order.base.service.mtime'] ); |
||
520 | $this->assertEquals( $this->object->getEditor(), $arrayObject['order.base.service.editor'] ); |
||
521 | |||
522 | $price = $this->object->getPrice(); |
||
523 | $this->assertEquals( $price->getValue(), $arrayObject['order.base.service.price'] ); |
||
524 | $this->assertEquals( $price->getCosts(), $arrayObject['order.base.service.costs'] ); |
||
525 | $this->assertEquals( $price->getRebate(), $arrayObject['order.base.service.rebate'] ); |
||
526 | $this->assertEquals( $price->getTaxRate(), $arrayObject['order.base.service.taxrate'] ); |
||
527 | $this->assertEquals( $price->getTaxRates(), $arrayObject['order.base.service.taxrates'] ); |
||
528 | } |
||
529 | |||
530 | public function testIsModified() |
||
531 | { |
||
532 | $this->assertFalse( $this->object->isModified() ); |
||
533 | } |
||
534 | |||
535 | |||
536 | public function testGetResourceType() |
||
539 | } |
||
540 | |||
541 | |||
542 | public function testCopyFrom() |
||
543 | { |
||
544 | $serviceCopy = new \Aimeos\MShop\Order\Item\Base\Service\Standard( $this->price ); |
||
545 | |||
546 | $manager = \Aimeos\MShop\Service\Manager\Factory::create( \TestHelperMShop::getContext() ); |
||
547 | |||
565 | } |
||
566 | } |
||
567 |