Passed
Push — master ( c9c069...cc1d41 )
by Aimeos
05:10
created

StandardTest::testGetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
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-2018
7
 */
8
9
10
namespace Aimeos\MShop\Product\Item;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $values;
17
18
19
	protected function setUp()
20
	{
21
		$this->values = array(
22
			'product.id' => 1,
23
			'product.siteid' => 99,
24
			'product.type' => 'test',
25
			'product.status' => 1,
26
			'product.code' => 'TEST',
27
			'product.label' => 'testproduct',
28
			'product.config' => array( 'css-class' => 'test' ),
29
			'product.datestart' => null,
30
			'product.dateend' => null,
31
			'product.ctime' => '2011-01-19 17:04:32',
32
			'product.mtime' => '2011-01-19 18:04:32',
33
			'product.editor' => 'unitTestUser',
34
			'product.target' => 'testtarget',
35
		);
36
37
		$propItems = array(
38
			2 => new \Aimeos\MShop\Common\Item\Property\Standard( 'product.property.', array(
39
				'product.property.id' => 2,
40
				'product.property.parentid' => 1,
41
				'product.property.type' => 'proptest',
42
				'product.property.languageid' => 'de',
43
				'languageid' => 'de',
44
			) ),
45
			3 => new \Aimeos\MShop\Common\Item\Property\Standard( 'product.property.', array(
46
				'product.property.id' => 3,
47
				'product.property.parentid' => 1,
48
				'product.property.type' => 'proptype',
49
				'product.property.languageid' => 'de',
50
				'languageid' => 'fr',
51
			) ),
52
		);
53
54
		$this->object = new \Aimeos\MShop\Product\Item\Standard( $this->values, [], [], $propItems );
55
	}
56
57
58
	protected function tearDown()
59
	{
60
		unset( $this->object, $this->values );
61
	}
62
63
64
	public function testMagicMethods()
65
	{
66
		$this->assertFalse( isset( $this->object->test ) );
67
		$this->assertEquals( null, $this->object->test );
68
69
		$this->object->test = 'value';
70
71
		$this->assertTrue( isset( $this->object->test ) );
72
		$this->assertEquals( 'value', $this->object->test );
73
	}
74
75
76
	public function testGetSet()
77
	{
78
		$this->assertEquals( false, $this->object->get( 'test', false ) );
79
80
		$this->object->set( 'test', 'value' );
81
82
		$this->assertEquals( 'value', $this->object->get( 'test', false ) );
83
	}
84
85
86
	public function testGetId()
87
	{
88
		$this->assertEquals( '1', $this->object->getId() );
89
	}
90
91
92
	public function testSetId()
93
	{
94
		$return = $this->object->setId( null );
95
96
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
97
		$this->assertNull( $this->object->getId() );
98
		$this->assertTrue( $this->object->isModified() );
99
100
		$return = $this->object->setId( 1 );
101
102
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
103
		$this->assertEquals( '1', $this->object->getId() );
104
		$this->assertFalse( $this->object->isModified() );
105
106
		$this->setExpectedException( \Aimeos\MShop\Exception::class );
107
		$this->object->setId( 2 );
108
	}
109
110
111
	public function testGetSiteId()
112
	{
113
		$this->assertEquals( 99, $this->object->getSiteId() );
114
	}
115
116
117
	public function testGetPropertyItems()
118
	{
119
		$propItems = $this->object->getPropertyItems();
120
121
		$this->assertEquals( 1, count( $propItems ) );
122
123
		foreach( $propItems as $propItem ) {
124
			$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Property\Iface::class, $propItem );
125
		}
126
	}
127
128
129
	public function testGetPropertyItemsAll()
130
	{
131
		$propItems = $this->object->getPropertyItems( null, false );
132
133
		$this->assertEquals( 2, count( $propItems ) );
134
135
		foreach( $propItems as $propItem ) {
136
			$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Property\Iface::class, $propItem );
137
		}
138
	}
139
140
141
	public function testGetPropertyItemsType()
142
	{
143
		$propItems = $this->object->getPropertyItems( 'proptest' );
144
145
		$this->assertEquals( 1, count( $propItems ) );
146
147
		foreach( $propItems as $propItem ) {
148
			$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Property\Iface::class, $propItem );
149
		}
150
	}
151
152
153
	public function testGetType()
154
	{
155
		$this->assertEquals( 'test', $this->object->getType() );
156
	}
157
158
159
	public function testSetType()
160
	{
161
		$this->assertFalse( $this->object->isModified() );
162
163
		$return = $this->object->setType( 'default' );
164
165
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
166
		$this->assertEquals( 'default', $this->object->getType() );
167
		$this->assertTrue( $this->object->isModified() );
168
	}
169
170
171
	public function testGetCode()
172
	{
173
		$this->assertEquals( 'TEST', $this->object->getCode() );
174
	}
175
176
177
	public function testSetCode()
178
	{
179
		$this->assertFalse( $this->object->isModified() );
180
181
		$return = $this->object->setCode( 'NEU' );
182
183
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
184
		$this->assertEquals( 'NEU', $this->object->getCode() );
185
		$this->assertTrue( $this->object->isModified() );
186
	}
187
188
189
	public function testGetEditor()
190
	{
191
		$this->assertEquals( 'unitTestUser', $this->object->getEditor() );
192
	}
193
194
195
	public function testGetStatus()
196
	{
197
		$this->assertEquals( 1, $this->object->getStatus() );
198
	}
199
200
201
	public function testSetStatus()
202
	{
203
		$return = $this->object->setStatus( 8 );
204
205
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
206
		$this->assertEquals( 8, $this->object->getStatus() );
207
		$this->assertTrue( $this->object->isModified() );
208
	}
209
210
211
	public function testGetLabel()
212
	{
213
		$this->assertEquals( 'testproduct', $this->object->getLabel() );
214
	}
215
216
217
	public function testSetLabel()
218
	{
219
		$return = $this->object->setLabel( 'editproduct' );
220
221
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
222
		$this->assertEquals( 'editproduct', $this->object->getLabel() );
223
		$this->assertTrue( $this->object->isModified() );
224
	}
225
226
227
	public function testGetConfig()
228
	{
229
		$this->assertEquals( array( 'css-class' => 'test' ), $this->object->getConfig() );
230
	}
231
232
233
	public function testGetConfigValue()
234
	{
235
		$this->assertEquals( 'test', $this->object->getConfigValue( 'css-class' ) );
236
	}
237
238
239
	public function testSetConfig()
240
	{
241
		$this->assertFalse( $this->object->isModified() );
242
243
		$return = $this->object->setConfig( array( 'key' => 'value' ) );
244
245
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
246
		$this->assertEquals( array( 'key' => 'value' ), $this->object->getConfig() );
247
		$this->assertTrue( $this->object->isModified() );
248
	}
249
250
251
	public function testGetTarget()
252
	{
253
		$this->assertEquals( 'testtarget', $this->object->getTarget() );
254
	}
255
256
257
	public function testSetTarget()
258
	{
259
		$this->assertFalse( $this->object->isModified() );
260
261
		$return = $this->object->setTarget( 'ttarget' );
262
263
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
264
		$this->assertEquals( 'ttarget', $this->object->getTarget() );
265
		$this->assertTrue( $this->object->isModified() );
266
	}
267
268
269
	public function testGetDateStart()
270
	{
271
		$this->assertEquals( null, $this->object->getDateStart() );
272
	}
273
274
275
	public function testSetDateStart()
276
	{
277
		$return = $this->object->setDateStart( '2010-04-22 06:22:22' );
278
279
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
280
		$this->assertEquals( '2010-04-22 06:22:22', $this->object->getDateStart() );
281
		$this->assertTrue( $this->object->isModified() );
282
	}
283
284
285
	public function testGetDateEnd()
286
	{
287
		$this->assertEquals( null, $this->object->getDateEnd() );
288
	}
289
290
291
	public function testSetDateEnd()
292
	{
293
		$return = $this->object->setDateEnd( '2010-05-22 06:22:22' );
294
295
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
296
		$this->assertEquals( '2010-05-22 06:22:22', $this->object->getDateEnd() );
297
		$this->assertTrue( $this->object->isModified() );
298
	}
299
300
301
	public function testGetTimeModified()
302
	{
303
		$this->assertEquals( '2011-01-19 18:04:32', $this->object->getTimeModified() );
304
	}
305
306
307
	public function testGetTimeCreated()
308
	{
309
		$this->assertEquals( '2011-01-19 17:04:32', $this->object->getTimeCreated() );
310
	}
311
312
313
	public function testSetTimeCreated()
314
	{
315
		$return = $this->object->setTimeCreated( '2010-05-22 06:22:22' );
316
317
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $return );
318
		$this->assertEquals( '2010-05-22 06:22:22', $this->object->getTimeCreated() );
319
		$this->assertTrue( $this->object->isModified() );
320
	}
321
322
323
	public function testIsAvailable()
324
	{
325
		$this->assertTrue( $this->object->isAvailable() );
326
		$this->object->setAvailable( false );
327
		$this->assertFalse( $this->object->isAvailable() );
328
	}
329
330
331
	public function testIsAvailableOnStatus()
332
	{
333
		$this->assertTrue( $this->object->isAvailable() );
334
		$this->object->setStatus( 0 );
335
		$this->assertFalse( $this->object->isAvailable() );
336
		$this->object->setStatus( -1 );
337
		$this->assertFalse( $this->object->isAvailable() );
338
	}
339
340
341
	public function testIsModified()
342
	{
343
		$this->assertFalse( $this->object->isModified() );
344
	}
345
346
347
	public function testIsModifiedTrue()
348
	{
349
		$this->object->setLabel( 'reeditProduct' );
350
		$this->assertTrue( $this->object->isModified() );
351
	}
352
353
354
	public function testGetResourceType()
355
	{
356
		$this->assertEquals( 'product', $this->object->getResourceType() );
357
	}
358
359
360
	public function testFromArray()
361
	{
362
		$item = new \Aimeos\MShop\Product\Item\Standard();
363
364
		$list = $entries = array(
365
			'product.id' => 1,
366
			'product.type' => 'test',
367
			'product.label' => 'test item',
368
			'product.code' => 'test',
369
			'product.datestart' => '2000-01-01 00:00:00',
370
			'product.dateend' => '2001-01-01 00:00:00',
371
			'product.config' => array( 'key' => 'value' ),
372
			'product.status' => 0,
373
			'product.target' => 'ttarget',
374
		);
375
376
		$item = $item->fromArray( $entries, true );
377
378
		$this->assertEquals( [], $entries );
379
		$this->assertEquals( $list['product.id'], $item->getId() );
380
		$this->assertEquals( $list['product.code'], $item->getCode() );
381
		$this->assertEquals( $list['product.label'], $item->getLabel() );
382
		$this->assertEquals( $list['product.type'], $item->getType() );
383
		$this->assertEquals( $list['product.datestart'], $item->getDateStart() );
384
		$this->assertEquals( $list['product.dateend'], $item->getDateEnd() );
385
		$this->assertEquals( $list['product.config'], $item->getConfig() );
386
		$this->assertEquals( $list['product.status'], $item->getStatus() );
387
		$this->assertEquals( $list['product.target'], $item->getTarget() );
388
		$this->assertNull( $item->getSiteId() );
389
	}
390
391
392
	public function testToArray()
393
	{
394
		$arrayObject = $this->object->toArray( true );
395
		$this->assertEquals( count( $this->values ), count( $arrayObject ) );
396
397
		$this->assertEquals( $this->object->getId(), $arrayObject['product.id'] );
398
		$this->assertEquals( $this->object->getSiteId(), $arrayObject['product.siteid'] );
399
		$this->assertEquals( $this->object->getCode(), $arrayObject['product.code'] );
400
		$this->assertEquals( $this->object->getType(), $arrayObject['product.type'] );
401
		$this->assertEquals( $this->object->getLabel(), $arrayObject['product.label'] );
402
		$this->assertEquals( $this->object->getStatus(), $arrayObject['product.status'] );
403
		$this->assertEquals( $this->object->getDateStart(), $arrayObject['product.datestart'] );
404
		$this->assertEquals( $this->object->getDateEnd(), $arrayObject['product.dateend'] );
405
		$this->assertEquals( $this->object->getConfig(), $arrayObject['product.config'] );
406
		$this->assertEquals( $this->object->getTimeCreated(), $arrayObject['product.ctime'] );
407
		$this->assertEquals( $this->object->getTimeModified(), $arrayObject['product.mtime'] );
408
		$this->assertEquals( $this->object->getEditor(), $arrayObject['product.editor'] );
409
		$this->assertEquals( $this->object->getTarget(), $arrayObject['product.target'] );
410
	}
411
412
}
413