Total Complexity | 50 |
Total Lines | 840 |
Duplicated Lines | 0 % |
Changes | 5 | ||
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 $context; |
||
16 | private $object; |
||
17 | private $editor = ''; |
||
18 | |||
19 | |||
20 | protected function setUp() : void |
||
21 | { |
||
22 | $this->editor = \TestHelper::context()->editor(); |
||
23 | $this->context = \TestHelper::context(); |
||
24 | $this->object = new \Aimeos\MShop\Order\Manager\Standard( $this->context ); |
||
25 | } |
||
26 | |||
27 | |||
28 | protected function tearDown() : void |
||
29 | { |
||
30 | unset( $this->object ); |
||
31 | } |
||
32 | |||
33 | |||
34 | public function testAggregate() |
||
35 | { |
||
36 | $search = $this->object->filter()->add( ['order.editor' => 'core'] ); |
||
37 | $result = $this->object->aggregate( $search, 'order.channel' ); |
||
38 | |||
39 | $this->assertEquals( 2, count( $result ) ); |
||
40 | $this->assertArrayHasKey( 'web', $result ); |
||
41 | $this->assertEquals( 3, $result->get( 'web' ) ); |
||
|
|||
42 | } |
||
43 | |||
44 | |||
45 | public function testAggregateMultiple() |
||
46 | { |
||
47 | $cols = ['order.channel', 'order.statuspayment']; |
||
48 | $search = $this->object->filter()->add( ['order.editor' => 'core'] )->order( $cols ); |
||
49 | $result = $this->object->aggregate( $search, $cols ); |
||
50 | |||
51 | $this->assertEquals( ['phone' => [6 => 1], 'web' => [5 => 1, 6 => 2]], $result->toArray() ); |
||
52 | } |
||
53 | |||
54 | |||
55 | public function testAggregateAvg() |
||
56 | { |
||
57 | $search = $this->object->filter()->add( ['order.editor' => 'core'] ); |
||
58 | $result = $this->object->aggregate( $search, 'order.cmonth', 'order.price', 'avg' ); |
||
59 | |||
60 | $this->assertEquals( 1, count( $result ) ); |
||
61 | $this->assertEquals( '784.75', round( $result->first(), 2 ) ); |
||
62 | } |
||
63 | |||
64 | |||
65 | public function testAggregateAvgMultiple() |
||
66 | { |
||
67 | $cols = ['order.cmonth', 'order.statuspayment']; |
||
68 | $search = $this->object->filter()->add( ['order.editor' => 'core'] )->order( $cols ); |
||
69 | $result = $this->object->aggregate( $search, $cols, 'order.price', 'avg' ); |
||
70 | |||
71 | $this->assertEquals( 1, count( $result ) ); |
||
72 | $this->assertArrayHasKey( 5, $result->first() ); |
||
73 | $this->assertArrayHasKey( 6, $result->first() ); |
||
74 | $this->assertEquals( '13.50', round( $result->first()[5], 2 ) ); |
||
75 | $this->assertEquals( '1041.83', round( $result->first()[6], 2 ) ); |
||
76 | } |
||
77 | |||
78 | |||
79 | public function testAggregateSum() |
||
80 | { |
||
81 | $search = $this->object->filter()->add( ['order.editor' => 'core'] ); |
||
82 | $result = $this->object->aggregate( $search, 'order.cmonth', 'order.price', 'sum' ); |
||
83 | |||
84 | $this->assertEquals( 1, count( $result ) ); |
||
85 | $this->assertEquals( '3139.00', $result->first() ); |
||
86 | } |
||
87 | |||
88 | |||
89 | public function testAggregateSumMultiple() |
||
90 | { |
||
91 | $cols = ['order.cmonth', 'order.statuspayment']; |
||
92 | $search = $this->object->filter()->add( ['order.editor' => 'core'] )->order( $cols ); |
||
93 | $result = $this->object->aggregate( $search, $cols, 'order.price', 'sum' ); |
||
94 | |||
95 | $this->assertEquals( 1, count( $result ) ); |
||
96 | $this->assertArrayHasKey( 5, $result->first() ); |
||
97 | $this->assertArrayHasKey( 6, $result->first() ); |
||
98 | $this->assertEquals( '13.50', round( $result->first()[5], 2 ) ); |
||
99 | $this->assertEquals( '3125.5', round( $result->first()[6], 2 ) ); |
||
100 | } |
||
101 | |||
102 | |||
103 | public function testAggregateTimes() |
||
104 | { |
||
105 | $search = $this->object->filter()->add( ['order.editor' => 'core'] ); |
||
106 | $search->setSortations( array( $search->sort( '-', 'order.cdate' ) ) ); |
||
107 | $result = $this->object->aggregate( $search, 'order.cmonth' )->toArray(); |
||
108 | |||
109 | $this->assertEquals( 1, count( $result ) ); |
||
110 | $this->assertEquals( 4, reset( $result ) ); |
||
111 | } |
||
112 | |||
113 | |||
114 | public function testAggregateAddress() |
||
115 | { |
||
116 | $search = $this->object->filter()->add( ['order.editor' => 'core'] ); |
||
117 | $result = $this->object->aggregate( $search, 'order.address.countryid' )->toArray(); |
||
118 | |||
119 | $this->assertEquals( 1, count( $result ) ); |
||
120 | $this->assertArrayHasKey( 'DE', $result ); |
||
121 | $this->assertEquals( 4, reset( $result ) ); |
||
122 | } |
||
123 | |||
124 | |||
125 | public function testAggregateAddressMultiple() |
||
126 | { |
||
127 | $cols = ['order.address.countryid', 'order.statuspayment']; |
||
128 | $search = $this->object->filter()->add( ['order.editor' => 'core'] )->order( $cols ); |
||
129 | $result = $this->object->aggregate( $search, $cols )->toArray(); |
||
130 | |||
131 | $this->assertEquals( ['DE' => [5 => 1, 6 => 3]], $result ); |
||
132 | } |
||
133 | |||
134 | |||
135 | public function testAggregateMonth() |
||
143 | } |
||
144 | |||
145 | |||
146 | public function testClear() |
||
149 | } |
||
150 | |||
151 | |||
152 | public function testDeleteItems() |
||
153 | { |
||
154 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [-1] ) ); |
||
155 | } |
||
156 | |||
157 | |||
158 | public function testGetResourceType() |
||
159 | { |
||
160 | $result = $this->object->getResourceType(); |
||
161 | |||
162 | $this->assertContains( 'order', $result ); |
||
163 | $this->assertContains( 'order/status', $result ); |
||
164 | $this->assertContains( 'order/address', $result ); |
||
165 | $this->assertContains( 'order/coupon', $result ); |
||
166 | $this->assertContains( 'order/product', $result ); |
||
167 | $this->assertContains( 'order/product/attribute', $result ); |
||
168 | $this->assertContains( 'order/service', $result ); |
||
169 | $this->assertContains( 'order/service/attribute', $result ); |
||
170 | } |
||
171 | |||
172 | |||
173 | public function testCreateItem() |
||
174 | { |
||
175 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Iface::class, $this->object->create() ); |
||
176 | } |
||
177 | |||
178 | |||
179 | public function testGetItem() |
||
180 | { |
||
181 | $search = $this->object->filter()->slice( 0, 1 ) |
||
182 | ->add( ['order.price' => '672.00', 'order.editor' => $this->editor] ); |
||
183 | |||
184 | $item = $this->object->search( $search )->first( new \RuntimeException( 'No order item found' ) ); |
||
185 | |||
186 | $actual = $this->object->get( $item->getId() ); |
||
187 | |||
188 | $this->assertEquals( $item, $actual ); |
||
189 | $this->assertEquals( '32.00', $item->getPrice()->getCosts() ); |
||
190 | $this->assertEquals( '5.00', $item->getPrice()->getRebate() ); |
||
191 | $this->assertEquals( '112.4034', $item->getPrice()->getTaxValue() ); |
||
192 | } |
||
193 | |||
194 | |||
195 | public function testSaveUpdateDeleteItem() |
||
196 | { |
||
197 | $search = $this->object->filter()->slice( 0, 1 ) |
||
198 | ->add( ['order.channel' => 'phone', 'order.editor' => $this->editor] ); |
||
199 | |||
200 | $item = $this->object->search( $search )->first( new \RuntimeException( 'No order item found' ) ); |
||
201 | |||
202 | $item->setId( null ); |
||
203 | $resultSaved = $this->object->save( $item ); |
||
204 | $itemSaved = $this->object->get( $item->getId() ); |
||
205 | |||
206 | $itemExp = clone $itemSaved; |
||
207 | $itemExp->setChannel( 'web' ); |
||
208 | $resultUpd = $this->object->save( $itemExp ); |
||
209 | $itemUpd = $this->object->get( $itemExp->getId() ); |
||
210 | |||
211 | $this->object->delete( $itemSaved->getId() ); |
||
212 | |||
213 | |||
214 | $itemPrice = $item->getPrice(); |
||
215 | $itemSavedPrice = $itemSaved->getPrice(); |
||
216 | |||
217 | $this->assertTrue( $item->getId() !== null ); |
||
218 | $this->assertEquals( $item->getId(), $itemSaved->getId() ); |
||
219 | $this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() ); |
||
220 | $this->assertEquals( $item->getChannel(), $itemSaved->getChannel() ); |
||
221 | $this->assertEquals( $item->getDatePayment(), $itemSaved->getDatePayment() ); |
||
222 | $this->assertEquals( $item->getDateDelivery(), $itemSaved->getDateDelivery() ); |
||
223 | $this->assertEquals( $item->getStatusPayment(), $itemSaved->getStatusPayment() ); |
||
224 | $this->assertEquals( $item->getStatusDelivery(), $itemSaved->getStatusDelivery() ); |
||
225 | $this->assertEquals( $item->getInvoiceNumber(), $itemSaved->getInvoiceNumber() ); |
||
226 | $this->assertEquals( $item->getRelatedId(), $itemSaved->getRelatedId() ); |
||
227 | $this->assertEquals( $item->getCustomerId(), $itemSaved->getCustomerId() ); |
||
228 | $this->assertEquals( $item->locale()->getLanguageId(), $itemSaved->locale()->getLanguageId() ); |
||
229 | $this->assertEquals( $item->getCustomerReference(), $itemSaved->getCustomerReference() ); |
||
230 | $this->assertEquals( $item->getComment(), $itemSaved->getComment() ); |
||
231 | $this->assertEquals( $item->getSiteCode(), $itemSaved->getSiteCode() ); |
||
232 | $this->assertEquals( $itemPrice->getValue(), $itemSavedPrice->getValue() ); |
||
233 | $this->assertEquals( $itemPrice->getCosts(), $itemSavedPrice->getCosts() ); |
||
234 | $this->assertEquals( $itemPrice->getRebate(), $itemSavedPrice->getRebate() ); |
||
235 | $this->assertEquals( $itemPrice->getTaxValue(), $itemSavedPrice->getTaxValue() ); |
||
236 | $this->assertEquals( $itemPrice->getCurrencyId(), $itemSavedPrice->getCurrencyId() ); |
||
237 | |||
238 | $this->assertEquals( $this->editor, $itemSaved->editor() ); |
||
239 | $this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() ); |
||
240 | $this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() ); |
||
241 | |||
242 | $itemExpPrice = $itemExp->getPrice(); |
||
243 | $itemUpdPrice = $itemUpd->getPrice(); |
||
244 | |||
245 | $this->assertEquals( $itemExp->getId(), $itemUpd->getId() ); |
||
246 | $this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() ); |
||
247 | $this->assertEquals( $itemExp->getChannel(), $itemUpd->getChannel() ); |
||
248 | $this->assertEquals( $itemExp->getDatePayment(), $itemUpd->getDatePayment() ); |
||
249 | $this->assertEquals( $itemExp->getDateDelivery(), $itemUpd->getDateDelivery() ); |
||
250 | $this->assertEquals( $itemExp->getStatusPayment(), $itemUpd->getStatusPayment() ); |
||
251 | $this->assertEquals( $itemExp->getStatusDelivery(), $itemUpd->getStatusDelivery() ); |
||
252 | $this->assertEquals( $itemExp->getInvoiceNumber(), $itemUpd->getInvoiceNumber() ); |
||
253 | $this->assertEquals( $itemExp->getRelatedId(), $itemUpd->getRelatedId() ); |
||
254 | $this->assertEquals( $itemExp->getCustomerId(), $itemUpd->getCustomerId() ); |
||
255 | $this->assertEquals( $itemExp->locale()->getLanguageId(), $itemUpd->locale()->getLanguageId() ); |
||
256 | $this->assertEquals( $itemExp->getCustomerReference(), $itemUpd->getCustomerReference() ); |
||
257 | $this->assertEquals( $itemExp->getComment(), $itemUpd->getComment() ); |
||
258 | $this->assertEquals( $itemExp->getSiteCode(), $itemUpd->getSiteCode() ); |
||
259 | $this->assertEquals( $itemExpPrice->getValue(), $itemUpdPrice->getValue() ); |
||
260 | $this->assertEquals( $itemExpPrice->getCosts(), $itemUpdPrice->getCosts() ); |
||
261 | $this->assertEquals( $itemExpPrice->getRebate(), $itemUpdPrice->getRebate() ); |
||
262 | $this->assertEquals( $itemExpPrice->getTaxValue(), $itemUpdPrice->getTaxValue() ); |
||
263 | $this->assertEquals( $itemExpPrice->getCurrencyId(), $itemUpdPrice->getCurrencyId() ); |
||
264 | |||
265 | $this->assertEquals( $this->editor, $itemUpd->editor() ); |
||
266 | $this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() ); |
||
267 | $this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() ); |
||
268 | |||
269 | $this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved ); |
||
270 | $this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd ); |
||
271 | |||
272 | $this->expectException( \Aimeos\MShop\Exception::class ); |
||
273 | $this->object->get( $itemSaved->getId() ); |
||
274 | } |
||
275 | |||
276 | |||
277 | public function testSaveStatusUpdatePayment() |
||
323 | } |
||
324 | |||
325 | |||
326 | public function testSaveStatusUpdateDelivery() |
||
327 | { |
||
328 | $statusManager = \Aimeos\MShop::create( $this->context, 'order/status' ); |
||
329 | |||
330 | $search = $this->object->filter(); |
||
331 | $conditions = array( |
||
332 | $search->compare( '==', 'order.channel', 'phone' ), |
||
333 | $search->compare( '==', 'order.editor', $this->editor ) |
||
334 | ); |
||
335 | $search->setConditions( $search->and( $conditions ) ); |
||
336 | $results = $this->object->search( $search )->toArray(); |
||
337 | |||
338 | if( ( $item = reset( $results ) ) === false ) { |
||
339 | throw new \RuntimeException( 'No order item found.' ); |
||
340 | } |
||
341 | |||
342 | $item->setId( null ); |
||
343 | $this->object->save( $item ); |
||
344 | |||
345 | |||
346 | $search = $statusManager->filter(); |
||
347 | $search->setConditions( $search->compare( '==', 'order.status.parentid', $item->getId() ) ); |
||
348 | $results = $statusManager->search( $search )->toArray(); |
||
349 | |||
350 | $this->object->delete( $item->getId() ); |
||
351 | |||
352 | $this->assertEquals( 0, count( $results ) ); |
||
353 | |||
354 | |||
355 | $item->setId( null ); |
||
356 | $item->setStatusDelivery( \Aimeos\MShop\Order\Item\Base::STAT_LOST ); |
||
357 | $this->object->save( $item ); |
||
358 | |||
359 | $search = $statusManager->filter(); |
||
360 | $search->setConditions( $search->compare( '==', 'order.status.parentid', $item->getId() ) ); |
||
361 | $results = $statusManager->search( $search )->toArray(); |
||
362 | |||
363 | $this->object->delete( $item->getId() ); |
||
364 | |||
365 | if( ( $statusItem = reset( $results ) ) === false ) { |
||
366 | throw new \RuntimeException( 'No status item found' ); |
||
367 | } |
||
368 | |||
369 | $this->assertEquals( 1, count( $results ) ); |
||
370 | $this->assertEquals( \Aimeos\MShop\Order\Item\Status\Base::STATUS_DELIVERY, $statusItem->getType() ); |
||
371 | $this->assertEquals( \Aimeos\MShop\Order\Item\Base::STAT_LOST, $statusItem->getValue() ); |
||
372 | } |
||
373 | |||
374 | |||
375 | public function testCreateSearch() |
||
376 | { |
||
377 | $this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter() ); |
||
378 | } |
||
379 | |||
380 | |||
381 | public function testCreateSearchDefault() |
||
382 | { |
||
383 | $search = $this->object->filter( true ); |
||
384 | |||
385 | $this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $search ); |
||
386 | $this->assertInstanceOf( \Aimeos\Base\Criteria\Expression\Combine\Iface::class, $search->getConditions() ); |
||
387 | |||
388 | $list = $search->getConditions()->getExpressions(); |
||
389 | $this->assertArrayHasKey( 0, $list ); |
||
390 | $this->assertInstanceOf( \Aimeos\Base\Criteria\Expression\Combine\Iface::class, $list[0] ); |
||
391 | } |
||
392 | |||
393 | |||
394 | public function testCreateSearchSite() |
||
395 | { |
||
396 | $result = $this->object->filter( false, true ); |
||
397 | $this->assertInstanceOf( \Aimeos\Base\Criteria\Expression\Combine\Iface::class, $result->getConditions() ); |
||
398 | } |
||
399 | |||
400 | |||
401 | public function testSearchItems() |
||
539 | } |
||
540 | |||
541 | |||
542 | public function testSearchItemsTotal() |
||
543 | { |
||
558 | } |
||
559 | } |
||
560 | |||
561 | |||
562 | public function testSearchItemsRef() |
||
563 | { |
||
564 | $total = 0; |
||
565 | $search = $this->object->filter()->slice( 0, 1 ); |
||
566 | $conditions = array( |
||
567 | $search->compare( '==', 'order.datepayment', '2008-02-15 12:34:56' ), |
||
568 | $search->compare( '==', 'order.editor', $this->editor ) |
||
569 | ); |
||
570 | $search->setConditions( $search->and( $conditions ) ); |
||
571 | $item = $this->object->search( $search, ['order/address', 'order/coupon', 'order/product', 'order/service'], $total )->first(); |
||
572 | |||
573 | $this->assertEquals( 2, count( $item->getAddresses() ) ); |
||
574 | $this->assertEquals( 2, count( $item->getCoupons() ) ); |
||
575 | $this->assertEquals( 4, count( $item->getProducts() ) ); |
||
576 | $this->assertEquals( 2, count( $item->getServices() ) ); |
||
577 | } |
||
578 | |||
579 | |||
580 | public function testGetSubManager() |
||
581 | { |
||
582 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'address' ) ); |
||
583 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'address', 'Standard' ) ); |
||
584 | |||
585 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'coupon' ) ); |
||
586 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'coupon', 'Standard' ) ); |
||
587 | |||
588 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'product' ) ); |
||
589 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'product', 'Standard' ) ); |
||
590 | |||
591 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'service' ) ); |
||
592 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'service', 'Standard' ) ); |
||
593 | |||
594 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'status' ) ); |
||
595 | $this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'status', 'Standard' ) ); |
||
596 | |||
597 | $this->expectException( \LogicException::class ); |
||
598 | $this->object->getSubManager( 'unknown' ); |
||
599 | } |
||
600 | |||
601 | |||
602 | public function testGetSubManagerInvalidName() |
||
606 | } |
||
607 | |||
608 | |||
609 | public function testSave() |
||
610 | { |
||
611 | $item = $this->getOrderItem(); |
||
612 | $ref = ['order/address', 'order/coupon', 'order/product', 'order/service']; |
||
613 | |||
614 | $basket = $this->getBasket( $item->getId(), $ref, true ); |
||
615 | $this->object->save( $basket ); |
||
616 | |||
617 | $newBasketId = $basket->getId(); |
||
618 | |||
619 | $basket = $this->getBasket( $newBasketId, $ref ); |
||
620 | $this->object->delete( $newBasketId ); |
||
621 | |||
622 | |||
623 | $this->assertEquals( $item->getCustomerId(), $basket->getCustomerId() ); |
||
624 | $this->assertEquals( $basket->locale()->getSiteId(), $basket->getSiteId() ); |
||
625 | |||
626 | $this->assertEquals( 1.50, $basket->getPrice()->getCosts() ); |
||
627 | |||
628 | $pos = 1; |
||
629 | $products = $basket->getProducts(); |
||
630 | $this->assertEquals( 4, count( $products ) ); |
||
631 | |||
632 | foreach( $products as $product ) |
||
633 | { |
||
634 | if( $product->getProductCode() != 'U:MD' ) { |
||
635 | $this->assertGreaterThanOrEqual( 2, count( $product->getAttributeItems() ) ); |
||
636 | } |
||
637 | $this->assertEquals( $pos++, $product->getPosition() ); |
||
638 | } |
||
639 | |||
640 | $this->assertEquals( 2, count( $basket->getAddresses() ) ); |
||
641 | |||
642 | $services = $basket->getServices(); |
||
643 | $this->assertEquals( 2, count( $services ) ); |
||
644 | |||
645 | $attributes = []; |
||
646 | foreach( $services as $list ) |
||
647 | { |
||
648 | foreach( $list as $service ) { |
||
649 | $attributes[$service->getCode()] = $service->getAttributeItems(); |
||
650 | } |
||
651 | } |
||
652 | |||
653 | $this->assertEquals( 9, count( $attributes['unitpaymentcode'] ) ); |
||
654 | $this->assertEquals( 0, count( $attributes['unitdeliverycode'] ) ); |
||
655 | |||
656 | $this->expectException( \Aimeos\MShop\Exception::class ); |
||
657 | $this->object->get( $newBasketId ); |
||
658 | } |
||
659 | |||
660 | |||
661 | public function testSaveExisting() |
||
662 | { |
||
663 | $item = $this->getOrderItem(); |
||
664 | $ref = ['order/address', 'order/coupon', 'order/product', 'order/service']; |
||
665 | |||
666 | $basket = $this->getBasket( $item->getId(), $ref, true ); |
||
667 | $this->object->save( $basket ); |
||
668 | $newBasketId = $basket->getId(); |
||
669 | $this->object->save( $basket ); |
||
670 | $newBasket = $this->getBasket( $newBasketId, $ref ); |
||
671 | |||
672 | $this->object->delete( $newBasketId ); |
||
673 | |||
674 | foreach( $basket->getAddresses() as $type => $list ) |
||
675 | { |
||
676 | $this->assertTrue( map( $list )->getId()->equals( map( $newBasket->getAddress( $type ) )->getId() ) ); |
||
677 | } |
||
678 | |||
679 | $this->assertTrue( $basket->getProducts()->getId()->equals( $newBasket->getProducts()->getId() ) ); |
||
680 | |||
681 | foreach( $basket->getServices() as $type => $list ) |
||
682 | { |
||
683 | $this->assertTrue( map( $list )->getId()->equals( map( $newBasket->getService( $type ) )->getId() ) ); |
||
684 | } |
||
685 | } |
||
686 | |||
687 | |||
688 | public function testSaveBundles() |
||
718 | } |
||
719 | |||
720 | |||
721 | public function testSaveAddress() |
||
722 | { |
||
723 | $item = $this->getOrderItem(); |
||
724 | |||
725 | $basket = $this->getBasket( $item->getId(), ['order/address'], true ); |
||
726 | $this->object->save( $basket ); |
||
727 | |||
728 | $newBasketId = $basket->getId(); |
||
729 | |||
730 | $ref = ['order/address', 'order/coupon', 'order/product', 'order/service']; |
||
731 | $basket = $this->getBasket( $newBasketId, $ref ); |
||
732 | $this->object->delete( $newBasketId ); |
||
733 | |||
734 | $this->assertGreaterThan( 0, count( $basket->getAddresses() ) ); |
||
735 | $this->assertEquals( [], $basket->getProducts()->toArray() ); |
||
736 | $this->assertEquals( [], $basket->getCoupons()->toArray() ); |
||
737 | $this->assertEquals( [], $basket->getServices()->toArray() ); |
||
738 | } |
||
739 | |||
740 | |||
741 | public function testSaveProduct() |
||
742 | { |
||
743 | $item = $this->getOrderItem(); |
||
744 | |||
745 | $basket = $this->getBasket( $item->getId(), ['order/product'], true ); |
||
746 | $this->object->save( $basket ); |
||
747 | |||
748 | $newBasketId = $basket->getId(); |
||
749 | |||
750 | $ref = ['order/address', 'order/coupon', 'order/product', 'order/service']; |
||
751 | $basket = $this->getBasket( $newBasketId, $ref ); |
||
752 | $this->object->delete( $newBasketId ); |
||
753 | |||
754 | $this->assertGreaterThan( 0, count( $basket->getProducts() ) ); |
||
755 | $this->assertEquals( [], $basket->getAddresses()->toArray() ); |
||
756 | $this->assertEquals( [], $basket->getCoupons()->toArray() ); |
||
757 | $this->assertEquals( [], $basket->getServices()->toArray() ); |
||
758 | } |
||
759 | |||
760 | |||
761 | public function testSaveService() |
||
762 | { |
||
763 | $item = $this->getOrderItem(); |
||
764 | |||
765 | $basket = $this->getBasket( $item->getId(), ['order/service'], true ); |
||
766 | $this->object->save( $basket ); |
||
767 | |||
768 | $newBasketId = $basket->getId(); |
||
769 | |||
770 | $ref = ['order/address', 'order/coupon', 'order/product', 'order/service']; |
||
771 | $basket = $this->getBasket( $newBasketId, $ref ); |
||
772 | $this->object->delete( $newBasketId ); |
||
773 | |||
774 | $this->assertGreaterThan( 0, count( $basket->getServices() ) ); |
||
775 | $this->assertEquals( [], $basket->getProducts()->toArray() ); |
||
776 | $this->assertEquals( [], $basket->getAddresses()->toArray() ); |
||
777 | $this->assertEquals( [], $basket->getCoupons()->toArray() ); |
||
778 | } |
||
779 | |||
780 | |||
781 | public function testLoadSaveCoupons() |
||
782 | { |
||
783 | $ref = ['order/address', 'order/product', 'order/service']; |
||
784 | |||
785 | $search = $this->object->filter()->add( ['order.price' => '53.50'] ); |
||
786 | $item = $this->object->search( $search )->first( new \RuntimeException( 'No order found' ) ); |
||
787 | |||
788 | $basket = $this->getBasket( $item->getId(), $ref, true ); |
||
789 | |||
790 | $this->assertEquals( '53.50', $basket->getPrice()->getValue() ); |
||
791 | $this->assertEquals( '1.50', $basket->getPrice()->getCosts() ); |
||
792 | $this->assertEquals( 0, count( $basket->getCoupons() ) ); |
||
793 | |||
794 | $basket->addCoupon( 'CDEF' ); |
||
795 | $basket->addCoupon( '90AB' ); |
||
796 | $this->assertEquals( 2, count( $basket->getCoupons() ) ); |
||
797 | |||
798 | $this->object->save( $basket ); |
||
799 | |||
800 | $ref = ['order/address', 'order/coupon', 'order/product', 'order/service']; |
||
801 | |||
802 | $newBasket = $this->object->get( $basket->getId(), $ref ); |
||
803 | $this->object->delete( $newBasket->getId() ); |
||
804 | |||
805 | $this->assertEquals( '53.50', $newBasket->getPrice()->getValue() ); |
||
806 | $this->assertEquals( '1.50', $newBasket->getPrice()->getCosts() ); |
||
807 | $this->assertEquals( '14.50', $newBasket->getPrice()->getRebate() ); |
||
808 | $this->assertEquals( 2, count( $newBasket->getCoupons() ) ); |
||
809 | } |
||
810 | |||
811 | |||
812 | /** |
||
813 | * Returns the basket object |
||
814 | * |
||
815 | * @return \Aimeos\MShop\Order\Item\Iface Order base item |
||
816 | * @throws \Exception If no found |
||
817 | */ |
||
818 | protected function getBasket( string $id, array $ref = [], $fresh = false ) |
||
819 | { |
||
820 | $basket = $this->object->get( $id, $ref ); |
||
821 | |||
822 | if( $fresh ) |
||
823 | { |
||
824 | $basket->setId( null ); |
||
825 | |||
826 | $basket->getAddresses()->flat( 1 )->setParentId( null )->setId( null ); |
||
827 | $basket->getServices()->flat( 1 )->setParentId( null )->setId( null ); |
||
828 | |||
829 | $basket->getProducts()->merge( $basket->getProducts()->getProducts()->flat( 1 ) ) |
||
830 | ->setParentId( null )->setPosition( null )->setId( null ); |
||
831 | } |
||
832 | |||
833 | return $basket; |
||
834 | } |
||
835 | |||
836 | |||
837 | /** |
||
838 | * Returns an order base item |
||
839 | * |
||
840 | * @return \Aimeos\MShop\Order\Item\Iface Order base item |
||
841 | * @throws \Exception If no found |
||
842 | */ |
||
843 | protected function getOrderItem() |
||
853 | } |
||
854 | } |
||
855 |