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

StandardTest::testGetSavePropertyItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 19
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\Media\Manager;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object = null;
16
	private $editor = '';
17
18
19
	protected function setUp()
20
	{
21
		$this->editor = \TestHelperMShop::getContext()->getEditor();
22
		$this->object = \Aimeos\MShop\Media\Manager\Factory::createManager( \TestHelperMShop::getContext() );
23
	}
24
25
	protected function tearDown()
26
	{
27
		unset( $this->object );
28
	}
29
30
31
	public function testCleanup()
32
	{
33
		$this->object->cleanup( array( -1 ) );
34
	}
35
36
37
	public function testGetResourceType()
38
	{
39
		$result = $this->object->getResourceType();
40
41
		$this->assertContains( 'media', $result );
42
		$this->assertContains( 'media/type', $result );
43
		$this->assertContains( 'media/lists', $result );
44
		$this->assertContains( 'media/lists/type', $result );
45
	}
46
47
48
	public function testGetSearchAttributes()
49
	{
50
		foreach( $this->object->getSearchAttributes() as $attribute ) {
51
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
52
		}
53
	}
54
55
	public function testCreateItem()
56
	{
57
		$item = $this->object->createItem();
58
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Media\\Item\\Iface', $item );
59
	}
60
61
	public function testCreateSearch()
62
	{
63
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $this->object->createSearch() );
64
	}
65
66
	public function testSearchItem()
67
	{
68
		//search without base criteria
69
		$search = $this->object->createSearch();
70
71
		$expr = [];
72
		$expr[] = $search->compare( '!=', 'media.id', null );
73
		$expr[] = $search->compare( '!=', 'media.siteid', null );
74
		$expr[] = $search->compare( '==', 'media.languageid', 'de' );
75
		$expr[] = $search->compare( '>', 'media.typeid', 0 );
76
		$expr[] = $search->compare( '==', 'media.domain', 'product' );
77
		$expr[] = $search->compare( '==', 'media.label', 'cn_colombie_266x221' );
78
		$expr[] = $search->compare( '==', 'media.url', 'prod_266x221/198_prod_266x221.jpg' );
79
		$expr[] = $search->compare( '==', 'media.preview', 'prod_266x221/198_prod_266x221.jpg' );
80
		$expr[] = $search->compare( '==', 'media.mimetype', 'image/jpeg' );
81
		$expr[] = $search->compare( '==', 'media.status', 1 );
82
		$expr[] = $search->compare( '>=', 'media.mtime', '1970-01-01 00:00:00' );
83
		$expr[] = $search->compare( '>=', 'media.ctime', '1970-01-01 00:00:00' );
84
		$expr[] = $search->compare( '==', 'media.editor', $this->editor );
85
86
		$expr[] = $search->compare( '!=', 'media.type.id', null );
87
		$expr[] = $search->compare( '!=', 'media.type.siteid', null );
88
		$expr[] = $search->compare( '==', 'media.type.domain', 'product' );
89
		$expr[] = $search->compare( '==', 'media.type.code', 'prod_266x221' );
90
		$expr[] = $search->compare( '>', 'media.type.label', '' );
91
		$expr[] = $search->compare( '==', 'media.type.status', 1 );
92
		$expr[] = $search->compare( '>=', 'media.type.mtime', '1970-01-01 00:00:00' );
93
		$expr[] = $search->compare( '>=', 'media.type.ctime', '1970-01-01 00:00:00' );
94
		$expr[] = $search->compare( '==', 'media.type.editor', $this->editor );
95
96
		$expr[] = $search->compare( '!=', 'media.lists.id', null );
97
		$expr[] = $search->compare( '!=', 'media.lists.siteid', null );
98
		$expr[] = $search->compare( '>', 'media.lists.parentid', 0 );
99
		$expr[] = $search->compare( '==', 'media.lists.domain', 'text' );
100
		$expr[] = $search->compare( '>', 'media.lists.typeid', 0 );
101
		$expr[] = $search->compare( '>', 'media.lists.refid', 0 );
102
		$expr[] = $search->compare( '==', 'media.lists.datestart', null );
103
		$expr[] = $search->compare( '==', 'media.lists.dateend', null );
104
		$expr[] = $search->compare( '!=', 'media.lists.config', null );
105
		$expr[] = $search->compare( '==', 'media.lists.position', 0 );
106
		$expr[] = $search->compare( '==', 'media.lists.status', 1 );
107
		$expr[] = $search->compare( '>=', 'media.lists.mtime', '1970-01-01 00:00:00' );
108
		$expr[] = $search->compare( '>=', 'media.lists.ctime', '1970-01-01 00:00:00' );
109
		$expr[] = $search->compare( '==', 'media.lists.editor', $this->editor );
110
111
		$expr[] = $search->compare( '!=', 'media.lists.type.id', null );
112
		$expr[] = $search->compare( '!=', 'media.lists.type.siteid', null );
113
		$expr[] = $search->compare( '==', 'media.lists.type.code', 'option' );
114
		$expr[] = $search->compare( '==', 'media.lists.type.domain', 'attribute' );
115
		$expr[] = $search->compare( '>', 'media.lists.type.label', '' );
116
		$expr[] = $search->compare( '==', 'media.lists.type.status', 1 );
117
		$expr[] = $search->compare( '>=', 'media.lists.type.mtime', '1970-01-01 00:00:00' );
118
		$expr[] = $search->compare( '>=', 'media.lists.type.ctime', '1970-01-01 00:00:00' );
119
		$expr[] = $search->compare( '==', 'media.lists.type.editor', $this->editor );
120
121
		$total = 0;
122
		$search->setConditions( $search->combine( '&&', $expr ) );
123
		$results = $this->object->searchItems( $search, [], $total );
124
		$this->assertEquals( 1, count( $results ) );
125
		$this->assertEquals( 1, $total );
126
127
		//search with base criteria
128
		$search = $this->object->createSearch( true );
129
		$conditions = array(
130
			$search->compare( '==', 'media.editor', $this->editor ),
131
			$search->getConditions()
132
		);
133
		$search->setConditions( $search->combine( '&&', $conditions ) );
134
		$search->setSlice( 0, 4 );
135
		$results = $this->object->searchItems( $search, [], $total );
136
		$this->assertEquals( 4, count( $results ) );
137
		$this->assertEquals( 13, $total );
138
139
		foreach( $results as $itemId => $item ) {
140
			$this->assertEquals( $itemId, $item->getId() );
141
		}
142
	}
143
144
	public function testGetItem()
145
	{
146
		$search = $this->object->createSearch();
147
		$conditions = array(
148
			$search->compare( '==', 'media.label', 'example image 1' ),
149
			$search->compare( '==', 'media.editor', $this->editor )
150
		);
151
		$search->setConditions( $search->combine( '&&', $conditions ) );
152
		$items = $this->object->searchItems( $search );
153
154
		if( ( $item = reset( $items ) ) === false ) {
155
			throw new \RuntimeException( 'No media item with label "path/to/folder/example1.jpg" found' );
156
		}
157
158
		$this->assertEquals( $item, $this->object->getItem( $item->getId() ) );
159
		$this->assertEquals( 2, count( $item->getPropertyItems() ) );
160
		$this->assertNotEquals( '', $item->getTypeName() );
161
	}
162
163
164
	public function testSaveInvalid()
165
	{
166
		$this->setExpectedException( '\Aimeos\MShop\Media\Exception' );
167
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
168
	}
169
170
171
	public function testSaveUpdateDeleteItem()
172
	{
173
		$search = $this->object->createSearch();
174
		$conditions = array(
175
			$search->compare( '~=', 'media.label', 'example' ),
176
			$search->compare( '==', 'media.editor', $this->editor )
177
		);
178
		$search->setConditions( $search->combine( '&&', $conditions ) );
179
		$items = $this->object->searchItems( $search );
180
181
		if( ( $item = reset( $items ) ) === false ) {
182
			throw new \RuntimeException( 'No media item with label "cn_colombie_123x103" found' );
183
		}
184
185
		$item->setId( null );
186
		$item->setLanguageId( 'de' );
187
		$item->setDomain( 'test_dom' );
188
		$item->setLabel( 'test' );
189
		$item->setMimeType( 'image/jpeg' );
190
		$item->setUrl( 'test.jpg' );
191
		$item->setPreview( 'xxxtest-preview.jpg' );
192
193
		$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...
194
		$itemSaved = $this->object->getItem( $item->getId() );
195
196
		$itemExp = clone $itemSaved;
197
		$itemExp->setPreview( 'test-preview.jpg' );
198
		$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...
199
		$itemUpd = $this->object->getItem( $itemExp->getId() );
200
201
		$this->object->deleteItem( $item->getId() );
202
203
204
		$this->assertTrue( $item->getId() !== null );
205
		$this->assertTrue( $itemSaved->getType() !== null );
206
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
207
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
208
		$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() );
209
		$this->assertEquals( $item->getLanguageId(), $itemSaved->getLanguageId() );
210
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
211
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
212
		$this->assertEquals( $item->getMimeType(), $itemSaved->getMimeType() );
213
		$this->assertEquals( $item->getUrl(), $itemSaved->getUrl() );
214
		$this->assertEquals( $item->getPreview(), $itemSaved->getPreview() );
215
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
216
217
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
218
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
219
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
220
221
		$this->assertTrue( $itemUpd->getType() !== null );
222
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
223
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
224
		$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() );
225
		$this->assertEquals( $itemExp->getLanguageId(), $itemUpd->getLanguageId() );
226
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
227
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
228
		$this->assertEquals( $itemExp->getMimeType(), $itemUpd->getMimeType() );
229
		$this->assertEquals( $itemExp->getUrl(), $itemUpd->getUrl() );
230
		$this->assertEquals( $itemExp->getPreview(), $itemUpd->getPreview() );
231
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
232
233
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
234
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
235
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
236
237
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultSaved );
238
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultUpd );
239
240
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
241
		$this->object->getItem( $item->getId() );
242
	}
243
244
245
	public function testGetSavePropertyItems()
246
	{
247
		$search = $this->object->createSearch();
248
		$search->setConditions( $search->compare( '==', 'media.label', 'example image 1' ) );
249
		$items = $this->object->searchItems( $search );
250
		$item = reset( $items );
251
252
		$item->setId( null )->setLabel( 'example image 1.1' );
253
		$this->object->saveItem( $item );
254
255
		$search->setConditions( $search->compare( '==', 'media.label', 'example image 1.1' ) );
256
		$items = $this->object->searchItems( $search );
257
		$item2 = reset( $items );
258
259
		$this->object->deleteItem( $item->getId() );
260
261
		$this->assertEquals( 2, count( $item->getPropertyItems() ) );
262
		$this->assertEquals( 2, count( $item2->getPropertyItems() ) );
263
	}
264
265
266
	public function testGetSubManager()
267
	{
268
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type' ) );
269
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type', 'Standard' ) );
270
271
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'lists' ) );
272
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'lists', 'Standard' ) );
273
274
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
275
		$this->object->getSubManager( 'unknown' );
276
	}
277
278
279
	public function testGetSubManagerInvalidName()
280
	{
281
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
282
		$this->object->getSubManager( 'lists', 'unknown' );
283
	}
284
}
285