Passed
Push — master ( 21ffe5...3ded41 )
by Aimeos
05:19
created

StandardTest::testGetSavePropertyItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 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-2024
7
 */
8
9
namespace Aimeos\MShop\Customer\Manager;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $context;
16
	private $fixture;
17
	private $address;
18
19
20
	protected function setUp() : void
21
	{
22
		$this->context = \TestHelper::context();
23
24
		$this->object = new \Aimeos\MShop\Customer\Manager\Standard( $this->context );
25
		$this->object = new \Aimeos\MShop\Common\Manager\Decorator\Lists( $this->object, $this->context );
26
		$this->object = new \Aimeos\MShop\Common\Manager\Decorator\Property( $this->object, $this->context );
27
		$this->object = new \Aimeos\MShop\Common\Manager\Decorator\Address( $this->object, $this->context );
28
		$this->object->setObject( $this->object );
29
30
		$this->fixture = array(
31
			'customer.label' => 'unitTest',
32
			'customer.status' => 2,
33
		);
34
35
		$this->address = new \Aimeos\MShop\Common\Item\Address\Standard( 'customer.' );
36
	}
37
38
39
	protected function tearDown() : void
40
	{
41
		unset( $this->object, $this->fixture, $this->address, $this->context );
42
	}
43
44
45
	public function testAggregate()
46
	{
47
		$search = $this->object->filter();
48
		$result = $this->object->aggregate( $search, 'customer.salutation' );
49
50
		$this->assertEquals( 2, count( $result ) );
51
		$this->assertArrayHasKey( 'mr', $result );
52
		$this->assertEquals( 1, $result->get( 'mr' ) );
53
	}
54
55
56
	public function testAggregateMultiple()
57
	{
58
		$cols = ['customer.salutation', 'customer.title'];
59
		$search = $this->object->filter()->order( $cols );
60
		$result = $this->object->aggregate( $search, $cols );
61
62
		$this->assertEquals( ['mr' => ['Dr' => 1], '' => ['' => 2]], $result->toArray() );
63
	}
64
65
66
	public function testClear()
67
	{
68
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( [-1] ) );
69
	}
70
71
72
	public function testGetResourceType()
73
	{
74
		$result = $this->object->getResourceType();
75
76
		$this->assertContains( 'customer', $result );
77
		$this->assertContains( 'customer/address', $result );
78
		$this->assertContains( 'customer/lists', $result );
79
	}
80
81
82
	public function testGetSearchAttributes()
83
	{
84
		foreach( $this->object->getSearchAttributes() as $attribute )
85
		{
86
			$this->assertInstanceOf( \Aimeos\Base\Criteria\Attribute\Iface::class, $attribute );
87
		}
88
	}
89
90
91
	public function testCreate()
92
	{
93
		$item = $this->object->create();
94
		$this->assertInstanceOf( \Aimeos\MShop\Customer\Item\Iface::class, $item );
95
	}
96
97
98
	public function testCreateAddressItem()
99
	{
100
		$item = $this->object->createAddressItem();
101
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Address\Iface::class, $item );
102
	}
103
104
105
	public function testCreateListsItem()
106
	{
107
		$item = $this->object->createListItem();
108
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Lists\Iface::class, $item );
109
	}
110
111
112
	public function testCreatePropertyItem()
113
	{
114
		$item = $this->object->createPropertyItem();
115
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Property\Iface::class, $item );
116
	}
117
118
119
	public function testFind()
120
	{
121
		$item = $this->object->find( '[email protected]' );
122
123
		$this->assertEquals( '[email protected]', $item->getCode() );
124
	}
125
126
127
	public function testFrom()
128
	{
129
		$items = $this->object->from( [[
130
			'customer.id' => 1,
131
			'customer.code' => 'test',
132
			'customer.label' => 'test',
133
			'address' => [[
134
				'customer.address.company' => 'test',
135
			]],
136
			'lists' => [
137
				'product' => [[
138
					'product.id' => '123',
139
				]],
140
			],
141
			'property' => [[
142
				'product.property.type' => 'newsleter',
143
				'product.property.value' => '1',
144
			]],
145
		]] );
146
147
		$item = $items->first();
148
		$this->assertEquals( 1, count( $items ) );
149
		$this->assertInstanceOf( \Aimeos\MShop\Customer\Item\Iface::class, $item );
150
		$this->assertEquals( 1, count( $item->getListItems( 'product' ) ) );
151
		$this->assertEquals( 1, count( $item->getRefItems( 'product' ) ) );
152
		$this->assertEquals( 1, count( $item->getPropertyItems() ) );
153
		$this->assertEquals( 1, count( $item->getAddressItems() ) );
154
	}
155
156
157
	public function testGet()
158
	{
159
		$domains = ['text', 'customer/property' => ['newsletter'], 'customer/property/type'];
160
		$search = $this->object->filter()->slice( 0, 1 );
161
		$conditions = array(
162
			$search->compare( '==', 'customer.code', '[email protected]' ),
163
			$search->compare( '==', 'customer.editor', $this->context->editor() )
164
		);
165
		$search->setConditions( $search->and( $conditions ) );
166
		$expected = $this->object->search( $search, $domains )->first();
167
168
		$actual = $this->object->get( $expected->getId(), $domains );
169
		$payAddr = $actual->getPaymentAddress();
170
171
		$this->assertEquals( 'unitCustomer001', $actual->getLabel() );
172
		$this->assertEquals( '[email protected]', $actual->getCode() );
173
		$this->assertEquals( 'mr', $payAddr->getSalutation() );
174
		$this->assertEquals( 'Example company', $payAddr->getCompany() );
175
		$this->assertEquals( 'Dr', $payAddr->getTitle() );
176
		$this->assertEquals( 'Our', $payAddr->getFirstname() );
177
		$this->assertEquals( 'Unittest', $payAddr->getLastname() );
178
		$this->assertEquals( 'Pickhuben', $payAddr->getAddress1() );
179
		$this->assertEquals( '2-4', $payAddr->getAddress2() );
180
		$this->assertEquals( '', $payAddr->getAddress3() );
181
		$this->assertEquals( '20457', $payAddr->getPostal() );
182
		$this->assertEquals( 'Hamburg', $payAddr->getCity() );
183
		$this->assertEquals( 'Hamburg', $payAddr->getState() );
184
		$this->assertEquals( 'de', $payAddr->getLanguageId() );
185
		$this->assertEquals( 'DE', $payAddr->getCountryId() );
186
		$this->assertEquals( '055544332211', $payAddr->getTelephone() );
187
		$this->assertEquals( '055544332212', $payAddr->getTelefax() );
188
		$this->assertEquals( '055544332213', $payAddr->getMobile() );
189
		$this->assertEquals( '[email protected]', $payAddr->getEMail() );
190
		$this->assertEquals( 'www.example.com', $payAddr->getWebsite() );
191
		$this->assertEquals( '10.0', $payAddr->getLongitude() );
192
		$this->assertEquals( '50.0', $payAddr->getLatitude() );
193
		$this->assertEquals( 1, $actual->getStatus() );
194
		$this->assertEquals( 'core', $actual->editor() );
195
196
		$this->assertEquals( $expected, $actual );
197
		$this->assertEquals( 1, count( $actual->getListItems( 'text', null, null, false ) ) );
198
		$this->assertEquals( 1, count( $actual->getRefItems( 'text', null, null, false ) ) );
199
		$this->assertEquals( 1, count( $actual->getPropertyItems() ) );
200
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Type\Iface::class, $actual->getPropertyItems()->first()?->getTypeItem() );
201
	}
202
203
204
	public function testSaveUpdateDelete()
205
	{
206
		$group = \Aimeos\MShop::create( $this->context, 'group' )->find( 'unitgroup' );
207
		$item = $this->object->create();
208
209
		$item->setCode( 'unitTest' );
210
		$item->setLabel( 'unitTest' );
211
		$item->setGroups( [$group->getId()] );
212
		$item = $this->object->save( $item );
213
		$itemSaved = $this->object->get( $item->getId(), ['group'] );
214
215
		$itemExp = clone $itemSaved;
216
		$itemExp->setCode( 'unitTest2' );
217
		$itemExp->setLabel( 'unitTest2' );
218
		$itemExp->setGroups( [] );
219
		$itemExp = $this->object->save( $itemExp );
220
		$itemUpd = $this->object->get( $itemExp->getId(), ['group'] );
221
222
		$this->object->delete( $item->getId() );
223
224
225
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $itemSaved );
226
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $itemUpd );
227
228
		$this->assertTrue( $item->getId() !== null );
229
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
230
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
231
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
232
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
233
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
234
		$this->assertEquals( $item->getPassword(), $itemSaved->getPassword() );
235
		$this->assertEquals( $item->getGroups(), $itemSaved->getGroups() );
236
		$this->assertEquals( $itemSaved->getPaymentAddress()->getId(), $itemSaved->getId() );
237
238
		$this->assertEquals( $this->context->editor(), $itemSaved->editor() );
239
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
240
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
241
242
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
243
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
244
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
245
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
246
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
247
		$this->assertEquals( $itemExp->getPassword(), $itemUpd->getPassword() );
248
		$this->assertEquals( $itemExp->getGroups(), $itemUpd->getGroups() );
249
		$this->assertEquals( $itemUpd->getPaymentAddress()->getId(), $itemUpd->getId() );
250
251
		$this->assertEquals( $this->context->editor(), $itemUpd->editor() );
252
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
253
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
254
255
256
		$this->expectException( \Aimeos\MShop\Exception::class );
257
		$this->object->get( $item->getId() );
258
	}
259
260
261
	public function testGetSaveAddressItems()
262
	{
263
		$item = $this->object->find( '[email protected]', ['customer/address'] );
264
265
		$item->setId( null )->setCode( 'xyz' );
266
		$item->getPaymentAddress()->setEmail( '[email protected]' );
267
		$item->addAddressItem( $this->object->createAddressItem() );
268
		$this->object->save( $item );
269
270
		$item2 = $this->object->find( 'xyz', ['customer/address'] );
271
272
		$this->object->delete( $item->getId() );
273
274
		$this->assertEquals( 2, count( $item->getAddressItems() ) );
275
		$this->assertEquals( 2, count( $item2->getAddressItems() ) );
276
	}
277
278
279
	public function testGetSavePropertyItems()
280
	{
281
		$item = $this->object->find( '[email protected]', ['customer/property'] );
282
283
		$item->setId( null )->setCode( 'xyz' );
284
		$item->getPaymentAddress()->setEmail( '[email protected]' );
285
		$this->object->save( $item );
286
287
		$item2 = $this->object->find( 'xyz', ['customer/property'] );
288
289
		$this->object->delete( $item->getId() );
290
291
		$this->assertEquals( 1, count( $item->getPropertyItems() ) );
292
		$this->assertEquals( 1, count( $item2->getPropertyItems() ) );
293
	}
294
295
296
	public function testFilter()
297
	{
298
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter() );
299
	}
300
301
302
	public function testSearch()
303
	{
304
		$item = $this->object->find( '[email protected]', ['text'] );
305
306
		if( ( $listItem = $item->getListItems( 'text', 'default' )->first() ) === null ) {
307
			throw new \RuntimeException( 'No list item found' );
308
		}
309
310
		$search = $this->object->filter();
311
312
		$expr = [];
313
		$expr[] = $search->compare( '!=', 'customer.id', null );
314
		$expr[] = $search->compare( '==', 'customer.label', 'unitCustomer001' );
315
		$expr[] = $search->compare( '==', 'customer.code', '[email protected]' );
316
		$expr[] = $search->compare( '>=', 'customer.password', '' );
317
		$expr[] = $search->compare( '==', 'customer.status', 1 );
318
		$expr[] = $search->compare( '>', 'customer.mtime', '1970-01-01 00:00:00' );
319
		$expr[] = $search->compare( '>', 'customer.ctime', '1970-01-01 00:00:00' );
320
		$expr[] = $search->compare( '==', 'customer.editor', $this->context->editor() );
321
322
		$expr[] = $search->compare( '==', 'customer.salutation', 'mr' );
323
		$expr[] = $search->compare( '==', 'customer.company', 'Example company' );
324
		$expr[] = $search->compare( '==', 'customer.vatid', 'DE999999999' );
325
		$expr[] = $search->compare( '==', 'customer.title', 'Dr' );
326
		$expr[] = $search->compare( '==', 'customer.firstname', 'Our' );
327
		$expr[] = $search->compare( '==', 'customer.lastname', 'Unittest' );
328
		$expr[] = $search->compare( '==', 'customer.address1', 'Pickhuben' );
329
		$expr[] = $search->compare( '==', 'customer.address2', '2-4' );
330
		$expr[] = $search->compare( '==', 'customer.address3', '' );
331
		$expr[] = $search->compare( '==', 'customer.postal', '20457' );
332
		$expr[] = $search->compare( '==', 'customer.city', 'Hamburg' );
333
		$expr[] = $search->compare( '==', 'customer.state', 'Hamburg' );
334
		$expr[] = $search->compare( '==', 'customer.languageid', 'de' );
335
		$expr[] = $search->compare( '==', 'customer.countryid', 'DE' );
336
		$expr[] = $search->compare( '==', 'customer.telephone', '055544332211' );
337
		$expr[] = $search->compare( '==', 'customer.telefax', '055544332212' );
338
		$expr[] = $search->compare( '==', 'customer.mobile', '055544332213' );
339
		$expr[] = $search->compare( '==', 'customer.email', '[email protected]' );
340
		$expr[] = $search->compare( '==', 'customer.website', 'www.example.com' );
341
		$expr[] = $search->compare( '==', 'customer.longitude', '10.0' );
342
		$expr[] = $search->compare( '>=', 'customer.latitude', '50.0' );
343
		$expr[] = $search->compare( '==', 'customer.birthday', '1999-01-01' );
344
345
		$param = ['text', 'default', $listItem->getRefId()];
346
		$expr[] = $search->compare( '!=', $search->make( 'customer:has', $param ), null );
347
348
		$param = ['text', 'default'];
349
		$expr[] = $search->compare( '!=', $search->make( 'customer:has', $param ), null );
350
351
		$param = ['text'];
352
		$expr[] = $search->compare( '!=', $search->make( 'customer:has', $param ), null );
353
354
		$param = ['newsletter', null, '1'];
355
		$expr[] = $search->compare( '!=', $search->make( 'customer:prop', $param ), null );
356
357
		$param = ['newsletter', null];
358
		$expr[] = $search->compare( '!=', $search->make( 'customer:prop', $param ), null );
359
360
		$param = ['newsletter'];
361
		$expr[] = $search->compare( '!=', $search->make( 'customer:prop', $param ), null );
362
363
		$expr[] = $search->compare( '!=', 'customer.address.id', null );
364
		$expr[] = $search->compare( '!=', 'customer.address.parentid', null );
365
		$expr[] = $search->compare( '==', 'customer.address.salutation', 'mr' );
366
		$expr[] = $search->compare( '==', 'customer.address.company', 'Example company' );
367
		$expr[] = $search->compare( '==', 'customer.address.vatid', 'DE999999999' );
368
		$expr[] = $search->compare( '==', 'customer.address.title', 'Dr' );
369
		$expr[] = $search->compare( '==', 'customer.address.firstname', 'Our' );
370
		$expr[] = $search->compare( '==', 'customer.address.lastname', 'Unittest' );
371
		$expr[] = $search->compare( '==', 'customer.address.address1', 'Pickhuben' );
372
		$expr[] = $search->compare( '==', 'customer.address.address2', '2-4' );
373
		$expr[] = $search->compare( '==', 'customer.address.address3', '' );
374
		$expr[] = $search->compare( '==', 'customer.address.postal', '20457' );
375
		$expr[] = $search->compare( '==', 'customer.address.city', 'Hamburg' );
376
		$expr[] = $search->compare( '==', 'customer.address.state', 'Hamburg' );
377
		$expr[] = $search->compare( '==', 'customer.address.languageid', 'de' );
378
		$expr[] = $search->compare( '==', 'customer.address.countryid', 'DE' );
379
		$expr[] = $search->compare( '==', 'customer.address.telephone', '055544332211' );
380
		$expr[] = $search->compare( '==', 'customer.address.telefax', '055544332212' );
381
		$expr[] = $search->compare( '==', 'customer.address.mobile', '055544332213' );
382
		$expr[] = $search->compare( '==', 'customer.address.email', '[email protected]' );
383
		$expr[] = $search->compare( '==', 'customer.address.website', 'www.example.com' );
384
		$expr[] = $search->compare( '==', 'customer.address.longitude', '10.0' );
385
		$expr[] = $search->compare( '==', 'customer.address.latitude', '50.0' );
386
		$expr[] = $search->compare( '==', 'customer.address.position', 0 );
387
		$expr[] = $search->compare( '==', 'customer.address.birthday', '2000-01-01' );
388
		$expr[] = $search->compare( '>=', 'customer.address.mtime', '1970-01-01 00:00:00' );
389
		$expr[] = $search->compare( '>=', 'customer.address.ctime', '1970-01-01 00:00:00' );
390
		$expr[] = $search->compare( '==', 'customer.address.editor', $this->context->editor() );
391
392
		$search->setConditions( $search->and( $expr ) );
393
		$result = $this->object->search( $search )->toArray();
394
		$this->assertEquals( 1, count( $result ) );
395
	}
396
397
398
	public function testSearchTotal()
399
	{
400
		$search = $this->object->filter();
401
		$search->setConditions( $search->compare( '==', 'customer.address.editor', $this->context->editor() ) );
402
		$search->slice( 0, 2 );
403
404
		$total = 0;
405
		$results = $this->object->search( $search, [], $total )->toArray();
406
407
		$this->assertEquals( 2, count( $results ) );
408
		$this->assertEquals( 3, $total );
409
410
		foreach( $results as $itemId => $item ) {
411
			$this->assertEquals( $itemId, $item->getId() );
412
		}
413
	}
414
415
416
	public function testSearchCriteria()
417
	{
418
		$search = $this->object->filter( true );
419
		$conditions = array(
420
			$search->compare( '==', 'customer.address.editor', $this->context->editor() ),
421
			$search->getConditions()
422
		);
423
		$search->setConditions( $search->and( $conditions ) );
424
		$this->assertEquals( 2, count( $this->object->search( $search )->toArray() ) );
425
	}
426
427
428
	public function testSearchRef()
429
	{
430
		$search = $this->object->filter();
431
		$search->setConditions( $search->compare( '==', 'customer.code', '[email protected]' ) );
432
433
		$item = $this->object->search( $search, ['customer/address', 'text'] )->first();
434
435
		$this->assertEquals( 1, count( $item->getRefItems( 'text', null, null, false ) ) );
436
		$this->assertEquals( 1, count( $item->getAddressItems() ) );
437
	}
438
439
440
	public function testGetSubManager()
441
	{
442
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'address' ) );
443
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'address', 'Standard' ) );
444
445
		$this->expectException( \LogicException::class );
446
		$this->object->getSubManager( 'unknown' );
447
	}
448
449
450
	public function testGetSubManagerInvalidName()
451
	{
452
		$this->expectException( \LogicException::class );
453
		$this->object->getSubManager( 'address', 'unknown' );
454
	}
455
}
456