Completed
Push — master ( 65beba...94b58b )
by Aimeos
02:04
created

LaravelTest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 281
Duplicated Lines 6.41 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
lcom 1
cbo 6
dl 18
loc 281
rs 10
c 1
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
A tearDown() 0 4 1
A testCleanup() 0 4 1
A testGetSearchAttributes() 0 7 2
A testCreateItem() 0 4 1
A testGetItem() 0 20 2
A testSaveUpdateDeleteItem() 0 50 1
A testGetSavePropertyItems() 0 15 1
A testCreateSearch() 0 4 1
A testSearchItems() 0 70 1
A testSearchItemsTotal() 0 16 2
A testSearchItemsCriteria() 10 10 1
A testSearchItemsRef() 0 14 2
A testGetSubManager() 8 8 1
A testGetSubManagerInvalidName() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2017
6
 */
7
8
9
namespace Aimeos\MShop\Customer\Manager;
10
11
12
class LaravelTest 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
		$context = \TestHelper::getContext();
23
		$this->editor = $context->getEditor();
24
		$this->object = new \Aimeos\MShop\Customer\Manager\Laravel( $context );
25
26
		$this->fixture = array(
27
			'label' => 'unitTest',
28
			'status' => 2,
29
		);
30
31
		$this->address = new \Aimeos\MShop\Common\Item\Address\Standard( 'common.address.' );
32
	}
33
34
35
	protected function tearDown()
36
	{
37
		unset( $this->object, $this->fixture, $this->address );
38
	}
39
40
41
	public function testCleanup()
42
	{
43
		$this->object->cleanup( array( -1 ) );
44
	}
45
46
47
	public function testGetSearchAttributes()
48
	{
49
		foreach( $this->object->getSearchAttributes() as $attribute )
50
		{
51
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
52
		}
53
	}
54
55
56
	public function testCreateItem()
57
	{
58
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Customer\\Item\\Iface', $this->object->createItem() );
59
	}
60
61
62
	public function testGetItem()
63
	{
64
		$search = $this->object->createSearch();
65
		$conditions = array(
66
			$search->compare( '==', 'customer.code', 'UTC003' ),
67
			$search->compare( '==', 'customer.editor', $this->editor )
68
		);
69
		$search->setConditions( $search->combine( '&&', $conditions ) );
70
		$items = $this->object->searchItems( $search, array( 'text' ) );
71
72
		if( ( $expected = reset( $items ) ) === false ) {
73
			throw new \RuntimeException( 'No customer item with code "UTC003" found' );
74
		}
75
76
		$actual = $this->object->getItem( $expected->getId(), array( 'text' ) );
77
78
		$this->assertEquals( $expected, $actual );
79
		$this->assertEquals( 3, count( $actual->getListItems( 'text' ) ) );
80
		$this->assertEquals( 3, count( $actual->getRefItems( 'text' ) ) );
81
	}
82
83
84
	public function testSaveUpdateDeleteItem()
85
	{
86
		$item = $this->object->createItem();
87
88
		$item->setCode( 'unitTest' );
89
		$item->setLabel( 'unitTest' );
90
		$resultSaved = $this->object->saveItem( $item );
91
		$itemSaved = $this->object->getItem( $item->getId() );
92
93
		$itemExp = clone $itemSaved;
94
		$itemExp->setCode( 'unitTest2' );
95
		$itemExp->setLabel( 'unitTest2' );
96
		$resultUpd = $this->object->saveItem( $itemExp );
97
		$itemUpd = $this->object->getItem( $itemExp->getId() );
98
99
		$this->object->deleteItem( $item->getId() );
100
101
		$this->assertTrue( $item->getId() !== null );
102
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
103
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
104
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
105
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
106
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
107
		$this->assertEquals( $item->getPaymentAddress(), $itemSaved->getPaymentAddress() );
108
		$this->assertEquals( $item->getBirthday(), $itemSaved->getBirthday() );
109
		$this->assertEquals( $item->getPassword(), $itemSaved->getPassword() );
110
111
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
112
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
113
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
114
115
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
116
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
117
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
118
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
119
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
120
		$this->assertEquals( $itemExp->getPaymentAddress(), $itemUpd->getPaymentAddress() );
121
		$this->assertEquals( $itemExp->getBirthday(), $itemUpd->getBirthday() );
122
		$this->assertEquals( $itemExp->getPassword(), $itemUpd->getPassword() );
123
124
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
125
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
126
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
127
128
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultSaved );
129
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultUpd );
130
131
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
132
		$this->object->getItem( $item->getId() );
133
	}
134
135
136
	public function testGetSavePropertyItems()
137
	{
138
		$item = $this->object->findItem( 'UTC001', ['customer/property'] );
139
140
		$item->setId( null )->setCode( 'xyz' );
141
		$item->getPaymentAddress()->setEmail( '[email protected]' );
142
		$this->object->saveItem( $item );
143
144
		$item2 = $this->object->findItem( 'UTC001', ['customer/property'] );
145
146
		$this->object->deleteItem( $item->getId() );
147
148
		$this->assertEquals( 1, count( $item->getPropertyItems() ) );
149
		$this->assertEquals( 1, count( $item2->getPropertyItems() ) );
150
	}
151
152
153
	public function testCreateSearch()
154
	{
155
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $this->object->createSearch() );
156
	}
157
158
159
	public function testSearchItems()
160
	{
161
		$total = 0;
162
		$search = $this->object->createSearch();
163
164
		$expr = [];
165
		$expr[] = $search->compare( '!=', 'customer.id', null );
166
		$expr[] = $search->compare( '==', 'customer.label', 'unitCustomer2' );
167
		$expr[] = $search->compare( '==', 'customer.code', 'UTC002' );
168
169
		$expr[] = $search->compare( '>=', 'customer.salutation', '' );
170
		$expr[] = $search->compare( '>=', 'customer.company', '' );
171
		$expr[] = $search->compare( '>=', 'customer.vatid', '' );
172
		$expr[] = $search->compare( '>=', 'customer.title', '' );
173
		$expr[] = $search->compare( '>=', 'customer.firstname', '' );
174
		$expr[] = $search->compare( '>=', 'customer.lastname', '' );
175
		$expr[] = $search->compare( '>=', 'customer.address1', '' );
176
		$expr[] = $search->compare( '>=', 'customer.address2', '' );
177
		$expr[] = $search->compare( '>=', 'customer.address3', '' );
178
		$expr[] = $search->compare( '>=', 'customer.postal', '' );
179
		$expr[] = $search->compare( '>=', 'customer.city', '' );
180
		$expr[] = $search->compare( '>=', 'customer.state', '' );
181
		$expr[] = $search->compare( '!=', 'customer.languageid', null );
182
		$expr[] = $search->compare( '>=', 'customer.countryid', '' );
183
		$expr[] = $search->compare( '>=', 'customer.telephone', '' );
184
		$expr[] = $search->compare( '>=', 'customer.email', '' );
185
		$expr[] = $search->compare( '>=', 'customer.telefax', '' );
186
		$expr[] = $search->compare( '>=', 'customer.website', '' );
187
		$expr[] = $search->compare( '>=', 'customer.longitude', '10.0' );
188
		$expr[] = $search->compare( '>=', 'customer.latitude', '50.0' );
189
190
		$expr[] = $search->compare( '==', 'customer.birthday', '1970-01-01' );
191
		$expr[] = $search->compare( '>=', 'customer.password', '' );
192
		$expr[] = $search->compare( '==', 'customer.status', 0 );
193
		$expr[] = $search->compare( '!=', 'customer.mtime', '1970-01-01 00:00:00' );
194
		$expr[] = $search->compare( '!=', 'customer.ctime', '1970-01-01 00:00:00' );
195
		$expr[] = $search->compare( '==', 'customer.editor', $this->editor );
196
197
		$expr[] = $search->compare( '!=', 'customer.address.id', null );
198
		$expr[] = $search->compare( '!=', 'customer.address.parentid', null );
199
		$expr[] = $search->compare( '==', 'customer.address.company', 'ABC GmbH' );
200
		$expr[] = $search->compare( '==', 'customer.address.vatid', 'DE999999999' );
201
		$expr[] = $search->compare( '==', 'customer.address.salutation', 'mr' );
202
		$expr[] = $search->compare( '==', 'customer.address.title', 'Dr.' );
203
		$expr[] = $search->compare( '==', 'customer.address.firstname', 'Good' );
204
		$expr[] = $search->compare( '==', 'customer.address.lastname', 'Unittest' );
205
		$expr[] = $search->compare( '==', 'customer.address.address1', 'Pickhuben' );
206
		$expr[] = $search->compare( '==', 'customer.address.address2', '2-4' );
207
		$expr[] = $search->compare( '==', 'customer.address.address3', '' );
208
		$expr[] = $search->compare( '==', 'customer.address.postal', '11099' );
209
		$expr[] = $search->compare( '==', 'customer.address.city', 'Berlin' );
210
		$expr[] = $search->compare( '==', 'customer.address.state', 'Berlin' );
211
		$expr[] = $search->compare( '==', 'customer.address.languageid', 'de' );
212
		$expr[] = $search->compare( '==', 'customer.address.countryid', 'DE' );
213
		$expr[] = $search->compare( '==', 'customer.address.telephone', '055544332221' );
214
		$expr[] = $search->compare( '==', 'customer.address.email', '[email protected]' );
215
		$expr[] = $search->compare( '==', 'customer.address.telefax', '055544333212' );
216
		$expr[] = $search->compare( '==', 'customer.address.website', 'unittest.aimeos.org' );
217
		$expr[] = $search->compare( '>=', 'customer.address.longitude', '10.0' );
218
		$expr[] = $search->compare( '>=', 'customer.address.latitude', '50.0' );
219
		$expr[] = $search->compare( '==', 'customer.address.flag', 0 );
220
		$expr[] = $search->compare( '==', 'customer.address.position', 1 );
221
		$expr[] = $search->compare( '!=', 'customer.address.mtime', '1970-01-01 00:00:00' );
222
		$expr[] = $search->compare( '!=', 'customer.address.ctime', '1970-01-01 00:00:00' );
223
		$expr[] = $search->compare( '==', 'customer.address.editor', $this->editor );
224
225
		$search->setConditions( $search->combine( '&&', $expr ) );
226
		$result = $this->object->searchItems( $search, [], $total );
227
		$this->assertEquals( 1, count( $result ) );
228
	}
229
230
231
	public function testSearchItemsTotal()
232
	{
233
		$total = 0;
234
235
		$search = $this->object->createSearch();
236
		$search->setConditions( $search->compare( '==', 'customer.address.editor', $this->editor ) );
237
		$search->setSlice( 0, 2 );
238
239
		$results = $this->object->searchItems( $search, [], $total );
240
		$this->assertEquals( 2, count( $results ) );
241
		$this->assertEquals( 3, $total );
242
243
		foreach($results as $itemId => $item) {
244
			$this->assertEquals( $itemId, $item->getId() );
245
		}
246
	}
247
248
249 View Code Duplication
	public function testSearchItemsCriteria()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
	{
251
		$search = $this->object->createSearch( true );
252
		$conditions = array(
253
			$search->compare( '==', 'customer.address.editor', $this->editor ),
254
			$search->getConditions()
255
		);
256
		$search->setConditions( $search->combine( '&&', $conditions ) );
257
		$this->assertEquals( 2, count( $this->object->searchItems( $search, [], $total ) ) );
258
	}
259
260
261
	public function testSearchItemsRef()
262
	{
263
		$search = $this->object->createSearch();
264
		$search->setConditions( $search->compare( '==', 'customer.code', 'UTC001' ) );
265
266
		$results = $this->object->searchItems( $search, ['customer/address', 'text'] );
267
268
		if( ( $item = reset( $results ) ) === false ) {
269
			throw new \Exception( 'No customer item for "UTC001" available' );
270
		}
271
272
		$this->assertEquals( 1, count( $item->getRefItems( 'text' ) ) );
273
		$this->assertEquals( 1, count( $item->getAddressItems() ) );
274
	}
275
276
277 View Code Duplication
	public function testGetSubManager()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
278
	{
279
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'address' ) );
280
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'address', 'Standard' ) );
281
282
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
283
		$this->object->getSubManager( 'unknown' );
284
	}
285
286
287
	public function testGetSubManagerInvalidName()
288
	{
289
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
290
		$this->object->getSubManager( 'address', 'unknown' );
291
	}
292
}
293