Completed
Push — master ( afe8d8...095350 )
by Aimeos
02:02
created

BaseTest::testCreateSubscriptions()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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