Passed
Push — master ( 598552...4c729e )
by Aimeos
05:45
created

StandardTest::testSetValueEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
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-2024
7
 */
8
9
namespace Aimeos\MShop\Price\Item;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $values;
16
17
18
	protected function setUp() : void
19
	{
20
		$this->values = array(
21
			'price.id' => 199,
22
			'price.siteid' => 99,
23
			'price.type' => 'default',
24
			'price.currencyid' => 'EUR',
25
			'price.domain' => 'product',
26
			'price.label' => 'Price label',
27
			'price.quantity' => 15,
28
			'price.value' => '195.50',
29
			'price.costs' => '19.95',
30
			'price.rebate' => '10.00',
31
			'price.taxvalue' => '34.3995',
32
			'price.taxrates' => ['tax' => '19.00', 'local' => '5.00'],
33
			'price.taxflag' => true,
34
			'price.status' => true,
35
			'price.mtime' => '2011-01-01 00:00:02',
36
			'price.ctime' => '2011-01-01 00:00:01',
37
			'price.editor' => 'unitTestUser',
38
			'.currencyid' => 'EUR',
39
		);
40
41
		$this->object = new \Aimeos\MShop\Price\Item\Standard( $this->values );
42
	}
43
44
45
	protected function tearDown() : void
46
	{
47
		unset( $this->object );
48
	}
49
50
51
	public function testAddItem()
52
	{
53
		$price = new \Aimeos\MShop\Price\Item\Standard( $this->values );
54
		$return = $this->object->addItem( $price );
55
56
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
57
		$this->assertEquals( '391.00', $this->object->getValue() );
58
		$this->assertEquals( '39.90', $this->object->getCosts() );
59
		$this->assertEquals( '20.00', $this->object->getRebate() );
60
		$this->assertEquals( '68.7990', $this->object->getTaxValue() );
61
		$this->assertEquals( 1, $this->object->getQuantity() );
62
	}
63
64
65
	public function testAddItemSelf()
66
	{
67
		$return = $this->object->addItem( $this->object );
68
69
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
70
		$this->assertEquals( '391.00', $this->object->getValue() );
71
		$this->assertEquals( '39.90', $this->object->getCosts() );
72
		$this->assertEquals( '20.00', $this->object->getRebate() );
73
		$this->assertEquals( '68.7990', $this->object->getTaxValue() );
74
		$this->assertEquals( 1, $this->object->getQuantity() );
75
	}
76
77
78
	public function testAddItemWrongCurrency()
79
	{
80
		$values = $this->values;
81
		$values['price.currencyid'] = 'USD';
82
83
		$price = new \Aimeos\MShop\Price\Item\Standard( $values );
84
85
		$this->expectException( \Aimeos\MShop\Price\Exception::class );
86
		$this->object->addItem( $price );
87
	}
88
89
90
	public function testClear()
91
	{
92
		$result = $this->object->clear();
93
94
		$this->assertInstanceOf( 'Aimeos\MShop\Price\Item\Iface', $result );
95
		$this->assertEquals( '0.00', $this->object->getValue() );
96
		$this->assertEquals( '0.00', $this->object->getCosts() );
97
		$this->assertEquals( '0.00', $this->object->getRebate() );
98
		$this->assertEquals( '0.0000', $this->object->getTaxValue() );
99
		$this->assertEquals( true, $this->object->getTaxFlag() );
100
		$this->assertEquals( 1, $this->object->getQuantity() );
101
	}
102
103
104
	public function testCompare()
105
	{
106
		$price = new \Aimeos\MShop\Price\Item\Standard( $this->values );
107
		$this->assertTrue( $this->object->compare( $price ) );
108
	}
109
110
111
	public function testCompareFail()
112
	{
113
		$values = $this->values;
114
		$values['price.value'] = '200.00';
115
116
		$price = new \Aimeos\MShop\Price\Item\Standard( $values );
117
		$this->assertFalse( $this->object->compare( $price ) );
118
	}
119
120
121
	public function testGetPrecision()
122
	{
123
		$this->assertEquals( 2, $this->object->getPrecision() );
124
	}
125
126
127
	public function testGetId()
128
	{
129
		$this->assertEquals( 199, $this->object->getId() );
130
	}
131
132
133
	public function testSetId()
134
	{
135
		$return = $this->object->setId( null );
136
137
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
138
		$this->assertNull( $this->object->getId() );
139
		$this->assertTrue( $this->object->isModified() );
140
	}
141
142
143
	public function testGetSiteId()
144
	{
145
		$this->assertEquals( 99, $this->object->getSiteId() );
146
	}
147
148
149
	public function testGetType()
150
	{
151
		$this->assertEquals( 'default', $this->object->getType() );
152
	}
153
154
155
	public function testSetType()
156
	{
157
		$this->object->setType( 'test' );
158
		$this->assertEquals( 'test', $this->object->getType() );
159
160
		$this->assertTrue( $this->object->isModified() );
161
	}
162
163
164
	public function testGetCurrencyId()
165
	{
166
		$this->assertEquals( 'EUR', $this->object->getCurrencyId() );
167
	}
168
169
170
	public function testSetCurrencyId()
171
	{
172
		$return = $this->object->setCurrencyId( 'USD' );
173
174
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
175
		$this->assertEquals( 'USD', $this->object->getCurrencyId() );
176
		$this->assertTrue( $this->object->isModified() );
177
	}
178
179
180
	public function testGetDomain()
181
	{
182
		$this->assertEquals( 'product', $this->object->getDomain() );
183
	}
184
185
186
	public function testSetDomain()
187
	{
188
		$return = $this->object->setDomain( 'service' );
189
190
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
191
		$this->assertEquals( 'service', $this->object->getDomain() );
192
		$this->assertTrue( $this->object->isModified() );
193
	}
194
195
196
	public function testGetLabel()
197
	{
198
		$this->assertEquals( 'Price label', $this->object->getLabel() );
199
	}
200
201
202
	public function testSetLabel()
203
	{
204
		$return = $this->object->setLabel( 'special price' );
205
206
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
207
		$this->assertEquals( 'special price', $this->object->getlabel() );
208
		$this->assertTrue( $this->object->isModified() );
209
	}
210
211
212
	public function testGetQuantity()
213
	{
214
		$this->assertEquals( 15, $this->object->getQuantity() );
215
	}
216
217
218
	public function testSetQuantity()
219
	{
220
		$return = $this->object->setQuantity( 20 );
221
222
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
223
		$this->assertEquals( 20, $this->object->getQuantity() );
224
		$this->assertTrue( $this->object->isModified() );
225
	}
226
227
228
	public function testGetValue()
229
	{
230
		$this->assertEquals( '195.50', $this->object->getValue() );
231
	}
232
233
234
	public function testSetValue()
235
	{
236
		$return = $this->object->setValue( 199.00 );
237
238
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
239
		$this->assertEquals( 199.00, $this->object->getValue() );
240
		$this->assertTrue( $this->object->isModified() );
241
242
		$this->expectException( \Aimeos\MShop\Price\Exception::class );
243
		$this->object->setValue( '190,90' );
244
	}
245
246
247
	public function testSetValueEmpty()
248
	{
249
		$return = $this->object->setValue( '' );
250
251
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
252
		$this->assertNull( $this->object->getValue() );
253
		$this->assertTrue( $this->object->isModified() );
254
	}
255
256
257
	public function testSetValueNull()
258
	{
259
		$return = $this->object->setValue( null );
260
261
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
262
		$this->assertNull( $this->object->getValue() );
263
		$this->assertTrue( $this->object->isModified() );
264
	}
265
266
267
	public function testGetCosts()
268
	{
269
		$this->assertEquals( '19.95', $this->object->getCosts() );
270
	}
271
272
273
	public function testSetCosts()
274
	{
275
		$return = $this->object->setValue( '20.00' );
276
277
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
278
		$this->assertEquals( 20.00, $this->object->getValue() );
279
		$this->assertTrue( $this->object->isModified() );
280
281
		$this->expectException( \Aimeos\MShop\Price\Exception::class );
282
		$this->object->setValue( '19,90' );
283
	}
284
285
286
	public function testGetRebate()
287
	{
288
		$this->assertEquals( '10.00', $this->object->getRebate() );
289
	}
290
291
292
	public function testSetRebate()
293
	{
294
		$return = $this->object->setRebate( '20.00' );
295
296
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
297
		$this->assertEquals( 20.00, $this->object->getRebate() );
298
		$this->assertTrue( $this->object->isModified() );
299
300
		$this->expectException( \Aimeos\MShop\Price\Exception::class );
301
		$this->object->setValue( '19,90' );
302
	}
303
304
305
	public function testGetTaxRate()
306
	{
307
		$this->assertEquals( '19.00', $this->object->getTaxRate() );
308
	}
309
310
311
	public function testGetTaxRates()
312
	{
313
		$this->assertEquals( ['tax' => '19.00', 'local' => '5.00'], $this->object->getTaxRates() );
314
	}
315
316
317
	public function testSetTaxRate()
318
	{
319
		$return = $this->object->setTaxRate( '22.00' );
320
321
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
322
		$this->assertEquals( '22.00', $this->object->getTaxRate() );
323
		$this->assertTrue( $this->object->isModified() );
324
	}
325
326
327
	public function testSetTaxRates()
328
	{
329
		$value = ['tax' => '22.00', 'local' => '10.00'];
330
		$return = $this->object->setTaxRates( $value );
331
332
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
333
		$this->assertEquals( ['tax' => '22.00', 'local' => '10.00'], $this->object->getTaxRates() );
334
		$this->assertTrue( $this->object->isModified() );
335
	}
336
337
338
	public function testGetTaxFlag()
339
	{
340
		$this->assertEquals( true, $this->object->getTaxFlag() );
341
	}
342
343
344
	public function testSetTaxFlag()
345
	{
346
		$return = $this->object->setTaxFlag( false );
347
348
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
349
		$this->assertEquals( false, $this->object->getTaxFlag() );
350
		$this->assertTrue( $this->object->isModified() );
351
	}
352
353
354
	public function testGetTaxValue()
355
	{
356
		$this->assertEquals( '34.3995', $this->object->getTaxValue() );
357
	}
358
359
360
	public function testGetTaxValueFromNetprice()
361
	{
362
		$values = array(
363
			'price.quantity' => 10,
364
			'price.value' => 195.50,
365
			'price.costs' => 19.95,
366
			'price.taxrates' => ['' => '19.00', 'local' => '5.00'],
367
			'price.taxflag' => false,
368
		);
369
370
		$object = new \Aimeos\MShop\Price\Item\Standard( $values );
371
		$this->assertEquals( '51.7080', $object->getTaxValue() );
372
373
374
		$values['price.taxflag'] = true;
375
376
		$object = new \Aimeos\MShop\Price\Item\Standard( $values );
377
		$this->assertEquals( '41.7000', $object->getTaxValue() );
378
	}
379
380
381
	public function testSetTaxValue()
382
	{
383
		$return = $this->object->setTaxValue( '100.00' );
384
385
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
386
		$this->assertEquals( '100.0000', $this->object->getTaxValue() );
387
		$this->assertTrue( $this->object->isModified() );
388
	}
389
390
391
	public function testGetStatus()
392
	{
393
		$this->assertEquals( 1, $this->object->getStatus() );
394
	}
395
396
397
	public function testSetStatus()
398
	{
399
		$return = $this->object->setStatus( 0 );
400
401
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $return );
402
		$this->assertEquals( 0, $this->object->getStatus() );
403
		$this->assertTrue( $this->object->isModified() );
404
	}
405
406
407
	public function testGetTimeModified()
408
	{
409
		$this->assertEquals( '2011-01-01 00:00:02', $this->object->getTimeModified() );
410
	}
411
412
413
	public function testGetTimeCreated()
414
	{
415
		$this->assertEquals( '2011-01-01 00:00:01', $this->object->getTimeCreated() );
416
	}
417
418
419
	public function testGetEditor()
420
	{
421
		$this->assertEquals( 'unitTestUser', $this->object->editor() );
422
	}
423
424
425
	public function testGetResourceType()
426
	{
427
		$this->assertEquals( 'price', $this->object->getResourceType() );
428
	}
429
430
431
	public function testFromArray()
432
	{
433
		$item = new \Aimeos\MShop\Price\Item\Standard();
434
435
		$list = $entries = array(
436
			'price.id' => 1,
437
			'price.type' => 'test',
438
			'price.label' => 'test item',
439
			'price.currencyid' => 'EUR',
440
			'price.quantity' => 3,
441
			'price.value' => '10.00',
442
			'price.costs' => '5.00',
443
			'price.rebate' => '2.00',
444
			'price.taxvalue' => '3.0000',
445
			'price.taxrates' => ['tax' => '20.00'],
446
			'price.taxrate' => '20.00',
447
			'price.taxflag' => false,
448
			'price.status' => 0,
449
		);
450
451
		$item = $item->fromArray( $entries, true );
452
453
		$this->assertEquals( [], $entries );
454
		$this->assertEquals( '', $item->getSiteId() );
455
		$this->assertEquals( $list['price.id'], $item->getId() );
456
		$this->assertEquals( $list['price.type'], $item->getType() );
457
		$this->assertEquals( $list['price.label'], $item->getLabel() );
458
		$this->assertEquals( $list['price.currencyid'], $item->getCurrencyId() );
459
		$this->assertEquals( $list['price.quantity'], $item->getQuantity() );
460
		$this->assertEquals( $list['price.value'], $item->getValue() );
461
		$this->assertEquals( $list['price.costs'], $item->getCosts() );
462
		$this->assertEquals( $list['price.rebate'], $item->getRebate() );
463
		$this->assertEquals( $list['price.taxvalue'], $item->getTaxValue() );
464
		$this->assertEquals( $list['price.taxrates'], $item->getTaxRates() );
465
		$this->assertEquals( $list['price.taxrate'], $item->getTaxRate() );
466
		$this->assertEquals( $list['price.taxflag'], $item->getTaxFlag() );
467
		$this->assertEquals( $list['price.status'], $item->getStatus() );
468
	}
469
470
471
	public function testToArray()
472
	{
473
		$arrayObject = $this->object->toArray( true );
474
475
		$this->assertEquals( count( $this->values ), count( $arrayObject ) );
476
477
		$this->assertEquals( $this->object->getId(), $arrayObject['price.id'] );
478
		$this->assertEquals( $this->object->getType(), $arrayObject['price.type'] );
479
		$this->assertEquals( $this->object->getSiteId(), $arrayObject['price.siteid'] );
480
		$this->assertEquals( $this->object->getLabel(), $arrayObject['price.label'] );
481
		$this->assertEquals( $this->object->getDomain(), $arrayObject['price.domain'] );
482
		$this->assertEquals( $this->object->getCurrencyId(), $arrayObject['price.currencyid'] );
483
		$this->assertEquals( $this->object->getQuantity(), $arrayObject['price.quantity'] );
484
		$this->assertEquals( $this->object->getValue(), $arrayObject['price.value'] );
485
		$this->assertEquals( $this->object->getCosts(), $arrayObject['price.costs'] );
486
		$this->assertEquals( $this->object->getRebate(), $arrayObject['price.rebate'] );
487
		$this->assertEquals( $this->object->getTaxValue(), $arrayObject['price.taxvalue'] );
488
		$this->assertEquals( $this->object->getTaxRates(), $arrayObject['price.taxrates'] );
489
		$this->assertEquals( $this->object->getTaxRate(), $arrayObject['price.taxrate'] );
490
		$this->assertEquals( $this->object->getTaxFlag(), $arrayObject['price.taxflag'] );
491
		$this->assertEquals( $this->object->getStatus(), $arrayObject['price.status'] );
492
		$this->assertEquals( $this->object->getTimeCreated(), $arrayObject['price.ctime'] );
493
		$this->assertEquals( $this->object->getTimeModified(), $arrayObject['price.mtime'] );
494
		$this->assertEquals( $this->object->editor(), $arrayObject['price.editor'] );
495
	}
496
497
498
	public function testIsAvailable()
499
	{
500
		$this->assertTrue( $this->object->isAvailable() );
501
		$this->object->setAvailable( false );
502
		$this->assertFalse( $this->object->isAvailable() );
503
	}
504
505
506
	public function testIsAvailableOnStatus()
507
	{
508
		$this->assertTrue( $this->object->isAvailable() );
509
		$this->object->setStatus( 0 );
510
		$this->assertFalse( $this->object->isAvailable() );
511
		$this->object->setStatus( -1 );
512
		$this->assertFalse( $this->object->isAvailable() );
513
	}
514
515
516
	public function testIsModified()
517
	{
518
		$this->assertFalse( $this->object->isModified() );
519
	}
520
}
521