Passed
Push — master ( af9504...7332c2 )
by Aimeos
14:14
created

StandardTest::testSaveUpdateDeleteItem()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 72
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 56
nc 2
nop 0
dl 0
loc 72
rs 8.9599
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, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2024
7
 */
8
9
10
namespace Aimeos\MShop\Service\Manager;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $editor = '';
17
18
19
	protected function setUp() : void
20
	{
21
		$this->editor = \TestHelper::context()->editor();
22
		$this->object = new \Aimeos\MShop\Service\Manager\Standard( \TestHelper::context() );
23
	}
24
25
26
	protected function tearDown() : void
27
	{
28
		$this->object = null;
29
	}
30
31
32
	public function testClear()
33
	{
34
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( [-1] ) );
35
	}
36
37
38
	public function testDelete()
39
	{
40
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [-1] ) );
41
	}
42
43
44
	public function testCreate()
45
	{
46
		$this->assertInstanceOf( \Aimeos\MShop\Service\Item\Iface::class, $this->object->create() );
47
	}
48
49
50
	public function testCreateType()
51
	{
52
		$item = $this->object->create( ['service.type' => 'delivery'] );
53
		$this->assertEquals( 'delivery', $item->getType() );
54
	}
55
56
57
	public function testSaveUpdateDelete()
58
	{
59
		$search = $this->object->filter();
60
		$conditions = array(
61
			$search->compare( '==', 'service.code', 'unitdeliverycode' ),
62
			$search->compare( '==', 'service.editor', $this->editor )
63
		);
64
		$search->setConditions( $search->and( $conditions ) );
65
66
		$results = $this->object->search( $search )->toArray();
67
68
		if( ( $item = reset( $results ) ) === false ) {
69
			throw new \RuntimeException( 'No service provider item found.' );
70
		}
71
72
		$item->setId( null );
73
		$item->setCode( 'newstaticdelivery' );
74
		$resultSaved = $this->object->save( $item );
75
		$itemSaved = $this->object->get( $item->getId() );
76
77
		$itemExp = clone $itemSaved;
78
		$itemExp->setCode( '2ndChang' );
79
		$itemExp->setLabel( '2ndNameChanged' );
80
		$itemExp->setPosition( '1' );
81
		$itemExp->setStatus( '1' );
82
		$itemExp->setProvider( 'HS' );
83
		$resultUpd = $this->object->save( $itemExp );
84
		$itemUpd = $this->object->get( $itemExp->getId() );
85
86
		$this->object->delete( $item->getId() );
87
88
89
		$this->assertTrue( $item->getId() !== null );
90
		$this->assertTrue( $itemSaved->getType() !== null );
91
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
92
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
93
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
94
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
95
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
96
		$this->assertEquals( $item->getProvider(), $itemSaved->getProvider() );
97
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
98
		$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
99
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
100
		$this->assertEquals( $item->getConfig(), $itemSaved->getConfig() );
101
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
102
103
		$this->assertEquals( $this->editor, $itemSaved->editor() );
104
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
105
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
106
107
		$this->assertTrue( $itemUpd->getType() !== null );
108
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
109
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
110
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
111
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
112
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
113
		$this->assertEquals( $itemExp->getProvider(), $itemUpd->getProvider() );
114
		$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() );
115
		$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() );
116
		$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() );
117
		$this->assertEquals( $itemExp->getConfig(), $itemUpd->getConfig() );
118
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
119
120
		$this->assertEquals( $this->editor, $itemUpd->editor() );
121
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
122
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
123
124
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
125
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
126
127
		$this->expectException( \Aimeos\MShop\Exception::class );
128
		$this->object->get( $itemSaved->getId() );
129
	}
130
131
132
	public function testFind()
133
	{
134
		$item = $this->object->find( 'unitdeliverycode' );
135
136
		$this->assertEquals( 'unitdeliverycode', $item->getCode() );
137
	}
138
139
140
	public function testGet()
141
	{
142
		$search = $this->object->filter()->slice( 0, 1 );
143
		$conditions = array(
144
			$search->compare( '==', 'service.code', 'unitdeliverycode' ),
145
			$search->compare( '==', 'service.editor', $this->editor )
146
		);
147
		$search->setConditions( $search->and( $conditions ) );
148
		$result = $this->object->search( $search, array( 'text' ) )->toArray();
149
150
		if( ( $item = reset( $result ) ) === false ) {
151
			throw new \RuntimeException( 'No item found' );
152
		}
153
154
		$this->assertEquals( $item, $this->object->get( $item->getId(), array( 'text' ) ) );
155
		$this->assertEquals( 5, count( $item->getRefItems( 'text' ) ) );
156
	}
157
158
159
	public function testSearchItem()
160
	{
161
		$item = $this->object->find( 'unitdeliverycode', ['text'] );
162
163
		if( ( $listItem = $item->getListItems( 'text', 'unittype1' )->first() ) === null ) {
164
			throw new \RuntimeException( 'No list item found' );
165
		}
166
167
		$total = 0;
168
		$search = $this->object->filter();
169
170
		$expr = [];
171
		$expr[] = $search->compare( '!=', 'service.id', null );
172
		$expr[] = $search->compare( '!=', 'service.siteid', null );
173
		$expr[] = $search->compare( '==', 'service.type', 'delivery' );
174
		$expr[] = $search->compare( '>=', 'service.position', 0 );
175
		$expr[] = $search->compare( '==', 'service.code', 'unitdeliverycode' );
176
		$expr[] = $search->compare( '==', 'service.label', 'unitlabel' );
177
		$expr[] = $search->compare( '==', 'service.provider', 'Standard' );
178
		$expr[] = $search->compare( '==', 'service.datestart', null );
179
		$expr[] = $search->compare( '==', 'service.dateend', null );
180
		$expr[] = $search->compare( '!=', 'service.config', null );
181
		$expr[] = $search->compare( '==', 'service.status', 1 );
182
		$expr[] = $search->compare( '>=', 'service.mtime', '1970-01-01 00:00:00' );
183
		$expr[] = $search->compare( '>=', 'service.ctime', '1970-01-01 00:00:00' );
184
		$expr[] = $search->compare( '==', 'service.editor', $this->editor );
185
186
		$param = ['text', 'unittype1', $listItem->getRefId()];
187
		$expr[] = $search->compare( '!=', $search->make( 'service:has', $param ), null );
188
189
		$param = ['text', 'unittype1'];
190
		$expr[] = $search->compare( '!=', $search->make( 'service:has', $param ), null );
191
192
		$param = ['text'];
193
		$expr[] = $search->compare( '!=', $search->make( 'service:has', $param ), null );
194
195
		$search->setConditions( $search->and( $expr ) );
196
		$results = $this->object->search( $search, [], $total )->toArray();
197
		$this->assertEquals( 1, count( $results ) );
198
		$this->assertEquals( 1, $total );
199
200
		foreach( $results as $itemId => $item ) {
201
			$this->assertEquals( $itemId, $item->getId() );
202
		}
203
	}
204
205
206
	public function testSearchItemBase()
207
	{
208
		$search = $this->object->filter( true );
209
		$expr = array(
210
			$search->compare( '==', 'service.provider', 'unitprovider' ),
211
			$search->compare( '==', 'service.editor', $this->editor ),
212
			$search->getConditions(),
213
		);
214
		$search->setConditions( $search->and( $expr ) );
215
		$this->assertEquals( 0, count( $this->object->search( $search )->toArray() ) );
216
	}
217
218
219
	public function testGetProvider()
220
	{
221
		$search = $this->object->filter();
222
		$conditions = array(
223
			$search->compare( '==', 'service.type', 'delivery' ),
224
			$search->compare( '==', 'service.editor', $this->editor )
225
		);
226
		$search->setConditions( $search->and( $conditions ) );
227
		$search->slice( 0, 1 );
228
		$result = $this->object->search( $search )->toArray();
229
230
		if( ( $item = reset( $result ) ) === false ) {
231
			throw new \RuntimeException( 'No service item found' );
232
		}
233
234
		$item->setProvider( 'Standard,Example' );
235
		$provider = $this->object->getProvider( $item, 'delivery' );
236
237
		$this->assertInstanceOf( \Aimeos\MShop\Service\Provider\Iface::class, $provider );
238
		$this->assertInstanceOf( \Aimeos\MShop\Service\Provider\Decorator\Example::class, $provider );
239
240
241
		$this->expectException( \LogicException::class );
242
		$this->object->getProvider( $this->object->create(), 'payment' );
243
	}
244
245
246
	public function testGetSubManager()
247
	{
248
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type' ) );
249
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type', 'Standard' ) );
250
251
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'lists' ) );
252
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'lists', 'Standard' ) );
253
254
		$this->expectException( \LogicException::class );
255
		$this->object->getSubManager( 'unknown' );
256
	}
257
258
259
	public function testGetSubManagerInvalidName()
260
	{
261
		$this->expectException( \LogicException::class );
262
		$this->object->getSubManager( 'lists', 'unknown' );
263
	}
264
265
266
	public function testGetResourceType()
267
	{
268
		$result = $this->object->getResourceType();
269
270
		$this->assertContains( 'service', $result );
271
		$this->assertContains( 'service/lists', $result );
272
	}
273
274
275
	public function testGetSearchAttributes()
276
	{
277
		$attribs = $this->object->getSearchAttributes();
278
		foreach( $attribs as $obj ) {
279
			$this->assertInstanceOf( \Aimeos\Base\Criteria\Attribute\Iface::class, $obj );
280
		}
281
	}
282
283
284
	public function testFilter()
285
	{
286
		$search = $this->object->filter();
287
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $search );
288
	}
289
}
290