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
|
|
|
} |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
protected function tearDown() |
23
|
|
|
{ |
24
|
|
|
$this->context->getSession()->set( 'aimeos', array() ); |
25
|
|
|
|
26
|
|
|
unset( $this->context ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function testCheckLocale() |
31
|
|
|
{ |
32
|
|
|
$object = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
33
|
|
|
->setConstructorArgs( [$this->context] ) |
34
|
|
|
->setMethods() |
35
|
|
|
->getMock(); |
36
|
|
|
|
37
|
|
|
$this->context->getSession()->set( 'aimeos/basket/locale', 'unittest|en|EUR' ); |
38
|
|
|
$this->access( 'checkLocale' )->invokeArgs( $object, ['unittest'] ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
public function testCopyAddresses() |
43
|
|
|
{ |
44
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( \TestHelperFrontend::getContext(), 'order/base' ); |
45
|
|
|
$ordBaseItem = $manager->createItem(); |
46
|
|
|
|
47
|
|
|
$address = $this->getAddress( 'Example company' ); |
48
|
|
|
$ordBaseItem->setAddress( $address, \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
$object = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
52
|
|
|
->setConstructorArgs( [$this->context] ) |
53
|
|
|
->setMethods() |
54
|
|
|
->getMock(); |
55
|
|
|
|
56
|
|
|
$result = $this->access( 'copyAddresses' )->invokeArgs( $object, [$ordBaseItem, ['test'], 'unittest|en|EUR'] ); |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->assertEquals( ['test'], $result ); |
60
|
|
|
$this->assertEquals( 1, count( $object->get()->getAddresses() ) ); |
61
|
|
|
|
62
|
|
|
$addr = $object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
63
|
|
|
$this->assertInstanceOf( '\Aimeos\MShop\Order\Item\Base\Address\Iface', $addr ); |
64
|
|
|
|
65
|
|
|
$object->clear(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function testCopyAddressesException() |
70
|
|
|
{ |
71
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( \TestHelperFrontend::getContext(), 'order/base' ); |
72
|
|
|
$ordBaseItem = $manager->createItem(); |
73
|
|
|
|
74
|
|
|
$address = $this->getAddress( 'Example company' ); |
75
|
|
|
$ordBaseItem->setAddress( $address, \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
$object = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
79
|
|
|
->setConstructorArgs( [$this->context] ) |
80
|
|
|
->setMethods( ['setAddress'] ) |
81
|
|
|
->getMock(); |
82
|
|
|
|
83
|
|
|
$object->expects( $this->once() )->method( 'setAddress' )->will( $this->throwException( new \Exception() ) ); |
84
|
|
|
|
85
|
|
|
$result = $this->access( 'copyAddresses' )->invokeArgs( $object, [$ordBaseItem, [], 'unittest|en|EUR'] ); |
86
|
|
|
|
87
|
|
|
$this->assertEquals( 1, count( $result ) ); |
88
|
|
|
$this->assertArrayHasKey( 'address', $result ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
public function testCopyCoupon() |
93
|
|
|
{ |
94
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( \TestHelperFrontend::getContext(), 'order/base' ); |
95
|
|
|
$ordBaseItem = $manager->createItem(); |
96
|
|
|
|
97
|
|
|
$product = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC', ['price'] ); |
98
|
|
|
$ordProdManager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base/product' ); |
99
|
|
|
$ordProdItem = $ordProdManager->createItem()->copyFrom( $product ); |
100
|
|
|
|
101
|
|
|
$priceItems = $product->getRefItems( 'price' ); |
102
|
|
|
$ordProdItem->setPrice( reset( $priceItems ) ); |
103
|
|
|
|
104
|
|
|
$ordBaseItem->addProduct( $ordProdItem ); |
105
|
|
|
$ordBaseItem->addCoupon( 'OPQR', [] ); |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
$object = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
109
|
|
|
->setConstructorArgs( [$this->context] ) |
110
|
|
|
->setMethods() |
111
|
|
|
->getMock(); |
112
|
|
|
|
113
|
|
|
$object->addProduct( $product->getId() ); |
114
|
|
|
|
115
|
|
|
$result = $this->access( 'copyCoupons' )->invokeArgs( $object, [$ordBaseItem, ['test'], 'unittest|en|EUR'] ); |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
$this->assertEquals( ['test'], $result ); |
119
|
|
|
|
120
|
|
|
$object->clear(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
public function testCopyCouponException() |
125
|
|
|
{ |
126
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( \TestHelperFrontend::getContext(), 'order/base' ); |
127
|
|
|
$ordBaseItem = $manager->createItem(); |
128
|
|
|
|
129
|
|
|
$ordBaseItem->addCoupon( '90AB', [] ); |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
$object = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
133
|
|
|
->setConstructorArgs( [$this->context] ) |
134
|
|
|
->setMethods( ['addCoupon'] ) |
135
|
|
|
->getMock(); |
136
|
|
|
|
137
|
|
|
$object->expects( $this->once() )->method( 'addCoupon' )->will( $this->throwException( new \Exception() ) ); |
138
|
|
|
|
139
|
|
|
$result = $this->access( 'copyCoupons' )->invokeArgs( $object, [$ordBaseItem, [], 'unittest|en|EUR'] ); |
140
|
|
|
|
141
|
|
|
$this->assertEquals( 1, count( $result ) ); |
142
|
|
|
$this->assertArrayHasKey( 'coupon', $result ); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
public function testCopyProduct() |
147
|
|
|
{ |
148
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( \TestHelperFrontend::getContext(), 'order/base' ); |
149
|
|
|
$ordBaseItem = $manager->createItem(); |
150
|
|
|
|
151
|
|
|
$product = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' ); |
152
|
|
|
$ordProdManager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base/product' ); |
153
|
|
|
$ordProdItem = $ordProdManager->createItem()->copyFrom( $product ); |
154
|
|
|
$ordBaseItem->addProduct( $ordProdItem ); |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
$object = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
158
|
|
|
->setConstructorArgs( [$this->context] ) |
159
|
|
|
->setMethods() |
160
|
|
|
->getMock(); |
161
|
|
|
|
162
|
|
|
$result = $this->access( 'copyProducts' )->invokeArgs( $object, [$ordBaseItem, ['test'], 'unittest|en|EUR'] ); |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
$this->assertEquals( ['test'], $result ); |
166
|
|
|
$this->assertEquals( 1, count( $object->get()->getProducts() ) ); |
167
|
|
|
$this->assertInstanceOf( '\Aimeos\MShop\Order\Item\Base\Product\Iface', $object->get()->getProduct( 0 ) ); |
168
|
|
|
|
169
|
|
|
$object->clear(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
public function testCopyProductException() |
174
|
|
|
{ |
175
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( \TestHelperFrontend::getContext(), 'order/base' ); |
176
|
|
|
$ordBaseItem = $manager->createItem(); |
177
|
|
|
|
178
|
|
|
$product = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' ); |
179
|
|
|
$ordProdManager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base/product' ); |
180
|
|
|
$ordProdItem = $ordProdManager->createItem()->copyFrom( $product ); |
181
|
|
|
$ordBaseItem->addProduct( $ordProdItem ); |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
$object = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
185
|
|
|
->setConstructorArgs( [$this->context] ) |
186
|
|
|
->setMethods( ['addProduct'] ) |
187
|
|
|
->getMock(); |
188
|
|
|
|
189
|
|
|
$object->expects( $this->once() )->method( 'addProduct' )->will( $this->throwException( new \Exception() ) ); |
190
|
|
|
|
191
|
|
|
$result = $this->access( 'copyProducts' )->invokeArgs( $object, [$ordBaseItem, [], 'unittest|en|EUR'] ); |
192
|
|
|
|
193
|
|
|
$this->assertEquals( 1, count( $result ) ); |
194
|
|
|
$this->assertArrayHasKey( 'product', $result ); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
public function testCopyServices() |
199
|
|
|
{ |
200
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( \TestHelperFrontend::getContext(), 'order/base' ); |
201
|
|
|
$ordBaseItem = $manager->createItem(); |
202
|
|
|
|
203
|
|
|
$serviceManager = \Aimeos\MShop\Factory::createManager( $this->context, 'service' ); |
204
|
|
|
$ordServManager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base/service' ); |
205
|
|
|
|
206
|
|
|
$serviceItem = $serviceManager->findItem( 'unitcode', [], 'service', 'delivery' ); |
207
|
|
|
$ordServItem = $ordServManager->createItem()->copyFrom( $serviceItem ); |
208
|
|
|
|
209
|
|
|
$ordBaseItem->setService( $ordServItem, \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY ); |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
$object = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
213
|
|
|
->setConstructorArgs( [$this->context] ) |
214
|
|
|
->setMethods() |
215
|
|
|
->getMock(); |
216
|
|
|
|
217
|
|
|
$result = $this->access( 'copyServices' )->invokeArgs( $object, [$ordBaseItem, ['test'], 'unittest|en|EUR'] ); |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
$this->assertEquals( ['test'], $result ); |
221
|
|
|
$this->assertEquals( 1, count( $object->get()->getServices() ) ); |
222
|
|
|
|
223
|
|
|
$service = $object->get()->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY ); |
224
|
|
|
$this->assertInstanceOf( '\Aimeos\MShop\Order\Item\Base\Service\Iface', $service ); |
225
|
|
|
|
226
|
|
|
$object->clear(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
public function testCopyServicesException() |
231
|
|
|
{ |
232
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( \TestHelperFrontend::getContext(), 'order/base' ); |
233
|
|
|
$ordBaseItem = $manager->createItem(); |
234
|
|
|
|
235
|
|
|
$serviceManager = \Aimeos\MShop\Factory::createManager( $this->context, 'service' ); |
236
|
|
|
$ordServManager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base/service' ); |
237
|
|
|
|
238
|
|
|
$serviceItem = $serviceManager->findItem( 'unitcode', [], 'service', 'delivery' ); |
239
|
|
|
$ordServItem = $ordServManager->createItem()->copyFrom( $serviceItem ); |
240
|
|
|
|
241
|
|
|
$ordBaseItem->setService( $ordServItem, \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY ); |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
$object = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
245
|
|
|
->setConstructorArgs( [$this->context] ) |
246
|
|
|
->setMethods( ['setService'] ) |
247
|
|
|
->getMock(); |
248
|
|
|
|
249
|
|
|
$object->expects( $this->once() )->method( 'setService' )->will( $this->throwException( new \Exception() ) ); |
250
|
|
|
|
251
|
|
|
$result = $this->access( 'copyServices' )->invokeArgs( $object, [$ordBaseItem, [], 'unittest|en|EUR'] ); |
252
|
|
|
|
253
|
|
|
$this->assertEquals( 0, count( $result ) ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param string $company |
259
|
|
|
*/ |
260
|
|
|
protected function getAddress( $company ) |
261
|
|
|
{ |
262
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( \TestHelperFrontend::getContext(), 'customer/address' ); |
263
|
|
|
|
264
|
|
|
$search = $manager->createSearch(); |
265
|
|
|
$search->setConditions( $search->compare( '==', 'customer.address.company', $company ) ); |
266
|
|
|
$items = $manager->searchItems( $search ); |
267
|
|
|
|
268
|
|
|
if( ( $item = reset( $items ) ) === false ) { |
269
|
|
|
throw new \RuntimeException( sprintf( 'No address item with company "%1$s" found', $company ) ); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$ordAddrManager = \Aimeos\MShop\Factory::createManager( \TestHelperFrontend::getContext(), 'order/base/address' ); |
273
|
|
|
$ordAddrItem = $ordAddrManager->createItem()->copyFrom( $item ); |
274
|
|
|
|
275
|
|
|
return $ordAddrItem; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
protected function access( $name ) |
280
|
|
|
{ |
281
|
|
|
$class = new \ReflectionClass( '\Aimeos\Controller\Frontend\Basket\Base' ); |
282
|
|
|
$method = $class->getMethod( $name ); |
283
|
|
|
$method->setAccessible( true ); |
284
|
|
|
|
285
|
|
|
return $method; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|