Completed
Push — master ( 325f51...2c9b99 )
by Aimeos
12:01
created

tests/MShop/Product/Manager/StandardTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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-2017
7
 */
8
9
10
namespace Aimeos\MShop\Product\Manager;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $context;
16
	private $object;
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\Product\Manager\Standard( $this->context );
26
	}
27
28
	protected function tearDown()
29
	{
30
		$this->object = null;
31
	}
32
33
34
	public function testCleanup()
35
	{
36
		$this->object->cleanup( array( -1 ) );
37
	}
38
39
40
	public function testCreateItem()
41
	{
42
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Product\\Item\\Iface', $this->object->createItem() );
43
	}
44
45
46
	public function testCreateSearch()
47
	{
48
		$search = $this->object->createSearch();
49
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\SQL', $search );
50
	}
51
52
53
	public function testGetResourceType()
54
	{
55
		$result = $this->object->getResourceType();
56
57
		$this->assertContains( 'product', $result );
58
		$this->assertContains( 'product/type', $result );
59
		$this->assertContains( 'product/lists', $result );
60
		$this->assertContains( 'product/lists/type', $result );
61
		$this->assertContains( 'product/property', $result );
62
		$this->assertContains( 'product/property/type', $result );
63
	}
64
65
66
	public function testGetSearchAttributes()
67
	{
68
		foreach( $this->object->getSearchAttributes() as $attribute ) {
69
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
70
		}
71
	}
72
73
74
	public function testFindItem()
75
	{
76
		$item = $this->object->findItem( 'CNC' );
77
78
		$this->assertEquals( 'CNC', $item->getCode() );
79
	}
80
81
82
	public function testGetItem()
83
	{
84
		$domains = array( 'text', 'product', 'price', 'media', 'attribute', 'product/property' );
85
86
		$search = $this->object->createSearch();
87
		$conditions = array(
88
				$search->compare( '==', 'product.code', 'CNC' ),
89
				$search->compare( '==', 'product.editor', $this->editor )
90
		);
91
		$search->setConditions( $search->combine( '&&', $conditions ) );
92
		$products = $this->object->searchItems( $search, $domains );
93
94
		if( ( $product = reset( $products ) ) === false ) {
95
			throw new \RuntimeException( sprintf( 'Found no Productitem with text "%1$s"', 'Cafe Noire Cappuccino' ) );
96
		}
97
98
		$this->assertEquals( $product, $this->object->getItem( $product->getId(), $domains ) );
99
		$this->assertEquals( 6, count( $product->getRefItems( 'text', null, null, false ) ) );
100
		$this->assertEquals( 4, count( $product->getPropertyItems() ) );
101
		$this->assertNotEquals( '', $product->getTypeName() );
102
	}
103
104
105
	public function testSaveUpdateDeleteItem()
106
	{
107
		$search = $this->object->createSearch();
108
		$conditions = array(
109
				$search->compare( '==', 'product.code', 'CNC' ),
110
				$search->compare( '==', 'product.editor', $this->editor )
111
		);
112
		$search->setConditions( $search->combine( '&&', $conditions ) );
113
		$items = $this->object->searchItems( $search );
114
115
		if( ( $item = reset( $items ) ) === false ) {
116
			throw new \RuntimeException( 'No product item found' );
117
		}
118
119
		$item->setId( null );
120
		$item->setCode( 'CNC unit test' );
121
		$resultSaved = $this->object->saveItem( $item );
122
		$itemSaved = $this->object->getItem( $item->getId() );
123
124
		$itemExp = clone $itemSaved;
125
		$itemExp->setCode( 'unit save test' );
126
		$resultUpd = $this->object->saveItem( $itemExp );
127
		$itemUpd = $this->object->getItem( $itemExp->getId() );
128
129
		$this->object->deleteItem( $itemSaved->getId() );
130
131
132
		$this->assertTrue( $item->getId() !== null );
133
		$this->assertTrue( $itemSaved->getType() !== null );
134
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
135
		$this->assertEquals( $item->getSiteid(), $itemSaved->getSiteId() );
136
		$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() );
137
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
138
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
139
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
140
		$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
141
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
142
		$this->assertEquals( $item->getConfig(), $itemSaved->getConfig() );
143
		$this->assertEquals( $item->getTarget(), $itemSaved->getTarget() );
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->assertTrue( $itemUpd->getType() !== null );
150
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
151
		$this->assertEquals( $itemExp->getSiteid(), $itemUpd->getSiteId() );
152
		$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() );
153
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
154
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
155
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
156
		$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() );
157
		$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() );
158
		$this->assertEquals( $itemExp->getConfig(), $itemUpd->getConfig() );
159
		$this->assertEquals( $itemExp->getTarget(), $itemUpd->getTarget() );
160
161
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
162
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
163
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
164
165
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultSaved );
166
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultUpd );
167
168
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
169
		$this->object->getItem( $itemSaved->getId() );
170
	}
171
172
173
	public function testGetSavePropertyItems()
174
	{
175
		$item = $this->object->findItem( 'CNE', ['product/property'] );
176
177
		$item->setId( null )->setCode( 'xyz' );
178
		$this->object->saveItem( $item );
179
180
		$item2 = $this->object->findItem( 'CNE', ['product/property'] );
181
182
		$this->object->deleteItem( $item->getId() );
183
184
		$this->assertEquals( 4, count( $item->getPropertyItems() ) );
185
		$this->assertEquals( 4, count( $item2->getPropertyItems() ) );
186
	}
187
188
189
	public function testSaveItemRefItems()
190
	{
191
		$context = \TestHelperMShop::getContext();
192
193
		$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $context );
194
		$typeManager = $manager->getSubManager( 'type' );
195
196
		$item = $manager->createItem();
197
		$item->setTypeId( $typeManager->findItem( 'default', [], 'product' )->getId() );
198
		$item->setCode( 'unitreftest' );
199
200
		$listManager = $manager->getSubManager( 'lists' );
201
		$listTypeManager = $listManager->getSubManager( 'type' );
202
203
		$listItem = $listManager->createItem();
204
		$listItem->setTypeId( $listTypeManager->findItem( 'default', [], 'product' )->getId() );
205
206
		$textManager = \Aimeos\MShop\Text\Manager\Factory::createManager( $context );
207
		$textTypeManager = $textManager->getSubManager( 'type' );
208
209
		$textItem = $textManager->createItem();
210
		$textItem->setTypeId( $textTypeManager->findItem( 'name', [], 'product' )->getId() );
211
212
213
		$item->addRefItem( 'text', $listItem, $textItem );
0 ignored issues
show
The method addRefItem() does not seem to exist on object<Aimeos\MShop\Common\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
214
215
		$item = $manager->saveItem( $item );
216
		$item2 = $manager->getItem( $item->getId(), ['text'] );
217
218
		$item->deleteRefItem( 'text', $listItem, $textItem );
219
220
		$item = $manager->saveItem( $item );
221
		$item3 = $manager->getItem( $item->getId(), ['text'] );
222
223
		$manager->deleteItem( $item->getId() );
224
225
226
		$this->assertEquals( 0, count( $item->getRefItems( 'text', 'name', 'default', false ) ) );
227
		$this->assertEquals( 1, count( $item2->getRefItems( 'text', 'name', 'default', false ) ) );
228
		$this->assertEquals( 0, count( $item3->getRefItems( 'text', 'name', 'default', false ) ) );
229
	}
230
231
232
	public function testSaveItemSitecheck()
233
	{
234
		$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperMShop::getContext() );
235
236
		$search = $manager->createSearch();
237
		$search->setConditions( $search->compare( '==', 'product.editor', $this->editor ) );
238
		$search->setSlice( 0, 1 );
239
		$products = $manager->searchItems( $search );
240
241
		if( ( $item = reset( $products ) ) === false ) {
242
			throw new \RuntimeException( 'No product found' );
243
		}
244
245
		$item->setId( null );
246
		$item->setCode( 'unittest' );
247
248
		$manager->saveItem( $item );
249
		$manager->getItem( $item->getId() );
250
		$manager->deleteItem( $item->getId() );
251
252
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
253
		$manager->getItem( $item->getId() );
254
	}
255
256
257
	public function testUpdateListItems()
258
	{
259
		$attrManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( $this->context );
260
		$attrId = $attrManager->findItem( 's', [], 'product', 'size' )->getId();
261
		$item = $this->object->findItem( 'CNC', array( 'attribute' ) );
262
263
		// create new list item
264
		$map = array( $attrId => array( 'product.lists.datestart' => '2000-01-01 00:00:00' ) );
265
		$this->object->updateListItems( $item, $map, 'attribute', 'hidden' );
266
267
		$item = $this->object->findItem( 'CNC', array( 'attribute' ) );
268
		$listItems = $item->getListItems( 'attribute', 'hidden', null, false );
269
270
		$this->assertEquals( 1, count( $listItems ) );
271
		$this->assertEquals( '2000-01-01 00:00:00', reset( $listItems )->getDateStart() );
272
273
274
		// update existing list item
275
		$map = array( $attrId => array( 'product.lists.config' => array( 'key' => 'value' ) ) );
276
		$this->object->updateListItems( $item, $map, 'attribute', 'hidden' );
277
278
		$item = $this->object->findItem( 'CNC', array( 'attribute' ) );
279
		$listItems = $item->getListItems( 'attribute', 'hidden' );
280
281
		$this->assertEquals( 1, count( $listItems ) );
282
		$this->assertEquals( '2000-01-01 00:00:00', reset( $listItems )->getDateStart() );
283
		$this->assertEquals( array( 'key' => 'value' ), reset( $listItems )->getConfig() );
284
285
286
		// delete existing list item
287
		$this->object->updateListItems( $item, [], 'attribute', 'hidden' );
288
289
		$item = $this->object->findItem( 'CNC', array( 'attribute' ) );
290
		$this->assertEquals( 0, count( $item->getListItems( 'attribute', 'hidden' ) ) );
291
	}
292
293
294
	public function testSearchItems()
295
	{
296
		$total = 0;
297
		$listManager = $this->object->getSubManager( 'lists' );
298
299
		$search = $listManager->createSearch();
300
		$expr = array(
301
			$search->compare( '==', 'product.lists.type.domain', 'product' ),
302
			$search->compare( '==', 'product.lists.type.code', 'suggestion' ),
303
			$search->compare( '==', 'product.lists.datestart', null ),
304
			$search->compare( '==', 'product.lists.dateend', null ),
305
			$search->compare( '!=', 'product.lists.config', null ),
306
			$search->compare( '==', 'product.lists.position', 0 ),
307
			$search->compare( '==', 'product.lists.status', 1 ),
308
			$search->compare( '==', 'product.lists.editor', $this->editor ),
309
		);
310
		$search->setConditions( $search->combine( '&&', $expr ) );
311
312
		$results = $listManager->searchItems( $search );
313
		if( ( $listItem = reset( $results ) ) === false ) {
314
			throw new \RuntimeException( 'No list item found' );
315
		}
316
317
		$search = $this->object->createSearch();
318
319
		$expr = [];
320
		$expr[] = $search->compare( '!=', 'product.id', null );
321
		$expr[] = $search->compare( '!=', 'product.siteid', null );
322
		$expr[] = $search->compare( '!=', 'product.typeid', null );
323
		$expr[] = $search->compare( '==', 'product.code', 'CNE' );
324
		$expr[] = $search->compare( '==', 'product.label', 'Cafe Noire Expresso' );
325
		$expr[] = $search->compare( '~=', 'product.config', 'css-class' );
326
		$expr[] = $search->compare( '==', 'product.datestart', null );
327
		$expr[] = $search->compare( '==', 'product.dateend', null );
328
		$expr[] = $search->compare( '==', 'product.status', 1 );
329
		$expr[] = $search->compare( '>=', 'product.ctime', '1970-01-01 00:00:00' );
330
		$expr[] = $search->compare( '>=', 'product.mtime', '1970-01-01 00:00:00' );
331
		$expr[] = $search->compare( '==', 'product.editor', $this->editor );
332
		$expr[] = $search->compare( '>=', 'product.target', '' );
333
334
		$param = array( 'product', $listItem->getTypeId(), array( $listItem->getRefId() ) );
335
		$expr[] = $search->compare( '>', $search->createFunction( 'product.contains', $param ), 0 );
336
337
		$expr[] = $search->compare( '!=', 'product.type.id', null );
338
		$expr[] = $search->compare( '!=', 'product.type.siteid', null );
339
		$expr[] = $search->compare( '==', 'product.type.domain', 'product' );
340
		$expr[] = $search->compare( '==', 'product.type.code', 'default' );
341
		$expr[] = $search->compare( '==', 'product.type.label', 'Article' );
342
		$expr[] = $search->compare( '==', 'product.type.status', 1 );
343
		$expr[] = $search->compare( '==', 'product.type.editor', $this->editor );
344
345
		$expr[] = $search->compare( '!=', 'product.lists.id', null );
346
		$expr[] = $search->compare( '!=', 'product.lists.siteid', null );
347
		$expr[] = $search->compare( '!=', 'product.lists.parentid', null );
348
		$expr[] = $search->compare( '!=', 'product.lists.typeid', null );
349
		$expr[] = $search->compare( '==', 'product.lists.domain', 'product' );
350
		$expr[] = $search->compare( '>', 'product.lists.refid', 0 );
351
		$expr[] = $search->compare( '==', 'product.lists.datestart', null );
352
		$expr[] = $search->compare( '==', 'product.lists.dateend', null );
353
		$expr[] = $search->compare( '!=', 'product.lists.config', null );
354
		$expr[] = $search->compare( '==', 'product.lists.position', 0 );
355
		$expr[] = $search->compare( '==', 'product.lists.status', 1 );
356
		$expr[] = $search->compare( '==', 'product.lists.editor', $this->editor );
357
358
		$expr[] = $search->compare( '!=', 'product.lists.type.id', null );
359
		$expr[] = $search->compare( '!=', 'product.lists.type.siteid', null );
360
		$expr[] = $search->compare( '==', 'product.lists.type.domain', 'product' );
361
		$expr[] = $search->compare( '==', 'product.lists.type.code', 'suggestion' );
362
		$expr[] = $search->compare( '==', 'product.lists.type.label', 'Suggestion' );
363
		$expr[] = $search->compare( '==', 'product.lists.type.status', 1 );
364
		$expr[] = $search->compare( '==', 'product.lists.type.editor', $this->editor );
365
366
		$expr[] = $search->compare( '!=', 'product.property.id', null );
367
		$expr[] = $search->compare( '!=', 'product.property.siteid', null );
368
		$expr[] = $search->compare( '!=', 'product.property.typeid', null );
369
		$expr[] = $search->compare( '==', 'product.property.languageid', null );
370
		$expr[] = $search->compare( '==', 'product.property.value', '1' );
371
		$expr[] = $search->compare( '==', 'product.property.editor', $this->editor );
372
373
		$expr[] = $search->compare( '!=', 'product.property.type.id', null );
374
		$expr[] = $search->compare( '!=', 'product.property.type.siteid', null );
375
		$expr[] = $search->compare( '==', 'product.property.type.code', 'package-weight' );
376
		$expr[] = $search->compare( '==', 'product.property.type.domain', 'product' );
377
		$expr[] = $search->compare( '==', 'product.property.type.label', 'Package Weight' );
378
		$expr[] = $search->compare( '==', 'product.property.type.status', 1 );
379
		$expr[] = $search->compare( '>=', 'product.property.type.mtime', '1970-01-01 00:00:00' );
380
		$expr[] = $search->compare( '>=', 'product.property.type.ctime', '1970-01-01 00:00:00' );
381
		$expr[] = $search->compare( '==', 'product.property.type.editor', $this->editor );
382
383
384
		$search->setConditions( $search->combine( '&&', $expr ) );
385
		$search->setSlice( 0, 1 );
386
387
		$results = $this->object->searchItems( $search, [], $total );
388
		$this->assertEquals( 1, count( $results ) );
389
		$this->assertEquals( 1, $total );
390
391
		foreach( $results as $itemId => $item ) {
392
			$this->assertEquals( $itemId, $item->getId() );
393
		}
394
395
		$search = $this->object->createSearch();
396
		$search->setConditions( $search->compare( '==', 'product.editor', $this->editor ) );
397
		$search->setSlice( 0, 10 );
398
		$results = $this->object->searchItems( $search, [], $total );
399
		$this->assertEquals( 10, count( $results ) );
400
		$this->assertEquals( 28, $total );
401
402
403
		$search = $this->object->createSearch( true );
404
		$expr = array(
405
			$search->compare( '==', 'product.code', array( 'CNC', 'CNE' ) ),
406
			$search->compare( '==', 'product.editor', $this->editor ),
407
			$search->getConditions(),
408
		);
409
		$search->setConditions( $search->combine( '&&', $expr ) );
410
		$result = $this->object->searchItems( $search, array( 'media' ) );
411
412
		$this->assertEquals( 2, count( $result ) );
413
	}
414
415
416
	public function testSearchWildcards()
417
	{
418
		$search = $this->object->createSearch();
419
		$search->setConditions( $search->compare( '=~', 'product.code', 'CN_' ) );
420
		$result = $this->object->searchItems( $search );
421
422
		$this->assertEquals( 0, count( $result ) );
423
424
425
		$search = $this->object->createSearch();
426
		$search->setConditions( $search->compare( '=~', 'product.code', 'CN%' ) );
427
		$result = $this->object->searchItems( $search );
428
429
		$this->assertEquals( 0, count( $result ) );
430
431
432
		$search = $this->object->createSearch();
433
		$search->setConditions( $search->compare( '=~', 'product.code', 'CN[C]' ) );
434
		$result = $this->object->searchItems( $search );
435
436
		$this->assertEquals( 0, count( $result ) );
437
	}
438
439
440
	public function testSearchItemsLimit()
441
	{
442
		$start = 0;
443
		$numproducts = 0;
444
445
		$search = $this->object->createSearch();
446
		$search->setConditions( $search->compare( '==', 'product.editor', 'core:unittest' ) );
447
		$search->setSlice( $start, 5 );
448
449
		do
450
		{
451
			$result = $this->object->searchItems( $search );
452
453
			foreach( $result as $item ) {
454
				$numproducts++;
455
			}
456
457
			$count = count( $result );
458
			$start += $count;
459
			$search->setSlice( $start, 5 );
460
		}
461
		while( $count > 0 );
462
463
		$this->assertEquals( 28, $numproducts );
464
	}
465
466
467
	public function testGetSubManager()
468
	{
469
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'lists' ) );
470
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'lists', 'Standard' ) );
471
472
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'property' ) );
473
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'property', 'Standard' ) );
474
475
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type' ) );
476
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type', 'Standard' ) );
477
478
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
479
		$this->object->getSubManager( 'unknown' );
480
	}
481
482
483
	public function testGetSubManagerInvalidName()
484
	{
485
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
486
		$this->object->getSubManager( 'lists', 'unknown' );
487
	}
488
}
489