Passed
Push — master ( 022b5e...74faaa )
by Aimeos
04:45
created

StandardTest::testSearchItemsRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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