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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
11 | class StandardTest extends \PHPUnit_Framework_TestCase |
||
12 | { |
||
13 | private $object; |
||
14 | private $context; |
||
15 | private $testItem; |
||
16 | |||
17 | |||
18 | protected function setUp() |
||
34 | |||
35 | |||
36 | protected function tearDown() |
||
43 | |||
44 | |||
45 | public function testAddDeleteProduct() |
||
76 | |||
77 | |||
78 | public function testAddProductBundle() |
||
98 | |||
99 | |||
100 | public function testAddProductVariant() |
||
130 | |||
131 | |||
132 | public function testAddProductVariantIncomplete() |
||
168 | |||
169 | |||
170 | public function testAddProductVariantNonUnique() |
||
203 | |||
204 | |||
205 | public function testAddProductVariantNotRequired() |
||
225 | |||
226 | |||
227 | public function testAddProductConfigAttribute() |
||
247 | |||
248 | |||
249 | public function testAddProductHiddenAttribute() |
||
284 | |||
285 | |||
286 | public function testAddProductCustomAttribute() |
||
311 | |||
312 | |||
313 | public function testAddProductAttributeNotAssigned() |
||
336 | |||
337 | |||
338 | public function testAddProductNegativeQuantityException() |
||
343 | |||
344 | |||
345 | public function testAddProductNotEnoughStockException() |
||
369 | |||
370 | |||
371 | public function testAddProductNoStockException() |
||
394 | |||
395 | |||
396 | public function testAddProductNoStockRequired() |
||
397 | { |
||
398 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() ); |
||
399 | |||
400 | $search = $productManager->createSearch(); |
||
401 | $search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) ); |
||
402 | |||
403 | $items = $productManager->searchItems( $search ); |
||
404 | |||
405 | if( ( $item = reset( $items ) ) === false ) { |
||
406 | throw new \Exception( 'Product not found' ); |
||
407 | } |
||
408 | |||
409 | $this->object->addProduct( $item->getId(), 5, array( 'stock' => false ) ); |
||
410 | } |
||
411 | |||
412 | |||
413 | public function testAddProductNoStockItem() |
||
414 | { |
||
415 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() ); |
||
416 | |||
417 | $search = $productManager->createSearch(); |
||
418 | $search->setConditions( $search->compare( '==', 'product.code', 'QRST' ) ); |
||
419 | |||
420 | $items = $productManager->searchItems( $search ); |
||
421 | |||
422 | if( ( $item = reset( $items ) ) === false ) { |
||
423 | throw new \Exception( 'Product not found' ); |
||
424 | } |
||
425 | |||
426 | $this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
427 | $this->object->addProduct( $item->getId(), 1 ); |
||
428 | } |
||
429 | |||
430 | |||
431 | public function testAddProductNoPriceException() |
||
432 | { |
||
433 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() ); |
||
434 | |||
435 | $search = $productManager->createSearch(); |
||
436 | $search->setConditions( $search->compare( '==', 'product.code', 'MNOP' ) ); |
||
437 | |||
438 | $items = $productManager->searchItems( $search ); |
||
439 | |||
440 | if( ( $item = reset( $items ) ) === false ) { |
||
441 | throw new \Exception( 'Product not found' ); |
||
442 | } |
||
443 | |||
444 | $this->setExpectedException( '\\Aimeos\\MShop\\Price\\Exception' ); |
||
445 | $this->object->addProduct( $item->getId(), 1 ); |
||
446 | } |
||
447 | |||
448 | |||
449 | public function testAddProductConfigAttributeException() |
||
454 | |||
455 | |||
456 | public function testAddProductEmptySelectionException() |
||
457 | { |
||
458 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() ); |
||
459 | $search = $productManager->createSearch(); |
||
460 | $search->setConditions( $search->compare( '==', 'product.code', 'U:noSel' ) ); |
||
461 | $items = $productManager->searchItems( $search ); |
||
462 | |||
463 | if( ( $item = reset( $items ) ) === false ) { |
||
464 | throw new \Exception( 'Product not found' ); |
||
465 | } |
||
466 | |||
467 | $this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
468 | $this->object->addProduct( $item->getId(), 1 ); |
||
469 | } |
||
470 | |||
471 | |||
472 | public function testAddProductSelectionWithPricelessItem() |
||
473 | { |
||
474 | $this->object->addProduct( $this->testItem->getId(), 1 ); |
||
475 | |||
476 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() ); |
||
477 | $search = $productManager->createSearch(); |
||
478 | |||
479 | $search->setConditions( $search->compare( '==', 'product.code', 'U:TESTPSUB01' ) ); |
||
480 | $items = $productManager->searchItems( $search ); |
||
481 | |||
482 | if( ( $item = reset( $items ) ) === false ) { |
||
483 | throw new \Exception( 'Product not found' ); |
||
484 | } |
||
485 | |||
486 | $this->assertEquals( 'U:TESTPSUB01', $this->object->get()->getProduct( 0 )->getProductCode() ); |
||
487 | } |
||
488 | |||
489 | |||
490 | public function testAddProductLowQuantityPriceException() |
||
491 | { |
||
492 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() ); |
||
493 | $search = $productManager->createSearch(); |
||
494 | $search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) ); |
||
495 | $items = $productManager->searchItems( $search ); |
||
496 | |||
497 | if( ( $item = reset( $items ) ) === false ) { |
||
498 | throw new \Exception( 'Product not found' ); |
||
499 | } |
||
500 | |||
501 | $this->setExpectedException( '\\Aimeos\\MShop\\Price\\Exception' ); |
||
502 | $this->object->addProduct( $item->getId(), 1 ); |
||
503 | } |
||
504 | |||
505 | |||
506 | public function testAddProductHigherQuantities() |
||
522 | |||
523 | |||
524 | public function testDeleteProductFlagError() |
||
525 | { |
||
526 | $this->object->addProduct( $this->testItem->getId(), 2 ); |
||
527 | |||
528 | $item = $this->object->get()->getProduct( 0 ); |
||
529 | $item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ); |
||
530 | |||
531 | $this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
532 | $this->object->deleteProduct( 0 ); |
||
533 | } |
||
534 | |||
535 | |||
536 | public function testEditProduct() |
||
549 | |||
550 | |||
551 | public function testEditProductAttributes() |
||
600 | |||
601 | |||
602 | public function testEditProductNotEnoughStock() |
||
603 | { |
||
604 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() ); |
||
605 | $search = $productManager->createSearch(); |
||
606 | $search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) ); |
||
607 | $items = $productManager->searchItems( $search ); |
||
608 | |||
609 | if( ( $item = reset( $items ) ) === false ) { |
||
610 | throw new \Exception( 'Product not found' ); |
||
611 | } |
||
612 | |||
613 | $this->object->addProduct( $item->getId(), 2, array(), array(), array(), array(), array(), 'unit_warehouse3' ); |
||
614 | |||
615 | $item = $this->object->get()->getProduct( 0 ); |
||
616 | $this->assertEquals( 2, $item->getQuantity() ); |
||
617 | |||
618 | try |
||
619 | { |
||
620 | $this->object->editProduct( 0, 5 ); |
||
621 | throw new \Exception( 'Expected exception not thrown' ); |
||
622 | } |
||
623 | catch( \Aimeos\Controller\Frontend\Basket\Exception $e ) |
||
624 | { |
||
625 | $item = $this->object->get()->getProduct( 0 ); |
||
626 | $this->assertEquals( 3, $item->getQuantity() ); |
||
627 | $this->assertEquals( 'IJKL', $item->getProductCode() ); |
||
628 | } |
||
629 | } |
||
630 | |||
631 | |||
632 | public function testEditProductNoStock() |
||
666 | |||
667 | |||
668 | public function testEditProductStockNotChecked() |
||
669 | { |
||
670 | $productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() ); |
||
671 | $search = $productManager->createSearch(); |
||
672 | $search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) ); |
||
673 | $items = $productManager->searchItems( $search ); |
||
674 | |||
675 | if( ( $item = reset( $items ) ) === false ) { |
||
676 | throw new \Exception( 'Product not found' ); |
||
677 | } |
||
678 | |||
679 | $this->object->addProduct( $item->getId(), 2, array(), array(), array(), array(), array(), 'unit_warehouse3' ); |
||
680 | |||
681 | $item = $this->object->get()->getProduct( 0 ); |
||
682 | $this->assertEquals( 2, $item->getQuantity() ); |
||
683 | |||
684 | $this->object->editProduct( 0, 5, array( 'stock' => false ) ); |
||
685 | |||
686 | $item = $this->object->get()->getProduct( 0 ); |
||
687 | $this->assertEquals( 5, $item->getQuantity() ); |
||
688 | $this->assertEquals( 'IJKL', $item->getProductCode() ); |
||
689 | } |
||
690 | |||
691 | |||
692 | public function testEditProductFlagError() |
||
693 | { |
||
694 | $this->object->addProduct( $this->testItem->getId(), 2 ); |
||
695 | |||
696 | $item = $this->object->get()->getProduct( 0 ); |
||
697 | $item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ); |
||
698 | |||
699 | $this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
700 | $this->object->editProduct( 0, 4 ); |
||
701 | } |
||
702 | |||
703 | |||
704 | public function testAddCoupon() |
||
705 | { |
||
706 | $this->object->addProduct( $this->testItem->getId(), 2 ); |
||
707 | $this->object->addCoupon( 'GHIJ' ); |
||
708 | |||
709 | $basket = $this->object->get(); |
||
710 | |||
711 | $this->assertEquals( 1, count( $basket->getCoupons() ) ); |
||
712 | } |
||
713 | |||
714 | |||
715 | public function testAddCouponInvalidCode() |
||
720 | |||
721 | |||
722 | public function testAddCouponMissingRequirements() |
||
727 | |||
728 | |||
729 | public function testDeleteCoupon() |
||
730 | { |
||
731 | $this->object->addProduct( $this->testItem->getId(), 2 ); |
||
732 | $this->object->addCoupon( '90AB' ); |
||
733 | $this->object->deleteCoupon( '90AB' ); |
||
734 | |||
735 | $basket = $this->object->get(); |
||
736 | |||
737 | $this->assertEquals( 0, count( $basket->getCoupons() ) ); |
||
738 | } |
||
739 | |||
740 | |||
741 | public function testClear() |
||
748 | |||
749 | |||
750 | public function testSetAddressDelete() |
||
757 | |||
758 | |||
759 | public function testSetBillingAddressByItem() |
||
760 | { |
||
761 | $item = $this->getAddress( 'Example company' ); |
||
762 | |||
763 | $this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, $item ); |
||
764 | |||
765 | $address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
||
766 | $this->assertEquals( 'Example company', $address->getCompany() ); |
||
767 | } |
||
768 | |||
769 | |||
770 | public function testSetBillingAddressByArray() |
||
771 | { |
||
772 | $fixture = array( |
||
773 | 'order.base.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>', |
||
774 | 'order.base.address.vatid' => 'DE999999999', |
||
775 | 'order.base.address.title' => '<br/>Dr.', |
||
776 | 'order.base.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR, |
||
777 | 'order.base.address.firstname' => 'firstunit', |
||
778 | 'order.base.address.lastname' => 'lastunit', |
||
779 | 'order.base.address.address1' => 'unit str.', |
||
780 | 'order.base.address.address2' => ' 166', |
||
781 | 'order.base.address.address3' => '4.OG', |
||
782 | 'order.base.address.postal' => '22769', |
||
783 | 'order.base.address.city' => 'Hamburg', |
||
784 | 'order.base.address.state' => 'Hamburg', |
||
785 | 'order.base.address.countryid' => 'de', |
||
786 | 'order.base.address.languageid' => 'de', |
||
787 | 'order.base.address.telephone' => '05554433221', |
||
788 | 'order.base.address.email' => '[email protected]', |
||
789 | 'order.base.address.telefax' => '05554433222', |
||
790 | 'order.base.address.website' => 'www.example.com', |
||
791 | 'order.base.address.flag' => 0, |
||
792 | ); |
||
793 | |||
794 | $this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, $fixture ); |
||
795 | |||
796 | $address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
||
797 | $this->assertEquals( 'Example company', $address->getCompany() ); |
||
798 | $this->assertEquals( 'Dr.', $address->getTitle() ); |
||
799 | $this->assertEquals( 'firstunit', $address->getFirstname() ); |
||
800 | } |
||
801 | |||
802 | |||
803 | public function testSetBillingAddressByArrayError() |
||
808 | |||
809 | |||
810 | public function testSetBillingAddressParameterError() |
||
815 | |||
816 | |||
817 | public function testSetDeliveryAddressByItem() |
||
818 | { |
||
819 | $item = $this->getAddress( 'Example company' ); |
||
820 | |||
821 | $this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY, $item ); |
||
822 | |||
823 | $address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY ); |
||
824 | $this->assertEquals( 'Example company', $address->getCompany() ); |
||
825 | } |
||
826 | |||
827 | |||
828 | public function testSetDeliveryAddressByArray() |
||
829 | { |
||
830 | $fixture = array( |
||
831 | 'order.base.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>', |
||
832 | 'order.base.address.vatid' => 'DE999999999', |
||
833 | 'order.base.address.title' => '<br/>Dr.', |
||
834 | 'order.base.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR, |
||
835 | 'order.base.address.firstname' => 'firstunit', |
||
836 | 'order.base.address.lastname' => 'lastunit', |
||
837 | 'order.base.address.address1' => 'unit str.', |
||
838 | 'order.base.address.address2' => ' 166', |
||
839 | 'order.base.address.address3' => '4.OG', |
||
840 | 'order.base.address.postal' => '22769', |
||
841 | 'order.base.address.city' => 'Hamburg', |
||
842 | 'order.base.address.state' => 'Hamburg', |
||
843 | 'order.base.address.countryid' => 'de', |
||
844 | 'order.base.address.languageid' => 'de', |
||
845 | 'order.base.address.telephone' => '05554433221', |
||
846 | 'order.base.address.email' => '[email protected]', |
||
847 | 'order.base.address.telefax' => '05554433222', |
||
848 | 'order.base.address.website' => 'www.example.com', |
||
849 | 'order.base.address.flag' => 0, |
||
850 | ); |
||
851 | $this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY, $fixture ); |
||
852 | |||
853 | $address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY ); |
||
854 | $this->assertEquals( 'Example company', $address->getCompany() ); |
||
855 | $this->assertEquals( 'Dr.', $address->getTitle() ); |
||
856 | $this->assertEquals( 'firstunit', $address->getFirstname() ); |
||
857 | } |
||
858 | |||
859 | |||
860 | public function testSetDeliveryAddressByArrayError() |
||
865 | |||
866 | |||
867 | public function testSetDeliveryAddressTypeError() |
||
872 | |||
873 | |||
874 | public function testSetServicePayment() |
||
875 | { |
||
876 | $service = $this->getService( 'unitpaymentcode' ); |
||
877 | |||
878 | $this->object->setService( 'payment', $service->getId(), array() ); |
||
879 | $this->assertEquals( 'unitpaymentcode', $this->object->get()->getService( 'payment' )->getCode() ); |
||
880 | |||
881 | $this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
882 | $this->object->setService( 'payment', $service->getId(), array( 'prepay' => true ) ); |
||
883 | } |
||
884 | |||
885 | |||
886 | public function testSetDeliveryOption() |
||
887 | { |
||
888 | $service = $this->getService( 'unitcode' ); |
||
889 | |||
890 | $this->object->setService( 'delivery', $service->getId(), array() ); |
||
891 | $this->assertEquals( 'unitcode', $this->object->get()->getService( 'delivery' )->getCode() ); |
||
892 | |||
893 | $this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
894 | $this->object->setService( 'delivery', $service->getId(), array( 'fast shipping' => true, 'air shipping' => false ) ); |
||
895 | } |
||
896 | |||
897 | |||
898 | public function testCheckLocale() |
||
942 | |||
943 | |||
944 | /** |
||
945 | * @param string $company |
||
946 | */ |
||
947 | protected function getAddress( $company ) |
||
962 | |||
963 | |||
964 | /** |
||
965 | * @param string $code |
||
966 | */ |
||
967 | protected function getService( $code ) |
||
982 | } |
||
983 |