Passed
Push — master ( dda986...554388 )
by Aimeos
05:36
created

StandardTest   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 378
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 30
eloc 200
c 3
b 0
f 0
dl 0
loc 378
rs 10

24 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetResourceType() 0 6 1
A testCreateItem() 0 4 1
A testGetSearchAttributes() 0 4 2
A testCreateSearch() 0 3 1
A testClear() 0 3 1
A testDeleteItems() 0 3 1
A testCreateItemType() 0 4 1
A setUp() 0 6 1
A tearDown() 0 3 1
A testGetSavePropertyItems() 0 16 1
A testGetMime() 0 6 1
A testScale() 0 20 1
A testGetItem() 0 12 1
A access() 0 7 1
A testSearchItem() 0 52 3
A testGetContent() 0 9 2
A testGetSubManager() 0 10 1
A testCopy() 0 25 1
A testGetContentException() 0 4 1
A testGetSubManagerInvalidName() 0 4 1
A testGetFile() 0 8 2
A testSaveUpdateDeleteItem() 0 67 1
A testSearchItemBase() 0 19 2
A testGetContentHttp() 0 6 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 */
8
9
10
namespace Aimeos\MShop\Media\Manager;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $context;
17
	private $editor = '';
18
19
20
	protected function setUp() : void
21
	{
22
		$this->context = \TestHelper::context();
23
		$this->editor = $this->context->editor();
24
25
		$this->object = new \Aimeos\MShop\Media\Manager\Standard( $this->context );
26
	}
27
28
29
	protected function tearDown() : void
30
	{
31
		unset( $this->object, $this->context );
32
	}
33
34
35
	public function testClear()
36
	{
37
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( [-1] ) );
38
	}
39
40
41
	public function testDeleteItems()
42
	{
43
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [-1] ) );
44
	}
45
46
47
	public function testGetResourceType()
48
	{
49
		$result = $this->object->getResourceType();
50
51
		$this->assertContains( 'media', $result );
52
		$this->assertContains( 'media/lists', $result );
53
	}
54
55
56
	public function testGetSearchAttributes()
57
	{
58
		foreach( $this->object->getSearchAttributes() as $attribute ) {
59
			$this->assertInstanceOf( \Aimeos\Base\Criteria\Attribute\Iface::class, $attribute );
60
		}
61
	}
62
63
64
	public function testCreateItem()
65
	{
66
		$item = $this->object->create();
67
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $item );
68
	}
69
70
71
	public function testCreateItemType()
72
	{
73
		$item = $this->object->create( ['media.type' => 'default'] );
74
		$this->assertEquals( 'default', $item->getType() );
75
	}
76
77
78
	public function testCreateSearch()
79
	{
80
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter() );
81
	}
82
83
84
	public function testCopy()
85
	{
86
		$fsm = $this->getMockBuilder( \Aimeos\Base\Filesystem\Manager\Standard::class )
87
			->setMethods( array( 'get' ) )
88
			->disableOriginalConstructor()
89
			->getMock();
90
91
		$fs = $this->getMockBuilder( \Aimeos\Base\Filesystem\Standard::class )
92
			->setMethods( array( 'has', 'copy' ) )
93
			->disableOriginalConstructor()
94
			->getMock();
95
96
		$fsm->expects( $this->once() )->method( 'get' )
97
			->will( $this->returnValue( $fs ) );
98
99
		$fs->expects( $this->exactly( 2 ) )->method( 'has' )
100
			->will( $this->returnValue( true ) );
101
102
		$fs->expects( $this->exactly( 2 ) )->method( 'copy' );
103
104
		$this->context->setFilesystemManager( $fsm );
105
106
		$item = $this->object->create()->setPreview( 'test' )->setUrl( 'test' );
107
108
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $this->object->copy( $item ) );
109
	}
110
111
112
	public function testSearchItem()
113
	{
114
		$search = $this->object->filter();
115
		$search->setConditions( $search->compare( '==', 'media.url', 'prod_266x221/198_prod_266x221.jpg' ) );
116
		$item = $this->object->search( $search, ['attribute'] )->first();
117
118
		if( $item && ( $listItem = $item->getListItems( 'attribute', 'option' )->first() ) === null ) {
119
			throw new \RuntimeException( 'No list item found' );
120
		}
121
122
		$search = $this->object->filter();
123
124
		$expr = [];
125
		$expr[] = $search->compare( '!=', 'media.id', null );
126
		$expr[] = $search->compare( '!=', 'media.siteid', null );
127
		$expr[] = $search->compare( '==', 'media.languageid', 'de' );
128
		$expr[] = $search->compare( '==', 'media.type', 'slideshow' );
129
		$expr[] = $search->compare( '==', 'media.domain', 'product' );
130
		$expr[] = $search->compare( '==', 'media.filesystem', 'fs-media' );
131
		$expr[] = $search->compare( '==', 'media.label', 'prod_266x221/198_prod_266x221.jpg' );
132
		$expr[] = $search->compare( '==', 'media.url', 'prod_266x221/198_prod_266x221.jpg' );
133
		$expr[] = $search->compare( '=~', 'media.preview', '{' );
134
		$expr[] = $search->compare( '==', 'media.mimetype', 'image/jpeg' );
135
		$expr[] = $search->compare( '==', 'media.status', 1 );
136
		$expr[] = $search->compare( '>=', 'media.mtime', '1970-01-01 00:00:00' );
137
		$expr[] = $search->compare( '>=', 'media.ctime', '1970-01-01 00:00:00' );
138
		$expr[] = $search->compare( '==', 'media.editor', $this->editor );
139
140
		$param = ['attribute', 'option', $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...
141
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
142
143
		$param = ['attribute', 'option'];
144
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
145
146
		$param = ['attribute'];
147
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
148
149
		$param = ['copyright', 'de', 'ich, 2019'];
150
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
151
152
		$param = ['copyright', 'de'];
153
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
154
155
		$param = ['copyright'];
156
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
157
158
		$total = 0;
159
		$search->setConditions( $search->and( $expr ) );
160
		$results = $this->object->search( $search, [], $total )->toArray();
161
162
		$this->assertEquals( 1, count( $results ) );
163
		$this->assertEquals( 1, $total );
164
	}
165
166
167
	public function testSearchItemBase()
168
	{
169
		//search with base criteria
170
		$search = $this->object->filter( true );
171
		$conditions = array(
172
			$search->compare( '==', 'media.editor', $this->editor ),
173
			$search->getConditions()
174
		);
175
		$search->setConditions( $search->and( $conditions ) );
176
		$search->slice( 0, 4 );
177
178
		$total = 0;
179
		$results = $this->object->search( $search, [], $total )->toArray();
180
181
		$this->assertEquals( 4, count( $results ) );
182
		$this->assertEquals( 16, $total );
183
184
		foreach( $results as $itemId => $item ) {
185
			$this->assertEquals( $itemId, $item->getId() );
186
		}
187
	}
188
189
	public function testGetItem()
190
	{
191
		$search = $this->object->filter()->slice( 0, 1 );
192
		$conditions = array(
193
			$search->compare( '==', 'media.label', 'path/to/folder/example1.jpg' ),
194
			$search->compare( '==', 'media.editor', $this->editor )
195
		);
196
		$search->setConditions( $search->and( $conditions ) );
197
		$item = $this->object->search( $search, ['media/property'] )->first();
198
199
		$this->assertEquals( $item, $this->object->get( $item->getId(), ['media/property'] ) );
200
		$this->assertEquals( 2, count( $item->getPropertyItems() ) );
201
	}
202
203
204
	public function testSaveUpdateDeleteItem()
205
	{
206
		$search = $this->object->filter();
207
		$conditions = array(
208
			$search->compare( '~=', 'media.label', 'example' ),
209
			$search->compare( '==', 'media.editor', $this->editor )
210
		);
211
		$search->setConditions( $search->and( $conditions ) );
212
		$item = $this->object->search( $search )->first();
213
214
		$item->setId( null );
215
		$item->setLanguageId( 'de' );
216
		$item->setDomain( 'test_dom' );
217
		$item->setLabel( 'test' );
218
		$item->setMimeType( 'image/jpeg' );
219
		$item->setUrl( 'test.jpg' );
220
		$item->setPreview( 'xxxtest-preview.jpg' );
221
222
		$resultSaved = $this->object->save( $item );
223
		$itemSaved = $this->object->get( $item->getId() );
224
225
		$itemExp = clone $itemSaved;
226
		$itemExp->setPreview( 'test-preview.jpg' );
227
		$resultUpd = $this->object->save( $itemExp );
228
		$itemUpd = $this->object->get( $itemExp->getId() );
229
230
		$this->object->delete( $item->getId() );
231
232
233
		$this->assertTrue( $item->getId() !== null );
234
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
235
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
236
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
237
		$this->assertEquals( $item->getLanguageId(), $itemSaved->getLanguageId() );
238
		$this->assertEquals( $item->getFileSystem(), $itemSaved->getFileSystem() );
239
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
240
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
241
		$this->assertEquals( $item->getMimeType(), $itemSaved->getMimeType() );
242
		$this->assertEquals( $item->getUrl(), $itemSaved->getUrl() );
243
		$this->assertEquals( $item->getPreview(), $itemSaved->getPreview() );
244
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
245
246
		$this->assertEquals( $this->editor, $itemSaved->editor() );
247
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
248
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
249
250
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
251
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
252
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
253
		$this->assertEquals( $itemExp->getLanguageId(), $itemUpd->getLanguageId() );
254
		$this->assertEquals( $itemExp->getFileSystem(), $itemUpd->getFileSystem() );
255
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
256
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
257
		$this->assertEquals( $itemExp->getMimeType(), $itemUpd->getMimeType() );
258
		$this->assertEquals( $itemExp->getUrl(), $itemUpd->getUrl() );
259
		$this->assertEquals( $itemExp->getPreview(), $itemUpd->getPreview() );
260
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
261
262
		$this->assertEquals( $this->editor, $itemUpd->editor() );
263
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
264
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
265
266
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
267
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
268
269
		$this->expectException( \Aimeos\MShop\Exception::class );
270
		$this->object->get( $item->getId() );
271
	}
272
273
274
	public function testGetSavePropertyItems()
275
	{
276
		$search = $this->object->filter();
277
		$search->setConditions( $search->compare( '==', 'media.label', 'path/to/folder/example1.jpg' ) );
278
		$item = $this->object->search( $search, ['media/property'] )->first();
279
280
		$item->setId( null )->setLabel( 'path/to/folder/example1-1.jpg' );
281
		$this->object->save( $item );
282
283
		$search->setConditions( $search->compare( '==', 'media.label', 'path/to/folder/example1-1.jpg' ) );
284
		$item2 = $this->object->search( $search, ['media/property'] )->first();
285
286
		$this->object->delete( $item->getId() );
287
288
		$this->assertEquals( 2, count( $item->getPropertyItems() ) );
289
		$this->assertEquals( 2, count( $item2->getPropertyItems() ) );
290
	}
291
292
293
	public function testGetSubManager()
294
	{
295
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type' ) );
296
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type', 'Standard' ) );
297
298
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'lists' ) );
299
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'lists', 'Standard' ) );
300
301
		$this->expectException( \Aimeos\MShop\Exception::class );
302
		$this->object->getSubManager( 'unknown' );
303
	}
304
305
306
	public function testGetSubManagerInvalidName()
307
	{
308
		$this->expectException( \Aimeos\MShop\Exception::class );
309
		$this->object->getSubManager( 'lists', 'unknown' );
310
	}
311
312
313
	public function testScale()
314
	{
315
		$object = $this->getMockBuilder( \Aimeos\MShop\Media\Manager\Standard::class )
316
			->setConstructorArgs( [$this->context] )
317
			->setMethods( ['getContent', 'store'] )
318
			->getMock();
319
320
		$object->expects( $this->once() )->method( 'getContent' )
321
			->will( $this->returnValue( file_get_contents( __DIR__ . '/_testfiles/test.gif' ) ) );
322
323
		$object->expects( $this->any() )->method( 'store' );
324
325
		$item = $this->object->create()->setUrl( 'test.gif' )->setPreview( 'preview.gif' )
326
			->setMimeType( 'image/gif' )->setDomain( 'product' );
327
328
		$result = $object->scale( $item, true );
329
330
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
331
		$this->assertEquals( 'test.gif', $result->getUrl() );
332
		$this->assertNotEquals( 'preview.gif', $result->getPreview() );
333
	}
334
335
336
	public function testGetContent()
337
	{
338
		$dest = dirname( __DIR__, 3 ) . '/tmp/';
339
		is_dir( $dest ) ?: mkdir( $dest, 0755, true );
340
		copy( __DIR__ . '/_testfiles/test.gif', $dest . 'test.gif' );
341
342
		$result = $this->access( 'getContent' )->invokeArgs( $this->object, ['test.gif'] );
343
344
		$this->assertNotEquals( '', $result );
345
	}
346
347
348
	public function testGetContentHttp()
349
	{
350
		$url = 'https://aimeos.org/fileadmin/logos/favicon.png';
351
		$result = $this->access( 'getContent' )->invokeArgs( $this->object, [$url] );
352
353
		$this->assertNotEquals( '', $result );
354
	}
355
356
357
	public function testGetContentException()
358
	{
359
		$this->expectException( \Aimeos\MShop\Media\Exception::class );
360
		$this->access( 'getContent' )->invokeArgs( $this->object, [''] );
361
	}
362
363
364
	public function testGetFile()
365
	{
366
		$dest = dirname( __DIR__, 3 ) . '/tmp/';
367
		is_dir( $dest ) ?: mkdir( $dest, 0755, true );
368
		copy( __DIR__ . '/_testfiles/test.gif', $dest . 'test.gif' );
369
370
		$result = $this->access( 'getFile' )->invokeArgs( $this->object, ['test.gif'] );
371
		$this->assertInstanceOf( \Aimeos\MW\Media\Iface::class, $result );
372
	}
373
374
375
	public function testGetMime()
376
	{
377
		$file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/_testfiles/test.png' );
378
379
		$result = $this->access( 'getMime' )->invokeArgs( $this->object, array( $file, 'files' ) );
380
		$this->assertEquals( true, in_array( $result, ['image/webp', 'image/png'] ) );
381
	}
382
383
384
	protected function access( $name )
385
	{
386
		$class = new \ReflectionClass( \Aimeos\MShop\Media\Manager\Standard::class );
387
		$method = $class->getMethod( $name );
388
		$method->setAccessible( true );
389
390
		return $method;
391
	}
392
}
393