Completed
Push — master ( fbf9c4...971278 )
by Aimeos
16:34
created

StandardTest::testSearchItemsDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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