Completed
Push — master ( 7f402b...a6068b )
by Aimeos
10:08
created

StandardTest::testGetSavePropertyItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 9.4285
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-2017
7
 */
8
9
10
namespace Aimeos\MShop\Attribute\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\Attribute\Manager\Factory::createManager( \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 testGetResourceType()
39
	{
40
		$result = $this->object->getResourceType();
41
42
		$this->assertContains( 'attribute', $result );
43
		$this->assertContains( 'attribute/type', $result );
44
		$this->assertContains( 'attribute/lists', $result );
45
		$this->assertContains( 'attribute/lists/type', $result );
46
	}
47
48
49
	public function testGetSearchAttributes()
50
	{
51
		foreach( $this->object->getSearchAttributes() as $obj ) {
52
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $obj );
53
		}
54
	}
55
56
57
	public function testCreateItem()
58
	{
59
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Attribute\\Item\\Iface', $this->object->createItem() );
60
	}
61
62
63
	public function testGetSubManager()
64
	{
65
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'lists' ) );
66
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'lists', 'Standard' ) );
67
68
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type' ) );
69
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type', 'Standard' ) );
70
71
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
72
		$this->object->getSubManager( 'unknown' );
73
	}
74
75
76
	public function testGetSubManagerInvalidName()
77
	{
78
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
79
		$this->object->getSubManager( 'lists', 'unknown' );
80
	}
81
82
83
	public function testFindItem()
84
	{
85
		$item = $this->object->findItem( 'm', array( 'text' ), 'product', 'size' );
86
87
		$this->assertEquals( 'm', $item->getCode() );
88
		$this->assertEquals( 'size', $item->getType() );
89
		$this->assertEquals( 'product', $item->getDomain() );
90
		$this->assertEquals( 1, count( $item->getListItems( 'text', null, null, false ) ) );
91
		$this->assertEquals( 1, count( $item->getRefItems( 'text', null, null, false ) ) );
92
	}
93
94
95
	public function testFindItemInvalid()
96
	{
97
		$this->setExpectedException( '\Aimeos\MShop\Exception' );
98
		$this->object->findItem( 'invalid' );
99
	}
100
101
102
	public function testFindItemMissing()
103
	{
104
		$this->setExpectedException( '\Aimeos\MShop\Exception' );
105
		$this->object->findItem( 'm', [], 'product' );
106
	}
107
108
109
	public function testGetItem()
110
	{
111
		$itemA = $this->object->findItem( 'black', [], 'product', 'color' );
112
		$itemB = $this->object->getItem( $itemA->getId(), ['attribute/property'] );
113
114
		$this->assertEquals( $itemA->getId(), $itemB->getId() );
115
		$this->assertEquals( 1, count( $itemB->getPropertyItems() ) );
116
		$this->assertEquals( 1, count( $itemB->getListItems( null, null, null, false ) ) );
117
		$this->assertNotEquals( '', $itemB->getTypeName() );
118
	}
119
120
121
	public function testGetItemLists()
122
	{
123
		$itemA = $this->object->findItem( 'xxl', [], 'product', 'size' );
124
		$itemB = $this->object->getItem( $itemA->getId(), ['text'] );
125
126
		$this->assertEquals( $itemA->getId(), $itemB->getId() );
127
		$this->assertEquals( 1, count( $itemB->getListItems( 'text', null, null, false ) ) );
128
		$this->assertEquals( 1, count( $itemB->getRefItems( 'text', null, null, false ) ) );
129
	}
130
131
132
	public function testSaveInvalid()
133
	{
134
		$this->setExpectedException( '\Aimeos\MShop\Attribute\Exception' );
135
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
136
	}
137
138
139
	public function testSaveUpdateDeleteItem()
140
	{
141
		$typeItem = $this->object->getSubManager( 'type' )->findItem( 'size', [], 'product' );
142
143
		$item = $this->object->createItem();
144
		$item->setId( null );
145
		$item->setDomain( 'tmpDomainx' );
146
		$item->setCode( '106x' );
147
		$item->setLabel( '106x' );
148
		$item->setTypeId( $typeItem->getId() );
149
		$item->setPosition( 0 );
150
		$item->setStatus( 7 );
151
		$resultSaved = $this->object->saveItem( $item );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $resultSaved is correct as $this->object->saveItem($item) (which targets Aimeos\MShop\Common\Manager\Iface::saveItem()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
152
		$itemSaved = $this->object->getItem( $item->getId() );
153
154
		$itemExp = clone $itemSaved;
155
		$itemExp->setDomain( 'tmpDomain' );
156
		$itemExp->setCode( '106' );
157
		$itemExp->setLabel( '106' );
158
		$resultUpd = $this->object->saveItem( $itemExp );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $resultUpd is correct as $this->object->saveItem($itemExp) (which targets Aimeos\MShop\Common\Manager\Iface::saveItem()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
159
		$itemUpd = $this->object->getItem( $itemExp->getId() );
160
161
		$this->object->deleteItem( $item->getId() );
162
163
		$context = \TestHelperMShop::getContext();
164
165
		$this->assertTrue( $item->getId() !== null );
166
		$this->assertTrue( $itemSaved->getType() !== null );
167
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
168
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
169
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
170
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
171
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
172
		$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() );
173
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
174
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
175
176
		$this->assertEquals( $context->getEditor(), $itemSaved->getEditor() );
177
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
178
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
179
180
		$this->assertTrue( $itemUpd->getType() !== null );
181
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
182
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
183
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
184
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
185
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
186
		$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() );
187
		$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() );
188
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
189
190
		$this->assertEquals( $context->getEditor(), $itemUpd->getEditor() );
191
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
192
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
193
194
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultSaved );
195
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultUpd );
196
197
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
198
		$this->object->getItem( $item->getId() );
199
	}
200
201
202
	public function testGetSavePropertyItems()
203
	{
204
		$item = $this->object->findItem( 'black', [], 'product', 'color' );
205
206
		$item->setId( null )->setCode( 'xyz' );
207
		$this->object->saveItem( $item );
208
209
		$item2 = $this->object->findItem( 'xyz', [], 'product', 'color' );
210
211
		$this->object->deleteItem( $item->getId() );
212
213
		$this->assertEquals( 1, count( $item->getPropertyItems() ) );
214
		$this->assertEquals( 1, count( $item2->getPropertyItems() ) );
215
	}
216
217
218
	public function testCreateSearch()
219
	{
220
		$search = $this->object->createSearch();
221
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $search );
222
	}
223
224
225
	public function testSearchItems()
226
	{
227
		$search = $this->object->createSearch();
228
229
		$expr = [];
230
		$expr[] = $search->compare( '!=', 'attribute.id', null );
231
		$expr[] = $search->compare( '!=', 'attribute.siteid', null );
232
		$expr[] = $search->compare( '!=', 'attribute.typeid', null );
233
		$expr[] = $search->compare( '==', 'attribute.position', 5 );
234
		$expr[] = $search->compare( '==', 'attribute.code', 'black' );
235
		$expr[] = $search->compare( '==', 'attribute.label', 'black' );
236
		$expr[] = $search->compare( '==', 'attribute.domain', 'product' );
237
		$expr[] = $search->compare( '==', 'attribute.status', 0 );
238
		$expr[] = $search->compare( '>=', 'attribute.mtime', '1970-01-01 00:00:00' );
239
		$expr[] = $search->compare( '>=', 'attribute.ctime', '1970-01-01 00:00:00' );
240
		$expr[] = $search->compare( '==', 'attribute.editor', $this->editor );
241
242
		$expr[] = $search->compare( '!=', 'attribute.type.id', null );
243
		$expr[] = $search->compare( '!=', 'attribute.type.siteid', null );
244
		$expr[] = $search->compare( '==', 'attribute.type.code', 'color' );
245
		$expr[] = $search->compare( '==', 'attribute.type.domain', 'product' );
246
		$expr[] = $search->compare( '==', 'attribute.type.label', 'Color' );
247
		$expr[] = $search->compare( '==', 'attribute.type.status', 1 );
248
		$expr[] = $search->compare( '>=', 'attribute.type.mtime', '1970-01-01 00:00:00' );
249
		$expr[] = $search->compare( '>=', 'attribute.type.ctime', '1970-01-01 00:00:00' );
250
		$expr[] = $search->compare( '==', 'attribute.type.editor', $this->editor );
251
252
		$expr[] = $search->compare( '!=', 'attribute.lists.id', null );
253
		$expr[] = $search->compare( '!=', 'attribute.lists.siteid', null );
254
		$expr[] = $search->compare( '!=', 'attribute.lists.parentid', null );
255
		$expr[] = $search->compare( '==', 'attribute.lists.domain', 'text' );
256
		$expr[] = $search->compare( '!=', 'attribute.lists.typeid', null );
257
		$expr[] = $search->compare( '>', 'attribute.lists.refid', 0 );
258
		$expr[] = $search->compare( '==', 'attribute.lists.datestart', '2000-01-01 00:00:00' );
259
		$expr[] = $search->compare( '==', 'attribute.lists.dateend', '2001-01-01 00:00:00' );
260
		$expr[] = $search->compare( '!=', 'attribute.lists.config', null );
261
		$expr[] = $search->compare( '==', 'attribute.lists.position', 0 );
262
		$expr[] = $search->compare( '==', 'attribute.lists.status', 1 );
263
		$expr[] = $search->compare( '>=', 'attribute.lists.mtime', '1970-01-01 00:00:00' );
264
		$expr[] = $search->compare( '>=', 'attribute.lists.ctime', '1970-01-01 00:00:00' );
265
		$expr[] = $search->compare( '==', 'attribute.lists.editor', $this->editor );
266
267
		$expr[] = $search->compare( '!=', 'attribute.lists.type.id', null );
268
		$expr[] = $search->compare( '!=', 'attribute.lists.type.siteid', null );
269
		$expr[] = $search->compare( '==', 'attribute.lists.type.code', 'default' );
270
		$expr[] = $search->compare( '==', 'attribute.lists.type.domain', 'text' );
271
		$expr[] = $search->compare( '==', 'attribute.lists.type.label', 'Standard' );
272
		$expr[] = $search->compare( '==', 'attribute.lists.type.status', 1 );
273
		$expr[] = $search->compare( '>=', 'attribute.lists.type.mtime', '1970-01-01 00:00:00' );
274
		$expr[] = $search->compare( '>=', 'attribute.lists.type.ctime', '1970-01-01 00:00:00' );
275
		$expr[] = $search->compare( '==', 'attribute.lists.type.editor', $this->editor );
276
277
		$expr[] = $search->compare( '!=', 'attribute.property.id', null );
278
		$expr[] = $search->compare( '!=', 'attribute.property.siteid', null );
279
		$expr[] = $search->compare( '!=', 'attribute.property.typeid', null );
280
		$expr[] = $search->compare( '==', 'attribute.property.languageid', 'de' );
281
		$expr[] = $search->compare( '==', 'attribute.property.value', '#000000' );
282
		$expr[] = $search->compare( '==', 'attribute.property.editor', $this->editor );
283
284
		$expr[] = $search->compare( '!=', 'attribute.property.type.id', null );
285
		$expr[] = $search->compare( '!=', 'attribute.property.type.siteid', null );
286
		$expr[] = $search->compare( '==', 'attribute.property.type.code', 'htmlcolor' );
287
		$expr[] = $search->compare( '==', 'attribute.property.type.domain', 'attribute' );
288
		$expr[] = $search->compare( '>', 'attribute.property.type.label', 'HTML' );
289
		$expr[] = $search->compare( '==', 'attribute.property.type.status', 1 );
290
		$expr[] = $search->compare( '>=', 'attribute.property.type.mtime', '1970-01-01 00:00:00' );
291
		$expr[] = $search->compare( '>=', 'attribute.property.type.ctime', '1970-01-01 00:00:00' );
292
		$expr[] = $search->compare( '==', 'attribute.property.type.editor', $this->editor );
293
294
		$total = 0;
295
		$search->setConditions( $search->combine( '&&', $expr ) );
296
		$results = $this->object->searchItems( $search, [], $total );
297
298
		$this->assertEquals( 1, $total );
299
		$this->assertEquals( 1, count( $results ) );
300
		$this->assertEquals( 1, count( reset( $results )->getPropertyItems() ) );
301
	}
302
303
304
	public function testSearchBase()
305
	{
306
		//search with base criteria
307
		$search = $this->object->createSearch( true );
308
		$expr = array(
309
			$search->compare( '==', 'attribute.type.domain', 'product' ),
310
			$search->compare( '~=', 'attribute.code', '3' ),
311
			$search->compare( '==', 'attribute.editor', $this->editor ),
312
			$search->getConditions(),
313
		);
314
		$search->setConditions( $search->combine( '&&', $expr ) );
315
		$search->setSlice( 0, 5 );
316
317
		$total = 0;
318
		$results = $this->object->searchItems( $search, [], $total );
319
320
		$this->assertEquals( 5, count( $results ) );
321
		$this->assertEquals( 10, $total );
322
323
		foreach( $results as $itemId => $item ) {
324
			$this->assertEquals( $itemId, $item->getId() );
325
		}
326
	}
327
328
329
	public function testSearchTotal()
330
	{
331
		$search = $this->object->createSearch();
332
		$conditions = array(
333
			$search->compare( '==', 'attribute.type.code', 'size' ),
334
			$search->compare( '==', 'attribute.type.domain', 'product' ),
335
			$search->compare( '==', 'attribute.editor', $this->editor )
336
		);
337
		$search->setConditions( $search->combine( '&&', $conditions ) );
338
		$search->setSlice( 0, 1 );
339
340
		$total = 0;
341
		$items = $this->object->searchItems( $search, [], $total );
342
		$this->assertEquals( 1, count( $items ) );
343
		$this->assertEquals( 6, $total );
344
	}
345
}
346