Passed
Push — master ( 3a20e7...179271 )
by Aimeos
04:17
created

StandardTest::testScaleHttp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 0
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-2023
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
		$item = $this->object->create()->setUrl( 'test.jpg' )->setPreviews( [1 => 'test-1.jpg'] )->setId( -1 );
46
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [$item] ) );
47
	}
48
49
50
	public function testGetResourceType()
51
	{
52
		$result = $this->object->getResourceType();
53
54
		$this->assertContains( 'media', $result );
55
		$this->assertContains( 'media/lists', $result );
56
	}
57
58
59
	public function testGetSearchAttributes()
60
	{
61
		foreach( $this->object->getSearchAttributes() as $attribute ) {
62
			$this->assertInstanceOf( \Aimeos\Base\Criteria\Attribute\Iface::class, $attribute );
63
		}
64
	}
65
66
67
	public function testCreateItem()
68
	{
69
		$item = $this->object->create();
70
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $item );
71
	}
72
73
74
	public function testCreateItemType()
75
	{
76
		$item = $this->object->create( ['media.type' => 'default'] );
77
		$this->assertEquals( 'default', $item->getType() );
78
	}
79
80
81
	public function testCreateSearch()
82
	{
83
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter() );
84
	}
85
86
87
	public function testCopy()
88
	{
89
		$fsm = $this->getMockBuilder( \Aimeos\Base\Filesystem\Manager\Standard::class )
90
			->onlyMethods( array( 'get' ) )
91
			->disableOriginalConstructor()
92
			->getMock();
93
94
		$fs = $this->getMockBuilder( \Aimeos\Base\Filesystem\Standard::class )
95
			->onlyMethods( array( 'has', 'copy' ) )
96
			->disableOriginalConstructor()
97
			->getMock();
98
99
		$fsm->expects( $this->once() )->method( 'get' )
100
			->will( $this->returnValue( $fs ) );
101
102
		$fs->expects( $this->exactly( 2 ) )->method( 'has' )
103
			->will( $this->returnValue( true ) );
104
105
		$fs->expects( $this->exactly( 2 ) )->method( 'copy' );
106
107
		$this->context->setFilesystemManager( $fsm );
108
109
		$item = $this->object->create()->setPreview( 'test' )->setUrl( 'test' );
110
111
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $this->object->copy( $item ) );
112
	}
113
114
115
	public function testSearchItem()
116
	{
117
		$search = $this->object->filter();
118
		$search->setConditions( $search->compare( '==', 'media.url', 'prod_266x221/198_prod_266x221.jpg' ) );
119
		$item = $this->object->search( $search, ['attribute'] )->first();
120
121
		if( $item && ( $listItem = $item->getListItems( 'attribute', 'option' )->first() ) === null ) {
122
			throw new \RuntimeException( 'No list item found' );
123
		}
124
125
		$search = $this->object->filter();
126
127
		$expr = [];
128
		$expr[] = $search->compare( '!=', 'media.id', null );
129
		$expr[] = $search->compare( '!=', 'media.siteid', null );
130
		$expr[] = $search->compare( '==', 'media.languageid', 'de' );
131
		$expr[] = $search->compare( '==', 'media.type', 'slideshow' );
132
		$expr[] = $search->compare( '==', 'media.domain', 'product' );
133
		$expr[] = $search->compare( '==', 'media.filesystem', 'fs-media' );
134
		$expr[] = $search->compare( '==', 'media.label', 'prod_266x221/198_prod_266x221.jpg' );
135
		$expr[] = $search->compare( '==', 'media.url', 'prod_266x221/198_prod_266x221.jpg' );
136
		$expr[] = $search->compare( '=~', 'media.preview', '{' );
137
		$expr[] = $search->compare( '==', 'media.mimetype', 'image/jpeg' );
138
		$expr[] = $search->compare( '==', 'media.status', 1 );
139
		$expr[] = $search->compare( '>=', 'media.mtime', '1970-01-01 00:00:00' );
140
		$expr[] = $search->compare( '>=', 'media.ctime', '1970-01-01 00:00:00' );
141
		$expr[] = $search->compare( '==', 'media.editor', $this->editor );
142
143
		$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...
144
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
145
146
		$param = ['attribute', 'option'];
147
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
148
149
		$param = ['attribute'];
150
		$expr[] = $search->compare( '!=', $search->make( 'media:has', $param ), null );
151
152
		$param = ['copyright', 'de', 'ich, 2019'];
153
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
154
155
		$param = ['copyright', 'de'];
156
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
157
158
		$param = ['copyright'];
159
		$expr[] = $search->compare( '!=', $search->make( 'media:prop', $param ), null );
160
161
		$total = 0;
162
		$search->setConditions( $search->and( $expr ) );
163
		$results = $this->object->search( $search, [], $total )->toArray();
164
165
		$this->assertEquals( 1, count( $results ) );
166
		$this->assertEquals( 1, $total );
167
	}
168
169
170
	public function testSearchItemBase()
171
	{
172
		//search with base criteria
173
		$search = $this->object->filter( true );
174
		$conditions = array(
175
			$search->compare( '==', 'media.editor', $this->editor ),
176
			$search->getConditions()
177
		);
178
		$search->setConditions( $search->and( $conditions ) );
179
		$search->slice( 0, 4 );
180
181
		$total = 0;
182
		$results = $this->object->search( $search, [], $total )->toArray();
183
184
		$this->assertEquals( 4, count( $results ) );
185
		$this->assertEquals( 16, $total );
186
187
		foreach( $results as $itemId => $item ) {
188
			$this->assertEquals( $itemId, $item->getId() );
189
		}
190
	}
191
192
	public function testGetItem()
193
	{
194
		$search = $this->object->filter()->slice( 0, 1 );
195
		$conditions = array(
196
			$search->compare( '==', 'media.label', 'path/to/folder/example1.jpg' ),
197
			$search->compare( '==', 'media.editor', $this->editor )
198
		);
199
		$search->setConditions( $search->and( $conditions ) );
200
		$item = $this->object->search( $search, ['media/property'] )->first();
201
202
		$this->assertEquals( $item, $this->object->get( $item->getId(), ['media/property'] ) );
203
		$this->assertEquals( 2, count( $item->getPropertyItems() ) );
204
	}
205
206
207
	public function testSaveUpdateDeleteItem()
208
	{
209
		$search = $this->object->filter();
210
		$conditions = array(
211
			$search->compare( '~=', 'media.label', 'example' ),
212
			$search->compare( '==', 'media.editor', $this->editor )
213
		);
214
		$search->setConditions( $search->and( $conditions ) );
215
		$item = $this->object->search( $search )->first();
216
217
		$item->setId( null );
218
		$item->setLanguageId( 'de' );
219
		$item->setDomain( 'test_dom' );
220
		$item->setLabel( 'test' );
221
		$item->setMimeType( 'image/jpeg' );
222
		$item->setUrl( 'test.jpg' );
223
		$item->setPreview( 'xxxtest-preview.jpg' );
224
		$item->setFileSystem( 'fs-media' );
225
226
		$resultSaved = $this->object->save( $item );
227
		$itemSaved = $this->object->get( $item->getId() );
228
229
		$itemExp = clone $itemSaved;
230
		$itemExp->setPreview( 'test-preview.jpg' );
231
		$resultUpd = $this->object->save( $itemExp );
232
		$itemUpd = $this->object->get( $itemExp->getId() );
233
234
		$this->object->delete( $item );
235
236
237
		$this->assertTrue( $item->getId() !== null );
238
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
239
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
240
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
241
		$this->assertEquals( $item->getLanguageId(), $itemSaved->getLanguageId() );
242
		$this->assertEquals( $item->getFileSystem(), $itemSaved->getFileSystem() );
243
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
244
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
245
		$this->assertEquals( $item->getMimeType(), $itemSaved->getMimeType() );
246
		$this->assertEquals( $item->getUrl(), $itemSaved->getUrl() );
247
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
248
		$this->assertEquals( 0, strncmp( $item->getPreview(), $itemSaved->getPreview(), 19 ) );
249
250
		$this->assertEquals( $this->editor, $itemSaved->editor() );
251
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
252
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
253
254
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
255
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
256
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
257
		$this->assertEquals( $itemExp->getLanguageId(), $itemUpd->getLanguageId() );
258
		$this->assertEquals( $itemExp->getFileSystem(), $itemUpd->getFileSystem() );
259
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
260
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
261
		$this->assertEquals( $itemExp->getMimeType(), $itemUpd->getMimeType() );
262
		$this->assertEquals( $itemExp->getUrl(), $itemUpd->getUrl() );
263
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
264
		$this->assertEquals( 0, strncmp( $itemExp->getPreview(), $itemUpd->getPreview(), 16 ) );
265
266
		$this->assertEquals( $this->editor, $itemUpd->editor() );
267
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
268
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
269
270
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
271
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
272
273
		$this->expectException( \Aimeos\MShop\Exception::class );
274
		$this->object->get( $item->getId() );
275
	}
276
277
278
	public function testGetSavePropertyItems()
279
	{
280
		$search = $this->object->filter();
281
		$search->setConditions( $search->compare( '==', 'media.label', 'path/to/folder/example1.jpg' ) );
282
		$item = $this->object->search( $search, ['media/property'] )->first();
283
284
		$item->setId( null )->setLabel( 'path/to/folder/example1-1.jpg' );
285
		$this->object->save( $item );
286
287
		$search->setConditions( $search->compare( '==', 'media.label', 'path/to/folder/example1-1.jpg' ) );
288
		$item2 = $this->object->search( $search, ['media/property'] )->first();
289
290
		$this->object->delete( $item->getId() );
291
292
		$this->assertEquals( 2, count( $item->getPropertyItems() ) );
293
		$this->assertEquals( 2, count( $item2->getPropertyItems() ) );
294
	}
295
296
297
	public function testGetSubManager()
298
	{
299
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type' ) );
300
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type', 'Standard' ) );
301
302
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'lists' ) );
303
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'lists', 'Standard' ) );
304
305
		$this->expectException( \LogicException::class );
306
		$this->object->getSubManager( 'unknown' );
307
	}
308
309
310
	public function testGetSubManagerInvalidName()
311
	{
312
		$this->expectException( \LogicException::class );
313
		$this->object->getSubManager( 'lists', 'unknown' );
314
	}
315
316
317
	public function testScale()
318
	{
319
		copy( __DIR__ . '/_testfiles/test.gif', 'tmp/test.gif' );
320
321
		$item = $this->object->create()->setUrl( 'test.gif' )
322
			->setMimeType( 'image/gif' )->setDomain( 'product' );
323
324
		$result = $this->object->scale( $item, true );
325
326
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
327
		$this->assertEquals( 'test.gif', $result->getUrl() );
328
		$this->assertNotEquals( '', $result->getPreview() );
329
	}
330
331
332
	public function testScaleHttp()
333
	{
334
335
		$item = $this->object->create()->setUrl( 'https://aimeos.org/fileadmin/logos/favicon.png' )
336
			->setMimeType( 'image/png' )->setDomain( 'product' );
337
338
		$result = $this->object->scale( $item, true );
339
340
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
341
		$this->assertEquals( 'https://aimeos.org/fileadmin/logos/favicon.png', $result->getUrl() );
342
		$this->assertNotEquals( '', $result->getPreview() );
343
	}
344
345
346
	public function testUpload()
347
	{
348
		$content = file_get_contents( __DIR__ . '/_testfiles/test.gif' );
349
		$file = new \Nyholm\Psr7\UploadedFile( __DIR__ . '/_testfiles/test.gif', strlen( $content ), UPLOAD_ERR_OK, 'test.gif' );
350
351
		$result = $this->object->upload( $this->object->create(), $file );
352
353
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
354
		$this->assertStringEndsWith( '_test.gif', $result->getUrl() );
355
		$this->assertEquals( 'image/gif', $result->getMimetype() );
356
		$this->assertEquals( 'test.gif', $result->getLabel() );
357
	}
358
359
360
	public function testUploadPreview()
361
	{
362
		$content = file_get_contents( __DIR__ . '/_testfiles/test.gif' );
363
		$file = new \Nyholm\Psr7\UploadedFile( __DIR__ . '/_testfiles/test.gif', strlen( $content ), UPLOAD_ERR_OK, 'test.gif' );
364
		$preview = new \Nyholm\Psr7\UploadedFile( __DIR__ . '/_testfiles/test.gif', strlen( $content ), UPLOAD_ERR_OK, 'test.gif' );
365
366
		$result = $this->object->upload( $this->object->create(), $file, $preview );
367
368
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $result );
369
		$this->assertStringEndsWith( '_test.gif', $result->getPreview() );
370
		$this->assertStringEndsWith( '_test.gif', $result->getUrl() );
371
		$this->assertEquals( 'image/gif', $result->getMimetype() );
372
		$this->assertEquals( 'test.gif', $result->getLabel() );
373
	}
374
375
376
	public function testUploadException()
377
	{
378
		$content = file_get_contents( __DIR__ . '/_testfiles/test.gif' );
379
		$file = new \Nyholm\Psr7\UploadedFile( __DIR__ . '/_testfiles/test.gif', strlen( $content ), 1, 'test.gif' );
380
381
		$this->expectException( \RuntimeException::class );
382
		$this->object->upload( $this->object->create(), $file );
383
	}
384
385
386
	public function testIsAllowed()
387
	{
388
		$this->assertTrue( $this->access( 'isAllowed' )->invokeArgs( $this->object, ['image/jpeg'] ) );
389
	}
390
391
392
	public function testIsAllowedException()
393
	{
394
		$this->expectException( \Aimeos\MShop\Media\Exception::class );
395
		$this->access( 'isAllowed' )->invokeArgs( $this->object, array( 'application/xxx' ) );
396
	}
397
398
399
	public function testImage()
400
	{
401
		copy( __DIR__ . '/_testfiles/test.gif', 'tmp/test.gif' );
402
403
		$img = $this->access( 'image' )->invokeArgs( $this->object, [$this->context->fs( 'fs-media' ), 'test.gif'] );
404
		$this->assertInstanceOf( \Intervention\Image\Interfaces\ImageInterface::class, $img );
0 ignored issues
show
Bug introduced by
The type Intervention\Image\Interfaces\ImageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
405
	}
406
407
408
	protected function access( $name )
409
	{
410
		$class = new \ReflectionClass( \Aimeos\MShop\Media\Manager\Standard::class );
411
		$method = $class->getMethod( $name );
412
		$method->setAccessible( true );
413
414
		return $method;
415
	}
416
}
417