LaravelTest::testSaveUpdateDeleteItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 34
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 46
rs 9.376
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 */
7
8
9
namespace Aimeos\MShop\Customer\Manager;
10
11
12
class LaravelTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $object;
16
17
18
	protected function setUp() : void
19
	{
20
		$this->context = \TestHelper::context();
21
22
		$this->object = new \Aimeos\MShop\Customer\Manager\Laravel( $this->context );
23
		$this->object = new \Aimeos\MShop\Common\Manager\Decorator\Lists( $this->object, $this->context );
24
		$this->object = new \Aimeos\MShop\Common\Manager\Decorator\Property( $this->object, $this->context );
25
		$this->object = new \Aimeos\MShop\Common\Manager\Decorator\Address( $this->object, $this->context );
26
		$this->object->setObject( $this->object );
27
	}
28
29
30
	protected function tearDown() : void
31
	{
32
		unset( $this->object, $this->context );
33
	}
34
35
36
	public function testAggregate()
37
	{
38
		$search = $this->object->filter();
39
		$result = $this->object->aggregate( $search, 'customer.salutation' );
40
41
		$this->assertEquals( 2, count( $result ) );
42
		$this->assertArrayHasKey( 'mr', $result );
43
		$this->assertEquals( 1, $result->get( 'mr' ) );
44
	}
45
46
47
	public function testAggregateMultiple()
48
	{
49
		$cols = ['customer.salutation', 'customer.title'];
50
		$search = $this->object->filter()->order( $cols );
51
		$result = $this->object->aggregate( $search, $cols );
52
53
		$this->assertEquals( ['mr' => ['Dr' => 1], '' => ['' => 2]], $result->toArray() );
54
	}
55
56
57
	public function testClear()
58
	{
59
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( array( -1 ) ) );
60
	}
61
62
63
	public function testGetSearchAttributes()
64
	{
65
		foreach( $this->object->getSearchAttributes() as $attribute )
66
		{
67
			$this->assertInstanceOf( '\\Aimeos\\Base\\Criteria\\Attribute\\Iface', $attribute );
68
		}
69
	}
70
71
72
	public function testCreateItem()
73
	{
74
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Customer\\Item\\Iface', $this->object->create() );
75
	}
76
77
78
	public function testGetItem()
79
	{
80
		$domains = ['text', 'customer/property' => ['newsletter']];
81
		$expected = $this->object->find( '[email protected]', $domains );
82
		$actual = $this->object->get( $expected->getId(), $domains );
83
84
		$this->assertEquals( $expected, $actual );
85
		$this->assertEquals( 1, count( $actual->getListItems( 'text' ) ) );
86
		$this->assertEquals( 1, count( $actual->getRefItems( 'text' ) ) );
87
		$this->assertEquals( 1, count( $actual->getPropertyItems() ) );
88
	}
89
90
91
	public function testSaveUpdateDeleteItem()
92
	{
93
		$item = $this->object->create();
94
95
		$item->setCode( '[email protected]' );
96
		$item->setLabel( 'unitTest' );
97
		$item = $this->object->save( $item );
98
		$itemSaved = $this->object->get( $item->getId() );
99
100
		$itemExp = clone $itemSaved;
101
		$itemExp->setCode( '[email protected]' );
102
		$itemExp->setLabel( 'unitTest2' );
103
		$itemExp = $this->object->save( $itemExp );
104
		$itemUpd = $this->object->get( $itemExp->getId() );
105
106
		$this->object->delete( $item->getId() );
107
108
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $item );
109
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $itemExp );
110
111
		$this->assertTrue( $item->getId() !== null );
112
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
113
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
114
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
115
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
116
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
117
		$this->assertEquals( $item->getPassword(), $itemSaved->getPassword() );
118
119
		$this->assertEquals( $this->context->editor(), $itemSaved->editor() );
120
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
121
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
122
123
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
124
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
125
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
126
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
127
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
128
		$this->assertEquals( $itemExp->getPassword(), $itemUpd->getPassword() );
129
130
		$this->assertEquals( $this->context->editor(), $itemUpd->editor() );
131
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
132
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
133
134
135
		$this->expectException( '\\Aimeos\\MShop\\Exception' );
136
		$this->object->get( $item->getId() );
137
	}
138
139
140
	public function testGetSaveAddressItems()
141
	{
142
		$item = $this->object->find( '[email protected]', ['customer/address'] );
143
144
		$item->setId( null )->setCode( '[email protected]' );
145
		$item->getPaymentAddress()->setEmail( '[email protected]' );
146
		$item->addAddressItem( new \Aimeos\MShop\Customer\Item\Address\Standard( 'customer.address.' ) );
147
		$this->object->save( $item );
148
149
		$item2 = $this->object->find( '[email protected]', ['customer/address'] );
150
151
		$this->object->delete( $item->getId() );
152
153
		$this->assertEquals( 2, count( $item->getAddressItems() ) );
154
		$this->assertEquals( 2, count( $item2->getAddressItems() ) );
155
	}
156
157
158
	public function testGetSavePropertyItems()
159
	{
160
		$item = $this->object->find( '[email protected]', ['customer/property'] );
161
162
		$item->setId( null )->setCode( '[email protected]' );
163
		$item->getPaymentAddress()->setEmail( '[email protected]' );
164
		$this->object->save( $item );
165
166
		$item2 = $this->object->find( '[email protected]', ['customer/property'] );
167
168
		$this->object->delete( $item->getId() );
169
170
		$this->assertEquals( 1, count( $item->getPropertyItems() ) );
171
		$this->assertEquals( 1, count( $item2->getPropertyItems() ) );
172
	}
173
174
175
	public function testCreateSearch()
176
	{
177
		$this->assertInstanceOf( '\\Aimeos\\Base\\Criteria\\Iface', $this->object->filter() );
178
	}
179
180
181
	public function testSearchItems()
182
	{
183
		$item = $this->object->find( '[email protected]', ['text'] );
184
		$listItem = $item->getListItems( 'text', 'default' )->first( new \RuntimeException( 'No list item found' ) );
185
186
		$search = $this->object->filter();
187
188
		$expr = [];
189
		$expr[] = $search->compare( '!=', 'customer.id', null );
190
		$expr[] = $search->compare( '==', 'customer.label', 'unitCustomer001' );
191
		$expr[] = $search->compare( '==', 'customer.code', '[email protected]' );
192
		$expr[] = $search->compare( '>=', 'customer.password', '' );
193
		$expr[] = $search->compare( '==', 'customer.status', 1 );
194
		$expr[] = $search->compare( '>', 'customer.mtime', '1970-01-01 00:00:00' );
195
		$expr[] = $search->compare( '>', 'customer.ctime', '1970-01-01 00:00:00' );
196
		$expr[] = $search->compare( '!=', 'customer.editor', '' );
197
198
		$expr[] = $search->compare( '==', 'customer.salutation', 'mr' );
199
		$expr[] = $search->compare( '==', 'customer.address.type', 'delivery' );
200
		$expr[] = $search->compare( '==', 'customer.company', 'Example company' );
201
		$expr[] = $search->compare( '==', 'customer.vatid', 'DE999999999' );
202
		$expr[] = $search->compare( '==', 'customer.title', 'Dr' );
203
		$expr[] = $search->compare( '==', 'customer.firstname', 'Our' );
204
		$expr[] = $search->compare( '==', 'customer.lastname', 'Unittest' );
205
		$expr[] = $search->compare( '==', 'customer.address1', 'Pickhuben' );
206
		$expr[] = $search->compare( '==', 'customer.address2', '2-4' );
207
		$expr[] = $search->compare( '==', 'customer.address3', '' );
208
		$expr[] = $search->compare( '==', 'customer.postal', '20457' );
209
		$expr[] = $search->compare( '==', 'customer.city', 'Hamburg' );
210
		$expr[] = $search->compare( '==', 'customer.state', 'Hamburg' );
211
		$expr[] = $search->compare( '==', 'customer.languageid', 'de' );
212
		$expr[] = $search->compare( '==', 'customer.countryid', 'DE' );
213
		$expr[] = $search->compare( '==', 'customer.telephone', '055544332211' );
214
		$expr[] = $search->compare( '==', 'customer.telefax', '055544332212' );
215
		$expr[] = $search->compare( '==', 'customer.mobile', '055544332213' );
216
		$expr[] = $search->compare( '==', 'customer.email', '[email protected]' );
217
		$expr[] = $search->compare( '==', 'customer.website', 'www.example.com' );
218
		$expr[] = $search->compare( '==', 'customer.longitude', '10.0' );
219
		$expr[] = $search->compare( '==', 'customer.latitude', '50.0' );
220
		$expr[] = $search->compare( '==', 'customer.birthday', '1999-01-01' );
221
222
		$param = ['text', 'default', $listItem->getRefId()];
223
		$expr[] = $search->compare( '!=', $search->make( 'customer:has', $param ), null );
224
225
		$param = ['text', 'default'];
226
		$expr[] = $search->compare( '!=', $search->make( 'customer:has', $param ), null );
227
228
		$param = ['text'];
229
		$expr[] = $search->compare( '!=', $search->make( 'customer:has', $param ), null );
230
231
		$param = ['newsletter', null, '1'];
232
		$expr[] = $search->compare( '!=', $search->make( 'customer:prop', $param ), null );
233
234
		$param = ['newsletter', null];
235
		$expr[] = $search->compare( '!=', $search->make( 'customer:prop', $param ), null );
236
237
		$param = ['newsletter'];
238
		$expr[] = $search->compare( '!=', $search->make( 'customer:prop', $param ), null );
239
240
		$expr[] = $search->compare( '!=', 'customer.address.id', null );
241
		$expr[] = $search->compare( '!=', 'customer.address.parentid', null );
242
		$expr[] = $search->compare( '==', 'customer.address.salutation', 'mr' );
243
		$expr[] = $search->compare( '==', 'customer.address.company', 'Example company' );
244
		$expr[] = $search->compare( '==', 'customer.address.vatid', 'DE999999999' );
245
		$expr[] = $search->compare( '==', 'customer.address.title', 'Dr' );
246
		$expr[] = $search->compare( '==', 'customer.address.firstname', 'Our' );
247
		$expr[] = $search->compare( '==', 'customer.address.lastname', 'Unittest' );
248
		$expr[] = $search->compare( '==', 'customer.address.address1', 'Pickhuben' );
249
		$expr[] = $search->compare( '==', 'customer.address.address2', '2-4' );
250
		$expr[] = $search->compare( '==', 'customer.address.address3', '' );
251
		$expr[] = $search->compare( '==', 'customer.address.postal', '20457' );
252
		$expr[] = $search->compare( '==', 'customer.address.city', 'Hamburg' );
253
		$expr[] = $search->compare( '==', 'customer.address.state', 'Hamburg' );
254
		$expr[] = $search->compare( '==', 'customer.address.languageid', 'de' );
255
		$expr[] = $search->compare( '==', 'customer.address.countryid', 'DE' );
256
		$expr[] = $search->compare( '==', 'customer.address.telephone', '055544332211' );
257
		$expr[] = $search->compare( '==', 'customer.address.telefax', '055544332212' );
258
		$expr[] = $search->compare( '==', 'customer.address.mobile', '055544332213' );
259
		$expr[] = $search->compare( '==', 'customer.address.email', '[email protected]' );
260
		$expr[] = $search->compare( '==', 'customer.address.website', 'www.example.com' );
261
		$expr[] = $search->compare( '==', 'customer.address.longitude', '10.0' );
262
		$expr[] = $search->compare( '==', 'customer.address.latitude', '50.0' );
263
		$expr[] = $search->compare( '==', 'customer.address.position', 0 );
264
		$expr[] = $search->compare( '==', 'customer.address.birthday', '2000-01-01' );
265
		$expr[] = $search->compare( '>=', 'customer.address.mtime', '1970-01-01 00:00:00' );
266
		$expr[] = $search->compare( '>=', 'customer.address.ctime', '1970-01-01 00:00:00' );
267
		$expr[] = $search->compare( '!=', 'customer.address.editor', '' );
268
269
		$search->setConditions( $search->and( $expr ) );
270
		$result = $this->object->search( $search );
271
		$this->assertEquals( 1, count( $result ) );
272
	}
273
274
275
	public function testSearchItemsTotal()
276
	{
277
		$total = 0;
278
		$search = $this->object->filter()->slice( 0, 2 );
279
280
		$results = $this->object->search( $search, [], $total );
281
282
		$this->assertEquals( 2, count( $results ) );
283
		$this->assertEquals( 3, $total );
284
285
		foreach( $results as $itemId => $item ) {
286
			$this->assertEquals( $itemId, $item->getId() );
287
		}
288
	}
289
290
291
	public function testSearchItemsCriteria()
292
	{
293
		$this->assertEquals( 2, count( $this->object->search( $this->object->filter( true ) ) ) );
294
	}
295
296
297
	public function testSearchItemsRef()
298
	{
299
		$search = $this->object->filter();
300
		$search->setConditions( $search->compare( '==', 'customer.code', '[email protected]' ) );
301
302
		if( ( $item = $this->object->search( $search, ['customer/address', 'text'] )->first() ) === null ) {
303
			throw new \Exception( 'No customer item for "[email protected]" available' );
304
		}
305
306
		$this->assertEquals( 1, count( $item->getRefItems( 'text' ) ) );
307
		$this->assertEquals( 1, count( $item->getAddressItems() ) );
308
	}
309
310
311
	public function testGetSubManager()
312
	{
313
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'address' ) );
314
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'address', 'Standard' ) );
315
316
		$this->expectException( \LogicException::class );
317
		$this->object->getSubManager( 'unknown' );
318
	}
319
320
321
	public function testGetSubManagerInvalidName()
322
	{
323
		$this->expectException( \LogicException::class );
324
		$this->object->getSubManager( 'address', 'unknown' );
325
	}
326
}
327