Completed
Push — master ( 24a86a...771b97 )
by Aimeos
10:31
created

StandardTest   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 328
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 328
rs 10
c 1
b 0
f 0
wmc 23
lcom 1
cbo 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 1
A tearDown() 0 4 1
A testCleanup() 0 4 1
A testGetResourceType() 0 10 1
A testGetSearchAttributes() 0 7 2
A testCreateItem() 0 5 1
A testFindItem() 0 6 1
A testGetItem() 0 20 2
A testSaveInvalid() 0 5 1
B testSaveUpdateDeleteItem() 0 57 1
A testGetSaveAddressItems() 0 16 1
A testGetSavePropertyItems() 0 15 1
A testCreateSearch() 0 4 1
B testSearchItems() 0 65 1
A testSearchItemsTotal() 0 16 2
A testSearchItemsCriteria() 0 10 1
A testSearchItemsRef() 0 14 2
A testGetSubManager() 0 8 1
A testGetSubManagerInvalidName() 0 5 1
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-2017
7
 */
8
9
namespace Aimeos\MShop\Customer\Manager;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $fixture;
16
	private $address;
17
	private $editor = '';
18
19
20
	protected function setUp()
21
	{
22
		$this->editor = \TestHelperMShop::getContext()->getEditor();
23
		$this->object = new \Aimeos\MShop\Customer\Manager\Standard( \TestHelperMShop::getContext() );
24
25
		$this->fixture = array(
26
			'customer.label' => 'unitTest',
27
			'customer.status' => 2,
28
		);
29
30
		$this->address = new \Aimeos\MShop\Common\Item\Address\Standard( 'customer.' );
31
	}
32
33
34
	protected function tearDown()
35
	{
36
		unset( $this->object, $this->fixture, $this->address );
37
	}
38
39
40
	public function testCleanup()
41
	{
42
		$this->object->cleanup( array( -1 ) );
43
	}
44
45
46
	public function testGetResourceType()
47
	{
48
		$result = $this->object->getResourceType();
49
50
		$this->assertContains( 'customer', $result );
51
		$this->assertContains( 'customer/address', $result );
52
		$this->assertContains( 'customer/group', $result );
53
		$this->assertContains( 'customer/lists', $result );
54
		$this->assertContains( 'customer/lists/type', $result );
55
	}
56
57
58
	public function testGetSearchAttributes()
59
	{
60
		foreach( $this->object->getSearchAttributes() as $attribute )
61
		{
62
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
63
		}
64
	}
65
66
67
	public function testCreateItem()
68
	{
69
		$item = $this->object->createItem();
70
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Customer\\Item\\Iface', $item );
71
	}
72
73
74
	public function testFindItem()
75
	{
76
		$item = $this->object->findItem( 'UTC003' );
77
78
		$this->assertEquals( 'UTC003', $item->getCode() );
79
	}
80
81
82
	public function testGetItem()
83
	{
84
		$search = $this->object->createSearch();
85
		$conditions = array(
86
			$search->compare( '==', 'customer.code', 'UTC003' ),
87
			$search->compare( '==', 'customer.editor', $this->editor )
88
		);
89
		$search->setConditions( $search->combine( '&&', $conditions ) );
90
		$items = $this->object->searchItems( $search, array( 'text' ) );
91
92
		if( ( $expected = reset( $items ) ) === false ) {
93
			throw new \RuntimeException( 'No customer item with code "UTC003" found' );
94
		}
95
96
		$actual = $this->object->getItem( $expected->getId(), array( 'text' ) );
97
98
		$this->assertEquals( $expected, $actual );
99
		$this->assertEquals( 3, count( $actual->getListItems( 'text', null, null, false ) ) );
100
		$this->assertEquals( 3, count( $actual->getRefItems( 'text', null, null, false ) ) );
101
	}
102
103
104
	public function testSaveInvalid()
105
	{
106
		$this->setExpectedException( '\Aimeos\MShop\Customer\Exception' );
107
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
108
	}
109
110
111
	public function testSaveUpdateDeleteItem()
112
	{
113
		$item = $this->object->createItem();
114
115
		$item->setCode( 'unitTest' );
116
		$item->setLabel( 'unitTest' );
117
		$item->setGroups( array( 1, 2, 3 ) );
118
		$resultSaved = $this->object->saveItem( $item );
119
		$itemSaved = $this->object->getItem( $item->getId(), array( 'customer/group' ) );
120
121
		$itemExp = clone $itemSaved;
122
		$itemExp->setCode( 'unitTest2' );
123
		$itemExp->setLabel( 'unitTest2' );
124
		$itemExp->setGroups( array( 2, 4 ) );
125
		$resultUpd = $this->object->saveItem( $itemExp );
126
		$itemUpd = $this->object->getItem( $itemExp->getId(), array( 'customer/group' ) );
127
128
		$this->object->deleteItem( $item->getId() );
129
130
131
		$this->assertTrue( $item->getId() !== null );
132
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
133
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
134
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
135
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
136
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
137
		$this->assertEquals( $item->getPaymentAddress(), $itemSaved->getPaymentAddress() );
138
		$this->assertEquals( $item->getBirthday(), $itemSaved->getBirthday() );
139
		$this->assertEquals( $item->getPassword(), $itemSaved->getPassword() );
140
		$this->assertEquals( $item->getGroups(), $itemSaved->getGroups() );
141
		$this->assertEquals( $itemSaved->getPaymentAddress()->getId(), $itemSaved->getId() );
142
143
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
144
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
145
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
146
147
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
148
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
149
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
150
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
151
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
152
		$this->assertEquals( $itemExp->getPaymentAddress(), $itemUpd->getPaymentAddress() );
153
		$this->assertEquals( $itemExp->getBirthday(), $itemUpd->getBirthday() );
154
		$this->assertEquals( $itemExp->getPassword(), $itemUpd->getPassword() );
155
		$this->assertEquals( $itemExp->getGroups(), $itemUpd->getGroups() );
156
		$this->assertEquals( $itemUpd->getPaymentAddress()->getId(), $itemUpd->getId() );
157
158
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
159
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
160
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
161
162
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultSaved );
163
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultUpd );
164
165
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
166
		$this->object->getItem( $item->getId() );
167
	}
168
169
170
	public function testGetSaveAddressItems()
171
	{
172
		$item = $this->object->findItem( 'UTC001', ['customer/address'] );
173
174
		$item->setId( null )->setCode( 'xyz' );
175
		$item->getPaymentAddress()->setEmail( '[email protected]' );
176
		$item->addAddressItem( new \Aimeos\MShop\Common\Item\Address\Standard( 'customer.address.' ) );
177
		$this->object->saveItem( $item );
178
179
		$item2 = $this->object->findItem( 'xyz', ['customer/address'] );
180
181
		$this->object->deleteItem( $item->getId() );
182
183
		$this->assertEquals( 2, count( $item->getAddressItems() ) );
184
		$this->assertEquals( 2, count( $item2->getAddressItems() ) );
185
	}
186
187
188
	public function testGetSavePropertyItems()
189
	{
190
		$item = $this->object->findItem( 'UTC001', ['customer/property'] );
191
192
		$item->setId( null )->setCode( 'xyz' );
193
		$item->getPaymentAddress()->setEmail( '[email protected]' );
194
		$this->object->saveItem( $item );
195
196
		$item2 = $this->object->findItem( 'xyz', ['customer/property'] );
197
198
		$this->object->deleteItem( $item->getId() );
199
200
		$this->assertEquals( 1, count( $item->getPropertyItems() ) );
201
		$this->assertEquals( 1, count( $item2->getPropertyItems() ) );
202
	}
203
204
205
	public function testCreateSearch()
206
	{
207
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $this->object->createSearch() );
208
	}
209
210
211
	public function testSearchItems()
212
	{
213
		$search = $this->object->createSearch();
214
215
		$expr = [];
216
		$expr[] = $search->compare( '!=', 'customer.id', null );
217
		$expr[] = $search->compare( '==', 'customer.label', 'unitCustomer002' );
218
		$expr[] = $search->compare( '==', 'customer.code', 'UTC002' );
219
220
		$expr[] = $search->compare( '>=', 'customer.salutation', '' );
221
		$expr[] = $search->compare( '>=', 'customer.company', '' );
222
		$expr[] = $search->compare( '>=', 'customer.vatid', '' );
223
		$expr[] = $search->compare( '>=', 'customer.title', '' );
224
		$expr[] = $search->compare( '>=', 'customer.firstname', '' );
225
		$expr[] = $search->compare( '>=', 'customer.lastname', '' );
226
		$expr[] = $search->compare( '>=', 'customer.address1', '' );
227
		$expr[] = $search->compare( '>=', 'customer.address2', '' );
228
		$expr[] = $search->compare( '>=', 'customer.address3', '' );
229
		$expr[] = $search->compare( '>=', 'customer.postal', '' );
230
		$expr[] = $search->compare( '>=', 'customer.city', '' );
231
		$expr[] = $search->compare( '>=', 'customer.state', '' );
232
		$expr[] = $search->compare( '!=', 'customer.languageid', null );
233
		$expr[] = $search->compare( '==', 'customer.countryid', null );
234
		$expr[] = $search->compare( '>=', 'customer.telephone', '' );
235
		$expr[] = $search->compare( '>=', 'customer.email', '' );
236
		$expr[] = $search->compare( '>=', 'customer.telefax', '' );
237
		$expr[] = $search->compare( '>=', 'customer.website', '' );
238
239
		$expr[] = $search->compare( '==', 'customer.birthday', null );
240
		$expr[] = $search->compare( '>=', 'customer.password', '' );
241
		$expr[] = $search->compare( '==', 'customer.status', 1 );
242
		$expr[] = $search->compare( '>', 'customer.mtime', '1970-01-01 00:00:00' );
243
		$expr[] = $search->compare( '>', 'customer.ctime', '1970-01-01 00:00:00' );
244
		$expr[] = $search->compare( '==', 'customer.editor', $this->editor );
245
246
		$expr[] = $search->compare( '!=', 'customer.address.id', null );
247
		$expr[] = $search->compare( '!=', 'customer.address.parentid', null );
248
		$expr[] = $search->compare( '==', 'customer.address.company', 'Example company LLC' );
249
		$expr[] = $search->compare( '==', 'customer.address.vatid', 'DE999999999' );
250
		$expr[] = $search->compare( '==', 'customer.address.salutation', 'mr' );
251
		$expr[] = $search->compare( '==', 'customer.address.title', 'Dr.' );
252
		$expr[] = $search->compare( '==', 'customer.address.firstname', 'Good' );
253
		$expr[] = $search->compare( '==', 'customer.address.lastname', 'Unittest' );
254
		$expr[] = $search->compare( '==', 'customer.address.address1', 'Pickhuben' );
255
		$expr[] = $search->compare( '==', 'customer.address.address2', '2-4' );
256
		$expr[] = $search->compare( '==', 'customer.address.address3', '' );
257
		$expr[] = $search->compare( '==', 'customer.address.postal', '11099' );
258
		$expr[] = $search->compare( '==', 'customer.address.city', 'Berlin' );
259
		$expr[] = $search->compare( '==', 'customer.address.state', 'Berlin' );
260
		$expr[] = $search->compare( '==', 'customer.address.languageid', 'de' );
261
		$expr[] = $search->compare( '==', 'customer.address.countryid', 'DE' );
262
		$expr[] = $search->compare( '==', 'customer.address.telephone', '055544332221' );
263
		$expr[] = $search->compare( '==', 'customer.address.email', '[email protected]' );
264
		$expr[] = $search->compare( '==', 'customer.address.telefax', '055544333212' );
265
		$expr[] = $search->compare( '==', 'customer.address.website', 'www.example.com' );
266
		$expr[] = $search->compare( '==', 'customer.address.flag', 0 );
267
		$expr[] = $search->compare( '==', 'customer.address.position', 1 );
268
		$expr[] = $search->compare( '>', 'customer.address.mtime', '1970-01-01 00:00:00' );
269
		$expr[] = $search->compare( '>', 'customer.address.ctime', '1970-01-01 00:00:00' );
270
		$expr[] = $search->compare( '==', 'customer.address.editor', $this->editor );
271
272
		$search->setConditions( $search->combine( '&&', $expr ) );
273
		$result = $this->object->searchItems( $search );
274
		$this->assertEquals( 1, count( $result ) );
275
	}
276
277
278
	public function testSearchItemsTotal()
279
	{
280
		$search = $this->object->createSearch();
281
		$search->setConditions( $search->compare( '==', 'customer.address.editor', $this->editor ) );
282
		$search->setSlice( 0, 2 );
283
284
		$total = 0;
285
		$results = $this->object->searchItems( $search, [], $total );
286
287
		$this->assertEquals( 2, count( $results ) );
288
		$this->assertEquals( 3, $total );
289
290
		foreach( $results as $itemId => $item ) {
291
			$this->assertEquals( $itemId, $item->getId() );
292
		}
293
	}
294
295
296
	public function testSearchItemsCriteria()
297
	{
298
		$search = $this->object->createSearch( true );
299
		$conditions = array(
300
			$search->compare( '==', 'customer.address.editor', $this->editor ),
301
			$search->getConditions()
302
		);
303
		$search->setConditions( $search->combine( '&&', $conditions ) );
304
		$this->assertEquals( 2, count( $this->object->searchItems( $search ) ) );
305
	}
306
307
308
	public function testSearchItemsRef()
309
	{
310
		$search = $this->object->createSearch();
311
		$search->setConditions( $search->compare( '==', 'customer.code', 'UTC001' ) );
312
313
		$results = $this->object->searchItems( $search, ['customer/address', 'text'] );
314
315
		if( ( $item = reset( $results ) ) === false ) {
316
			throw new \Exception( 'No customer item for "UTC001" available' );
317
		}
318
319
		$this->assertEquals( 1, count( $item->getRefItems( 'text', null, null, false ) ) );
320
		$this->assertEquals( 1, count( $item->getAddressItems() ) );
321
	}
322
323
324
	public function testGetSubManager()
325
	{
326
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'address' ) );
327
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'address', 'Standard' ) );
328
329
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
330
		$this->object->getSubManager( 'unknown' );
331
	}
332
333
334
	public function testGetSubManagerInvalidName()
335
	{
336
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
337
		$this->object->getSubManager( 'address', 'unknown' );
338
	}
339
}
340