Passed
Push — master ( 0dde8b...fa860a )
by Aimeos
04:21
created

StandardTest::testSearchItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 60
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 48
nc 2
nop 0
dl 0
loc 60
rs 9.1344
c 0
b 0
f 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 Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 */
8
9
10
namespace Aimeos\MShop\Supplier\Manager;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object = null;
16
	private $editor = '';
17
18
19
	protected function setUp()
20
	{
21
		$this->editor = \TestHelperMShop::getContext()->getEditor();
22
		$this->object = new \Aimeos\MShop\Supplier\Manager\Standard( \TestHelperMShop::getContext() );
23
	}
24
25
26
	protected function tearDown()
27
	{
28
		unset( $this->object );
29
	}
30
31
32
	public function testCleanup()
33
	{
34
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->cleanup( [-1] ) );
35
	}
36
37
38
	public function testDeleteItems()
39
	{
40
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->deleteItems( [-1] ) );
41
	}
42
43
44
	public function testGetResourceType()
45
	{
46
		$result = $this->object->getResourceType();
47
48
		$this->assertContains( 'supplier', $result );
49
		$this->assertContains( 'supplier/address', $result );
50
		$this->assertContains( 'supplier/lists', $result );
51
	}
52
53
54
	public function testGetSearchAttributes()
55
	{
56
		foreach( $this->object->getSearchAttributes() as $attribute )
57
		{
58
			$this->assertInstanceOf( \Aimeos\MW\Criteria\Attribute\Iface::class, $attribute );
59
		}
60
	}
61
62
	public function testCreateItem()
63
	{
64
		$item = $this->object->createItem();
65
		$this->assertInstanceOf( \Aimeos\MShop\Supplier\Item\Iface::class, $item );
66
	}
67
68
69
	public function testFindItem()
70
	{
71
		$item = $this->object->findItem( 'unitCode001' );
72
73
		$this->assertEquals( 'unitCode001', $item->getCode() );
74
	}
75
76
77
	public function testGetItem()
78
	{
79
		$search = $this->object->createSearch()->setSlice( 0, 1 );
80
		$conditions = array(
81
			$search->compare( '~=', 'supplier.label', 'unitSupplier' ),
82
			$search->compare( '==', 'supplier.editor', $this->editor ),
83
		);
84
		$search->setConditions( $search->combine( '&&', $conditions ) );
85
		$items = $this->object->searchItems( $search );
86
87
		if( ( $item = reset( $items ) ) === false ) {
88
			throw new \RuntimeException( 'No supplier item with label "unitSupplier" found' );
89
		}
90
91
		$this->assertEquals( $item, $this->object->getItem( $item->getId() ) );
92
	}
93
94
95
	public function testSaveInvalid()
96
	{
97
		$this->setExpectedException( \Aimeos\MW\Common\Exception::class );
98
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
99
	}
100
101
102
	public function testSaveUpdateDeleteItem()
103
	{
104
		$search = $this->object->createSearch();
105
		$conditions = array(
106
			$search->compare( '==', 'supplier.label', 'unitSupplier001' ),
107
			$search->compare( '==', 'supplier.editor', $this->editor ),
108
		);
109
		$search->setConditions( $search->combine( '&&', $conditions ) );
110
		$items = $this->object->searchItems( $search );
111
112
		if( ( $item = reset( $items ) ) === false ) {
113
			throw new \RuntimeException( 'No supplier item found' );
114
		}
115
116
		$item->setId( null );
117
		$item->setCode( 'unitTest01' );
118
		$resultSaved = $this->object->saveItem( $item );
119
		$itemSaved = $this->object->getItem( $item->getId() );
120
121
		$itemExp = clone $itemSaved;
122
		$itemExp->setLabel( 'unitTest' );
123
		$itemExp->setStatus( 2 );
124
		$resultUpd = $this->object->saveItem( $itemExp );
125
		$itemUpd = $this->object->getItem( $itemExp->getId() );
126
127
		$this->object->deleteItem( $itemSaved->getId() );
128
129
130
		$this->assertTrue( $item->getId() !== null );
131
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
132
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
133
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
134
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
135
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
136
137
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
138
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
139
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
140
141
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
142
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
143
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
144
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
145
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
146
147
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
148
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
149
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
150
151
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
152
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
153
154
		$this->setExpectedException( \Aimeos\MShop\Exception::class );
155
		$this->object->getItem( $itemSaved->getId() );
156
	}
157
158
	public function testCreateSearch()
159
	{
160
		$this->assertInstanceOf( \Aimeos\MW\Criteria\Iface::class, $this->object->createSearch() );
161
	}
162
163
164
	public function testSearchItem()
165
	{
166
		$item = $this->object->findItem( 'unitCode001', ['text'] );
167
168
		if( ( $listItem = current( $item->getListItems( 'text', 'default' ) ) ) === false ) {
169
			throw new \RuntimeException( 'No list item found' );
170
		}
171
172
		$search = $this->object->createSearch();
173
174
		$expr = [];
175
		$expr[] = $search->compare( '!=', 'supplier.id', null );
176
		$expr[] = $search->compare( '!=', 'supplier.siteid', null );
177
		$expr[] = $search->compare( '==', 'supplier.label', 'unitSupplier001' );
178
		$expr[] = $search->compare( '==', 'supplier.code', 'unitCode001' );
179
		$expr[] = $search->compare( '==', 'supplier.status', 1 );
180
		$expr[] = $search->compare( '>=', 'supplier.mtime', '1970-01-01 00:00:00' );
181
		$expr[] = $search->compare( '>=', 'supplier.ctime', '1970-01-01 00:00:00' );
182
		$expr[] = $search->compare( '==', 'supplier.editor', $this->editor );
183
184
		$param = ['text','default', '0'];
185
		$expr[] = $search->compare( '==', $search->createFunction( 'supplier:has', $param ), null );
186
187
		$param = ['text','default', $listItem->getRefId()];
188
		$expr[] = $search->compare( '!=', $search->createFunction( 'supplier:has', $param ), null );
189
190
		$param = ['text','default'];
191
		$expr[] = $search->compare( '!=', $search->createFunction( 'supplier:has', $param ), null );
192
193
		$param = ['text'];
194
		$expr[] = $search->compare( '!=', $search->createFunction( 'supplier:has', $param ), null );
195
196
		$expr[] = $search->compare( '!=', 'supplier.address.id', null );
197
		$expr[] = $search->compare( '!=', 'supplier.address.siteid', null );
198
		$expr[] = $search->compare( '!=', 'supplier.address.parentid', 0 );
199
		$expr[] = $search->compare( '==', 'supplier.address.company', 'Example company' );
200
		$expr[] = $search->compare( '==', 'supplier.address.vatid', 'DE999999999' );
201
		$expr[] = $search->compare( '==', 'supplier.address.salutation', 'mrs' );
202
		$expr[] = $search->compare( '==', 'supplier.address.title', '' );
203
		$expr[] = $search->compare( '==', 'supplier.address.firstname', 'Our' );
204
		$expr[] = $search->compare( '==', 'supplier.address.lastname', 'Unittest' );
205
		$expr[] = $search->compare( '==', 'supplier.address.address1', 'Pickhuben' );
206
		$expr[] = $search->compare( '==', 'supplier.address.address2', '2' );
207
		$expr[] = $search->compare( '==', 'supplier.address.address3', '' );
208
		$expr[] = $search->compare( '==', 'supplier.address.postal', '20457' );
209
		$expr[] = $search->compare( '==', 'supplier.address.city', 'Hamburg' );
210
		$expr[] = $search->compare( '==', 'supplier.address.languageid', 'de' );
211
		$expr[] = $search->compare( '==', 'supplier.address.countryid', 'DE' );
212
		$expr[] = $search->compare( '==', 'supplier.address.telephone', '055544332211' );
213
		$expr[] = $search->compare( '==', 'supplier.address.telefax', '055544332212' );
214
		$expr[] = $search->compare( '==', 'supplier.address.email', '[email protected]' );
215
		$expr[] = $search->compare( '==', 'supplier.address.website', 'www.example.com' );
216
		$expr[] = $search->compare( '==', 'supplier.address.position', 0 );
217
		$expr[] = $search->compare( '>=', 'supplier.address.mtime', '1970-01-01 00:00:00' );
218
		$expr[] = $search->compare( '>=', 'supplier.address.ctime', '1970-01-01 00:00:00' );
219
		$expr[] = $search->compare( '==', 'supplier.address.editor', $this->editor );
220
221
		$search->setConditions( $search->combine( '&&', $expr ) );
222
		$result = $this->object->searchItems( $search );
223
		$this->assertEquals( 1, count( $result ) );
224
	}
225
226
227
	public function testSearchItemTotal()
228
	{
229
		$search = $this->object->createSearch();
230
		$search->setConditions( $search->compare( '==', 'supplier.editor', $this->editor ) );
231
		$search->setSlice( 0, 2 );
232
233
		$total = 0;
234
		$results = $this->object->searchItems( $search, [], $total );
235
236
		$this->assertEquals( 2, count( $results ) );
237
		$this->assertEquals( 3, $total );
238
	}
239
240
241
	public function testSearchItemCriteria()
242
	{
243
		$search = $this->object->createSearch( true );
244
		$conditions = array(
245
			$search->compare( '==', 'supplier.editor', $this->editor ),
246
			$search->getConditions()
247
		);
248
		$search->setConditions( $search->combine( '&&', $conditions ) );
249
		$results = $this->object->searchItems( $search );
250
251
		$this->assertEquals( 2, count( $results ) );
252
253
		foreach( $results as $itemId => $item ) {
254
			$this->assertEquals( $itemId, $item->getId() );
255
		}
256
	}
257
258
259
	public function testSearchItemsRef()
260
	{
261
		$search = $this->object->createSearch();
262
		$search->setConditions( $search->compare( '==', 'supplier.code', 'unitCode001' ) );
263
264
		$results = $this->object->searchItems( $search, ['supplier/address', 'text'] );
265
266
		if( ( $item = reset( $results ) ) === false ) {
267
			throw new \Exception( 'No supplier item for "unitCode001" available' );
268
		}
269
270
		$this->assertEquals( 3, count( $item->getRefItems( 'text', null, null, false ) ) );
271
		$this->assertEquals( 1, count( $item->getAddressItems() ) );
272
	}
273
274
275
	public function testGetSubManager()
276
	{
277
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'address' ) );
278
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'address', 'Standard' ) );
279
280
		$this->setExpectedException( \Aimeos\MShop\Exception::class );
281
		$this->object->getSubManager( 'unknown' );
282
	}
283
284
285
	public function testGetSubManagerInvalidName()
286
	{
287
		$this->setExpectedException( \Aimeos\MShop\Exception::class );
288
		$this->object->getSubManager( 'address', 'unknown' );
289
	}
290
}
291