Passed
Push — master ( f7bbd3...17cd43 )
by Aimeos
04:27
created

StandardTest::testSearchItemsProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 29
nc 1
nop 0
dl 0
loc 38
rs 9.456
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-2018
7
 */
8
9
10
namespace Aimeos\MShop\Price\Manager;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $editor = '';
17
18
19
	protected function setUp()
20
	{
21
		$this->editor = \TestHelperMShop::getContext()->getEditor();
22
		$this->object = \Aimeos\MShop\Price\Manager\Factory::create( \TestHelperMShop::getContext() );
23
	}
24
25
	protected function tearDown()
26
	{
27
		unset( $this->object );
28
	}
29
30
31
	public function testCleanup()
32
	{
33
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->cleanup( [-1] ) );
34
	}
35
36
37
	public function testDeleteItems()
38
	{
39
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->deleteItems( [-1] ) );
40
	}
41
42
43
	public function testGetResourceType()
44
	{
45
		$result = $this->object->getResourceType();
46
47
		$this->assertContains( 'price', $result );
48
		$this->assertContains( 'price/lists', $result );
49
	}
50
51
52
	public function testGetSearchAttributes()
53
	{
54
		foreach( $this->object->getSearchAttributes() as $object ) {
55
			$this->assertInstanceOf( \Aimeos\MW\Criteria\Attribute\Iface::class, $object );
56
		}
57
	}
58
59
60
	public function testCreateItem()
61
	{
62
		$this->assertInstanceOf( \Aimeos\MShop\Price\Item\Iface::class, $this->object->createItem() );
63
	}
64
65
66
	public function testCreateItemType()
67
	{
68
		$item = $this->object->createItem( ['price.type' => 'default'] );
69
		$this->assertEquals( 'default', $item->getType() );
70
	}
71
72
73
	public function testGetItem()
74
	{
75
		$search = $this->object->createSearch()->setSlice( 0, 1 );
76
		$conditions = array(
77
			$search->compare( '==', 'price.value', 12.00 ),
78
			$search->compare( '==', 'price.editor', $this->editor )
79
		);
80
		$search->setConditions( $search->combine( '&&', $conditions ) );
81
		$results = $this->object->searchItems( $search );
82
83
		if( ( $item = reset( $results ) ) === false ) {
84
			throw new \RuntimeException( 'No results available' );
85
		}
86
87
		$itemB = $this->object->getItem( $item->getId() );
88
		$this->assertEquals( 19.00, $itemB->getTaxRate() );
89
	}
90
91
92
	public function testSaveInvalid()
93
	{
94
		$this->setExpectedException( \Aimeos\MW\Common\Exception::class );
95
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
96
	}
97
98
99
	public function testSaveUpdateDeleteItem()
100
	{
101
		$search = $this->object->createSearch();
102
		$search->setConditions( $search->compare( '==', 'price.editor', $this->editor ) );
103
		$items = $this->object->searchItems( $search );
104
105
		if( ( $item = reset( $items ) ) === false ) {
106
			throw new \RuntimeException( 'No item found' );
107
		}
108
109
		$item->setId( null );
110
		$item->setLabel( 'price label' );
111
		$resultSaved = $this->object->saveItem( $item );
112
		$itemSaved = $this->object->getItem( $item->getId() );
113
114
		$itemExp = clone $itemSaved;
115
		$itemExp->setDomain( 'unittest' );
116
		$resultUpd = $this->object->saveItem( $itemExp );
117
		$itemUpd = $this->object->getItem( $itemExp->getId() );
118
119
		$this->object->deleteItem( $itemSaved->getId() );
120
121
122
		$this->assertTrue( $item->getId() !== null );
123
		$this->assertTrue( $itemSaved->getType() !== null );
124
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
125
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
126
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
127
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
128
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
129
		$this->assertEquals( $item->getCurrencyId(), $itemSaved->getCurrencyId() );
130
		$this->assertEquals( $item->getQuantity(), $itemSaved->getQuantity() );
131
		$this->assertEquals( $item->getValue(), $itemSaved->getValue() );
132
		$this->assertEquals( $item->getCosts(), $itemSaved->getCosts() );
133
		$this->assertEquals( $item->getRebate(), $itemSaved->getRebate() );
134
		$this->assertEquals( $item->getTaxRate(), $itemSaved->getTaxRate() );
135
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
136
137
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
138
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
139
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
140
141
		$this->assertTrue( $itemUpd->getType() !== null );
142
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
143
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
144
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
145
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
146
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
147
		$this->assertEquals( $itemExp->getCurrencyId(), $itemUpd->getCurrencyId() );
148
		$this->assertEquals( $itemExp->getQuantity(), $itemUpd->getQuantity() );
149
		$this->assertEquals( $itemExp->getValue(), $itemUpd->getValue() );
150
		$this->assertEquals( $itemExp->getCosts(), $itemUpd->getCosts() );
151
		$this->assertEquals( $itemExp->getRebate(), $itemUpd->getRebate() );
152
		$this->assertEquals( $itemExp->getTaxRate(), $itemUpd->getTaxRate() );
153
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
154
155
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
156
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
157
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
158
159
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
160
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
161
162
		$this->setExpectedException( \Aimeos\MShop\Exception::class );
163
		$this->object->getItem( $itemSaved->getId() );
164
	}
165
166
167
	public function testCreateSearch()
168
	{
169
		$this->assertInstanceOf( \Aimeos\MW\Criteria\SQL::class, $this->object->createSearch() );
170
	}
171
172
173
	public function testSearchItems()
174
	{
175
		$search = $this->object->createSearch();
176
		$search->setConditions( $search->compare( '==', 'price.value', '99.99' ) );
177
		$item = current( $this->object->searchItems( $search, ['customer'] ) );
178
179
		if( $item && ( $listItem = current( $item->getListItems( 'customer', 'test' ) ) ) === false ) {
180
			throw new \RuntimeException( 'No list item found' );
181
		}
182
183
		$total = 0;
184
		$search = $this->object->createSearch();
185
186
		$expr = [];
187
		$expr[] = $search->compare( '!=', 'price.id', null );
188
		$expr[] = $search->compare( '!=', 'price.siteid', null );
189
		$expr[] = $search->compare( '==', 'price.type', 'default' );
190
		$expr[] = $search->compare( '==', 'price.domain', 'attribute' );
191
		$expr[] = $search->compare( '>=', 'price.label', '' );
192
		$expr[] = $search->compare( '==', 'price.currencyid', 'EUR' );
193
		$expr[] = $search->compare( '==', 'price.quantity', 1 );
194
		$expr[] = $search->compare( '==', 'price.value', '99.99' );
195
		$expr[] = $search->compare( '==', 'price.costs', '9.99' );
196
		$expr[] = $search->compare( '==', 'price.rebate', '0.00' );
197
		$expr[] = $search->compare( '==', 'price.taxrate', '19.00' );
198
		$expr[] = $search->compare( '==', 'price.status', 1 );
199
		$expr[] = $search->compare( '>=', 'price.mtime', '1970-01-01 00:00:00' );
200
		$expr[] = $search->compare( '>=', 'price.ctime', '1970-01-01 00:00:00' );
201
		$expr[] = $search->compare( '==', 'price.editor', $this->editor );
202
203
		$param = ['customer','test', '0'];
204
		$expr[] = $search->compare( '==', $search->createFunction( 'price:has', $param ), null );
205
206
		$param = ['customer','test', $listItem->getRefId()];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $listItem does not seem to be defined for all execution paths leading up to this point.
Loading history...
207
		$expr[] = $search->compare( '!=', $search->createFunction( 'price:has', $param ), null );
208
209
		$param = ['customer','test'];
210
		$expr[] = $search->compare( '!=', $search->createFunction( 'price:has', $param ), null );
211
212
		$param = ['customer'];
213
		$expr[] = $search->compare( '!=', $search->createFunction( 'price:has', $param ), null );
214
215
		$search->setConditions( $search->combine( '&&', $expr ) );
216
		$results = $this->object->searchItems( $search, [], $total );
217
218
		$this->assertEquals( 1, count( $results ) );
219
	}
220
221
222
	public function testSearchItemsProperty()
223
	{
224
		$total = 0;
225
		$search = $this->object->createSearch();
226
227
		$expr = [];
228
		$expr[] = $search->compare( '!=', 'price.id', null );
229
		$expr[] = $search->compare( '!=', 'price.siteid', null );
230
		$expr[] = $search->compare( '==', 'price.type', 'default' );
231
		$expr[] = $search->compare( '==', 'price.domain', 'product' );
232
		$expr[] = $search->compare( '>=', 'price.label', '' );
233
		$expr[] = $search->compare( '==', 'price.currencyid', 'EUR' );
234
		$expr[] = $search->compare( '==', 'price.quantity', 1 );
235
		$expr[] = $search->compare( '==', 'price.value', '600.00' );
236
		$expr[] = $search->compare( '==', 'price.costs', '30.00' );
237
		$expr[] = $search->compare( '==', 'price.rebate', '0.00' );
238
		$expr[] = $search->compare( '==', 'price.taxrate', '19.00' );
239
		$expr[] = $search->compare( '==', 'price.status', 1 );
240
		$expr[] = $search->compare( '>=', 'price.mtime', '1970-01-01 00:00:00' );
241
		$expr[] = $search->compare( '>=', 'price.ctime', '1970-01-01 00:00:00' );
242
		$expr[] = $search->compare( '==', 'price.editor', $this->editor );
243
244
		$param = ['taxrate-local', null, '0.0'];
245
		$expr[] = $search->compare( '==', $search->createFunction( 'price:prop', $param ), null );
246
247
		$param = ['taxrate-local', null, '5.0'];
248
		$expr[] = $search->compare( '!=', $search->createFunction( 'price:prop', $param ), null );
249
250
		$param = ['taxrate-local', null];
251
		$expr[] = $search->compare( '!=', $search->createFunction( 'price:prop', $param ), null );
252
253
		$param = ['taxrate-local'];
254
		$expr[] = $search->compare( '!=', $search->createFunction( 'price:prop', $param ), null );
255
256
		$search->setConditions( $search->combine( '&&', $expr ) );
257
		$results = $this->object->searchItems( $search, [], $total );
258
259
		$this->assertEquals( 1, count( $results ) );
260
	}
261
262
263
	public function testSearchItemsTotal()
264
	{
265
		$total = 0;
266
		$search = $this->object->createSearch();
267
		$search->setConditions( $search->compare( '==', 'price.editor', $this->editor ) );
268
		$search->setSlice( 0, 10 );
269
		$results = $this->object->searchItems( $search, [], $total );
270
		$this->assertEquals( 10, count( $results ) );
271
		$this->assertEquals( 27, $total );
272
	}
273
274
275
	public function testSearchItemsBase()
276
	{
277
		$search = $this->object->createSearch( true );
278
		$conditions = array(
279
			$search->compare( '==', 'price.editor', $this->editor ),
280
			$search->getConditions()
281
		);
282
		$search->setConditions( $search->combine( '&&', $conditions ) );
283
		$results = $this->object->searchItems( $search );
284
		$this->assertEquals( 25, count( $results ) );
285
286
		foreach( $results as $itemId => $item ) {
287
			$this->assertEquals( $itemId, $item->getId() );
288
		}
289
	}
290
291
292
	public function testGetSubManager()
293
	{
294
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type' ) );
295
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type', 'Standard' ) );
296
	}
297
298
299
	public function testGetLowestPrice()
300
	{
301
		$item = $this->object->createItem();
302
		$item->setValue( '1.00' );
303
304
		$lowest = $this->object->getLowestPrice( array( $item ), 1 );
305
306
		$this->assertEquals( $item, $lowest );
307
	}
308
309
310
	public function testGetLowestPriceQuantity()
311
	{
312
		$item = $this->object->createItem();
313
		$item->setValue( '10.00' );
314
315
		$item2 = $this->object->createItem();
316
		$item2->setValue( '5.00' );
317
		$item2->setQuantity( 5 );
318
319
		$lowest = $this->object->getLowestPrice( array( $item, $item2 ), 10 );
320
321
		$this->assertEquals( $item2, $lowest );
322
	}
323
324
325
	public function testGetLowestPriceCurrency()
326
	{
327
		$item = $this->object->createItem();
328
		$item->setValue( '1.00' );
329
330
		$this->setExpectedException( \Aimeos\MShop\Price\Exception::class );
331
		$this->object->getLowestPrice( array( $item ), 1, 'USD' );
332
	}
333
334
335
	public function testGetLowestPriceNoPrice()
336
	{
337
		$this->setExpectedException( \Aimeos\MShop\Price\Exception::class );
338
		$this->object->getLowestPrice( [], 1 );
339
	}
340
341
342
	public function testGetLowestPriceNoPriceForQuantity()
343
	{
344
		$item = $this->object->createItem();
345
		$item->setValue( '1.00' );
346
		$item->setQuantity( 5 );
347
348
		$this->setExpectedException( \Aimeos\MShop\Price\Exception::class );
349
		$this->object->getLowestPrice( array( $item ), 1 );
350
	}
351
352
353
	public function testGetLowestPriceWrongItem()
354
	{
355
		$this->setExpectedException( \Aimeos\MW\Common\Exception::class );
356
		$this->object->getLowestPrice( array( new \stdClass() ), 1 );
357
	}
358
}
359