Passed
Push — master ( e47fc3...a0c80c )
by Aimeos
04:21
created

StandardTest::testGetUrlVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
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-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 testGetUrlVersion()
189
	{
190
		$this->assertStringStartsWith( 'http://www.url.com/test.jpg?v=', $this->object->getUrl( true ) );
191
	}
192
193
194
	public function testSetUrl()
195
	{
196
		$return = $this->object->setUrl( null )->setUrl( '/pictures/category.jpg' );
197
198
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
199
		$this->assertEquals( '/pictures/category.jpg', $this->object->getUrl() );
200
		$this->assertTrue( $this->object->isModified() );
201
	}
202
203
204
	public function testGetPreview()
205
	{
206
		$this->assertStringStartsWith( '/directory/test.jpg', $this->object->getPreview() );
207
		$this->assertStringStartsWith( '/directory/test.jpg', $this->object->getPreview( 100 ) );
208
		$this->assertStringStartsWith( '/directory/test2.jpg', $this->object->getPreview( true ) );
209
		$this->assertStringStartsWith( '/directory/test2.jpg', $this->object->getPreview( 150 ) );
210
		$this->assertStringStartsWith( '/directory/test2.jpg', $this->object->getPreview( 250 ) );
211
	}
212
213
214
	public function testGetPreviews()
215
	{
216
		$this->assertEquals( [100 => '/directory/test.jpg', 200 => '/directory/test2.jpg'], $this->object->getPreviews() );
217
	}
218
219
220
	public function testGetPreviewsVersion()
221
	{
222
		$expected = [100 => '/directory/test.jpg?v=', 200 => '/directory/test2.jpg?v='];
223
224
		foreach( $this->object->getPreviews( true ) as $key => $path ) {
225
			$this->assertStringStartsWith( $expected[$key], $path );
226
		}
227
	}
228
229
230
	public function testSetPreview()
231
	{
232
		$return = $this->object->setPreview( '/pictures/category.jpg' );
233
234
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
235
		$this->assertStringStartsWith( '/pictures/category.jpg', $this->object->getPreview() );
236
		$this->assertEquals( [1 => '/pictures/category.jpg'], $this->object->getPreviews() );
237
		$this->assertTrue( $this->object->isModified() );
238
	}
239
240
241
	public function testSetPreviews()
242
	{
243
		$return = $this->object->setPreviews( [1 => '/pictures/category.jpg'] );
244
245
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
246
		$this->assertEquals( [1 => '/pictures/category.jpg'], $this->object->getPreviews() );
247
		$this->assertTrue( $this->object->isModified() );
248
	}
249
250
251
	public function testGetStatus()
252
	{
253
		$this->assertEquals( 6, $this->object->getStatus() );
254
	}
255
256
257
	public function testSetStatus()
258
	{
259
		$return = $this->object->setStatus( 0 );
260
261
		$this->assertInstanceOf( \Aimeos\MShop\Media\Item\Iface::class, $return );
262
		$this->assertEquals( 0, $this->object->getStatus() );
263
		$this->assertTrue( $this->object->isModified() );
264
	}
265
266
267
	public function testGetTimeModified()
268
	{
269
		$this->assertEquals( '2011-01-01 00:00:02', $this->object->getTimeModified() );
270
	}
271
272
273
	public function testGetTimeCreated()
274
	{
275
		$this->assertEquals( '2011-01-01 00:00:01', $this->object->getTimeCreated() );
276
	}
277
278
279
	public function testGetEditor()
280
	{
281
		$this->assertEquals( 'unitTestUser', $this->object->editor() );
282
	}
283
284
285
	public function testGetName()
286
	{
287
		$this->assertEquals( 'testPicture', $this->object->getName() );
288
	}
289
290
291
	public function testGetNameProperties()
292
	{
293
		$values = ['media.value' => 'title', 'media.type' => 'name', '.languageid' => null];
294
		$propItems = [new \Aimeos\MShop\Common\Item\Property\Standard( 'media.', $values )];
295
		$object = new \Aimeos\MShop\Media\Item\Standard( $this->values, [], [], $propItems );
296
297
		$this->assertEquals( 'title', $object->getName() );
298
	}
299
300
301
	public function testGetResourceType()
302
	{
303
		$this->assertEquals( 'media', $this->object->getResourceType() );
304
	}
305
306
307
	public function testFromArray()
308
	{
309
		$item = new \Aimeos\MShop\Media\Item\Standard();
310
311
		$list = $entries = array(
312
			'media.id' => 1,
313
			'media.domain' => 'product',
314
			'media.label' => 'test item',
315
			'media.languageid' => 'de',
316
			'media.type' => 'test',
317
			'media.filesystem' => 'fs-test',
318
			'media.mimetype' => 'image/jpeg',
319
			'media.previews' => [1 => 'preview.jpg'],
320
			'media.preview' => 'preview.jpg',
321
			'media.url' => 'image.jpg',
322
			'media.status' => 0,
323
		);
324
325
		$item = $item->fromArray( $entries, true );
326
327
		$this->assertEquals( [], $entries );
328
		$this->assertEquals( $list['media.id'], $item->getId() );
329
		$this->assertEquals( $list['media.domain'], $item->getDomain() );
330
		$this->assertEquals( $list['media.label'], $item->getLabel() );
331
		$this->assertEquals( $list['media.filesystem'], $item->getFileSystem() );
332
		$this->assertEquals( $list['media.languageid'], $item->getLanguageId() );
333
		$this->assertEquals( $list['media.type'], $item->getType() );
334
		$this->assertEquals( $list['media.mimetype'], $item->getMimetype() );
335
		$this->assertEquals( $list['media.previews'], $item->getPreviews() );
336
		$this->assertEquals( $list['media.preview'], $item->getPreview() );
337
		$this->assertEquals( $list['media.url'], $item->getUrl() );
338
		$this->assertEquals( $list['media.status'], $item->getStatus() );
339
		$this->assertEquals( '', $item->getSiteId() );
340
	}
341
342
343
	public function testToArray()
344
	{
345
		$arrayObject = $this->object->toArray( true );
346
347
		$this->assertEquals( count( $this->values ), count( $arrayObject ) );
348
349
		$this->assertEquals( $this->object->getId(), $arrayObject['media.id'] );
350
		$this->assertEquals( $this->object->getSiteId(), $arrayObject['media.siteid'] );
351
		$this->assertEquals( $this->object->getDomain(), $arrayObject['media.domain'] );
352
		$this->assertEquals( $this->object->getLabel(), $arrayObject['media.label'] );
353
		$this->assertEquals( $this->object->getFileSystem(), $arrayObject['media.filesystem'] );
354
		$this->assertEquals( $this->object->getLanguageId(), $arrayObject['media.languageid'] );
355
		$this->assertEquals( $this->object->getMimeType(), $arrayObject['media.mimetype'] );
356
		$this->assertEquals( $this->object->getType(), $arrayObject['media.type'] );
357
		$this->assertEquals( $this->object->getUrl(), $arrayObject['media.url'] );
358
		$this->assertEquals( $this->object->getPreview(), $arrayObject['media.preview'] );
359
		$this->assertEquals( $this->object->getPreviews(), $arrayObject['media.previews'] );
360
		$this->assertEquals( $this->object->getStatus(), $arrayObject['media.status'] );
361
		$this->assertEquals( $this->object->getTimeCreated(), $arrayObject['media.ctime'] );
362
		$this->assertEquals( $this->object->getTimeModified(), $arrayObject['media.mtime'] );
363
		$this->assertEquals( $this->object->editor(), $arrayObject['media.editor'] );
364
	}
365
366
367
	public function testIsAvailable()
368
	{
369
		$this->assertTrue( $this->object->isAvailable() );
370
		$this->object->setAvailable( false );
371
		$this->assertFalse( $this->object->isAvailable() );
372
	}
373
374
375
	public function testIsAvailableOnStatus()
376
	{
377
		$this->assertTrue( $this->object->isAvailable() );
378
		$this->object->setStatus( 0 );
379
		$this->assertFalse( $this->object->isAvailable() );
380
		$this->object->setStatus( -1 );
381
		$this->assertFalse( $this->object->isAvailable() );
382
	}
383
384
}
385