Completed
Push — master ( 0b39e0...ec78c4 )
by Aimeos
09:03
created

StandardTest::testSearchItems()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 92
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 70
nc 5
nop 0
dl 0
loc 92
rs 8.3275
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Catalog\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 = new \Aimeos\MShop\Catalog\Manager\Standard( \TestHelperMShop::getContext() );
23
	}
24
25
26
	protected function tearDown()
27
	{
28
		unset( $this->object );
29
	}
30
31
32
	public function testCleanup()
33
	{
34
		$this->object->cleanup( array( -1 ) );
35
	}
36
37
38
	public function testCreateItem()
39
	{
40
		$item = $this->object->createItem();
41
42
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Catalog\\Item\\Iface', $item );
43
		$this->assertEquals( \TestHelperMShop::getContext()->getLocale()->getSiteId(), $item->getNode()->siteid );
44
	}
45
46
47
	public function testCreateSearch()
48
	{
49
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $this->object->createSearch() );
50
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $this->object->createSearch( true ) );
51
	}
52
53
54
	public function testDeleteItems()
55
	{
56
		$this->setExpectedException( 'Aimeos\MW\Tree\Exception' );
57
		$this->object->deleteItems( array( -1 ) );
58
	}
59
60
61
	public function testGetResourceType()
62
	{
63
		$result = $this->object->getResourceType();
64
65
		$this->assertContains( 'catalog', $result );
66
		$this->assertContains( 'catalog/lists', $result );
67
		$this->assertContains( 'catalog/lists/type', $result );
68
	}
69
70
71
	public function testGetSearchAttributes()
72
	{
73
		foreach( $this->object->getSearchAttributes() as $attribute ) {
74
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
75
		}
76
	}
77
78
79
	public function testRegisterItemFilter()
80
	{
81
		$callback = function( \Aimeos\MShop\Common\Item\ListRef\Iface $item, $index )
82
		{
83
			return true;
84
		};
85
86
		$this->object->registerItemFilter( 'test', $callback );
87
	}
88
89
90
	public function testSearchItems()
91
	{
92
		$listManager = $this->object->getSubManager( 'lists' );
93
94
		$search = $listManager->createSearch();
95
		$expr = array(
96
			$search->compare( '==', 'catalog.lists.type.domain', 'product' ),
97
			$search->compare( '==', 'catalog.lists.type.code', 'promotion' ),
98
			$search->compare( '==', 'catalog.lists.datestart', '2010-01-01 00:00:00' ),
99
			$search->compare( '==', 'catalog.lists.dateend', '2099-01-01 00:00:00' ),
100
			$search->compare( '!=', 'catalog.lists.config', null ),
101
			$search->compare( '==', 'catalog.lists.position', 0 ),
102
			$search->compare( '==', 'catalog.lists.status', 1 ),
103
			$search->compare( '==', 'catalog.lists.editor', $this->editor ),
104
		);
105
		$search->setConditions( $search->combine( '&&', $expr ) );
106
107
		$results = $listManager->searchItems( $search );
108
		if( ( $listItem = reset( $results ) ) === false ) {
109
			throw new \RuntimeException( 'No list item found' );
110
		}
111
112
		$search = $this->object->createSearch();
113
114
		$expr = array();
115
		$expr[] = $search->compare( '!=', 'catalog.id', null );
116
		$expr[] = $search->compare( '!=', 'catalog.siteid', null );
117
		$expr[] = $search->compare( '==', 'catalog.code', 'cafe' );
118
		$expr[] = $search->compare( '==', 'catalog.level', 2 );
119
		$expr[] = $search->compare( '==', 'catalog.left', 3 );
120
		$expr[] = $search->compare( '==', 'catalog.right', 4 );
121
		$expr[] = $search->compare( '==', 'catalog.status', 1 );
122
		$expr[] = $search->compare( '==', 'catalog.label', 'Kaffee' );
123
		$expr[] = $search->compare( '~=', 'catalog.config', '{' );
124
		$expr[] = $search->compare( '>=', 'catalog.mtime', '1970-01-01 00:00:00' );
125
		$expr[] = $search->compare( '>=', 'catalog.ctime', '1970-01-01 00:00:00' );
126
		$expr[] = $search->compare( '==', 'catalog.editor', $this->editor );
127
128
		$param = array( 'product', $listItem->getTypeId(), array( $listItem->getRefId() ) );
129
		$expr[] = $search->compare( '>', $search->createFunction( 'catalog.contains', $param ), 0 );
130
131
		$expr[] = $search->compare( '!=', 'catalog.lists.id', null );
132
		$expr[] = $search->compare( '!=', 'catalog.lists.siteid', null );
133
		$expr[] = $search->compare( '!=', 'catalog.lists.parentid', null );
134
		$expr[] = $search->compare( '!=', 'catalog.lists.typeid', null );
135
		$expr[] = $search->compare( '!=', 'catalog.lists.refid', null );
136
		$expr[] = $search->compare( '>=', 'catalog.lists.datestart', '1970-01-01 00:00:00' );
137
		$expr[] = $search->compare( '>=', 'catalog.lists.dateend', '1970-01-01 00:00:00' );
138
		$expr[] = $search->compare( '==', 'catalog.lists.status', 1 );
139
		$expr[] = $search->compare( '!=', 'catalog.lists.config', null );
140
		$expr[] = $search->compare( '>=', 'catalog.lists.position', 0 );
141
		$expr[] = $search->compare( '>=', 'catalog.lists.mtime', '1970-01-01 00:00:00' );
142
		$expr[] = $search->compare( '>=', 'catalog.lists.ctime', '1970-01-01 00:00:00' );
143
		$expr[] = $search->compare( '==', 'catalog.lists.editor', $this->editor );
144
145
		$expr[] = $search->compare( '!=', 'catalog.lists.type.id', null );
146
		$expr[] = $search->compare( '!=', 'catalog.lists.type.siteid', null );
147
		$expr[] = $search->compare( '>=', 'catalog.lists.type.code', '' );
148
		$expr[] = $search->compare( '==', 'catalog.lists.type.status', 1 );
149
		$expr[] = $search->compare( '>=', 'catalog.lists.type.label', '' );
150
		$expr[] = $search->compare( '>=', 'catalog.lists.type.mtime', '1970-01-01 00:00:00' );
151
		$expr[] = $search->compare( '>=', 'catalog.lists.type.ctime', '1970-01-01 00:00:00' );
152
		$expr[] = $search->compare( '==', 'catalog.lists.type.editor', $this->editor );
153
154
155
		$total = 0;
156
		$search->setConditions( $search->combine( '&&', $expr ) );
157
		$search->setSlice( 0, 1 );
158
159
		$items = $this->object->searchItems( $search, array(), $total );
160
161
		$this->assertEquals( 1, $total );
162
		$this->assertEquals( 1, count( $items ) );
163
164
		foreach( $items as $itemId => $item ) {
165
			$this->assertInstanceOf( '\\Aimeos\\MShop\\Catalog\\Item\\Iface', $item );
166
			$this->assertEquals( $itemId, $item->getId() );
167
		}
168
169
		$conditions = array(
170
			$search->compare( '==', 'catalog.label', 'Misc' ),
171
			$search->compare( '==', 'catalog.editor', $this->editor )
172
		);
173
		$search->setConditions( $search->combine( '&&', $conditions ) );
174
		$items = $this->object->searchItems( $search, array( 'text' ) );
175
176
		if( ( $item = reset( $items ) ) === false ) {
177
			throw new \RuntimeException( 'Catalog item not found' );
178
		}
179
180
		$this->assertEquals( 'Sonstiges', $item->getName() );
181
	}
182
183
184
	public function testFindItem()
185
	{
186
		$item = $this->object->findItem( 'root' );
187
188
		$this->assertEquals( 'root', $item->getCode() );
189
	}
190
191
192
	public function testGetItem()
193
	{
194
		$search = $this->object->createSearch();
195
		$conditions = array(
196
			$search->compare( '==', 'catalog.label', 'Root' ),
197
			$search->compare( '==', 'catalog.editor', $this->editor )
198
		);
199
		$search->setConditions( $search->combine( '&&', $conditions ) );
200
		$items = $this->object->searchItems( $search, array( 'text' ) );
201
202
		if( ( $item = reset( $items ) ) === false ) {
203
			throw new \RuntimeException( 'Catalog item not found' );
204
		}
205
206
		$testItem = $this->object->getItem( $item->getId() );
207
208
		$this->assertEquals( \TestHelperMShop::getContext()->getLocale()->getSiteId(), $testItem->getSiteId() );
209
		$this->assertEquals( $item->getId(), $testItem->getId() );
210
		$this->assertEquals( 'Root', $testItem->getLabel() );
211
		$this->assertEquals( 'Root', $testItem->getName() );
212
		$this->assertEquals( 1, $testItem->getStatus() );
213
	}
214
215
216
	public function testGetTree()
217
	{
218
		$search = $this->object->createSearch();
219
		$conditions = array(
220
			$search->compare( '==', 'catalog.code', 'root' ),
221
			$search->compare( '==', 'catalog.editor', $this->editor )
222
		);
223
		$search->setConditions( $search->combine( '&&', $conditions ) );
224
		$items = $this->object->searchItems( $search );
225
226
		if( ( $item = reset( $items ) ) === false ) {
227
			throw new \RuntimeException( 'Catalog item not found' );
228
		}
229
230
		$rootItem = $this->object->getTree( $item->getId(), array( 'text' ), \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE );
231
		$categoryItem = $rootItem->getChild( 0 );
232
		$miscItem = $categoryItem->getChild( 2 );
233
234
		$this->assertEquals( \TestHelperMShop::getContext()->getLocale()->getSiteId(), $miscItem->getSiteId() );
235
		$this->assertEquals( 'Misc', $miscItem->getLabel() );
236
		$this->assertEquals( 'Sonstiges', $miscItem->getName() );
237
	}
238
239
240
	public function testGetTreeWithConditions()
241
	{
242
		$search = $this->object->createSearch();
243
		$conditions = array(
244
			$search->compare( '==', 'catalog.code', array( 'root', 'categories' ) ),
245
			$search->compare( '==', 'catalog.editor', $this->editor )
246
		);
247
		$search->setConditions( $search->combine( '&&', $conditions ) );
248
		$items = $this->object->searchItems( $search );
249
		$parentIds = array();
250
251
		foreach( $items as $item ) {
252
			$parentIds[] = $item->getId();
253
		}
254
255
		if( count( $parentIds ) != 2 ) {
256
			throw new \RuntimeException( 'Not all categories found!' );
257
		}
258
259
		$parentIds[] = 0;
260
261
		$search = $this->object->createSearch();
262
		$search->setConditions( $search->compare( '==', 'catalog.parentid', $parentIds ) );
263
264
		$tree = $this->object->getTree( null, array(), \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE, $search );
265
266
		$categorycat = $tree->getChild( 0 );
267
		$groupcat = $tree->getChild( 1 );
268
		$groupcatChildren = $groupcat->getChildren();
269
		$categorycatChildren = $categorycat->getChildren();
270
		$cafecat = $categorycat->getChild( 0 );
271
272
		$caffein = $this->object->createItem();
273
		$caffein->setCode( 'caffein' );
274
		$caffein->setLabel( 'Caffein' );
275
276
		$this->object->insertItem( $caffein, $cafecat->getId() );
277
		$this->object->deleteItem( $caffein->getId() );
278
279
		$this->assertEquals( 0, $tree->getNode()->parentid );
280
		$this->assertEquals( 'categories', $categorycat->getCode() );
281
		$this->assertEquals( 'group', $groupcat->getCode() );
282
		$this->assertEquals( $tree->getId(), $categorycat->getNode()->parentid );
283
		$this->assertEquals( $tree->getId(), $groupcat->getNode()->parentid );
284
		$this->assertEquals( $categorycat->getId(), $cafecat->getNode()->parentid );
285
		$this->assertEquals( $cafecat->getId(), $caffein->getNode()->parentid );
286
		$this->assertEquals( array(), $groupcatChildren );
287
		$this->assertEquals( 3, count( $categorycatChildren ) );
288
	}
289
290
291
	public function testGetTreeWithFilter()
292
	{
293
		$this->assertEquals( 2, count( $this->object->getTree()->getChildren() ) );
294
295
		$callback = function( \Aimeos\MShop\Common\Item\ListRef\Iface $item, $index )
296
		{
297
			return (bool) $index % 2;
298
		};
299
300
		$this->object->registerItemFilter( 'test', $callback );
301
		$tree = $this->object->getTree();
302
303
		$rootItem = $this->object->getTree();
304
		$this->assertEquals( 1, count( $tree->getChildren() ) );
305
306
		$groupItem = $rootItem->getChild( 0 );
307
		$this->assertEquals( 'Groups', $groupItem->getLabel() );
308
		$this->assertEquals( 1, count( $groupItem->getChildren() ) );
309
		$this->assertEquals( 'Internet', $groupItem->getChild( 0 )->getLabel() );
310
	}
311
312
313
	public function testGetPath()
314
	{
315
		$search = $this->object->createSearch();
316
		$conditions = array(
317
			$search->compare( '==', 'catalog.label', 'Kaffee' ),
318
			$search->compare( '==', 'catalog.editor', $this->editor )
319
		);
320
		$search->setConditions( $search->combine( '&&', $conditions ) );
321
		$items = $this->object->searchItems( $search, array( 'text' ) );
322
323
		if( ( $item = reset( $items ) ) === false ) {
324
			throw new \RuntimeException( 'Catalog item not found' );
325
		}
326
327
		$items = $this->object->getPath( $item->getId() );
328
		$expected = array( 'Root', 'Categories', 'Kaffee' );
329
330
		foreach( $items as $item ) {
331
			$this->assertEquals( array_shift( $expected ), $item->getLabel() );
332
		}
333
334
		$this->assertEquals( 0, count( $expected ) );
335
	}
336
337
338
	public function testSaveInvalid()
339
	{
340
		$this->setExpectedException( '\Aimeos\MShop\Catalog\Exception' );
341
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
342
	}
343
344
345
	public function testSaveInsertMoveDeleteItem()
346
	{
347
		$search = $this->object->createSearch();
348
		$conditions = array(
349
			$search->compare( '==', 'catalog.label', 'Root' ),
350
			$search->compare( '==', 'catalog.editor', $this->editor )
351
		);
352
		$search->setConditions( $search->combine( '&&', $conditions ) );
353
		$items = $this->object->searchItems( $search, array( 'text' ) );
354
355
		if( ( $item = reset( $items ) ) === false ) {
356
			throw new \RuntimeException( 'No root node found' );
357
		}
358
359
		$parentId = $item->getId();
360
		$item->setId( null );
361
		$item->setLabel( 'Root child' );
362
		$item->setCode( 'new Root child' );
363
		$this->object->insertItem( $item, $parentId );
364
		$this->object->moveItem( $item->getId(), $parentId, $parentId );
365
		$itemSaved = $this->object->getItem( $item->getId() );
366
367
		$itemExp = clone $itemSaved;
368
		$itemExp->setStatus( true );
369
		$this->object->saveItem( $itemExp );
370
		$itemUpd = $this->object->getItem( $itemExp->getId() );
371
372
		$this->object->deleteItem( $itemSaved->getId() );
373
374
		$context = \TestHelperMShop::getContext();
375
376
		$this->assertTrue( $item->getId() !== null );
377
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
378
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
379
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
380
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
381
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
382
383
		$this->assertEquals( $context->getEditor(), $itemSaved->getEditor() );
384
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
385
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
386
387
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
388
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
389
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
390
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
391
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
392
393
		$this->assertEquals( $context->getEditor(), $itemUpd->getEditor() );
394
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
395
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
396
397
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
398
		$this->object->getItem( $item->getId() );
399
	}
400
401
402
	public function testGetSubManager()
403
	{
404
		$target = '\\Aimeos\\MShop\\Common\\Manager\\Iface';
405
		$this->assertInstanceOf( $target, $this->object->getSubManager( 'lists' ) );
406
		$this->assertInstanceOf( $target, $this->object->getSubManager( 'lists', 'Standard' ) );
407
408
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
409
		$this->object->getSubManager( 'unknown' );
410
	}
411
412
413
	public function testGetSubManagerInvalidName()
414
	{
415
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
416
		$this->object->getSubManager( 'lists', 'unknown' );
417
	}
418
}