BaseTest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 329
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 167
dl 0
loc 329
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testCopyCoupon() 0 28 1
A testCreateSubscriptions() 0 26 2
A getAddress() 0 15 2
A tearDown() 0 6 1
A testGetOrderProductAttributes() 0 17 1
A testCopyProductException() 0 22 1
A testCopyCouponException() 0 19 1
A testCopyAddressesException() 0 20 1
A testCopyServicesException() 0 24 1
A setUp() 0 4 1
A testCopyAddresses() 0 24 1
A testCopyProduct() 0 24 1
A testCheckLocale() 0 11 1
A access() 0 7 1
A testCopyServices() 0 32 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
6
 */
7
8
namespace Aimeos\Controller\Frontend\Basket;
9
10
11
class BaseTest extends \PHPUnit\Framework\TestCase
12
{
13
	private $context;
14
15
16
	protected function setUp() : void
17
	{
18
		\Aimeos\MShop::cache( true );
19
		$this->context = \TestHelper::context();
20
	}
21
22
23
	protected function tearDown() : void
24
	{
25
		\Aimeos\MShop::cache( false );
26
		$this->context->session()->set( 'aimeos', [] );
27
28
		unset( $this->context );
29
	}
30
31
32
	public function testCheckLocale()
33
	{
34
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
35
			->setConstructorArgs( [$this->context] )
36
			->onlyMethods( [] )
37
			->getMock();
38
39
		$this->context->session()->set( 'aimeos/basket/locale', 'unittest|en|EUR' );
40
		$this->access( 'checkLocale' )->invokeArgs( $object, [$this->context->locale(), 'unittest'] );
41
42
		$this->assertEquals( 'unittest|de|EUR', $this->context->session()->get( 'aimeos/basket/locale' ) );
43
	}
44
45
46
	public function testCopyAddresses()
47
	{
48
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
49
		$ordBaseItem = $manager->create();
50
51
		$address = $this->getAddress( 'Example company' );
52
		$ordBaseItem->addAddress( $address, \Aimeos\MShop\Order\Item\Address\Base::TYPE_PAYMENT );
53
54
55
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
56
			->setConstructorArgs( [$this->context] )
57
			->onlyMethods( [] )
58
			->getMock();
59
60
		$result = $this->access( 'copyAddresses' )->invokeArgs( $object, [$ordBaseItem, ['test'], 'unittest|en|EUR'] );
61
62
63
		$this->assertEquals( ['test'], $result );
64
		$this->assertEquals( 1, count( $object->get()->getAddresses() ) );
65
66
		$addr = $object->get()->getAddress( \Aimeos\MShop\Order\Item\Address\Base::TYPE_PAYMENT, 0 );
67
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Address\Iface::class, $addr );
68
69
		$object->clear();
70
	}
71
72
73
	public function testCopyAddressesException()
74
	{
75
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
76
		$ordBaseItem = $manager->create();
77
78
		$address = $this->getAddress( 'Example company' );
79
		$ordBaseItem->addAddress( $address, \Aimeos\MShop\Order\Item\Address\Base::TYPE_PAYMENT );
80
81
82
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
83
			->setConstructorArgs( [$this->context] )
84
			->onlyMethods( ['get'] )
85
			->getMock();
86
87
		$object->expects( $this->once() )->method( 'get' )->will( $this->throwException( new \Exception() ) );
88
89
		$result = $this->access( 'copyAddresses' )->invokeArgs( $object, [$ordBaseItem, [], 'unittest|en|EUR'] );
90
91
		$this->assertEquals( 1, count( $result ) );
92
		$this->assertArrayHasKey( 'address', $result );
93
	}
94
95
96
	public function testCopyCoupon()
97
	{
98
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
99
		$ordBaseItem = $manager->create();
100
101
		$product = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC', ['price'] );
102
		$ordProdManager = \Aimeos\MShop::create( $this->context, 'order/product' );
103
		$ordProdItem = $ordProdManager->create()->copyFrom( $product )->setStockType( 'default' );
104
105
		$ordProdItem->setPrice( $product->getRefItems( 'price' )->first() );
106
107
		$ordBaseItem->addProduct( $ordProdItem );
108
		$ordBaseItem->addCoupon( 'OPQR', [] );
109
110
111
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
112
			->setConstructorArgs( [$this->context] )
113
			->onlyMethods( [] )
114
			->getMock();
115
116
		$object->addProduct( $product );
117
118
		$result = $this->access( 'copyCoupons' )->invokeArgs( $object, [$ordBaseItem, ['test'], 'unittest|en|EUR'] );
119
120
121
		$this->assertEquals( ['test'], $result );
122
123
		$object->clear();
124
	}
125
126
127
	public function testCopyCouponException()
128
	{
129
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
130
		$ordBaseItem = $manager->create();
131
132
		$ordBaseItem->addCoupon( '90AB', [] );
133
134
135
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
136
			->setConstructorArgs( [$this->context] )
137
			->onlyMethods( ['addCoupon'] )
138
			->getMock();
139
140
		$object->expects( $this->once() )->method( 'addCoupon' )->will( $this->throwException( new \Exception() ) );
141
142
		$result = $this->access( 'copyCoupons' )->invokeArgs( $object, [$ordBaseItem, [], 'unittest|en|EUR'] );
143
144
		$this->assertEquals( 1, count( $result ) );
145
		$this->assertArrayHasKey( 'coupon', $result );
146
	}
147
148
149
	public function testCopyProduct()
150
	{
151
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
152
		$ordBaseItem = $manager->create();
153
154
		$product = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC' );
155
		$ordProdManager = \Aimeos\MShop::create( $this->context, 'order/product' );
156
		$ordProdItem = $ordProdManager->create()->copyFrom( $product )->setStockType( 'default' );
157
		$ordBaseItem->addProduct( $ordProdItem );
158
159
160
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
161
			->setConstructorArgs( [$this->context] )
162
			->onlyMethods( [] )
163
			->getMock();
164
165
		$result = $this->access( 'copyProducts' )->invokeArgs( $object, [$ordBaseItem, ['test'], 'unittest|en|EUR'] );
166
167
168
		$this->assertEquals( ['test'], $result );
169
		$this->assertEquals( 1, count( $object->get()->getProducts() ) );
170
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Product\Iface::class, $object->get()->getProduct( 0 ) );
171
172
		$object->clear();
173
	}
174
175
176
	public function testCopyProductException()
177
	{
178
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
179
		$ordBaseItem = $manager->create();
180
181
		$product = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC' );
182
		$ordProdManager = \Aimeos\MShop::create( $this->context, 'order/product' );
183
		$ordProdItem = $ordProdManager->create()->copyFrom( $product )->setStockType( 'default' );
184
		$ordBaseItem->addProduct( $ordProdItem );
185
186
187
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
188
			->setConstructorArgs( [$this->context] )
189
			->onlyMethods( ['addProduct'] )
190
			->getMock();
191
192
		$object->expects( $this->once() )->method( 'addProduct' )->will( $this->throwException( new \Exception() ) );
193
194
		$result = $this->access( 'copyProducts' )->invokeArgs( $object, [$ordBaseItem, [], 'unittest|en|EUR'] );
195
196
		$this->assertEquals( 1, count( $result ) );
197
		$this->assertArrayHasKey( 'product', $result );
198
	}
199
200
201
	public function testCopyServices()
202
	{
203
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
204
		$ordBaseItem = $manager->create();
205
206
		$serviceManager = \Aimeos\MShop::create( $this->context, 'service' );
207
		$ordServManager = \Aimeos\MShop::create( $this->context, 'order/service' );
208
209
		$serviceItem = $serviceManager->find( 'unitdeliverycode', [], 'service', 'delivery' );
210
		$ordServItem = $ordServManager->create()->copyFrom( $serviceItem );
211
212
		$ordBaseItem->addService( $ordServItem, \Aimeos\MShop\Order\Item\Service\Base::TYPE_DELIVERY );
213
214
215
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
216
			->setConstructorArgs( [$this->context] )
217
			->onlyMethods( [] )
218
			->getMock();
219
220
		$result = $this->access( 'copyServices' )->invokeArgs( $object, [$ordBaseItem, ['test'], 'unittest|en|EUR'] );
221
222
223
		$this->assertEquals( ['test'], $result );
224
		$this->assertEquals( 1, count( $object->get()->getServices() ) );
225
226
		$services = $object->get()->getService( \Aimeos\MShop\Order\Item\Service\Base::TYPE_DELIVERY );
227
228
		foreach( $services as $service ) {
229
			$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Service\Iface::class, $service );
230
		}
231
232
		$object->clear();
233
	}
234
235
236
	public function testCopyServicesException()
237
	{
238
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'order' );
239
		$ordBaseItem = $manager->create();
240
241
		$serviceManager = \Aimeos\MShop::create( $this->context, 'service' );
242
		$ordServManager = \Aimeos\MShop::create( $this->context, 'order/service' );
243
244
		$serviceItem = $serviceManager->find( 'unitdeliverycode', [], 'service', 'delivery' );
245
		$ordServItem = $ordServManager->create()->copyFrom( $serviceItem );
246
247
		$ordBaseItem->addService( $ordServItem, \Aimeos\MShop\Order\Item\Service\Base::TYPE_DELIVERY );
248
249
250
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
251
			->setConstructorArgs( [$this->context] )
252
			->onlyMethods( ['addService'] )
253
			->getMock();
254
255
		$object->expects( $this->once() )->method( 'addService' )->will( $this->throwException( new \Exception() ) );
256
257
		$result = $this->access( 'copyServices' )->invokeArgs( $object, [$ordBaseItem, [], 'unittest|en|EUR'] );
258
259
		$this->assertEquals( 0, count( $result ) );
260
	}
261
262
263
	public function testCreateSubscriptions()
264
	{
265
		$manager = \Aimeos\MShop::create( $this->context, 'order' );
266
267
		$search = $manager->filter();
268
		$search->setConditions( $search->compare( '==', 'order.price', '53.50' ) );
269
270
		if( ( $basket = $manager->search( $search, ['order/product'] )->first() ) === null ) {
271
			throw new \Exception( sprintf( 'No order item found for price "%1$s"', '53,50' ) );
272
		}
273
274
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
275
			->setConstructorArgs( [$this->context] )
276
			->onlyMethods( ['getAttributes'] )
277
			->getMock();
278
279
		$stub = $this->getMockBuilder( \Aimeos\MShop\Subscription\Manager\Standard::class )
280
			->setConstructorArgs( [$this->context] )
281
			->onlyMethods( ['save'] )
282
			->getMock();
283
284
		\Aimeos\MShop::inject( \Aimeos\MShop\Subscription\Manager\Standard::class, $stub );
285
286
		$stub->expects( $this->exactly( 2 ) )->method( 'save' );
287
288
		$this->access( 'createSubscriptions' )->invokeArgs( $object, [$basket] );
289
	}
290
291
292
	public function testGetOrderProductAttributes()
293
	{
294
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->create()
295
			->setCode( 'special_instructions' )->setCode( 'test' )->setType( 'test' )->setId( '-1' );
296
297
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
298
			->setConstructorArgs( [$this->context] )
299
			->onlyMethods( ['getAttributes'] )
300
			->getMock();
301
302
		$object->expects( $this->once() )->method( 'getAttributes' )
303
			->willReturn( map( [1 => $attrItem] ) );
304
305
		$result = $this->access( 'getOrderProductAttributes' )->invokeArgs( $object, ['test', ['1'], ['1' => 'test']] );
306
307
		$this->assertEquals( 1, count( $result ) );
308
		$this->assertEquals( 'test', $result[0]->getValue() );
309
	}
310
311
312
	/**
313
	 * @param string $company
314
	 */
315
	protected function getAddress( $company )
316
	{
317
		$manager = \Aimeos\MShop::create( \TestHelper::context(), 'customer/address' );
318
319
		$search = $manager->filter();
320
		$search->setConditions( $search->compare( '==', 'customer.address.company', $company ) );
321
322
		if( ( $item = $manager->search( $search )->first() ) === null ) {
323
			throw new \RuntimeException( sprintf( 'No address item with company "%1$s" found', $company ) );
324
		}
325
326
		$ordAddrManager = \Aimeos\MShop::create( \TestHelper::context(), 'order/address' );
327
		$ordAddrItem = $ordAddrManager->create()->copyFrom( $item );
328
329
		return $ordAddrItem;
330
	}
331
332
333
	protected function access( $name )
334
	{
335
		$class = new \ReflectionClass( \Aimeos\Controller\Frontend\Basket\Base::class );
336
		$method = $class->getMethod( $name );
337
		$method->setAccessible( true );
338
339
		return $method;
340
	}
341
}
342