Passed
Push — master ( fa51cc...048b45 )
by Aimeos
04:35
created

StandardTest::testStoreNone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 */
8
9
10
namespace Aimeos\MShop\Order\Manager\Base;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $context;
17
	private $editor = '';
18
19
20
	protected function setUp() : void
21
	{
22
		$this->context = \TestHelperMShop::context();
23
		$this->editor = $this->context->editor();
24
25
		$this->object = new \Aimeos\MShop\Order\Manager\Base\Standard( $this->context );
26
	}
27
28
29
	protected function tearDown() : void
30
	{
31
		unset( $this->object );
32
	}
33
34
35
	public function testAggregate()
36
	{
37
		$search = $this->object->filter();
38
		$search->setConditions( $search->compare( '==', 'order.base.editor', 'core:lib/mshoplib' ) );
39
		$result = $this->object->aggregate( $search, 'order.base.rebate' )->toArray();
40
41
		$this->assertEquals( 4, count( $result ) );
42
		$this->assertArrayHasKey( '5.00', $result );
43
		$this->assertEquals( 1, $result['5.00'] );
44
	}
45
46
47
	public function testAggregateAvg()
48
	{
49
		$search = $this->object->filter();
50
		$search->setConditions( $search->compare( '==', 'order.base.editor', 'core:lib/mshoplib' ) );
51
		$result = $this->object->aggregate( $search, 'order.base.address.email', 'order.base.price', 'avg' )->toArray();
52
53
		$this->assertEquals( 1, count( $result ) );
54
		$this->assertArrayHasKey( '[email protected]', $result );
55
		$this->assertEquals( '784.75', round( $result['[email protected]'], 2 ) );
56
	}
57
58
59
	public function testAggregateSum()
60
	{
61
		$search = $this->object->filter();
62
		$search->setConditions( $search->compare( '==', 'order.base.editor', 'core:lib/mshoplib' ) );
63
		$result = $this->object->aggregate( $search, 'order.base.address.email', 'order.base.price', 'sum' )->toArray();
64
65
		$this->assertEquals( 1, count( $result ) );
66
		$this->assertArrayHasKey( '[email protected]', $result );
67
		$this->assertEquals( '3139.00', $result['[email protected]'] );
68
	}
69
70
71
	public function testClear()
72
	{
73
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( [-1] ) );
74
	}
75
76
77
	public function testDeleteItems()
78
	{
79
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [-1] ) );
80
	}
81
82
83
	public function testGetResourceType()
84
	{
85
		$result = $this->object->getResourceType();
86
87
		$this->assertContains( 'order/base', $result );
88
		$this->assertContains( 'order/base/address', $result );
89
		$this->assertContains( 'order/base/coupon', $result );
90
		$this->assertContains( 'order/base/product', $result );
91
		$this->assertContains( 'order/base/product/attribute', $result );
92
		$this->assertContains( 'order/base/service', $result );
93
		$this->assertContains( 'order/base/service/attribute', $result );
94
	}
95
96
97
	public function testCreateItem()
98
	{
99
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $this->object->create() );
100
	}
101
102
103
	public function testGetItem()
104
	{
105
		$search = $this->object->filter()->slice( 0, 1 );
106
		$search->setConditions( $search->compare( '==', 'order.base.price', '672.00' ) );
107
		$results = $this->object->search( $search )->toArray();
108
109
		if( ( $expected = reset( $results ) ) === false ) {
110
			throw new \Aimeos\MShop\Order\Exception( 'No order base item found' );
111
		}
112
113
		$item = $this->object->get( $expected->getId() );
114
115
		$this->assertEquals( $expected, $item );
116
		$this->assertEquals( '32.00', $item->getPrice()->getCosts() );
117
		$this->assertEquals( '5.00', $item->getPrice()->getRebate() );
118
		$this->assertEquals( '112.4034', $item->getPrice()->getTaxValue() );
119
	}
120
121
122
	public function testSaveUpdateDeleteItem()
123
	{
124
		$orderProductManager = \Aimeos\MShop\Order\Manager\Factory::create( $this->context )
125
			->getSubManager( 'base' )->getSubManager( 'product' );
126
127
		$search = $this->object->filter()->add( ['order.base.costs' => '1.50', 'order.base.editor' => $this->editor] );
128
		$item = $this->object->search( $search, ['order/base/product'] )
129
			->first( new \RuntimeException( 'No order base item found' ) );
130
131
132
		$item->setId( null );
133
		$item->setComment( 'Unittest1' );
134
		$resultSaved = $this->object->save( $item );
135
136
		$product = $item->getProducts()->first()->setBaseId( $item->getId() )->setId( null );
137
		$orderProductManager->save( $product );
138
139
		$itemSaved = $this->object->get( $item->getId() );
140
		$itemPrice = $item->getPrice();
141
		$itemSavedPrice = $item->getPrice();
142
143
144
		$itemExp = clone $itemSaved;
145
		$itemExp->setComment( 'Unittest2' );
146
		$itemExp->setCustomerId( 'unittest1' );
147
		$resultUpd = $this->object->save( $itemExp );
148
		$itemUpd = $this->object->get( $itemExp->getId() );
149
		$itemExpPrice = $itemExp->getPrice();
150
		$itemUpdPrice = $itemUpd->getPrice();
151
152
153
		$this->object->delete( $itemSaved->getId() );
154
155
156
		$this->assertTrue( $item->getId() !== null );
157
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
158
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
159
		$this->assertEquals( $item->getCustomerId(), $itemSaved->getCustomerId() );
160
		$this->assertEquals( $item->locale()->getLanguageId(), $itemSaved->locale()->getLanguageId() );
161
		$this->assertEquals( $item->getCustomerReference(), $itemSaved->getCustomerReference() );
162
		$this->assertEquals( $item->getComment(), $itemSaved->getComment() );
163
		$this->assertEquals( $item->getSiteCode(), $itemSaved->getSiteCode() );
164
		$this->assertEquals( $itemPrice->getValue(), $itemSavedPrice->getValue() );
165
		$this->assertEquals( $itemPrice->getCosts(), $itemSavedPrice->getCosts() );
166
		$this->assertEquals( $itemPrice->getRebate(), $itemSavedPrice->getRebate() );
167
		$this->assertEquals( $itemPrice->getTaxValue(), $itemSavedPrice->getTaxValue() );
168
		$this->assertEquals( $itemPrice->getCurrencyId(), $itemSavedPrice->getCurrencyId() );
169
170
		$this->assertEquals( $this->editor, $itemSaved->editor() );
171
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
172
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
173
174
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
175
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
176
		$this->assertEquals( $itemExp->getCustomerId(), $itemUpd->getCustomerId() );
177
		$this->assertEquals( $itemExp->locale()->getLanguageId(), $itemUpd->locale()->getLanguageId() );
178
		$this->assertEquals( $itemExp->getCustomerReference(), $itemUpd->getCustomerReference() );
179
		$this->assertEquals( $itemExp->getComment(), $itemUpd->getComment() );
180
		$this->assertEquals( $itemExp->getSiteCode(), $itemUpd->getSiteCode() );
181
		$this->assertEquals( $itemExpPrice->getValue(), $itemUpdPrice->getValue() );
182
		$this->assertEquals( $itemExpPrice->getCosts(), $itemUpdPrice->getCosts() );
183
		$this->assertEquals( $itemExpPrice->getRebate(), $itemUpdPrice->getRebate() );
184
		$this->assertEquals( $itemExpPrice->getTaxValue(), $itemUpdPrice->getTaxValue() );
185
		$this->assertEquals( $itemExpPrice->getCurrencyId(), $itemUpdPrice->getCurrencyId() );
186
187
		$this->assertEquals( $this->editor, $itemUpd->editor() );
188
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
189
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
190
191
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
192
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
193
194
		$this->expectException( \Aimeos\MShop\Exception::class );
195
		$this->object->get( $itemSaved->getId() );
196
	}
197
198
199
	public function testCreateSearch()
200
	{
201
		$this->assertInstanceOf( \Aimeos\MW\Criteria\Iface::class, $this->object->filter() );
202
	}
203
204
205
	public function testCreateSearchDefault()
206
	{
207
		$search = $this->object->filter( true );
208
209
		$this->assertInstanceOf( \Aimeos\MW\Criteria\Iface::class, $search );
210
		$this->assertInstanceOf( \Aimeos\MW\Criteria\Expression\Combine\Iface::class, $search->getConditions() );
211
212
		$list = $search->getConditions()->getExpressions();
213
		$this->assertArrayHasKey( 0, $list );
214
		$this->assertInstanceOf( \Aimeos\MW\Criteria\Expression\Compare\Iface::class, $list[0] );
215
		$this->assertEquals( 'order.base.customerid', $list[0]->getName() );
216
	}
217
218
219
	public function testCreateSearchSite()
220
	{
221
		$result = $this->object->filter( false, true );
222
		$this->assertInstanceOf( \Aimeos\MW\Criteria\Expression\Combine\Iface::class, $result->getConditions() );
223
	}
224
225
226
	public function testSearchItems()
227
	{
228
		$siteid = $this->context->locale()->getSiteId();
229
230
		$total = 0;
231
		$search = $this->object->filter();
232
233
		$expr = [];
234
		$expr[] = $search->compare( '!=', 'order.base.id', null );
235
		$expr[] = $search->compare( '==', 'order.base.siteid', $siteid );
236
		$expr[] = $search->compare( '==', 'order.base.sitecode', 'unittest' );
237
		$expr[] = $search->compare( '>=', 'order.base.customerid', '' );
238
		$expr[] = $search->compare( '==', 'order.base.languageid', 'de' );
239
		$expr[] = $search->compare( '==', 'order.base.currencyid', 'EUR' );
240
		$expr[] = $search->compare( '==', 'order.base.price', '53.50' );
241
		$expr[] = $search->compare( '==', 'order.base.costs', '1.50' );
242
		$expr[] = $search->compare( '==', 'order.base.rebate', '14.50' );
243
		$expr[] = $search->compare( '==', 'order.base.taxvalue', '0.0000' );
244
		$expr[] = $search->compare( '~=', 'order.base.customerref', 'ABC-1234' );
245
		$expr[] = $search->compare( '~=', 'order.base.comment', 'This is a comment' );
246
		$expr[] = $search->compare( '>=', 'order.base.mtime', '1970-01-01 00:00:00' );
247
		$expr[] = $search->compare( '>=', 'order.base.ctime', '1970-01-01 00:00:00' );
248
		$expr[] = $search->compare( '==', 'order.base.editor', $this->editor );
249
250
		$expr[] = $search->compare( '!=', 'order.base.address.id', null );
251
		$expr[] = $search->compare( '==', 'order.base.address.siteid', $siteid );
252
		$expr[] = $search->compare( '!=', 'order.base.address.baseid', null );
253
		$expr[] = $search->compare( '==', 'order.base.address.type', 'payment' );
254
		$expr[] = $search->compare( '==', 'order.base.address.company', 'Example company' );
255
		$expr[] = $search->compare( '==', 'order.base.address.vatid', 'DE999999999' );
256
		$expr[] = $search->compare( '==', 'order.base.address.salutation', 'mr' );
257
		$expr[] = $search->compare( '==', 'order.base.address.title', '' );
258
		$expr[] = $search->compare( '==', 'order.base.address.firstname', 'Our' );
259
		$expr[] = $search->compare( '==', 'order.base.address.lastname', 'Unittest' );
260
		$expr[] = $search->compare( '==', 'order.base.address.address1', 'Durchschnitt' );
261
		$expr[] = $search->compare( '==', 'order.base.address.address2', '1' );
262
		$expr[] = $search->compare( '==', 'order.base.address.address3', '' );
263
		$expr[] = $search->compare( '==', 'order.base.address.postal', '20146' );
264
		$expr[] = $search->compare( '==', 'order.base.address.city', 'Hamburg' );
265
		$expr[] = $search->compare( '==', 'order.base.address.state', 'Hamburg' );
266
		$expr[] = $search->compare( '==', 'order.base.address.countryid', 'DE' );
267
		$expr[] = $search->compare( '==', 'order.base.address.languageid', 'de' );
268
		$expr[] = $search->compare( '==', 'order.base.address.telephone', '055544332211' );
269
		$expr[] = $search->compare( '==', 'order.base.address.email', '[email protected]' );
270
		$expr[] = $search->compare( '==', 'order.base.address.telefax', '055544332213' );
271
		$expr[] = $search->compare( '==', 'order.base.address.website', 'www.example.net' );
272
		$expr[] = $search->compare( '>=', 'order.base.address.mtime', '1970-01-01 00:00:00' );
273
		$expr[] = $search->compare( '>=', 'order.base.address.ctime', '1970-01-01 00:00:00' );
274
		$expr[] = $search->compare( '==', 'order.base.address.editor', $this->editor );
275
276
		$expr[] = $search->compare( '!=', 'order.base.coupon.id', null );
277
		$expr[] = $search->compare( '==', 'order.base.coupon.siteid', $siteid );
278
		$expr[] = $search->compare( '!=', 'order.base.coupon.baseid', null );
279
		$expr[] = $search->compare( '!=', 'order.base.coupon.productid', null );
280
		$expr[] = $search->compare( '==', 'order.base.coupon.code', 'OPQR' );
281
		$expr[] = $search->compare( '>=', 'order.base.coupon.mtime', '1970-01-01 00:00:00' );
282
		$expr[] = $search->compare( '>=', 'order.base.coupon.ctime', '1970-01-01 00:00:00' );
283
		$expr[] = $search->compare( '>=', 'order.base.coupon.editor', '' );
284
285
		$expr[] = $search->compare( '!=', 'order.base.product.id', null );
286
		$expr[] = $search->compare( '==', 'order.base.product.siteid', $siteid );
287
		$expr[] = $search->compare( '!=', 'order.base.product.baseid', null );
288
		$expr[] = $search->compare( '==', 'order.base.product.orderproductid', null );
289
		$expr[] = $search->compare( '>=', 'order.base.product.type', '' );
290
		$expr[] = $search->compare( '!=', 'order.base.product.productid', null );
291
		$expr[] = $search->compare( '==', 'order.base.product.prodcode', 'CNE' );
292
		$expr[] = $search->compare( '==', 'order.base.product.supplierid', 'unitSupplier001' );
293
		$expr[] = $search->compare( '==', 'order.base.product.suppliername', 'Test supplier' );
294
		$expr[] = $search->compare( '==', 'order.base.product.name', 'Cafe Noire Expresso' );
295
		$expr[] = $search->compare( '==', 'order.base.product.mediaurl', 'somewhere/thump1.jpg' );
296
		$expr[] = $search->compare( '==', 'order.base.product.quantity', 9 );
297
		$expr[] = $search->compare( '==', 'order.base.product.price', '4.50' );
298
		$expr[] = $search->compare( '==', 'order.base.product.costs', '0.00' );
299
		$expr[] = $search->compare( '==', 'order.base.product.rebate', '0.00' );
300
		$expr[] = $search->compare( '=~', 'order.base.product.taxrates', '{' );
301
		$expr[] = $search->compare( '==', 'order.base.product.taxvalue', '0.0000' );
302
		$expr[] = $search->compare( '==', 'order.base.product.flags', 0 );
303
		$expr[] = $search->compare( '==', 'order.base.product.position', 1 );
304
		$expr[] = $search->compare( '==', 'order.base.product.statuspayment', 5 );
305
		$expr[] = $search->compare( '==', 'order.base.product.statusdelivery', 1 );
306
		$expr[] = $search->compare( '>=', 'order.base.product.mtime', '1970-01-01 00:00:00' );
307
		$expr[] = $search->compare( '>=', 'order.base.product.ctime', '1970-01-01 00:00:00' );
308
		$expr[] = $search->compare( '==', 'order.base.product.editor', $this->editor );
309
310
		$expr[] = $search->compare( '!=', 'order.base.product.attribute.id', null );
311
		$expr[] = $search->compare( '==', 'order.base.product.attribute.siteid', $siteid );
312
		$expr[] = $search->compare( '!=', 'order.base.product.attribute.parentid', null );
313
		$expr[] = $search->compare( '==', 'order.base.product.attribute.code', 'width' );
314
		$expr[] = $search->compare( '==', 'order.base.product.attribute.value', '33' );
315
		$expr[] = $search->compare( '==', 'order.base.product.attribute.name', '33' );
316
		$expr[] = $search->compare( '==', 'order.base.product.attribute.quantity', 1 );
317
		$expr[] = $search->compare( '>=', 'order.base.product.attribute.mtime', '1970-01-01 00:00:00' );
318
		$expr[] = $search->compare( '>=', 'order.base.product.attribute.ctime', '1970-01-01 00:00:00' );
319
		$expr[] = $search->compare( '==', 'order.base.product.attribute.editor', $this->editor );
320
321
		$expr[] = $search->compare( '!=', 'order.base.service.id', null );
322
		$expr[] = $search->compare( '==', 'order.base.service.siteid', $siteid );
323
		$expr[] = $search->compare( '!=', 'order.base.service.baseid', null );
324
		$expr[] = $search->compare( '==', 'order.base.service.type', 'payment' );
325
		$expr[] = $search->compare( '!=', 'order.base.service.serviceid', null );
326
		$expr[] = $search->compare( '==', 'order.base.service.code', 'unitpaymentcode' );
327
		$expr[] = $search->compare( '==', 'order.base.service.name', 'unitpaymentcode' );
328
		$expr[] = $search->compare( '==', 'order.base.service.price', '0.00' );
329
		$expr[] = $search->compare( '==', 'order.base.service.costs', '0.00' );
330
		$expr[] = $search->compare( '==', 'order.base.service.rebate', '0.00' );
331
		$expr[] = $search->compare( '=~', 'order.base.service.taxrates', '{' );
332
		$expr[] = $search->compare( '==', 'order.base.service.taxvalue', '0.0000' );
333
		$expr[] = $search->compare( '>=', 'order.base.service.mtime', '1970-01-01 00:00:00' );
334
		$expr[] = $search->compare( '>=', 'order.base.service.ctime', '1970-01-01 00:00:00' );
335
		$expr[] = $search->compare( '==', 'order.base.service.editor', $this->editor );
336
337
		$expr[] = $search->compare( '!=', 'order.base.service.attribute.id', null );
338
		$expr[] = $search->compare( '==', 'order.base.service.attribute.siteid', $siteid );
339
		$expr[] = $search->compare( '!=', 'order.base.service.attribute.parentid', null );
340
		$expr[] = $search->compare( '==', 'order.base.service.attribute.code', 'NAME' );
341
		$expr[] = $search->compare( '==', 'order.base.service.attribute.value', '"CreditCard"' );
342
		$expr[] = $search->compare( '==', 'order.base.service.attribute.quantity', 1 );
343
		$expr[] = $search->compare( '>=', 'order.base.service.attribute.mtime', '1970-01-01 00:00:00' );
344
		$expr[] = $search->compare( '>=', 'order.base.service.attribute.ctime', '1970-01-01 00:00:00' );
345
		$expr[] = $search->compare( '==', 'order.base.service.attribute.editor', $this->editor );
346
347
		$search->setConditions( $search->and( $expr ) );
348
		$ref = ['order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service'];
349
		$result = $this->object->search( $search, $ref, $total );
350
351
		$this->assertEquals( 1, $total );
352
		$this->assertEquals( 1, $result->count() );
353
354
		$item = $result->first();
355
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $item );
356
		$this->assertEquals( 2, count( $item->getAddresses() ) );
357
		$this->assertEquals( 2, count( $item->getCoupons() ) );
358
		$this->assertEquals( 4, count( $item->getProducts() ) );
359
		$this->assertEquals( 2, count( $item->getServices() ) );
360
	}
361
362
363
	public function testSearchItemsRef()
364
	{
365
		$search = $this->object->filter()->slice( 0, 1 );
366
367
		$search->setConditions( $search->compare( '!=', 'order.base.customerid', '' ) );
368
		$result = $this->object->search( $search, ['customer'] );
369
370
		$this->assertEquals( 1, count( $result ) );
371
		$this->assertNotNull( $result->first()->getCustomerItem() );
372
	}
373
374
375
	public function testSearchItemsTotal()
376
	{
377
		$search = $this->object->filter();
378
		$conditions = array(
379
			$search->compare( '>=', 'order.base.customerid', '' ),
380
			$search->compare( '==', 'order.base.editor', $this->editor )
381
		);
382
		$search->setConditions( $search->and( $conditions ) );
383
		$search->slice( 0, 1 );
384
		$total = 0;
385
		$items = $this->object->search( $search, [], $total );
386
		$this->assertEquals( 1, count( $items ) );
387
		$this->assertGreaterThanOrEqual( 4, $total );
388
389
		foreach( $items as $itemId => $item ) {
390
			$this->assertEquals( $itemId, $item->getId() );
391
		}
392
	}
393
394
395
	public function testSearchItemsDefault()
396
	{
397
		$search = $this->object->filter( true );
398
		$items = $this->object->search( $search );
399
400
		$this->assertEquals( 0, count( $items ) );
401
	}
402
403
404
	public function testGetSubManager()
405
	{
406
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'address' ) );
407
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'address', 'Standard' ) );
408
409
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'coupon' ) );
410
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'coupon', 'Standard' ) );
411
412
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'product' ) );
413
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'product', 'Standard' ) );
414
415
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'service' ) );
416
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'service', 'Standard' ) );
417
418
		$this->expectException( \Aimeos\MShop\Exception::class );
419
		$this->object->getSubManager( 'unknown' );
420
	}
421
422
423
	public function testGetSubManagerInvalidName()
424
	{
425
		$this->expectException( \Aimeos\MShop\Exception::class );
426
		$this->object->getSubManager( 'address', 'unknown' );
427
	}
428
429
430
	public function testLoad()
431
	{
432
		$item = $this->getOrderItem();
433
		$order = $this->object->load( $item->getId() );
434
435
436
		foreach( $order->getAddresses() as $addresses )
437
		{
438
			foreach( $addresses as $address )
439
			{
440
				$this->assertNotEquals( '', $address->getId() );
441
				$this->assertNotEquals( '', $address->getId() );
442
				$this->assertNotEquals( '', $address->getBaseId() );
443
			}
444
		}
445
446
		$this->assertEquals( 2, count( $order->getCoupons() ) );
447
448
		foreach( $order->getCoupons() as $code => $products )
449
		{
450
			$this->assertNotEquals( '', $code );
451
452
			foreach( $products as $product ) {
453
				$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Product\Iface::class, $product );
454
			}
455
		}
456
457
		foreach( $order->getProducts() as $product )
458
		{
459
			$this->assertNotEquals( '', $product->getId() );
460
			$this->assertNotEquals( '', $product->getId() );
461
			$this->assertNotEquals( '', $product->getBaseId() );
462
			$this->assertGreaterThan( 0, $product->getPosition() );
463
		}
464
465
		foreach( $order->getServices() as $list )
466
		{
467
			foreach( $list as $service )
468
			{
469
				$this->assertNotEquals( '', $service->getId() );
470
				$this->assertNotEquals( '', $service->getId() );
471
				$this->assertNotEquals( '', $service->getBaseId() );
472
			}
473
		}
474
	}
475
476
477
	public function testLoadNone()
478
	{
479
		$item = $this->getOrderItem();
480
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_NONE );
481
482
		$this->assertEquals( [], $order->getProducts()->toArray() );
483
		$this->assertEquals( [], $order->getCoupons()->toArray() );
484
		$this->assertEquals( [], $order->getServices()->toArray() );
485
		$this->assertEquals( [], $order->getAddresses()->toArray() );
486
	}
487
488
489
	public function testLoadAddress()
490
	{
491
		$item = $this->getOrderItem();
492
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ADDRESS );
493
494
		$this->assertGreaterThan( 0, count( $order->getAddresses() ) );
495
		$this->assertEquals( [], $order->getCoupons()->toArray() );
496
		$this->assertEquals( [], $order->getProducts()->toArray() );
497
		$this->assertEquals( [], $order->getServices()->toArray() );
498
	}
499
500
501
	public function testLoadProduct()
502
	{
503
		$item = $this->getOrderItem();
504
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT );
505
506
		$this->assertGreaterThan( 0, count( $order->getProducts() ) );
507
		$this->assertEquals( [], $order->getCoupons()->toArray() );
508
		$this->assertEquals( [], $order->getServices()->toArray() );
509
		$this->assertEquals( [], $order->getAddresses()->toArray() );
510
	}
511
512
513
	public function testLoadCoupon()
514
	{
515
		$item = $this->getOrderItem();
516
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_COUPON );
517
518
		$this->assertGreaterThan( 0, count( $order->getProducts() ) );
519
		$this->assertGreaterThan( 0, count( $order->getCoupons() ) );
520
		$this->assertEquals( [], $order->getServices()->toArray() );
521
		$this->assertEquals( [], $order->getAddresses()->toArray() );
522
	}
523
524
525
	public function testLoadService()
526
	{
527
		$item = $this->getOrderItem();
528
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE );
529
530
		$this->assertGreaterThan( 0, count( $order->getServices() ) );
531
		$this->assertEquals( [], $order->getCoupons()->toArray() );
532
		$this->assertEquals( [], $order->getProducts()->toArray() );
533
		$this->assertEquals( [], $order->getAddresses()->toArray() );
534
	}
535
536
537
	public function testLoadFresh()
538
	{
539
		$item = $this->getOrderItem();
540
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, true );
541
542
543
		$this->assertEquals( 2, count( $order->getCoupons() ) );
544
545
		foreach( $order->getAddresses() as $list )
546
		{
547
			foreach( $list as $address )
548
			{
549
				$this->assertEquals( null, $address->getId() );
550
				$this->assertEquals( null, $address->getBaseId() );
551
			}
552
		}
553
554
		foreach( $order->getProducts() as $product )
555
		{
556
			$this->assertEquals( null, $product->getId() );
557
			$this->assertEquals( null, $product->getBaseId() );
558
			$this->assertEquals( null, $product->getPosition() );
559
		}
560
561
		foreach( $order->getServices() as $list )
562
		{
563
			foreach( $list as $service )
564
			{
565
				$this->assertEquals( null, $service->getId() );
566
				$this->assertEquals( null, $service->getBaseId() );
567
			}
568
		}
569
	}
570
571
572
	public function testLoadFreshNone()
573
	{
574
		$item = $this->getOrderItem();
575
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_NONE, true );
576
577
		$this->assertEquals( [], $order->getAddresses()->toArray() );
578
		$this->assertEquals( [], $order->getCoupons()->toArray() );
579
		$this->assertEquals( [], $order->getProducts()->toArray() );
580
		$this->assertEquals( [], $order->getServices()->toArray() );
581
	}
582
583
584
	public function testLoadFreshAddress()
585
	{
586
		$item = $this->getOrderItem();
587
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ADDRESS, true );
588
589
		$this->assertGreaterThan( 0, count( $order->getAddresses() ) );
590
		$this->assertEquals( [], $order->getCoupons()->toArray() );
591
		$this->assertEquals( [], $order->getProducts()->toArray() );
592
		$this->assertEquals( [], $order->getServices()->toArray() );
593
	}
594
595
596
	public function testLoadFreshProduct()
597
	{
598
		$item = $this->getOrderItem();
599
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT, true );
600
601
		$this->assertGreaterThan( 0, count( $order->getProducts() ) );
602
		$this->assertEquals( [], $order->getCoupons()->toArray() );
603
		$this->assertEquals( [], $order->getAddresses()->toArray() );
604
		$this->assertEquals( [], $order->getServices()->toArray() );
605
	}
606
607
608
	public function testLoadFreshCoupon()
609
	{
610
		$item = $this->getOrderItem();
611
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_COUPON, true );
612
613
		$this->assertEquals( [], $order->getAddresses()->toArray() );
614
		$this->assertEquals( 2, count( $order->getCoupons() ) );
615
		$this->assertEquals( [], $order->getProducts()->toArray() );
616
		$this->assertEquals( [], $order->getServices()->toArray() );
617
	}
618
619
620
	public function testLoadFreshService()
621
	{
622
		$item = $this->getOrderItem();
623
		$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE, true );
624
625
		$this->assertGreaterThan( 0, count( $order->getServices() ) );
626
		$this->assertEquals( [], $order->getCoupons()->toArray() );
627
		$this->assertEquals( [], $order->getAddresses()->toArray() );
628
		$this->assertEquals( [], $order->getProducts()->toArray() );
629
	}
630
631
632
	public function testStore()
633
	{
634
		$item = $this->getOrderItem();
635
636
		$basket = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, true );
637
		$this->object->store( $basket );
638
639
		$newBasketId = $basket->getId();
640
641
		$basket = $this->object->load( $newBasketId );
642
		$this->object->delete( $newBasketId );
643
644
645
		$this->assertEquals( $item->getCustomerId(), $basket->getCustomerId() );
646
		$this->assertEquals( $basket->locale()->getSiteId(), $basket->getSiteId() );
647
648
		$this->assertEquals( 1.50, $basket->getPrice()->getCosts() );
649
650
		$pos = 0;
651
		$products = $basket->getProducts();
652
		$this->assertEquals( 2, count( $products ) );
653
654
		foreach( $products as $product )
655
		{
656
			$this->assertGreaterThanOrEqual( 2, count( $product->getAttributeItems() ) );
657
			$this->assertEquals( $pos++, $product->getPosition() );
658
		}
659
660
		$this->assertEquals( 2, count( $basket->getAddresses() ) );
661
662
		$services = $basket->getServices();
663
		$this->assertEquals( 2, count( $services ) );
664
665
		$attributes = [];
666
		foreach( $services as $list )
667
		{
668
			foreach( $list as $service ) {
669
				$attributes[$service->getCode()] = $service->getAttributeItems();
670
			}
671
		}
672
673
		$this->assertEquals( 9, count( $attributes['unitpaymentcode'] ) );
674
		$this->assertEquals( 0, count( $attributes['unitdeliverycode'] ) );
675
676
		$this->expectException( \Aimeos\MShop\Exception::class );
677
		$this->object->get( $newBasketId );
678
	}
679
680
681
	public function testStoreExisting()
682
	{
683
		$item = $this->getOrderItem();
684
685
		$basket = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, true );
686
		$this->object->store( $basket );
687
		$newBasketId = $basket->getId();
688
		$this->object->store( $basket );
689
		$newBasket = $this->object->load( $newBasketId );
690
691
		$this->object->delete( $newBasketId );
692
693
694
		foreach( $basket->getAddresses() as $key => $list )
695
		{
696
			foreach( $list as $pos => $address ) {
697
				$this->assertEquals( $address->getId(), $newBasket->getAddress( $key, $pos )->getId() );
698
			}
699
		}
700
701
		$newProducts = $newBasket->getProducts();
702
703
		foreach( $basket->getProducts() as $key => $product ) {
704
			$this->assertEquals( $product->getId(), $newProducts[$key]->getId() );
705
		}
706
707
		$newServices = $newBasket->getServices();
708
709
		foreach( $basket->getServices() as $key => $list )
710
		{
711
			foreach( $list as $pos => $service ) {
712
				$this->assertEquals( $service->getId(), $newServices[$key][$pos]->getId() );
713
			}
714
		}
715
	}
716
717
718
	public function testStoreBundles()
719
	{
720
		$search = $this->object->filter()->add( ['order.base.sitecode' => 'unittest', 'order.base.price' => 2400.00] );
721
		$item = $this->object->search( $search )->first( new \RuntimeException( 'No order found' ) );
722
723
		$basket = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, true );
724
		$this->object->store( $basket );
725
726
		$newBasketId = $basket->getId();
727
728
		$basket = $this->object->load( $newBasketId );
729
		$this->object->delete( $newBasketId );
730
731
		$this->assertEquals( $item->getCustomerId(), $basket->getCustomerId() );
732
		$this->assertEquals( $basket->locale()->getSiteId(), $basket->getSiteId() );
733
734
		$pos = 0;
735
		$products = $basket->getProducts();
736
737
		$this->assertEquals( 2, count( $products ) );
738
		foreach( $products as $product )
739
		{
740
			$this->assertEquals( 2, count( $product->getProducts() ) );
741
			$this->assertEquals( $pos, $product->getPosition() );
742
			$pos += 3; // two sub-products in between
743
		}
744
745
		$this->expectException( \Aimeos\MShop\Exception::class );
746
		$this->object->get( $newBasketId );
747
	}
748
749
750
	public function testStoreAddress()
751
	{
752
		$item = $this->getOrderItem();
753
754
		$basket = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ADDRESS, true );
755
		$this->object->store( $basket );
756
757
		$newBasketId = $basket->getId();
758
759
		$basket = $this->object->load( $newBasketId, \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL );
760
		$this->object->delete( $newBasketId );
761
762
		$this->assertGreaterThan( 0, count( $basket->getAddresses() ) );
763
		$this->assertEquals( [], $basket->getProducts()->toArray() );
764
		$this->assertEquals( [], $basket->getCoupons()->toArray() );
765
		$this->assertEquals( [], $basket->getServices()->toArray() );
766
	}
767
768
769
	public function testStoreProduct()
770
	{
771
		$item = $this->getOrderItem();
772
773
		$basket = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT, true );
774
		$this->object->store( $basket );
775
776
		$newBasketId = $basket->getId();
777
778
		$basket = $this->object->load( $newBasketId, \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL );
779
		$this->object->delete( $newBasketId );
780
781
		$this->assertGreaterThan( 0, count( $basket->getProducts() ) );
782
		$this->assertEquals( [], $basket->getAddresses()->toArray() );
783
		$this->assertEquals( [], $basket->getCoupons()->toArray() );
784
		$this->assertEquals( [], $basket->getServices()->toArray() );
785
	}
786
787
788
	public function testStoreService()
789
	{
790
		$item = $this->getOrderItem();
791
792
		$basket = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE, true );
793
		$this->object->store( $basket );
794
795
		$newBasketId = $basket->getId();
796
797
		$basket = $this->object->load( $newBasketId, \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL );
798
		$this->object->delete( $newBasketId );
799
800
		$this->assertGreaterThan( 0, count( $basket->getServices() ) );
801
		$this->assertEquals( [], $basket->getProducts()->toArray() );
802
		$this->assertEquals( [], $basket->getAddresses()->toArray() );
803
		$this->assertEquals( [], $basket->getCoupons()->toArray() );
804
	}
805
806
807
	public function testLoadStoreCoupons()
808
	{
809
		$search = $this->object->filter()->add( ['order.base.price' => '53.50'] );
810
		$item = $this->object->search( $search )->first( new \RuntimeException( 'No order found' ) );
811
812
		$parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT;
813
		$basket = $this->object->load( $item->getId(), $parts, true );
814
815
		$this->assertEquals( '58.50', $basket->getPrice()->getValue() );
816
		$this->assertEquals( '1.50', $basket->getPrice()->getCosts() );
817
		$this->assertEquals( 0, count( $basket->getCoupons() ) );
818
819
		$productBasket = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, true );
0 ignored issues
show
Unused Code introduced by
The assignment to $productBasket is dead and can be removed.
Loading history...
820
821
		$basket->addCoupon( 'CDEF' );
822
		$basket->addCoupon( '90AB' );
823
		$this->assertEquals( 2, count( $basket->getCoupons() ) );
824
825
		$this->object->store( $basket );
826
		$newBasket = $this->object->load( $basket->getId() );
827
		$this->object->delete( $newBasket->getId() );
828
829
		$this->assertEquals( '52.50', $newBasket->getPrice()->getValue() );
830
		$this->assertEquals( '1.50', $newBasket->getPrice()->getCosts() );
831
		$this->assertEquals( '6.00', $newBasket->getPrice()->getRebate() );
832
		$this->assertEquals( 2, count( $newBasket->getCoupons() ) );
833
	}
834
835
836
	/**
837
	 * Returns an order base item
838
	 *
839
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item
840
	 * @throws \Exception If no found
841
	 */
842
	protected function getOrderItem()
843
	{
844
		$search = $this->object->filter()->add( [
845
			'order.base.sitecode' => 'unittest',
846
			'order.base.price' => 53.50,
847
			'order.base.rebate' => 14.50,
848
			'order.base.editor' => $this->editor
849
		] );
850
851
		return $this->object->search( $search )->first( new \RuntimeException( 'No order found' ) );
852
	}
853
}
854