Passed
Push — master ( f317d9...8fa1ff )
by Aimeos
05:07
created

StandardTest::testGetPreviewsVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
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-2022
7
 */
8
9
10
namespace Aimeos\MShop\Media\Item;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $values;
17
18
19
	protected function setUp() : void
20
	{
21
		$this->values = array(
22
			'media.id' => 1,
23
			'media.siteid' => 123,
24
			'media.type' => 'category',
25
			'media.domain' => 'test_dom',
26
			'media.label' => 'testPicture',
27
			'media.mimetype' => 'image/jpeg',
28
			'media.filesystem' => 'fs-mimeicon',
29
			'media.url' => 'http://www.url.com/test.jpg',
30
			'media.previews' => [100 => '/directory/test.jpg', 200 => '/directory/test2.jpg'],
31
			'media.status' => 6,
32
			'media.languageid' => 'de',
33
			'media.mtime' => '2011-01-01 00:00:02',
34
			'media.ctime' => '2011-01-01 00:00:01',
35
			'media.editor' => 'unitTestUser',
36
			'.languageid' => 'de',
37
		);
38
39
		$this->object = new \Aimeos\MShop\Media\Item\Standard( $this->values );
40
	}
41
42
43
	protected function tearDown() : void
44
	{
45
		unset( $this->object );
46
	}
47
48
49
	public function testGetId()
50
	{
51
		$this->assertEquals( 1, $this->object->getId() );
52
	}
53
54
55
	public function testSetId()
56
	{
57
		$return = $this->object->setId( null );
58
59
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
60
		$this->assertEquals( null, $this->object->getId() );
61
		$this->assertTrue( $this->object->isModified() );
62
	}
63
64
65
	public function testGetDomain()
66
	{
67
		$this->assertEquals( 'test_dom', $this->object->getDomain() );
68
	}
69
70
71
	public function testSetDomain()
72
	{
73
		$return = $this->object->setDomain( 'test' );
74
75
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
76
		$this->assertEquals( 'test', $this->object->getDomain() );
77
		$this->assertTrue( $this->object->isModified() );
78
	}
79
80
81
	public function testGetType()
82
	{
83
		$this->assertEquals( 'category', $this->object->getType() );
84
	}
85
86
87
	public function testSetType()
88
	{
89
		$return = $this->object->setType( 'size' );
90
91
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
92
		$this->assertEquals( 'size', $this->object->getType() );
93
		$this->assertTrue( $this->object->isModified() );
94
	}
95
96
97
	public function testGetFileSystem()
98
	{
99
		$this->assertEquals( 'fs-mimeicon', $this->object->getFileSystem() );
100
	}
101
102
103
	public function testSetFileSystem()
104
	{
105
		$return = $this->object->setFileSystem( 'fs-test' );
106
107
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
108
		$this->assertEquals( 'fs-test', $this->object->getFileSystem() );
109
		$this->assertTrue( $this->object->isModified() );
110
	}
111
112
113
	public function testGetLabel()
114
	{
115
		$this->assertEquals( 'testPicture', $this->object->getLabel() );
116
	}
117
118
119
	public function testSetLabel()
120
	{
121
		$return = $this->object->setLabel( 'newPicture' );
122
123
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
124
		$this->assertEquals( 'newPicture', $this->object->getLabel() );
125
		$this->assertTrue( $this->object->isModified() );
126
	}
127
128
129
	public function testGetLanguageId()
130
	{
131
		$this->assertEquals( 'de', $this->object->getLanguageId() );
132
	}
133
134
135
	public function testSetLanguageId()
136
	{
137
		$return = $this->object->setLanguageId( 'en' );
138
139
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
140
		$this->assertEquals( 'en', $this->object->getLanguageId() );
141
		$this->assertTrue( $this->object->isModified() );
142
	}
143
144
145
	public function testSetLanguageIdInvalid()
146
	{
147
		$this->expectException( \Aimeos\MShop\Exception::class );
148
		$this->object->setLanguageId( '00' );
149
	}
150
151
152
	public function testGetMimeType()
153
	{
154
		$this->assertEquals( 'image/jpeg', $this->object->getMimeType() );
155
	}
156
157
158
	public function testSetMimeType()
159
	{
160
		$return = $this->object->setMimeType( 'image/png' );
161
162
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
163
		$this->assertEquals( 'image/png', $this->object->getMimeType() );
164
		$this->assertTrue( $this->object->isModified() );
165
	}
166
167
168
	public function testSetMimeTypeNoSlash()
169
	{
170
		$this->expectException( \Aimeos\MShop\Media\Exception::class );
171
		$this->object->setMimeType( 'image' );
172
	}
173
174
175
	public function testSetMimeTypeInvalidCategory()
176
	{
177
		$this->expectException( \Aimeos\MShop\Media\Exception::class );
178
		$this->object->setMimeType( 'image+audio/test' );
179
	}
180
181
182
	public function testGetUrl()
183
	{
184
		$this->assertEquals( 'http://www.url.com/test.jpg', $this->object->getUrl() );
185
	}
186
187
188
	public function testSetUrl()
189
	{
190
		$return = $this->object->setUrl( null )->setUrl( '/pictures/category.jpg' );
191
192
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
193
		$this->assertEquals( '/pictures/category.jpg', $this->object->getUrl() );
194
		$this->assertTrue( $this->object->isModified() );
195
	}
196
197
198
	public function testGetPreview()
199
	{
200
		$this->assertStringStartsWith( '/directory/test.jpg', $this->object->getPreview() );
201
		$this->assertStringStartsWith( '/directory/test.jpg', $this->object->getPreview( 100 ) );
202
		$this->assertStringStartsWith( '/directory/test2.jpg', $this->object->getPreview( true ) );
203
		$this->assertStringStartsWith( '/directory/test2.jpg', $this->object->getPreview( 150 ) );
204
		$this->assertStringStartsWith( '/directory/test2.jpg', $this->object->getPreview( 250 ) );
205
	}
206
207
208
	public function testGetPreviews()
209
	{
210
		$this->assertEquals( [100 => '/directory/test.jpg', 200 => '/directory/test2.jpg'], $this->object->getPreviews() );
211
	}
212
213
214
	public function testGetPreviewsVersion()
215
	{
216
		$expected = [100 => '/directory/test.jpg', 200 => '/directory/test2.jpg'];
217
218
		foreach( $this->object->getPreviews() as $key => $path ) {
219
			$this->assertStringStartsWith( $expected[$key], $path );
220
		}
221
	}
222
223
224
	public function testSetPreview()
225
	{
226
		$return = $this->object->setPreview( '/pictures/category.jpg' );
227
228
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
229
		$this->assertStringStartsWith( '/pictures/category.jpg', $this->object->getPreview() );
230
		$this->assertEquals( [1 => '/pictures/category.jpg'], $this->object->getPreviews() );
231
		$this->assertTrue( $this->object->isModified() );
232
	}
233
234
235
	public function testSetPreviews()
236
	{
237
		$return = $this->object->setPreviews( [1 => '/pictures/category.jpg'] );
238
239
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
240
		$this->assertEquals( [1 => '/pictures/category.jpg'], $this->object->getPreviews() );
241
		$this->assertTrue( $this->object->isModified() );
242
	}
243
244
245
	public function testGetStatus()
246
	{
247
		$this->assertEquals( 6, $this->object->getStatus() );
248
	}
249
250
251
	public function testSetStatus()
252
	{
253
		$return = $this->object->setStatus( 0 );
254
255
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
256
		$this->assertEquals( 0, $this->object->getStatus() );
257
		$this->assertTrue( $this->object->isModified() );
258
	}
259
260
261
	public function testGetTimeModified()
262
	{
263
		$this->assertEquals( '2011-01-01 00:00:02', $this->object->getTimeModified() );
264
	}
265
266
267
	public function testGetTimeCreated()
268
	{
269
		$this->assertEquals( '2011-01-01 00:00:01', $this->object->getTimeCreated() );
270
	}
271
272
273
	public function testGetEditor()
274
	{
275
		$this->assertEquals( 'unitTestUser', $this->object->editor() );
276
	}
277
278
279
	public function testGetName()
280
	{
281
		$this->assertEquals( 'testPicture', $this->object->getName() );
282
	}
283
284
285
	public function testGetNameProperties()
286
	{
287
		$values = ['media.value' => 'title', 'media.type' => 'name', '.languageid' => null];
288
		$propItems = [new \Aimeos\MShop\Common\Item\Property\Standard( 'media.', $values )];
289
		$object = new \Aimeos\MShop\Media\Item\Standard( $this->values, [], [], $propItems );
290
291
		$this->assertEquals( 'title', $object->getName() );
292
	}
293
294
295
	public function testGetResourceType()
296
	{
297
		$this->assertEquals( 'media', $this->object->getResourceType() );
298
	}
299
300
301
	public function testFromArray()
302
	{
303
		$item = new \Aimeos\MShop\Media\Item\Standard();
304
305
		$list = $entries = array(
306
			'media.id' => 1,
307
			'media.domain' => 'product',
308
			'media.label' => 'test item',
309
			'media.languageid' => 'de',
310
			'media.type' => 'test',
311
			'media.filesystem' => 'fs-test',
312
			'media.mimetype' => 'image/jpeg',
313
			'media.previews' => [1 => 'preview.jpg'],
314
			'media.preview' => 'preview.jpg',
315
			'media.url' => 'image.jpg',
316
			'media.status' => 0,
317
		);
318
319
		$item = $item->fromArray( $entries, true );
320
321
		$this->assertEquals( [], $entries );
322
		$this->assertEquals( $list['media.id'], $item->getId() );
323
		$this->assertEquals( $list['media.domain'], $item->getDomain() );
324
		$this->assertEquals( $list['media.label'], $item->getLabel() );
325
		$this->assertEquals( $list['media.filesystem'], $item->getFileSystem() );
326
		$this->assertEquals( $list['media.languageid'], $item->getLanguageId() );
327
		$this->assertEquals( $list['media.type'], $item->getType() );
328
		$this->assertEquals( $list['media.mimetype'], $item->getMimetype() );
329
		$this->assertEquals( $list['media.previews'], $item->getPreviews() );
330
		$this->assertEquals( $list['media.preview'], $item->getPreview() );
331
		$this->assertEquals( $list['media.url'], $item->getUrl() );
332
		$this->assertEquals( $list['media.status'], $item->getStatus() );
333
		$this->assertEquals( '', $item->getSiteId() );
334
	}
335
336
337
	public function testToArray()
338
	{
339
		$arrayObject = $this->object->toArray( true );
340
341
		$this->assertEquals( count( $this->values ), count( $arrayObject ) );
342
343
		$this->assertEquals( $this->object->getId(), $arrayObject['media.id'] );
344
		$this->assertEquals( $this->object->getSiteId(), $arrayObject['media.siteid'] );
345
		$this->assertEquals( $this->object->getDomain(), $arrayObject['media.domain'] );
346
		$this->assertEquals( $this->object->getLabel(), $arrayObject['media.label'] );
347
		$this->assertEquals( $this->object->getFileSystem(), $arrayObject['media.filesystem'] );
348
		$this->assertEquals( $this->object->getLanguageId(), $arrayObject['media.languageid'] );
349
		$this->assertEquals( $this->object->getMimeType(), $arrayObject['media.mimetype'] );
350
		$this->assertEquals( $this->object->getType(), $arrayObject['media.type'] );
351
		$this->assertEquals( $this->object->getUrl(), $arrayObject['media.url'] );
352
		$this->assertEquals( $this->object->getPreview(), $arrayObject['media.preview'] );
353
		$this->assertEquals( $this->object->getPreviews(), $arrayObject['media.previews'] );
354
		$this->assertEquals( $this->object->getStatus(), $arrayObject['media.status'] );
355
		$this->assertEquals( $this->object->getTimeCreated(), $arrayObject['media.ctime'] );
356
		$this->assertEquals( $this->object->getTimeModified(), $arrayObject['media.mtime'] );
357
		$this->assertEquals( $this->object->editor(), $arrayObject['media.editor'] );
358
	}
359
360
361
	public function testIsAvailable()
362
	{
363
		$this->assertTrue( $this->object->isAvailable() );
364
		$this->object->setAvailable( false );
365
		$this->assertFalse( $this->object->isAvailable() );
366
	}
367
368
369
	public function testIsAvailableOnStatus()
370
	{
371
		$this->assertTrue( $this->object->isAvailable() );
372
		$this->object->setStatus( 0 );
373
		$this->assertFalse( $this->object->isAvailable() );
374
		$this->object->setStatus( -1 );
375
		$this->assertFalse( $this->object->isAvailable() );
376
	}
377
378
}
379