Passed
Push — master ( beeae9...b3aaac )
by Aimeos
04:26
created

StandardTest::testGetServiceId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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-2022
7
 */
8
9
10
namespace Aimeos\MShop\Order\Item\Base\Service;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $values;
17
	private $price;
18
	private $attribute = [];
19
20
21
	protected function setUp() : void
22
	{
23
		$this->price = \Aimeos\MShop::create( \TestHelper::context(), 'price' )->create();
24
25
		$attrValues = array(
26
			'order.base.service.attribute.id' => 3,
27
			'order.base.service.attribute.siteid' => 99,
28
			'order.base.service.attribute.parentid' => 42,
29
			'order.base.service.attribute.name' => 'UnitName',
30
			'order.base.service.attribute.type' => 'default',
31
			'order.base.service.attribute.code' => 'UnitCode',
32
			'order.base.service.attribute.value' => 'UnitValue',
33
			'order.base.service.attribute.mtime' => '2020-12-31 23:59:59',
34
			'order.base.service.attribute.ctime' => '2011-01-01 00:00:01',
35
			'order.base.service.attribute.editor' => 'unitTestUser'
36
		);
37
38
		$this->attribute = array( new \Aimeos\MShop\Order\Item\Base\Service\Attribute\Standard( $attrValues ) );
39
40
		$this->values = array(
41
			'order.base.service.id' => 1,
42
			'order.base.service.siteid' => 99,
43
			'order.base.service.serviceid' => 'ServiceID',
44
			'order.base.service.baseid' => 42,
45
			'order.base.service.code' => 'UnitCode',
46
			'order.base.service.name' => 'UnitName',
47
			'order.base.service.mediaurl' => 'Url for test',
48
			'order.base.service.position' => 1,
49
			'order.base.service.type' => 'payment',
50
			'order.base.service.mtime' => '2012-01-01 00:00:01',
51
			'order.base.service.ctime' => '2011-01-01 00:00:01',
52
			'order.base.service.editor' => 'unitTestUser'
53
		);
54
55
		$servItem = \Aimeos\MShop::create( \TestHelper::context(), 'service' )->create();
56
		$this->object = new \Aimeos\MShop\Order\Item\Base\Service\Standard(
57
			$this->price, $this->values, $this->attribute, $servItem
58
		);
59
	}
60
61
62
	protected function tearDown() : void
63
	{
64
		unset( $this->object );
65
	}
66
67
68
	public function testGetServiceItem()
69
	{
70
		$this->assertInstanceOf( \Aimeos\MShop\Service\Item\Iface::class, $this->object->getServiceItem() );
71
		$this->assertNull( ( new \Aimeos\MShop\Order\Item\Base\Service\Standard( $this->price ) )->getServiceItem() );
72
	}
73
74
75
	public function testGetId()
76
	{
77
		$this->assertEquals( 1, $this->object->getId() );
78
	}
79
80
81
	public function testSetId()
82
	{
83
		$return = $this->object->setId( null );
84
85
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
86
		$this->assertEquals( null, $this->object->getId() );
87
		$this->assertTrue( $this->object->isModified() );
88
89
		$return = $this->object->setId( 5 );
90
91
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
92
		$this->assertEquals( 5, $this->object->getId() );
93
		$this->assertFalse( $this->object->isModified() );
94
	}
95
96
97
	public function testGetSiteId()
98
	{
99
		$this->assertEquals( 99, $this->object->getSiteId() );
100
	}
101
102
103
	public function testSetSiteId()
104
	{
105
		$this->object->setSiteId( 100 );
106
		$this->assertEquals( 100, $this->object->getSiteId() );
107
		$this->assertTrue( $this->object->isModified() );
108
	}
109
110
111
	public function testGetBaseId()
112
	{
113
		$this->assertEquals( 42, $this->object->getBaseId() );
114
	}
115
116
117
	public function testSetBaseId()
118
	{
119
		$return = $this->object->setBaseId( 111 );
120
121
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
122
		$this->assertEquals( 111, $this->object->getBaseId() );
123
		$this->assertTrue( $this->object->isModified() );
124
	}
125
126
127
	public function testSetBaseIdReset()
128
	{
129
		$return = $this->object->setBaseId( null );
130
131
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
132
		$this->assertEquals( null, $this->object->getBaseId() );
133
		$this->assertTrue( $this->object->isModified() );
134
	}
135
136
137
	public function testGetServiceId()
138
	{
139
		$this->assertEquals( 'ServiceID', $this->object->getServiceId() );
140
	}
141
142
143
	public function testSetServiceId()
144
	{
145
		$return = $this->object->setServiceId( 'testServiceID' );
146
147
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
148
		$this->assertEquals( 'testServiceID', $this->object->getServiceId() );
149
		$this->assertTrue( $this->object->isModified() );
150
	}
151
152
153
	public function testGetCode()
154
	{
155
		$this->assertEquals( 'UnitCode', $this->object->getCode() );
156
	}
157
158
159
	public function testSetCode()
160
	{
161
		$return = $this->object->setCode( 'testCode' );
162
163
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
164
		$this->assertEquals( 'testCode', $this->object->getCode() );
165
		$this->assertTrue( $this->object->isModified() );
166
	}
167
168
169
	public function testGetName()
170
	{
171
		$this->assertEquals( 'UnitName', $this->object->getName() );
172
	}
173
174
175
	public function testSetName()
176
	{
177
		$return = $this->object->setName( 'testName' );
178
179
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
180
		$this->assertEquals( 'testName', $this->object->getName() );
181
		$this->assertTrue( $this->object->isModified() );
182
	}
183
184
185
	public function testGetMediaUrl()
186
	{
187
		$this->assertEquals( 'Url for test', $this->object->getMediaUrl() );
188
	}
189
190
191
	public function testSetMediaUrl()
192
	{
193
		$return = $this->object->setMediaUrl( 'testUrl' );
194
195
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
196
		$this->assertEquals( 'testUrl', $this->object->getMediaUrl() );
197
		$this->assertTrue( $this->object->isModified() );
198
	}
199
200
201
	public function testGetType()
202
	{
203
		$this->assertEquals( 'payment', $this->object->getType() );
204
	}
205
206
207
	public function testSetType()
208
	{
209
		$return = $this->object->setType( 'delivery' );
210
211
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
212
		$this->assertEquals( 'delivery', $this->object->getType() );
213
		$this->assertTrue( $this->object->isModified() );
214
	}
215
216
217
	public function testGetPrice()
218
	{
219
		$this->assertSame( $this->price, $this->object->getPrice() );
220
	}
221
222
223
	public function testSetPrice()
224
	{
225
		$this->price->setCosts( '5.00' );
226
		$return = $this->object->setPrice( $this->price );
227
228
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
229
		$this->assertFalse( $this->object->isModified() );
230
		$this->assertSame( $this->price, $this->object->getPrice() );
231
	}
232
233
234
	public function testGetPosition()
235
	{
236
		$this->assertEquals( 1, $this->object->getPosition() );
237
	}
238
239
240
	public function testSetPosition()
241
	{
242
		$return = $this->object->setPosition( 2 );
243
244
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
245
		$this->assertEquals( 2, $this->object->getPosition() );
246
		$this->assertTrue( $this->object->isModified() );
247
	}
248
249
250
	public function testSetPositionReset()
251
	{
252
		$return = $this->object->setPosition( null );
253
254
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
255
		$this->assertEquals( null, $this->object->getPosition() );
256
		$this->assertTrue( $this->object->isModified() );
257
	}
258
259
260
	public function testSetPositionInvalid()
261
	{
262
		$this->expectException( \Aimeos\MShop\Order\Exception::class );
263
		$this->object->setPosition( -1 );
264
	}
265
266
267
	public function testAddAttributeItems()
268
	{
269
		$item = \Aimeos\MShop::create( \TestHelper::context(), 'order/base/service' )->createAttributeItem();
270
271
		$return = $this->object->addAttributeItems( [$item] );
272
273
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
274
		$this->assertEquals( 2, count( $this->object->getAttributeItems() ) );
275
		$this->assertTrue( $this->object->isModified() );
276
	}
277
278
279
	public function testGetAttribute()
280
	{
281
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
282
		$attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' );
283
284
		$attrItem001 = $attManager->create();
285
		$attrItem001->setAttributeId( '1' );
286
		$attrItem001->setCode( 'code_001' );
287
		$attrItem001->setValue( 'value_001' );
288
289
		$attrItem002 = $attManager->create();
290
		$attrItem002->setAttributeId( '2' );
291
		$attrItem002->setCode( 'code_002' );
292
		$attrItem002->setType( 'test_002' );
293
		$attrItem002->setValue( 'value_002' );
294
295
		$this->object->setAttributeItems( array( $attrItem001, $attrItem002 ) );
296
297
		$result = $this->object->getAttribute( 'code_001' );
298
		$this->assertEquals( 'value_001', $result );
299
300
		$result = $this->object->getAttribute( 'code_002', ['test_002'] );
301
		$this->assertEquals( 'value_002', $result );
302
303
		$result = $this->object->getAttribute( 'code_002', 'test_002' );
304
		$this->assertEquals( 'value_002', $result );
305
306
		$result = $this->object->getAttribute( 'code_002' );
307
		$this->assertEquals( null, $result );
308
309
		$result = $this->object->getAttribute( 'code_003' );
310
		$this->assertEquals( null, $result );
311
312
		$this->object->setAttributeItems( [] );
313
314
		$result = $this->object->getAttribute( 'code_001' );
315
		$this->assertEquals( null, $result );
316
	}
317
318
319
	public function testGetAttributeList()
320
	{
321
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
322
		$attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' );
323
324
		$attrItem001 = $attManager->create();
325
		$attrItem001->setAttributeId( '1' );
326
		$attrItem001->setCode( 'code_001' );
327
		$attrItem001->setType( 'test_001' );
328
		$attrItem001->setValue( 'value_001' );
329
330
		$attrItem002 = $attManager->create();
331
		$attrItem002->setAttributeId( '2' );
332
		$attrItem002->setCode( 'code_001' );
333
		$attrItem002->setType( 'test_001' );
334
		$attrItem002->setValue( 'value_002' );
335
336
		$this->object->setAttributeItems( array( $attrItem001, $attrItem002 ) );
337
338
		$result = $this->object->getAttribute( 'code_001', 'test_001' );
339
		$this->assertEquals( ['value_001', 'value_002'], $result );
340
	}
341
342
343
	public function testGetAttributeItem()
344
	{
345
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
346
		$attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' );
347
348
		$attrItem001 = $attManager->create();
349
		$attrItem001->setAttributeId( '1' );
350
		$attrItem001->setCode( 'code_001' );
351
		$attrItem001->setValue( 'value_001' );
352
353
		$attrItem002 = $attManager->create();
354
		$attrItem002->setAttributeId( '2' );
355
		$attrItem002->setCode( 'code_002' );
356
		$attrItem002->setType( 'test_002' );
357
		$attrItem002->setValue( 'value_002' );
358
359
		$this->object->setAttributeItems( array( $attrItem001, $attrItem002 ) );
360
361
		$result = $this->object->getAttributeItem( 'code_001' );
362
		$this->assertEquals( 'value_001', $result->getValue() );
363
364
		$result = $this->object->getAttributeItem( 'code_002', 'test_002' );
365
		$this->assertEquals( 'value_002', $result->getValue() );
366
367
		$result = $this->object->getAttributeItem( 'code_002' );
368
		$this->assertEquals( null, $result );
369
370
		$result = $this->object->getAttributeItem( 'code_003' );
371
		$this->assertEquals( null, $result );
372
373
		$this->object->setAttributeItems( [] );
374
375
		$result = $this->object->getAttributeItem( 'code_001' );
376
		$this->assertEquals( null, $result );
377
	}
378
379
380
	public function testGetAttributeItemList()
381
	{
382
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
383
		$attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' );
384
385
		$attrItem001 = $attManager->create();
386
		$attrItem001->setAttributeId( '1' );
387
		$attrItem001->setCode( 'code_001' );
388
		$attrItem001->setType( 'test_001' );
389
		$attrItem001->setValue( 'value_001' );
390
391
		$attrItem002 = $attManager->create();
392
		$attrItem002->setAttributeId( '2' );
393
		$attrItem002->setCode( 'code_001' );
394
		$attrItem002->setType( 'test_001' );
395
		$attrItem002->setValue( 'value_002' );
396
397
		$this->object->setAttributeItems( array( $attrItem001, $attrItem002 ) );
398
399
		$result = $this->object->getAttributeItem( 'code_001', 'test_001' );
400
		$this->assertEquals( 2, count( $result ) );
401
	}
402
403
404
	public function testGetAttributeItems()
405
	{
406
		$this->assertEquals( $this->attribute, $this->object->getAttributeItems()->toArray() );
407
	}
408
409
410
	public function testGetAttributeItemsByType()
411
	{
412
		$this->assertEquals( $this->attribute, $this->object->getAttributeItems( 'default' )->toArray() );
413
	}
414
415
416
	public function testGetAttributeItemsInvalidType()
417
	{
418
		$this->assertEquals( [], $this->object->getAttributeItems( 'invalid' )->toArray() );
419
	}
420
421
422
	public function testSetAttributeItem()
423
	{
424
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
425
		$attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' );
426
427
		$item = $attManager->create();
428
		$item->setAttributeId( '1' );
429
		$item->setCode( 'test_code' );
430
		$item->setType( 'test_type' );
431
		$item->setValue( 'test_value' );
432
433
		$return = $this->object->setAttributeItem( $item );
434
435
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
436
		$this->assertEquals( 'test_value', $this->object->getAttributeItem( 'test_code', 'test_type' )->getValue() );
437
		$this->assertTrue( $this->object->isModified() );
438
439
		$item = $attManager->create();
440
		$item->setAttributeId( '1' );
441
		$item->setCode( 'test_code' );
442
		$item->setType( 'test_type' );
443
		$item->setValue( 'test_value2' );
444
445
		$return = $this->object->setAttributeItem( $item );
446
447
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
448
		$this->assertEquals( 'test_value2', $this->object->getAttributeItem( 'test_code', 'test_type' )->getValue() );
449
		$this->assertTrue( $this->object->isModified() );
450
	}
451
452
453
	public function testSetAttributeItems()
454
	{
455
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
456
		$attManager = $manager->getSubManager( 'base' )->getSubManager( 'service' )->getSubManager( 'attribute' );
457
458
		$list = array(
459
			$attManager->create(),
460
			$attManager->create(),
461
		);
462
463
		$return = $this->object->setAttributeItems( $list );
464
465
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
466
		$this->assertEquals( $list, $this->object->getAttributeItems()->toArray() );
467
		$this->assertTrue( $this->object->isModified() );
468
	}
469
470
471
	public function testGetTimeModified()
472
	{
473
		$this->assertEquals( '2012-01-01 00:00:01', $this->object->getTimeModified() );
474
	}
475
476
477
	public function testGetTimeCreated()
478
	{
479
		$this->assertEquals( '2011-01-01 00:00:01', $this->object->getTimeCreated() );
480
	}
481
482
483
	public function testGetEditor()
484
	{
485
		$this->assertEquals( 'unitTestUser', $this->object->editor() );
486
	}
487
488
489
	public function testFromArray()
490
	{
491
		$item = new \Aimeos\MShop\Order\Item\Base\Service\Standard( new \Aimeos\MShop\Price\Item\Standard() );
492
493
		$list = $entries = array(
494
			'order.base.service.id' => 1,
495
			'order.base.service.baseid' => 2,
496
			'order.base.service.serviceid' => 3,
497
			'order.base.service.position' => 4,
498
			'order.base.service.currencyid' => 'EUR',
499
			'order.base.service.price' => '1.00',
500
			'order.base.service.costs' => '0.50',
501
			'order.base.service.rebate' => '0.50',
502
			'order.base.service.taxrates' => ['tax' => '20.00'],
503
			'order.base.service.taxvalue' => '0.2500',
504
			'order.base.service.taxflag' => 1,
505
			'order.base.service.code' => 'test',
506
			'order.base.service.name' => 'test item',
507
			'order.base.service.type' => 'delivery',
508
		);
509
510
		$item = $item->fromArray( $entries, true );
511
512
		$this->assertEquals( [], $entries );
513
		$this->assertEquals( '', $item->getSiteId() );
514
		$this->assertEquals( $list['order.base.service.id'], $item->getId() );
515
		$this->assertEquals( $list['order.base.service.baseid'], $item->getBaseId() );
516
		$this->assertEquals( $list['order.base.service.serviceid'], $item->getServiceId() );
517
		$this->assertEquals( $list['order.base.service.position'], $item->getPosition() );
518
		$this->assertEquals( $list['order.base.service.currencyid'], $item->getPrice()->getCurrencyId() );
519
		$this->assertEquals( $list['order.base.service.price'], $item->getPrice()->getValue() );
520
		$this->assertEquals( $list['order.base.service.costs'], $item->getPrice()->getCosts() );
521
		$this->assertEquals( $list['order.base.service.rebate'], $item->getPrice()->getRebate() );
522
		$this->assertEquals( $list['order.base.service.taxrates'], $item->getPrice()->getTaxRates() );
523
		$this->assertEquals( $list['order.base.service.taxvalue'], $item->getPrice()->getTaxValue() );
524
		$this->assertEquals( $list['order.base.service.taxflag'], $item->getPrice()->getTaxFlag() );
525
		$this->assertEquals( $list['order.base.service.code'], $item->getCode() );
526
		$this->assertEquals( $list['order.base.service.name'], $item->getName() );
527
		$this->assertEquals( $list['order.base.service.type'], $item->getType() );
528
	}
529
530
531
	public function testToArray()
532
	{
533
		$arrayObject = $this->object->toArray( true );
534
535
		$this->assertEquals( count( $this->values ) + 8, count( $arrayObject ) );
536
537
		$this->assertEquals( $this->object->getId(), $arrayObject['order.base.service.id'] );
538
		$this->assertEquals( $this->object->getBaseId(), $arrayObject['order.base.service.baseid'] );
539
		$this->assertEquals( $this->object->getServiceId(), $arrayObject['order.base.service.serviceid'] );
540
		$this->assertEquals( $this->object->getPosition(), $arrayObject['order.base.service.position'] );
541
		$this->assertEquals( $this->object->getCode(), $arrayObject['order.base.service.code'] );
542
		$this->assertEquals( $this->object->getName(), $arrayObject['order.base.service.name'] );
543
		$this->assertEquals( $this->object->getType(), $arrayObject['order.base.service.type'] );
544
		$this->assertEquals( $this->object->getTimeCreated(), $arrayObject['order.base.service.ctime'] );
545
		$this->assertEquals( $this->object->getTimeModified(), $arrayObject['order.base.service.mtime'] );
546
		$this->assertEquals( $this->object->editor(), $arrayObject['order.base.service.editor'] );
547
548
		$price = $this->object->getPrice();
549
		$this->assertEquals( $price->getCurrencyId(), $arrayObject['order.base.service.currencyid'] );
550
		$this->assertEquals( $price->getValue(), $arrayObject['order.base.service.price'] );
551
		$this->assertEquals( $price->getCosts(), $arrayObject['order.base.service.costs'] );
552
		$this->assertEquals( $price->getRebate(), $arrayObject['order.base.service.rebate'] );
553
		$this->assertEquals( $price->getTaxRate(), $arrayObject['order.base.service.taxrate'] );
554
		$this->assertEquals( $price->getTaxRates(), $arrayObject['order.base.service.taxrates'] );
555
		$this->assertEquals( $price->getTaxValue(), $arrayObject['order.base.service.taxvalue'] );
556
		$this->assertEquals( $price->getTaxFlag(), $arrayObject['order.base.service.taxflag'] );
557
	}
558
559
	public function testIsModified()
560
	{
561
		$this->assertFalse( $this->object->isModified() );
562
	}
563
564
565
	public function testGetResourceType()
566
	{
567
		$this->assertEquals( 'order/base/service', $this->object->getResourceType() );
568
	}
569
570
571
	public function testCopyFrom()
572
	{
573
		$serviceCopy = new \Aimeos\MShop\Order\Item\Base\Service\Standard( $this->price );
574
575
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'service' );
576
577
		$filter = $manager->filter()->add( ['service.provider' => 'Standard'] );
578
		$item = $manager->search( $filter )->first( new \RuntimeException( 'No service found' ) );
579
580
		$return = $serviceCopy->copyFrom( $item->set( 'customprop', 123 ) );
581
582
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $return );
583
		$this->assertEquals( 'unitdeliverycode', $serviceCopy->getCode() );
584
		$this->assertEquals( 'unitlabel', $serviceCopy->getName() );
585
		$this->assertEquals( 'delivery', $serviceCopy->getType() );
586
		$this->assertEquals( '', $serviceCopy->getMediaUrl() );
587
		$this->assertEquals( '123', $serviceCopy->get( 'customprop' ) );
588
589
		$this->assertTrue( $serviceCopy->isModified() );
590
	}
591
}
592