Passed
Push — master ( eade66...a4d1b5 )
by Aimeos
03:55
created

FosUserTest::testSaveUpdateDeleteItem()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 81
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 70
c 1
b 0
f 0
dl 0
loc 81
rs 8.6545
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2020
6
 */
7
8
9
namespace Aimeos\MShop\Customer\Manager\Address;
10
11
12
class FosUserTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $fixture = null;
15
	private $object = null;
16
	private $editor = '';
17
18
19
	protected function setUp() : void
20
	{
21
		$context = \TestHelper::getContext();
22
		$this->editor = $context->getEditor();
23
		$customer = new \Aimeos\MShop\Customer\Manager\FosUser( $context );
24
25
		$search = $customer->createSearch();
26
		$conditions = array(
27
			$search->compare( '==', 'customer.code', '[email protected]' ),
28
			$search->compare( '==', 'customer.editor', $this->editor )
29
		);
30
		$search->setConditions( $search->combine( '&&', $conditions ) );
31
32
		if( ( $customerItem = $customer->searchItems( $search )->first() ) === null ) {
33
			throw new \RuntimeException( sprintf( 'No customer item found for code "%1$s"', '[email protected]' ) );
34
		}
35
36
		$this->fixture = array(
37
			'customer.address.parentid' => $customerItem->getId(),
38
			'customer.address.company' => 'ABC GmbH',
39
			'customer.address.vatid' => 'DE999999999',
40
			'customer.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR,
41
			'customer.address.title' => 'Herr',
42
			'customer.address.firstname' => 'firstunit',
43
			'customer.address.lastname' => 'lastunit',
44
			'customer.address.address1' => 'unit str.',
45
			'customer.address.address2' => ' 166',
46
			'customer.address.address3' => '4.OG',
47
			'customer.address.postal' => '22769',
48
			'customer.address.city' => 'Hamburg',
49
			'customer.address.state' => 'Hamburg',
50
			'customer.address.countryid' => 'de',
51
			'customer.address.languageid' => 'de',
52
			'customer.address.telephone' => '05554433221',
53
			'customer.address.email' => '[email protected]',
54
			'customer.address.telefax' => '05554433222',
55
			'customer.address.website' => 'unittest.aimeos.org',
56
			'customer.address.longitude' => '10.0',
57
			'customer.address.latitude' => '50.0',
58
			'customer.address.position' => 1,
59
			'customer.address.birthday' => '2000-01-01',
60
			'customer.address.siteid' => $context->getLocale()->getSiteId(),
61
		);
62
63
		$this->object = $customer->getSubManager( 'address', 'FosUser' );
64
	}
65
66
67
	protected function tearDown() : void
68
	{
69
		unset( $this->object, $this->fixture );
70
	}
71
72
73
	public function testClear()
74
	{
75
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( array( -1 ) ) );
76
	}
77
78
	public function testGetSearchAttributes()
79
	{
80
		foreach( $this->object->getSearchAttributes() as $attribute ) {
81
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
82
		}
83
	}
84
85
	public function testGetSubManager()
86
	{
87
		$this->expectException( '\\Aimeos\\MShop\\Exception' );
88
		$this->object->getSubManager( 'unknown' );
89
	}
90
91
92
	public function testCreateItem()
93
	{
94
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Address\\Iface', $this->object->createItem() );
95
	}
96
97
98
	public function testGetItem()
99
	{
100
		$search = $this->object->createSearch()->setSlice( 0, 1 );
101
		$search->setConditions( $search->compare( '~=', 'customer.address.company', 'Example company' ) );
102
103
		if( ( $item = $this->object->searchItems( $search )->first() ) === null ) {
104
			throw new \RuntimeException( 'No address item found' );
105
		}
106
107
		$this->assertEquals( $item, $this->object->getItem( $item->getId() ) );
108
	}
109
110
111
	public function testSaveUpdateDeleteItem()
112
	{
113
		$item = new \Aimeos\MShop\Common\Item\Address\Standard( 'customer.address.', $this->fixture );
114
		$item->setId( null );
115
		$resultSaved = $this->object->saveItem( $item );
116
		$itemSaved = $this->object->getItem( $item->getId() );
117
118
		$itemExp = clone $itemSaved;
119
		$itemExp->setPosition( 1 );
120
		$itemExp->setCity( 'Berlin' );
121
		$itemExp->setState( 'Berlin' );
122
		$resultUpd = $this->object->saveItem( $itemExp );
123
		$itemUpd = $this->object->getItem( $itemExp->getId() );
124
125
		$this->object->deleteItem( $itemSaved->getId() );
126
127
128
		$this->assertTrue( $item->getId() !== null );
129
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
130
		$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
131
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
132
		$this->assertEquals( $item->getCompany(), $itemSaved->getCompany() );
133
		$this->assertEquals( $item->getVatID(), $itemSaved->getVatID() );
134
		$this->assertEquals( $item->getSalutation(), $itemSaved->getSalutation() );
135
		$this->assertEquals( $item->getTitle(), $itemSaved->getTitle() );
136
		$this->assertEquals( $item->getFirstname(), $itemSaved->getFirstname() );
137
		$this->assertEquals( $item->getLastname(), $itemSaved->getLastname() );
138
		$this->assertEquals( $item->getAddress1(), $itemSaved->getAddress1() );
139
		$this->assertEquals( $item->getAddress2(), $itemSaved->getAddress2() );
140
		$this->assertEquals( $item->getAddress3(), $itemSaved->getAddress3() );
141
		$this->assertEquals( $item->getPostal(), $itemSaved->getPostal() );
142
		$this->assertEquals( $item->getCity(), $itemSaved->getCity() );
143
		$this->assertEquals( $item->getState(), $itemSaved->getState() );
144
		$this->assertEquals( $item->getCountryId(), $itemSaved->getCountryId() );
145
		$this->assertEquals( $item->getLanguageId(), $itemSaved->getLanguageId() );
146
		$this->assertEquals( $item->getTelephone(), $itemSaved->getTelephone() );
147
		$this->assertEquals( $item->getEmail(), $itemSaved->getEmail() );
148
		$this->assertEquals( $item->getTelefax(), $itemSaved->getTelefax() );
149
		$this->assertEquals( $item->getWebsite(), $itemSaved->getWebsite() );
150
		$this->assertEquals( $item->getLongitude(), $itemSaved->getLongitude() );
151
		$this->assertEquals( $item->getLatitude(), $itemSaved->getLatitude() );
152
		$this->assertEquals( $item->getBirthday(), $itemSaved->getBirthday() );
153
154
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
155
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
156
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
157
158
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
159
		$this->assertEquals( $itemExp->getParentId(), $itemUpd->getParentId() );
160
		$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() );
161
		$this->assertEquals( $itemExp->getCompany(), $itemUpd->getCompany() );
162
		$this->assertEquals( $itemExp->getVatID(), $itemUpd->getVatID() );
163
		$this->assertEquals( $itemExp->getSalutation(), $itemUpd->getSalutation() );
164
		$this->assertEquals( $itemExp->getTitle(), $itemUpd->getTitle() );
165
		$this->assertEquals( $itemExp->getFirstname(), $itemUpd->getFirstname() );
166
		$this->assertEquals( $itemExp->getLastname(), $itemUpd->getLastname() );
167
		$this->assertEquals( $itemExp->getAddress1(), $itemUpd->getAddress1() );
168
		$this->assertEquals( $itemExp->getAddress2(), $itemUpd->getAddress2() );
169
		$this->assertEquals( $itemExp->getAddress3(), $itemUpd->getAddress3() );
170
		$this->assertEquals( $itemExp->getPostal(), $itemUpd->getPostal() );
171
		$this->assertEquals( $itemExp->getCity(), $itemUpd->getCity() );
172
		$this->assertEquals( $itemExp->getState(), $itemUpd->getState() );
173
		$this->assertEquals( $itemExp->getCountryId(), $itemUpd->getCountryId() );
174
		$this->assertEquals( $itemExp->getLanguageId(), $itemUpd->getLanguageId() );
175
		$this->assertEquals( $itemExp->getTelephone(), $itemUpd->getTelephone() );
176
		$this->assertEquals( $itemExp->getEmail(), $itemUpd->getEmail() );
177
		$this->assertEquals( $itemExp->getTelefax(), $itemUpd->getTelefax() );
178
		$this->assertEquals( $itemExp->getWebsite(), $itemUpd->getWebsite() );
179
		$this->assertEquals( $itemExp->getLongitude(), $itemUpd->getLongitude() );
180
		$this->assertEquals( $itemExp->getLatitude(), $itemUpd->getLatitude() );
181
		$this->assertEquals( $itemExp->getBirthday(), $itemUpd->getBirthday() );
182
183
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
184
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
185
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
186
187
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
188
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
189
190
		$this->expectException( '\\Aimeos\\MShop\\Exception' );
191
		$this->object->getItem( $itemSaved->getId() );
192
	}
193
194
195
	public function testCreateSearch()
196
	{
197
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $this->object->createSearch() );
198
	}
199
200
201
	public function testSearchItem()
202
	{
203
		$search = $this->object->createSearch();
204
205
		$conditions = array(
206
			$search->compare( '!=', 'customer.address.id', null ),
207
			$search->compare( '!=', 'customer.address.parentid', null ),
208
			$search->compare( '==', 'customer.address.company', 'Example company' ),
209
			$search->compare( '==', 'customer.address.vatid', 'DE999999999' ),
210
			$search->compare( '==', 'customer.address.salutation', 'mr' ),
211
			$search->compare( '==', 'customer.address.title', 'Dr' ),
212
			$search->compare( '==', 'customer.address.firstname', 'Our' ),
213
			$search->compare( '==', 'customer.address.lastname', 'Unittest' ),
214
			$search->compare( '==', 'customer.address.address1', 'Pickhuben' ),
215
			$search->compare( '==', 'customer.address.address2', '2-4' ),
216
			$search->compare( '==', 'customer.address.address3', '' ),
217
			$search->compare( '==', 'customer.address.postal', '20457' ),
218
			$search->compare( '==', 'customer.address.city', 'Hamburg' ),
219
			$search->compare( '==', 'customer.address.state', 'Hamburg' ),
220
			$search->compare( '==', 'customer.address.countryid', 'DE' ),
221
			$search->compare( '==', 'customer.address.languageid', 'de' ),
222
			$search->compare( '==', 'customer.address.telephone', '055544332211' ),
223
			$search->compare( '==', 'customer.address.email', '[email protected]' ),
224
			$search->compare( '==', 'customer.address.telefax', '055544332212' ),
225
			$search->compare( '==', 'customer.address.website', 'www.example.com' ),
226
			$search->compare( '==', 'customer.address.longitude', '10.0' ),
227
			$search->compare( '==', 'customer.address.latitude', '50.0' ),
228
			$search->compare( '==', 'customer.address.birthday', '2000-01-01' ),
229
			$search->compare( '>=', 'customer.address.mtime', '1970-01-01 00:00:00' ),
230
			$search->compare( '>=', 'customer.address.ctime', '1970-01-01 00:00:00' ),
231
			$search->compare( '==', 'customer.address.editor', $this->editor ),
232
		);
233
		$search->setConditions( $search->combine( '&&', $conditions ) );
234
		$this->assertEquals( 1, count( $this->object->searchItems( $search ) ) );
235
	}
236
237
238
	public function testSearchItemTotal()
239
	{
240
		$total = 0;
241
		$search = $this->object->createSearch();
242
243
		$conditions = array(
244
			$search->compare( '~=', 'customer.address.company', 'Example company' ),
245
			$search->compare( '==', 'customer.address.editor', $this->editor )
246
		);
247
248
		$search->setConditions( $search->combine( '&&', $conditions ) );
249
		$search->setSlice( 0, 2 );
250
251
		$results = $this->object->searchItems( $search, [], $total );
252
253
		$this->assertEquals( 2, count( $results ) );
254
		$this->assertEquals( 3, $total );
255
256
		foreach( $results as $id => $item ) {
257
			$this->assertEquals( $id, $item->getId() );
258
		}
259
	}
260
}
261