Total Complexity | 41 |
Total Lines | 534 |
Duplicated Lines | 0 % |
Changes | 24 | ||
Bugs | 1 | Features | 0 |
Complex classes like StandardTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StandardTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class StandardTest extends \PHPUnit\Framework\TestCase |
||
13 | { |
||
14 | private $object; |
||
15 | private $context; |
||
16 | private $testItem; |
||
17 | |||
18 | |||
19 | protected function setUp() : void |
||
20 | { |
||
21 | \Aimeos\MShop::cache( true ); |
||
22 | |||
23 | $this->context = \TestHelperFrontend::getContext(); |
||
24 | |||
25 | $manager = \Aimeos\MShop::create( $this->context, 'product' ); |
||
26 | $this->testItem = $manager->findItem( 'U:TESTP', ['attribute', 'media', 'price', 'product', 'text'] ); |
||
27 | |||
28 | $this->object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context ); |
||
29 | } |
||
30 | |||
31 | |||
32 | protected function tearDown() : void |
||
40 | } |
||
41 | |||
42 | |||
43 | public function testAdd() |
||
49 | } |
||
50 | |||
51 | |||
52 | public function testClear() |
||
53 | { |
||
54 | $this->object->addProduct( $this->testItem ); |
||
55 | |||
56 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $this->object->clear() ); |
||
57 | $this->assertEquals( 0, count( $this->object->get()->getProducts() ) ); |
||
58 | } |
||
59 | |||
60 | |||
61 | public function testGet() |
||
62 | { |
||
63 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $this->object->get() ); |
||
64 | } |
||
65 | |||
66 | |||
67 | public function testSave() |
||
68 | { |
||
69 | $stub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Base\Standard::class ) |
||
70 | ->setConstructorArgs( [$this->context] ) |
||
71 | ->setMethods( ['setSession'] ) |
||
72 | ->getMock(); |
||
73 | |||
74 | \Aimeos\MShop::inject( 'order/base', $stub ); |
||
75 | |||
76 | $stub->expects( $this->exactly( 2 ) )->method( 'setSession' ); |
||
77 | |||
78 | $object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context ); |
||
79 | $object->addProduct( $this->testItem ); |
||
80 | |||
81 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $object->save() ); |
||
82 | } |
||
83 | |||
84 | |||
85 | public function testSetType() |
||
86 | { |
||
87 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $this->object->setType( 'test' ) ); |
||
88 | } |
||
89 | |||
90 | |||
91 | public function testLoad() |
||
106 | } |
||
107 | |||
108 | |||
109 | public function testStore() |
||
110 | { |
||
135 | } |
||
136 | |||
137 | |||
138 | public function testStoreLimit() |
||
139 | { |
||
140 | $this->context->setEditor( 'core:lib/mshoplib' ); |
||
141 | $config = $this->context->getConfig(); |
||
142 | $config->set( 'controller/frontend/basket/limit-count', 0 ); |
||
143 | $config->set( 'controller/frontend/basket/limit-seconds', 86400 * 365 ); |
||
144 | |||
145 | $object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context ); |
||
146 | |||
147 | $this->expectException( \Aimeos\Controller\Frontend\Basket\Exception::class ); |
||
148 | $object->store(); |
||
149 | } |
||
150 | |||
151 | |||
152 | public function testAddDeleteProduct() |
||
168 | } |
||
169 | |||
170 | |||
171 | public function testAddProductFractionalQuantity() |
||
172 | { |
||
173 | $basket = $this->object->get(); |
||
|
|||
174 | $manager = \Aimeos\MShop::create( $this->context, 'product' ); |
||
175 | $item = $manager->findItem( 'CNC', ['attribute', 'media', 'price', 'product', 'text'] ); |
||
176 | $item->setConfig( ['quantity-step' => '0.1'] ); |
||
177 | |||
178 | $result1 = $this->object->addProduct( $item, 2.31 ); |
||
179 | $this->assertEquals( 2.4, $this->object->get()->getProduct( 0 )->getQuantity() ); |
||
180 | } |
||
181 | |||
182 | |||
183 | public function testAddProductCustomAttribute() |
||
184 | { |
||
185 | $attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'custom', [], 'product', 'date' ); |
||
186 | $attrValues = [$attrItem->getId() => '2000-01-01']; |
||
187 | |||
188 | $result = $this->object->addProduct( $this->testItem, 1, [], [], $attrValues ); |
||
189 | $basket = $this->object->get(); |
||
190 | |||
191 | $this->assertEquals( 1, count( $basket->getProducts() ) ); |
||
192 | $this->assertEquals( '2000-01-01', $basket->getProduct( 0 )->getAttribute( 'date', 'custom' ) ); |
||
193 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
194 | } |
||
195 | |||
196 | |||
197 | public function testAddProductCustomPrice() |
||
198 | { |
||
199 | $attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'custom', [], 'product', 'price' ); |
||
200 | $attrValues = [$attrItem->getId() => '0.01']; |
||
201 | |||
202 | $result = $this->object->addProduct( $this->testItem, 1, [], [], $attrValues ); |
||
203 | $basket = $this->object->get(); |
||
204 | |||
205 | $this->assertEquals( 1, count( $basket->getProducts() ) ); |
||
206 | $this->assertEquals( '0.01', $basket->getProduct( 0 )->getPrice()->getValue() ); |
||
207 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
208 | } |
||
209 | |||
210 | |||
211 | public function testAddProductCustomPriceException() |
||
212 | { |
||
213 | $attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'custom', [], 'product', 'price' ); |
||
214 | $attrValues = [$attrItem->getId() => ',']; |
||
215 | |||
216 | $this->expectException( \Aimeos\Controller\Frontend\Basket\Exception::class ); |
||
217 | $this->object->addProduct( $this->testItem, 1, [], [], $attrValues ); |
||
218 | } |
||
219 | |||
220 | |||
221 | public function testAddProductAttributePrice() |
||
222 | { |
||
223 | $attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'xs', [], 'product', 'size' ); |
||
224 | |||
225 | $result = $this->object->addProduct( $this->testItem, 1, [], [$attrItem->getId() => 2] ); |
||
226 | |||
227 | $this->assertEquals( '43.90', $this->object->get()->getPrice()->getValue() ); |
||
228 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
229 | } |
||
230 | |||
231 | |||
232 | public function testAddProductAttributeNotAssigned() |
||
233 | { |
||
234 | $attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( '30', [], 'product', 'width' ); |
||
235 | $ids = [$attrItem->getId()]; |
||
236 | |||
237 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
238 | $this->object->addProduct( $this->testItem, 1, [], $ids, $ids ); |
||
239 | } |
||
240 | |||
241 | |||
242 | public function testAddProductNegativeQuantityException() |
||
243 | { |
||
244 | $this->expectException( '\\Aimeos\\MShop\\Order\\Exception' ); |
||
245 | $this->object->addProduct( $this->testItem, -1 ); |
||
246 | } |
||
247 | |||
248 | |||
249 | public function testAddProductNoPriceException() |
||
250 | { |
||
251 | $item = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'MNOP' ); |
||
252 | |||
253 | $this->expectException( '\\Aimeos\\MShop\\Price\\Exception' ); |
||
254 | $this->object->addProduct( $item ); |
||
255 | } |
||
256 | |||
257 | |||
258 | public function testAddProductConfigAttributeException() |
||
259 | { |
||
260 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
261 | $this->object->addProduct( $this->testItem, 1, [], [-1] ); |
||
262 | } |
||
263 | |||
264 | |||
265 | public function testAddProductLowQuantityPriceException() |
||
266 | { |
||
267 | $item = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'IJKL', ['attribute', 'price'] ); |
||
268 | |||
269 | $this->expectException( '\\Aimeos\\MShop\\Price\\Exception' ); |
||
270 | $this->object->addProduct( $item ); |
||
271 | } |
||
272 | |||
273 | |||
274 | public function testAddProductHigherQuantities() |
||
275 | { |
||
276 | $item = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'IJKL', ['price'] ); |
||
277 | |||
278 | $result = $this->object->addProduct( $item, 2 ); |
||
279 | |||
280 | $this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() ); |
||
281 | $this->assertEquals( 'IJKL', $this->object->get()->getProduct( 0 )->getProductCode() ); |
||
282 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
283 | } |
||
284 | |||
285 | |||
286 | public function testAddProductOptionalParameters() |
||
287 | { |
||
288 | $item = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'IJKL', ['price'] ); |
||
289 | |||
290 | $product = $this->object->addProduct( $item, 2, [], [], [], 'stock', 'supplier', 123 )->get()->getProduct( 0 ); |
||
291 | |||
292 | $this->assertEquals( 'stock', $product->getStockType() ); |
||
293 | $this->assertEquals( 'supplier', $product->getSupplierCode() ); |
||
294 | $this->assertEquals( 123, $product->getSiteId() ); |
||
295 | } |
||
296 | |||
297 | |||
298 | public function testDeleteProductFlagError() |
||
299 | { |
||
300 | $this->object->addProduct( $this->testItem, 2 ); |
||
301 | |||
302 | $item = $this->object->get()->getProduct( 0 ); |
||
303 | $item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ); |
||
304 | |||
305 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
306 | $this->object->deleteProduct( 0 ); |
||
307 | } |
||
308 | |||
309 | |||
310 | public function testUpdateProduct() |
||
311 | { |
||
312 | $this->object->addProduct( $this->testItem ); |
||
313 | |||
314 | $item = $this->object->get()->getProduct( 0 ); |
||
315 | $this->assertEquals( 1, $item->getQuantity() ); |
||
316 | |||
317 | $result = $this->object->updateProduct( 0, 4 ); |
||
318 | $item = $this->object->get()->getProduct( 0 ); |
||
319 | |||
320 | $this->assertEquals( 4, $item->getQuantity() ); |
||
321 | $this->assertEquals( 'U:TESTP', $item->getProductCode() ); |
||
322 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
323 | } |
||
324 | |||
325 | |||
326 | public function testUpdateProductFlagError() |
||
327 | { |
||
328 | $this->object->addProduct( $this->testItem, 2 ); |
||
329 | |||
330 | $item = $this->object->get()->getProduct( 0 ); |
||
331 | $item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ); |
||
332 | |||
333 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
334 | $this->object->updateProduct( 0, 4 ); |
||
335 | } |
||
336 | |||
337 | |||
338 | public function testAddCoupon() |
||
339 | { |
||
340 | $this->object->addProduct( $this->testItem, 2 ); |
||
341 | |||
342 | $result = $this->object->addCoupon( 'GHIJ' ); |
||
343 | $basket = $this->object->get(); |
||
344 | |||
345 | $this->assertEquals( 1, count( $basket->getCoupons() ) ); |
||
346 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
347 | } |
||
348 | |||
349 | |||
350 | public function testAddCouponExceedCount() |
||
351 | { |
||
352 | $this->object->addProduct( $this->testItem, 2 ); |
||
353 | $this->object->addCoupon( 'GHIJ' ); |
||
354 | |||
355 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
356 | $this->object->addCoupon( 'GHIJ' ); |
||
357 | } |
||
358 | |||
359 | |||
360 | public function testAddCouponInvalidCode() |
||
361 | { |
||
362 | $this->expectException( \Aimeos\MShop\Plugin\Provider\Exception::class ); |
||
363 | $this->object->addCoupon( 'invalid' ); |
||
364 | } |
||
365 | |||
366 | |||
367 | public function testDeleteCoupon() |
||
368 | { |
||
369 | $this->object->addProduct( $this->testItem, 2 ); |
||
370 | $this->object->addCoupon( '90AB' ); |
||
371 | |||
372 | $result = $this->object->deleteCoupon( '90AB' ); |
||
373 | $basket = $this->object->get(); |
||
374 | |||
375 | $this->assertEquals( 0, count( $basket->getCoupons() ) ); |
||
376 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
377 | } |
||
378 | |||
379 | |||
380 | public function testAddAddress() |
||
381 | { |
||
382 | $values = array( |
||
383 | 'order.base.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>', |
||
384 | 'order.base.address.vatid' => 'DE999999999', |
||
385 | 'order.base.address.title' => '<br/>Dr.', |
||
386 | 'order.base.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR, |
||
387 | 'order.base.address.firstname' => 'firstunit', |
||
388 | 'order.base.address.lastname' => 'lastunit', |
||
389 | 'order.base.address.address1' => 'unit str.', |
||
390 | 'order.base.address.address2' => ' 166', |
||
391 | 'order.base.address.address3' => '4.OG', |
||
392 | 'order.base.address.postal' => '22769', |
||
393 | 'order.base.address.city' => 'Hamburg', |
||
394 | 'order.base.address.state' => 'Hamburg', |
||
395 | 'order.base.address.countryid' => 'de', |
||
396 | 'order.base.address.languageid' => 'de', |
||
397 | 'order.base.address.telephone' => '05554433221', |
||
398 | 'order.base.address.email' => '[email protected]', |
||
399 | 'order.base.address.telefax' => '05554433222', |
||
400 | 'order.base.address.website' => 'www.example.com', |
||
401 | ); |
||
402 | |||
403 | $result = $this->object->addAddress( 'payment', $values ); |
||
404 | $address = $this->object->get()->getAddress( 'payment', 0 ); |
||
405 | |||
406 | $this->assertEquals( 'Example company', $address->getCompany() ); |
||
407 | $this->assertEquals( 'Dr.', $address->getTitle() ); |
||
408 | $this->assertEquals( 'firstunit', $address->getFirstname() ); |
||
409 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
410 | } |
||
411 | |||
412 | |||
413 | public function testDeleteAddress() |
||
422 | } |
||
423 | |||
424 | |||
425 | |||
426 | public function testAddServicePayment() |
||
427 | { |
||
428 | $manager = \Aimeos\MShop::create( $this->context, 'service' ); |
||
429 | $service = $manager->findItem( 'unitpaymentcode', [], 'service', 'payment' ); |
||
430 | |||
431 | $this->object->addService( $service ); |
||
432 | $item = $this->object->get()->getService( 'payment', 0 )->getCode(); |
||
433 | $this->assertEquals( 'unitpaymentcode', $item ); |
||
434 | |||
435 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
436 | $this->object->addService( $service, ['prepay' => true] ); |
||
437 | } |
||
438 | |||
439 | |||
440 | public function testAddServiceDelivery() |
||
441 | { |
||
442 | $manager = \Aimeos\MShop::create( $this->context, 'service' ); |
||
443 | $service = $manager->findItem( 'unitcode', [], 'service', 'delivery' ); |
||
444 | |||
445 | $this->object->addService( $service ); |
||
446 | $item = $this->object->get()->getService( 'delivery', 0 ); |
||
447 | $this->assertEquals( 'unitcode', $item->getCode() ); |
||
448 | |||
449 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
450 | $this->object->addService( $service, ['fast shipping' => true, 'air shipping' => false] ); |
||
451 | } |
||
452 | |||
453 | |||
454 | public function testDeleteServices() |
||
464 | } |
||
465 | |||
466 | |||
467 | public function testDeleteServicePosition() |
||
468 | { |
||
469 | $manager = \Aimeos\MShop::create( $this->context, 'service' ); |
||
470 | $service = $manager->findItem( 'unitcode', [], 'service', 'delivery' ); |
||
471 | |||
472 | $this->assertSame( $this->object, $this->object->addService( $service ) ); |
||
473 | $this->assertEquals( 'unitcode', $this->object->get()->getService( 'delivery', 0 )->getCode() ); |
||
474 | |||
475 | $this->assertSame( $this->object, $this->object->deleteService( 'delivery', 0 ) ); |
||
476 | $this->assertEquals( 0, count( $this->object->get()->getService( 'delivery' ) ) ); |
||
477 | } |
||
478 | |||
479 | |||
480 | public function testCheckLocale() |
||
527 | } |
||
528 | |||
529 | |||
530 | /** |
||
531 | * @param string $company |
||
532 | */ |
||
533 | protected function getAddress( $company ) |
||
534 | { |
||
535 | $customer = \Aimeos\MShop\Customer\Manager\Factory::create( \TestHelperFrontend::getContext(), 'Standard' ); |
||
536 | $addressManager = $customer->getSubManager( 'address', 'Standard' ); |
||
537 | |||
546 | } |
||
547 | } |
||
548 |