Passed
Push — master ( 5d4f58...4ee10f )
by Aimeos
04:18
created

StandardTest::testGetItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 2
nc 2
nop 0
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-2022
7
 */
8
9
10
namespace Aimeos\MShop\Order\Manager\Base\Service;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $context;
16
	private $object;
17
	private $editor = '';
18
19
20
	protected function setUp() : void
21
	{
22
		$this->editor = \TestHelper::context()->editor();
23
		$this->context = \TestHelper::context();
24
		$this->object = new \Aimeos\MShop\Order\Manager\Base\Service\Standard( $this->context );
25
	}
26
27
28
	protected function tearDown() : void
29
	{
30
		unset( $this->object );
31
	}
32
33
34
	public function testAggregate()
35
	{
36
		$search = $this->object->filter();
37
		$search->setConditions( $search->compare( '==', 'order.base.service.editor', 'core' ) );
38
		$result = $this->object->aggregate( $search, 'order.base.service.code' )->toArray();
39
40
		$this->assertEquals( 4, count( $result ) );
41
		$this->assertArrayHasKey( 'unitpaymentcode', $result );
42
		$this->assertEquals( 2, $result['unitpaymentcode'] );
43
	}
44
45
46
	public function testAggregateAvg()
47
	{
48
		$search = $this->object->filter();
49
		$search->setConditions( $search->compare( '==', 'order.base.service.editor', 'core' ) );
50
		$result = $this->object->aggregate( $search, 'order.base.service.type', 'order.base.service.costs', 'avg' )->toArray();
51
52
		$this->assertEquals( 2, count( $result ) );
53
		$this->assertArrayHasKey( 'delivery', $result );
54
		$this->assertEquals( '2.50', round( $result['delivery'], 2 ) );
55
	}
56
57
58
	public function testAggregateSum()
59
	{
60
		$search = $this->object->filter();
61
		$search->setConditions( $search->compare( '==', 'order.base.service.editor', 'core' ) );
62
		$result = $this->object->aggregate( $search, 'order.base.service.type', 'order.base.service.costs', 'sum' )->toArray();
63
64
		$this->assertEquals( 2, count( $result ) );
65
		$this->assertArrayHasKey( 'delivery', $result );
66
		$this->assertEquals( '10.00', $result['delivery'] );
67
	}
68
69
70
	public function testClear()
71
	{
72
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( [-1] ) );
73
	}
74
75
76
	public function testCreateItem()
77
	{
78
		$actual = $this->object->create();
79
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Iface::class, $actual );
80
	}
81
82
83
	public function testCreateAttributeItem()
84
	{
85
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface::class, $this->object->createAttributeItem() );
86
	}
87
88
89
	public function testDeleteItems()
90
	{
91
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [-1] ) );
92
	}
93
94
95
	public function testGetResourceType()
96
	{
97
		$result = $this->object->getResourceType();
98
99
		$this->assertContains( 'order/base/service', $result );
100
		$this->assertContains( 'order/base/service/attribute', $result );
101
	}
102
103
104
	public function testGetSearchAttributes()
105
	{
106
		foreach( $this->object->getSearchAttributes() as $attribute )
107
		{
108
			$this->assertInstanceOf( \Aimeos\Base\Criteria\Attribute\Iface::class, $attribute );
109
		}
110
	}
111
112
113
	public function testCreateSearch()
114
	{
115
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter() );
116
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter( true ) );
117
	}
118
119
120
	public function testSearchItems()
121
	{
122
		$siteid = $this->context->locale()->getSiteId();
123
124
		$total = 0;
125
		$search = $this->object->filter();
126
127
		$expr = [];
128
		$expr[] = $search->compare( '!=', 'order.base.service.id', null );
129
		$expr[] = $search->compare( '==', 'order.base.service.siteid', $siteid );
130
		$expr[] = $search->compare( '!=', 'order.base.service.baseid', null );
131
		$expr[] = $search->compare( '!=', 'order.base.service.serviceid', null );
132
		$expr[] = $search->compare( '==', 'order.base.service.type', 'payment' );
133
		$expr[] = $search->compare( '==', 'order.base.service.code', 'unitpaymentcode' );
134
		$expr[] = $search->compare( '==', 'order.base.service.name', 'unitpaymentcode' );
135
		$expr[] = $search->compare( '==', 'order.base.service.mediaurl', 'somewhere/thump1.jpg' );
136
		$expr[] = $search->compare( '==', 'order.base.service.price', '0.00' );
137
		$expr[] = $search->compare( '==', 'order.base.service.costs', '0.00' );
138
		$expr[] = $search->compare( '==', 'order.base.service.rebate', '0.00' );
139
		$expr[] = $search->compare( '=~', 'order.base.service.taxrates', '{' );
140
		$expr[] = $search->compare( '==', 'order.base.service.taxflag', 1 );
141
		$expr[] = $search->compare( '==', 'order.base.service.taxvalue', '0.00' );
142
		$expr[] = $search->compare( '==', 'order.base.service.position', 0 );
143
		$expr[] = $search->compare( '>=', 'order.base.service.mtime', '1970-01-01 00:00:00' );
144
		$expr[] = $search->compare( '>=', 'order.base.service.ctime', '1970-01-01 00:00:00' );
145
		$expr[] = $search->compare( '==', 'order.base.service.editor', $this->editor );
146
147
		$expr[] = $search->compare( '!=', 'order.base.service.attribute.id', null );
148
		$expr[] = $search->compare( '==', 'order.base.service.attribute.siteid', $siteid );
149
		$expr[] = $search->compare( '!=', 'order.base.service.attribute.parentid', null );
150
		$expr[] = $search->compare( '==', 'order.base.service.attribute.code', 'NAME' );
151
		$expr[] = $search->compare( '==', 'order.base.service.attribute.value', '"CreditCard"' );
152
		$expr[] = $search->compare( '==', 'order.base.service.attribute.quantity', 1 );
153
		$expr[] = $search->compare( '>=', 'order.base.service.attribute.mtime', '1970-01-01 00:00:00' );
154
		$expr[] = $search->compare( '>=', 'order.base.service.attribute.ctime', '1970-01-01 00:00:00' );
155
		$expr[] = $search->compare( '==', 'order.base.service.attribute.editor', $this->editor );
156
157
		$search->setConditions( $search->and( $expr ) );
158
		$result = $this->object->search( $search, [], $total )->toArray();
159
160
		$this->assertEquals( 1, count( $result ) );
161
		$this->assertEquals( 1, $total );
162
	}
163
164
165
	public function testSearchItemRef()
166
	{
167
		$search = $this->object->filter()->slice( 0, 1 );
168
		$search->setConditions( $search->compare( '==', 'order.base.service.code', 'unitpaymentcode' ) );
169
		$result = $this->object->search( $search, ['service'] );
170
171
		$this->assertEquals( 1, count( $result ) );
172
		$this->assertNotNull( $result->first()->getServiceItem() );
173
	}
174
175
176
	public function testSearchItemTotal()
177
	{
178
		$total = 0;
179
		$search = $this->object->filter()->slice( 0, 1 );
180
181
		$search->setConditions( $search->and( [
182
			$search->compare( '==', 'order.base.service.code', array( 'unitpaymentcode', 'not exists' ) ),
183
			$search->compare( '==', 'order.base.service.editor', $this->editor )
184
		] ) );
185
186
		$results = $this->object->search( $search, [], $total )->toArray();
187
188
		$this->assertEquals( 1, count( $results ) );
189
		$this->assertEquals( 2, $total );
190
191
		foreach( $results as $itemId => $item ) {
192
			$this->assertEquals( $itemId, $item->getId() );
193
		}
194
	}
195
196
197
	public function testGetSubManager()
198
	{
199
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'attribute' ) );
200
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'attribute', 'Standard' ) );
201
202
		$this->expectException( \Aimeos\MShop\Exception::class );
203
		$this->object->getSubManager( 'unknown' );
204
	}
205
206
207
	public function testGetSubManagerInvalidName()
208
	{
209
		$this->expectException( \Aimeos\MShop\Exception::class );
210
		$this->object->getSubManager( 'attribute', 'unknown' );
211
	}
212
213
214
	public function testGetSubManagerInvalidType()
215
	{
216
		$this->expectException( \Aimeos\MShop\Exception::class );
217
		$this->object->getSubManager( '%^unknown' );
218
	}
219
220
221
	public function testGetSubManagerInvalidDefaultName()
222
	{
223
		$this->expectException( \Aimeos\MShop\Exception::class );
224
		$this->object->getSubManager( 'attribute', '%^unknown' );
225
	}
226
227
228
	public function testGetItem()
229
	{
230
		$search = $this->object->filter()->slice( 0, 1 );
231
		$conditions = array(
232
			$search->compare( '==', 'order.base.service.code', 'unitpaymentcode' ),
233
			$search->compare( '==', 'order.base.service.editor', $this->editor ),
234
		);
235
		$search->setConditions( $search->and( $conditions ) );
236
		$results = $this->object->search( $search )->toArray();
237
238
		if( !( $item = reset( $results ) ) ) {
239
			throw new \RuntimeException( 'empty results' );
240
		}
241
242
		$actual = $this->object->get( $item->getId() );
243
		$this->assertEquals( $item, $actual );
244
	}
245
246
247
	public function testSaveUpdateDeleteItem()
248
	{
249
		$search = $this->object->filter();
250
		$conditions = array(
251
			$search->compare( '==', 'order.base.service.code', 'unitpaymentcode' ),
252
			$search->compare( '==', 'order.base.service.editor', $this->editor ),
253
			$search->compare( '==', 'order.base.service.attribute.code', 'NAME' ),
254
		);
255
		$search->setConditions( $search->and( $conditions ) );
256
		$orderItems = $this->object->search( $search )->toArray();
257
258
		if( !( $item = reset( $orderItems ) ) ) {
259
			throw new \RuntimeException( 'empty search result' );
260
		}
261
262
		$item->setId( null );
263
		$item->setCode( 'unittest1' );
264
		$resultSaved = $this->object->save( $item );
265
		$itemSaved = $this->object->get( $item->getId() );
266
267
268
		$itemExp = clone $itemSaved;
269
		$itemExp->setCode( 'unittest1' );
270
		$resultUpd = $this->object->save( $itemExp );
271
		$itemUpd = $this->object->get( $itemExp->getId() );
272
273
		$this->object->delete( $itemSaved->getId() );
274
275
276
		$this->assertTrue( $item->getId() !== null );
277
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
278
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
279
		$this->assertEquals( $item->getBaseId(), $itemSaved->getBaseId() );
280
		$this->assertEquals( $item->getServiceId(), $itemSaved->getServiceId() );
281
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
282
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
283
		$this->assertEquals( $item->getName(), $itemSaved->getName() );
284
		$this->assertEquals( $item->getMediaUrl(), $itemSaved->getMediaUrl() );
285
		$this->assertEquals( $item->getPrice()->getValue(), $itemSaved->getPrice()->getValue() );
286
		$this->assertEquals( $item->getPrice()->getCosts(), $itemSaved->getPrice()->getCosts() );
287
		$this->assertEquals( $item->getPrice()->getRebate(), $itemSaved->getPrice()->getRebate() );
288
		$this->assertEquals( $item->getPrice()->getTaxflag(), $itemSaved->getPrice()->getTaxflag() );
289
		$this->assertEquals( $item->getPrice()->getTaxValue(), $itemSaved->getPrice()->getTaxValue() );
290
		$this->assertNotEquals( [], $item->getAttributeItems()->toArray() );
291
292
		$this->assertEquals( $this->editor, $itemSaved->editor() );
293
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
294
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
295
296
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
297
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
298
		$this->assertEquals( $itemExp->getBaseId(), $itemUpd->getBaseId() );
299
		$this->assertEquals( $itemExp->getServiceId(), $itemUpd->getServiceID() );
300
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
301
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
302
		$this->assertEquals( $itemExp->getName(), $itemUpd->getName() );
303
		$this->assertEquals( $itemExp->getMediaUrl(), $itemUpd->getMediaUrl() );
304
		$this->assertEquals( $itemExp->getPrice()->getValue(), $itemUpd->getPrice()->getValue() );
305
		$this->assertEquals( $itemExp->getPrice()->getCosts(), $itemUpd->getPrice()->getCosts() );
306
		$this->assertEquals( $itemExp->getPrice()->getRebate(), $itemUpd->getPrice()->getRebate() );
307
		$this->assertEquals( $itemExp->getPrice()->getTaxflag(), $itemUpd->getPrice()->getTaxflag() );
308
		$this->assertEquals( $itemExp->getPrice()->getTaxValue(), $itemUpd->getPrice()->getTaxValue() );
309
		$this->assertNotEquals( [], $itemUpd->getAttributeItems()->toArray() );
310
311
		$this->assertEquals( $this->editor, $itemUpd->editor() );
312
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
313
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
314
315
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
316
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
317
318
		$this->expectException( \Aimeos\MShop\Exception::class );
319
		$this->object->get( $itemSaved->getId() );
320
	}
321
}
322