Passed
Push — master ( fa68de...8ca22d )
by Aimeos
05:33
created

StandardTest::testCreateTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
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 testCreateTransaction()
90
	{
91
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface::class, $this->object->createTransaction() );
92
	}
93
94
95
	public function testDeleteItems()
96
	{
97
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [-1] ) );
98
	}
99
100
101
	public function testGetResourceType()
102
	{
103
		$result = $this->object->getResourceType();
104
105
		$this->assertContains( 'order/base/service', $result );
106
		$this->assertContains( 'order/base/service/attribute', $result );
107
	}
108
109
110
	public function testGetSearchAttributes()
111
	{
112
		foreach( $this->object->getSearchAttributes() as $attribute )
113
		{
114
			$this->assertInstanceOf( \Aimeos\Base\Criteria\Attribute\Iface::class, $attribute );
115
		}
116
	}
117
118
119
	public function testCreateSearch()
120
	{
121
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter() );
122
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter( true ) );
123
	}
124
125
126
	public function testSearchItems()
127
	{
128
		$siteid = $this->context->locale()->getSiteId();
129
130
		$total = 0;
131
		$search = $this->object->filter();
132
133
		$expr = [];
134
		$expr[] = $search->compare( '!=', 'order.base.service.id', null );
135
		$expr[] = $search->compare( '==', 'order.base.service.siteid', $siteid );
136
		$expr[] = $search->compare( '!=', 'order.base.service.baseid', null );
137
		$expr[] = $search->compare( '!=', 'order.base.service.serviceid', null );
138
		$expr[] = $search->compare( '==', 'order.base.service.type', 'payment' );
139
		$expr[] = $search->compare( '==', 'order.base.service.code', 'unitpaymentcode' );
140
		$expr[] = $search->compare( '==', 'order.base.service.name', 'unitpaymentcode' );
141
		$expr[] = $search->compare( '==', 'order.base.service.mediaurl', 'somewhere/thump1.jpg' );
142
		$expr[] = $search->compare( '==', 'order.base.service.price', '0.00' );
143
		$expr[] = $search->compare( '==', 'order.base.service.costs', '0.00' );
144
		$expr[] = $search->compare( '==', 'order.base.service.rebate', '0.00' );
145
		$expr[] = $search->compare( '=~', 'order.base.service.taxrates', '{' );
146
		$expr[] = $search->compare( '==', 'order.base.service.taxflag', 1 );
147
		$expr[] = $search->compare( '==', 'order.base.service.taxvalue', '0.00' );
148
		$expr[] = $search->compare( '==', 'order.base.service.position', 0 );
149
		$expr[] = $search->compare( '>=', 'order.base.service.mtime', '1970-01-01 00:00:00' );
150
		$expr[] = $search->compare( '>=', 'order.base.service.ctime', '1970-01-01 00:00:00' );
151
		$expr[] = $search->compare( '==', 'order.base.service.editor', $this->editor );
152
153
		$expr[] = $search->compare( '!=', 'order.base.service.attribute.id', null );
154
		$expr[] = $search->compare( '==', 'order.base.service.attribute.siteid', $siteid );
155
		$expr[] = $search->compare( '!=', 'order.base.service.attribute.parentid', null );
156
		$expr[] = $search->compare( '==', 'order.base.service.attribute.code', 'NAME' );
157
		$expr[] = $search->compare( '==', 'order.base.service.attribute.value', '"CreditCard"' );
158
		$expr[] = $search->compare( '==', 'order.base.service.attribute.quantity', 1 );
159
		$expr[] = $search->compare( '>=', 'order.base.service.attribute.mtime', '1970-01-01 00:00:00' );
160
		$expr[] = $search->compare( '>=', 'order.base.service.attribute.ctime', '1970-01-01 00:00:00' );
161
		$expr[] = $search->compare( '==', 'order.base.service.attribute.editor', $this->editor );
162
163
		$search->setConditions( $search->and( $expr ) );
164
		$result = $this->object->search( $search, [], $total )->toArray();
165
166
		$this->assertEquals( 1, count( $result ) );
167
		$this->assertEquals( 1, $total );
168
	}
169
170
171
	public function testSearchItemRef()
172
	{
173
		$search = $this->object->filter()->slice( 0, 1 );
174
		$search->setConditions( $search->compare( '==', 'order.base.service.code', 'unitpaymentcode' ) );
175
		$result = $this->object->search( $search, ['service'] );
176
177
		$this->assertEquals( 1, count( $result ) );
178
		$this->assertNotNull( $result->first()->getServiceItem() );
179
	}
180
181
182
	public function testSearchItemTotal()
183
	{
184
		$total = 0;
185
		$search = $this->object->filter()->slice( 0, 1 );
186
187
		$search->setConditions( $search->and( [
188
			$search->compare( '==', 'order.base.service.code', array( 'unitpaymentcode', 'not exists' ) ),
189
			$search->compare( '==', 'order.base.service.editor', $this->editor )
190
		] ) );
191
192
		$results = $this->object->search( $search, [], $total )->toArray();
193
194
		$this->assertEquals( 1, count( $results ) );
195
		$this->assertEquals( 2, $total );
196
197
		foreach( $results as $itemId => $item ) {
198
			$this->assertEquals( $itemId, $item->getId() );
199
		}
200
	}
201
202
203
	public function testGetSubManager()
204
	{
205
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'attribute' ) );
206
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'attribute', 'Standard' ) );
207
208
		$this->expectException( \Aimeos\MShop\Exception::class );
209
		$this->object->getSubManager( 'unknown' );
210
	}
211
212
213
	public function testGetSubManagerInvalidName()
214
	{
215
		$this->expectException( \Aimeos\MShop\Exception::class );
216
		$this->object->getSubManager( 'attribute', 'unknown' );
217
	}
218
219
220
	public function testGetSubManagerInvalidType()
221
	{
222
		$this->expectException( \Aimeos\MShop\Exception::class );
223
		$this->object->getSubManager( '%^unknown' );
224
	}
225
226
227
	public function testGetSubManagerInvalidDefaultName()
228
	{
229
		$this->expectException( \Aimeos\MShop\Exception::class );
230
		$this->object->getSubManager( 'attribute', '%^unknown' );
231
	}
232
233
234
	public function testGetItem()
235
	{
236
		$search = $this->object->filter()->slice( 0, 1 );
237
		$conditions = array(
238
			$search->compare( '==', 'order.base.service.code', 'unitpaymentcode' ),
239
			$search->compare( '==', 'order.base.service.editor', $this->editor ),
240
		);
241
		$search->setConditions( $search->and( $conditions ) );
242
		$results = $this->object->search( $search )->toArray();
243
244
		if( !( $item = reset( $results ) ) ) {
245
			throw new \RuntimeException( 'empty results' );
246
		}
247
248
		$actual = $this->object->get( $item->getId() );
249
		$this->assertEquals( $item, $actual );
250
	}
251
252
253
	public function testSaveUpdateDeleteItem()
254
	{
255
		$search = $this->object->filter();
256
		$conditions = array(
257
			$search->compare( '==', 'order.base.service.code', 'unitpaymentcode' ),
258
			$search->compare( '==', 'order.base.service.editor', $this->editor ),
259
			$search->compare( '==', 'order.base.service.attribute.code', 'NAME' ),
260
		);
261
		$search->setConditions( $search->and( $conditions ) );
262
		$orderItems = $this->object->search( $search )->toArray();
263
264
		if( !( $item = reset( $orderItems ) ) ) {
265
			throw new \RuntimeException( 'empty search result' );
266
		}
267
268
		$item->setId( null );
269
		$item->setCode( 'unittest1' );
270
		$resultSaved = $this->object->save( $item );
271
		$itemSaved = $this->object->get( $item->getId() );
272
273
274
		$itemExp = clone $itemSaved;
275
		$itemExp->setCode( 'unittest1' );
276
		$resultUpd = $this->object->save( $itemExp );
277
		$itemUpd = $this->object->get( $itemExp->getId() );
278
279
		$this->object->delete( $itemSaved->getId() );
280
281
282
		$this->assertTrue( $item->getId() !== null );
283
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
284
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
285
		$this->assertEquals( $item->getBaseId(), $itemSaved->getBaseId() );
286
		$this->assertEquals( $item->getServiceId(), $itemSaved->getServiceId() );
287
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
288
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
289
		$this->assertEquals( $item->getName(), $itemSaved->getName() );
290
		$this->assertEquals( $item->getMediaUrl(), $itemSaved->getMediaUrl() );
291
		$this->assertEquals( $item->getPrice()->getValue(), $itemSaved->getPrice()->getValue() );
292
		$this->assertEquals( $item->getPrice()->getCosts(), $itemSaved->getPrice()->getCosts() );
293
		$this->assertEquals( $item->getPrice()->getRebate(), $itemSaved->getPrice()->getRebate() );
294
		$this->assertEquals( $item->getPrice()->getTaxflag(), $itemSaved->getPrice()->getTaxflag() );
295
		$this->assertEquals( $item->getPrice()->getTaxValue(), $itemSaved->getPrice()->getTaxValue() );
296
		$this->assertNotEquals( [], $item->getAttributeItems()->toArray() );
297
298
		$this->assertEquals( $this->editor, $itemSaved->editor() );
299
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
300
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
301
302
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
303
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
304
		$this->assertEquals( $itemExp->getBaseId(), $itemUpd->getBaseId() );
305
		$this->assertEquals( $itemExp->getServiceId(), $itemUpd->getServiceID() );
306
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
307
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
308
		$this->assertEquals( $itemExp->getName(), $itemUpd->getName() );
309
		$this->assertEquals( $itemExp->getMediaUrl(), $itemUpd->getMediaUrl() );
310
		$this->assertEquals( $itemExp->getPrice()->getValue(), $itemUpd->getPrice()->getValue() );
311
		$this->assertEquals( $itemExp->getPrice()->getCosts(), $itemUpd->getPrice()->getCosts() );
312
		$this->assertEquals( $itemExp->getPrice()->getRebate(), $itemUpd->getPrice()->getRebate() );
313
		$this->assertEquals( $itemExp->getPrice()->getTaxflag(), $itemUpd->getPrice()->getTaxflag() );
314
		$this->assertEquals( $itemExp->getPrice()->getTaxValue(), $itemUpd->getPrice()->getTaxValue() );
315
		$this->assertNotEquals( [], $itemUpd->getAttributeItems()->toArray() );
316
317
		$this->assertEquals( $this->editor, $itemUpd->editor() );
318
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
319
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
320
321
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
322
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
323
324
		$this->expectException( \Aimeos\MShop\Exception::class );
325
		$this->object->get( $itemSaved->getId() );
326
	}
327
}
328