Passed
Push — master ( af9504...7332c2 )
by Aimeos
14:14
created

StandardTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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