From cede04f014733bffc5104f2e26579e8ce242f462 Mon Sep 17 00:00:00 2001 From: Andriy Kravets Date: Tue, 27 Nov 2018 18:33:00 +0200 Subject: [PATCH 1/8] Fixed bug, when exception occurred on order with coupons cancel, made by guest after creating of customer account. --- .../Sales/Model/Order/CustomerAssignment.php | 58 ++++ .../AssignOrderToCustomerObserver.php | 23 +- ...onDataAfterOrderCustomerAssignObserver.php | 52 ++++ app/code/Magento/SalesRule/etc/events.xml | 3 + ...CouponDataAfterOrderCustomerAssignTest.php | 269 ++++++++++++++++++ 5 files changed, 398 insertions(+), 7 deletions(-) create mode 100644 app/code/Magento/Sales/Model/Order/CustomerAssignment.php create mode 100644 app/code/Magento/SalesRule/Observer/AssignCouponDataAfterOrderCustomerAssignObserver.php create mode 100644 dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php diff --git a/app/code/Magento/Sales/Model/Order/CustomerAssignment.php b/app/code/Magento/Sales/Model/Order/CustomerAssignment.php new file mode 100644 index 0000000000000..b6ae86461c398 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/CustomerAssignment.php @@ -0,0 +1,58 @@ +eventManager = $eventManager; + $this->orderRepository = $orderRepository; + } + + /** + * @param OrderInterface $order + * @param CustomerInterface $customer + */ + public function execute(OrderInterface $order, CustomerInterface $customer)/*: void*/ + { + $order->setCustomerId($customer->getId()); + $order->setCustomerIsGuest(false); + $this->orderRepository->save($order); + + $this->eventManager->dispatch( + 'sales_order_customer_assign_after', [ + 'order' => $order, + 'customer' => $customer + ] + ); + } +} diff --git a/app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php b/app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php index f41ea6888264f..aba7d551d5330 100644 --- a/app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php +++ b/app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php @@ -12,6 +12,7 @@ use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Sales\Api\OrderRepositoryInterface; +use Magento\Sales\Model\Order\CustomerAssignment; /** * Assign order to customer created after issuing guest order. @@ -24,11 +25,22 @@ class AssignOrderToCustomerObserver implements ObserverInterface private $orderRepository; /** + * @var CustomerAssignment + */ + private $customerAssignmentService; + + /** + * AssignOrderToCustomerObserver constructor. + * * @param OrderRepositoryInterface $orderRepository + * @param CustomerAssignment $customerAssignmentService */ - public function __construct(OrderRepositoryInterface $orderRepository) - { + public function __construct( + OrderRepositoryInterface $orderRepository, + CustomerAssignment $customerAssignmentService + ) { $this->orderRepository = $orderRepository; + $this->customerAssignmentService = $customerAssignmentService; } /** @@ -44,11 +56,8 @@ public function execute(Observer $observer) if (array_key_exists('__sales_assign_order_id', $delegateData)) { $orderId = $delegateData['__sales_assign_order_id']; $order = $this->orderRepository->get($orderId); - if (!$order->getCustomerId()) { - //if customer ID wasn't already assigned then assigning. - $order->setCustomerId($customer->getId()); - $order->setCustomerIsGuest(0); - $this->orderRepository->save($order); + if (!$order->getCustomerId() && $customer->getId()) { + $this->customerAssignmentService->execute($order, $customer); } } } diff --git a/app/code/Magento/SalesRule/Observer/AssignCouponDataAfterOrderCustomerAssignObserver.php b/app/code/Magento/SalesRule/Observer/AssignCouponDataAfterOrderCustomerAssignObserver.php new file mode 100644 index 0000000000000..d9699d334ff6a --- /dev/null +++ b/app/code/Magento/SalesRule/Observer/AssignCouponDataAfterOrderCustomerAssignObserver.php @@ -0,0 +1,52 @@ +updateCouponUsages = $updateCouponUsages; + } + + /** + * @inheritDoc + */ + public function execute(Observer $observer) + { + $event = $observer->getEvent(); + /** @var OrderInterface $order */ + $order = $event->getData(self::EVENT_KEY_ORDER); + + if ($order->getCustomerId()) { + $this->updateCouponUsages->execute($order, true); + } + } +} diff --git a/app/code/Magento/SalesRule/etc/events.xml b/app/code/Magento/SalesRule/etc/events.xml index 8261860bbb7ce..eec0da74f619e 100644 --- a/app/code/Magento/SalesRule/etc/events.xml +++ b/app/code/Magento/SalesRule/etc/events.xml @@ -24,4 +24,7 @@ + + + diff --git a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php new file mode 100644 index 0000000000000..d04423daabdf9 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php @@ -0,0 +1,269 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class); + $this->orderRepository = $this->objectManager->get(Magento\Sales\Model\OrderRepository::class); + $this->delegateCustomerService = $this->objectManager->get(Order\OrderCustomerDelegate::class); + $this->customerRepository = $this->objectManager->get(\Magento\Customer\Api\CustomerRepositoryInterface::class); + $this->ruleCustomerFactory = $this->objectManager->get(Magento\SalesRule\Model\Rule\CustomerFactory::class);; + $this->assignCouponToCustomerObserver = $this->objectManager->get( + Magento\SalesRule\Observer\AssignCouponDataAfterOrderCustomerAssignObserver::class + ); + } + + /** + * @magentoAppIsolation enabled + * @magentoDataFixture Magento/Sales/_files/order.php + */ + public function testCouponDataHasBeenAssignedTest() + { + $rule = $this->prepareSalesRule(); + $coupon = $this->attachSalesruleCoupon($rule); + + $order = $this->makeOrderWithCouponAsGuest($coupon); + $this->delegateOrderToBeAssigned($order); + + $customer = $this->registerNewCustomer(); + $ruleCustomer = $this->getSalesruleCustomerUsage($customer, $rule); + + // Assert, that rule customer model has been created for specific customer + $this->assertEquals( + $ruleCustomer->getCustomerId(), + $customer->getId() + ); + + // Assert, that customer has increased coupon usage of specific rule + $this->assertEquals( + 1, + $ruleCustomer->getTimesUsed() + ); + } + + /** + * @magentoAppIsolation enabled + * @magentoDataFixture Magento/Sales/_files/order.php + */ + public function testOrderCancelingDecreasesCouponUsages() + { + $rule = $this->prepareSalesRule(); + $coupon = $this->attachSalesruleCoupon($rule); + + $order = $this->makeOrderWithCouponAsGuest($coupon); + $this->delegateOrderToBeAssigned($order); + + $customer = $this->registerNewCustomer(); + + $order->setCustomerId($customer->getId()); + $this->processOrder($order); + + // Should not throw exception as bux is fixed now + $this->cancelOrder($order); + $ruleCustomer = $this->getSalesruleCustomerUsage($customer, $rule); + + // Assert, that rule customer model has been created for specific customer + $this->assertEquals( + $ruleCustomer->getCustomerId(), + $customer->getId() + ); + + // Assert, that customer has increased coupon usage of specific rule + $this->assertEquals( + 0, + $ruleCustomer->getTimesUsed() + ); + + } + + /** + * @param Order $order + * @return \Magento\Sales\Api\Data\OrderInterface + */ + private function processOrder(Order $order) + { + $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING); + $order->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING); + return $this->orderRepository->save($order); + } + + /** + * @param Order $order + */ + private function cancelOrder(Order $order) + { + $order->cancel(); + } + + /** + * @param Customer $customer + * @param Rule $rule + * @return Rule\Customer + */ + private function getSalesruleCustomerUsage(Customer $customer, Rule $rule) : Magento\SalesRule\Model\Rule\Customer + { + $ruleCustomer = $this->ruleCustomerFactory->create(); + return $ruleCustomer->loadByCustomerRule($customer->getId(), $rule->getRuleId()); + } + + /** + * @return Rule + */ + private function prepareSalesRule() : Rule + { + /** @var Rule $salesRule */ + $salesRule = $this->objectManager->create(Rule::class); + $salesRule->setData( + [ + 'name' => '15$ fixed discount on whole cart', + 'is_active' => 1, + 'customer_group_ids' => [GroupManagement::NOT_LOGGED_IN_ID], + 'coupon_type' => Rule::COUPON_TYPE_SPECIFIC, + 'conditions' => [ + [ + 'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class, + 'attribute' => 'base_subtotal', + 'operator' => '>', + 'value' => 45, + ], + ], + 'simple_action' => Rule::CART_FIXED_ACTION, + 'discount_amount' => 15, + 'discount_step' => 0, + 'stop_rules_processing' => 1, + 'website_ids' => [ + $this->objectManager->get(StoreManagerInterface::class)->getWebsite()->getId(), + ], + ] + ); + $this->objectManager->get( + \Magento\SalesRule\Model\ResourceModel\Rule::class + )->save($salesRule); + + return $salesRule; + } + + /** + * @param Rule $salesRule + * @return Coupon + */ + private function attachSalesruleCoupon(Rule $salesRule) : Coupon + { + $coupon = $this->objectManager->create(Coupon::class); + $coupon->setRuleId($salesRule->getId()) + ->setCode('CART_FIXED_DISCOUNT_15') + ->setType(0); + + $this->objectManager->get(CouponRepositoryInterface::class)->save($coupon); + + return $coupon; + } + + /** + * @param Coupon $coupon + * @return Order + */ + private function makeOrderWithCouponAsGuest(Coupon $coupon) : Order + { + $order = $this->objectManager->create(\Magento\Sales\Model\Order::class); + $order->loadByIncrementId('100000001') + ->setCustomerIsGuest(true) + ->setCouponCode($coupon->getCode()) + ->setCreatedAt('2014-10-25 10:10:10') + ->setAppliedRuleIds($coupon->getRuleId()) + ->save(); + + return $order; + } + + /** + * @param Order $order + */ + private function delegateOrderToBeAssigned(Order $order) + { + $this->delegateCustomerService->delegateNew($order->getId()); + } + + /** + * @return Customer + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\State\InputMismatchException + */ + private function registerNewCustomer() : Customer + { + $customer = $this->objectManager->create( + \Magento\Customer\Api\Data\CustomerInterface::class + ); + + /** @var Magento\Customer\Api\Data\CustomerInterface $customer */ + $customer->setWebsiteId(1) + ->setEmail('customer@example.com') + ->setGroupId(1) + ->setStoreId(1) + ->setPrefix('Mr.') + ->setFirstname('John') + ->setMiddlename('A') + ->setLastname('Smith') + ->setSuffix('Esq.') + ->setDefaultBilling(1) + ->setDefaultShipping(1) + ->setTaxvat('12') + ->setGender(0); + + $customer = $this->customerRepository->save($customer, 'password'); + + return $customer; + } +} + From 11480e8b44d26ecf8e692c1a3406423b1532031f Mon Sep 17 00:00:00 2001 From: Andriy Kravets Date: Tue, 4 Dec 2018 19:44:54 +0200 Subject: [PATCH 2/8] Refactored Unit-tests, Integration tests --- .../AssignOrderToCustomerObserver.php | 10 +- .../AssignOrderToCustomerObserverTest.php | 20 +++- ...CouponDataAfterOrderCustomerAssignTest.php | 107 ++++++++++++------ 3 files changed, 90 insertions(+), 47 deletions(-) diff --git a/app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php b/app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php index aba7d551d5330..9857fa39fa51a 100644 --- a/app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php +++ b/app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php @@ -27,20 +27,20 @@ class AssignOrderToCustomerObserver implements ObserverInterface /** * @var CustomerAssignment */ - private $customerAssignmentService; + private $assignmentService; /** * AssignOrderToCustomerObserver constructor. * * @param OrderRepositoryInterface $orderRepository - * @param CustomerAssignment $customerAssignmentService + * @param CustomerAssignment $assignmentService */ public function __construct( OrderRepositoryInterface $orderRepository, - CustomerAssignment $customerAssignmentService + CustomerAssignment $assignmentService ) { $this->orderRepository = $orderRepository; - $this->customerAssignmentService = $customerAssignmentService; + $this->assignmentService = $assignmentService; } /** @@ -57,7 +57,7 @@ public function execute(Observer $observer) $orderId = $delegateData['__sales_assign_order_id']; $order = $this->orderRepository->get($orderId); if (!$order->getCustomerId() && $customer->getId()) { - $this->customerAssignmentService->execute($order, $customer); + $this->assignmentService->execute($order, $customer); } } } diff --git a/app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php b/app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php index c6e02151b9bc1..e49eba8880324 100644 --- a/app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php +++ b/app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php @@ -12,6 +12,7 @@ use Magento\Framework\Event\Observer; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\OrderRepositoryInterface; +use Magento\Sales\Model\Order\CustomerAssignment; use Magento\Sales\Observer\AssignOrderToCustomerObserver; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject; @@ -27,6 +28,9 @@ class AssignOrderToCustomerObserverTest extends TestCase /** @var OrderRepositoryInterface|PHPUnit_Framework_MockObject_MockObject */ protected $orderRepositoryMock; + /** @var CustomerAssignment | PHPUnit_Framework_MockObject_MockObject */ + protected $customerAssignmentMock; + /** * Set Up */ @@ -35,7 +39,12 @@ protected function setUp() $this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class) ->disableOriginalConstructor() ->getMock(); - $this->sut = new AssignOrderToCustomerObserver($this->orderRepositoryMock); + + $this->customerAssignmentMock = $this->getMockBuilder(CustomerAssignment::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->sut = new AssignOrderToCustomerObserver($this->orderRepositoryMock, $this->customerAssignmentMock); } /** @@ -69,13 +78,12 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId) $orderMock->expects($this->once())->method('getCustomerId')->willReturn($customerId); $this->orderRepositoryMock->expects($this->once())->method('get')->with($orderId) ->willReturn($orderMock); - if (!$customerId) { - $this->orderRepositoryMock->expects($this->once())->method('save')->with($orderMock); - $this->sut->execute($observerMock); - return ; + if ($customerId) { + $this->customerAssignmentMock->expects($this->once())->method('execute')->with($orderMock, $customerMock); + } else { + $this->customerAssignmentMock->expects($this->never())->method('execute'); } - $this->orderRepositoryMock->expects($this->never())->method('save')->with($orderMock); $this->sut->execute($observerMock); } diff --git a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php index d04423daabdf9..f110635a48f8c 100644 --- a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php +++ b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php @@ -8,10 +8,20 @@ use Magento\SalesRule\Model\Rule; use Magento\Store\Model\StoreManagerInterface; use Magento\Customer\Model\Data\Customer; +use Magento\TestFramework\Helper\Bootstrap; - +/** + * Class AssignCouponDataAfterOrderCustomerAssignTest + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\TestCase { + /** + * @var \Magento\Framework\ObjectManagerInterface + */ + private $objectManager; + /** * @var \Magento\Quote\Api\GuestCartManagementInterface */ @@ -22,11 +32,6 @@ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\Te */ private $orderRepository; - /** - * @var \Magento\Framework\ObjectManagerInterface - */ - private $objectManager; - /** * @var \Magento\Framework\Event\ManagerInterface */ @@ -48,11 +53,32 @@ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\Te private $ruleCustomerFactory; /** - * @inheritdoc + * @var Rule */ - protected function setUp() + private $salesRule; + + /** + * @var Coupon + */ + private $coupon; + + /** + * @var Order + */ + private $order; + + /** + * @var Customer + */ + private $customer; + + /** + * AssignCouponDataAfterOrderCustomerAssignTest constructor. + */ + public function __construct($name = null, array $data = [], $dataName = '') { - $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + parent::__construct($name, $data, $dataName); + $this->objectManager = Bootstrap::getObjectManager(); $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class); $this->orderRepository = $this->objectManager->get(Magento\Sales\Model\OrderRepository::class); $this->delegateCustomerService = $this->objectManager->get(Order\OrderCustomerDelegate::class); @@ -63,25 +89,44 @@ protected function setUp() ); } + /** + * @inheritdoc + */ + protected function setUp() + { + $this->salesRule = $this->prepareSalesRule(); + $this->coupon = $this->attachSalesruleCoupon($this->salesRule); + $this->order = $this->makeOrderWithCouponAsGuest($this->coupon); + $this->delegateOrderToBeAssigned($this->order); + $this->customer = $this->registerNewCustomer(); + $this->order->setCustomerId($this->customer->getId()); + } + + /** + * @inheritdoc + */ + protected function tearDown() + { + unset( + $this->order, + $this->coupon, + $this->customer, + $this->salesRule + ); + } + /** * @magentoAppIsolation enabled * @magentoDataFixture Magento/Sales/_files/order.php */ public function testCouponDataHasBeenAssignedTest() { - $rule = $this->prepareSalesRule(); - $coupon = $this->attachSalesruleCoupon($rule); - - $order = $this->makeOrderWithCouponAsGuest($coupon); - $this->delegateOrderToBeAssigned($order); - - $customer = $this->registerNewCustomer(); - $ruleCustomer = $this->getSalesruleCustomerUsage($customer, $rule); + $ruleCustomer = $this->getSalesruleCustomerUsage($this->customer, $this->salesRule); // Assert, that rule customer model has been created for specific customer $this->assertEquals( $ruleCustomer->getCustomerId(), - $customer->getId() + $this->customer->getId() ); // Assert, that customer has increased coupon usage of specific rule @@ -97,25 +142,16 @@ public function testCouponDataHasBeenAssignedTest() */ public function testOrderCancelingDecreasesCouponUsages() { - $rule = $this->prepareSalesRule(); - $coupon = $this->attachSalesruleCoupon($rule); - - $order = $this->makeOrderWithCouponAsGuest($coupon); - $this->delegateOrderToBeAssigned($order); - - $customer = $this->registerNewCustomer(); - - $order->setCustomerId($customer->getId()); - $this->processOrder($order); + $this->processOrder($this->order); // Should not throw exception as bux is fixed now - $this->cancelOrder($order); - $ruleCustomer = $this->getSalesruleCustomerUsage($customer, $rule); + $this->cancelOrder($this->order); + $ruleCustomer = $this->getSalesruleCustomerUsage($this->customer, $this->salesRule); // Assert, that rule customer model has been created for specific customer $this->assertEquals( $ruleCustomer->getCustomerId(), - $customer->getId() + $this->customer->getId() ); // Assert, that customer has increased coupon usage of specific rule @@ -123,7 +159,6 @@ public function testOrderCancelingDecreasesCouponUsages() 0, $ruleCustomer->getTimesUsed() ); - } /** @@ -186,7 +221,7 @@ private function prepareSalesRule() : Rule ], ] ); - $this->objectManager->get( + Bootstrap::getObjectManager()->get( \Magento\SalesRule\Model\ResourceModel\Rule::class )->save($salesRule); @@ -204,7 +239,7 @@ private function attachSalesruleCoupon(Rule $salesRule) : Coupon ->setCode('CART_FIXED_DISCOUNT_15') ->setType(0); - $this->objectManager->get(CouponRepositoryInterface::class)->save($coupon); + Bootstrap::getObjectManager()->get(CouponRepositoryInterface::class)->save($coupon); return $coupon; } @@ -215,7 +250,7 @@ private function attachSalesruleCoupon(Rule $salesRule) : Coupon */ private function makeOrderWithCouponAsGuest(Coupon $coupon) : Order { - $order = $this->objectManager->create(\Magento\Sales\Model\Order::class); + $order = Bootstrap::getObjectManager()->create(\Magento\Sales\Model\Order::class); $order->loadByIncrementId('100000001') ->setCustomerIsGuest(true) ->setCouponCode($coupon->getCode()) @@ -242,7 +277,7 @@ private function delegateOrderToBeAssigned(Order $order) */ private function registerNewCustomer() : Customer { - $customer = $this->objectManager->create( + $customer = Bootstrap::getObjectManager()->create( \Magento\Customer\Api\Data\CustomerInterface::class ); From 10fbf4041c98b12ee4bb19f09f04efc7856dfa60 Mon Sep 17 00:00:00 2001 From: Andriy Kravets Date: Wed, 5 Dec 2018 16:07:17 +0200 Subject: [PATCH 3/8] Static test improvement. --- .../Sales/Model/Order/CustomerAssignment.php | 3 ++- .../AssignOrderToCustomerObserverTest.php | 14 +++++----- ...CouponDataAfterOrderCustomerAssignTest.php | 26 +++++++------------ 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/CustomerAssignment.php b/app/code/Magento/Sales/Model/Order/CustomerAssignment.php index b6ae86461c398..8bcfc1dc49de4 100644 --- a/app/code/Magento/Sales/Model/Order/CustomerAssignment.php +++ b/app/code/Magento/Sales/Model/Order/CustomerAssignment.php @@ -49,7 +49,8 @@ public function execute(OrderInterface $order, CustomerInterface $customer)/*: v $this->orderRepository->save($order); $this->eventManager->dispatch( - 'sales_order_customer_assign_after', [ + 'sales_order_customer_assign_after', + [ 'order' => $order, 'customer' => $customer ] diff --git a/app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php b/app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php index e49eba8880324..18371274049e3 100644 --- a/app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php +++ b/app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php @@ -29,7 +29,7 @@ class AssignOrderToCustomerObserverTest extends TestCase protected $orderRepositoryMock; /** @var CustomerAssignment | PHPUnit_Framework_MockObject_MockObject */ - protected $customerAssignmentMock; + protected $assignmentMock; /** * Set Up @@ -40,11 +40,11 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); - $this->customerAssignmentMock = $this->getMockBuilder(CustomerAssignment::class) + $this->assignmentMock = $this->getMockBuilder(CustomerAssignment::class) ->disableOriginalConstructor() ->getMock(); - $this->sut = new AssignOrderToCustomerObserver($this->orderRepositoryMock, $this->customerAssignmentMock); + $this->sut = new AssignOrderToCustomerObserver($this->orderRepositoryMock, $this->assignmentMock); } /** @@ -78,12 +78,14 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId) $orderMock->expects($this->once())->method('getCustomerId')->willReturn($customerId); $this->orderRepositoryMock->expects($this->once())->method('get')->with($orderId) ->willReturn($orderMock); + if ($customerId) { - $this->customerAssignmentMock->expects($this->once())->method('execute')->with($orderMock, $customerMock); - } else { - $this->customerAssignmentMock->expects($this->never())->method('execute'); + $this->assignmentMock->expects($this->once())->method('execute')->with($orderMock, $customerMock); + $this->sut->execute($observerMock); + return; } + $this->assignmentMock->expects($this->never())->method('execute'); $this->sut->execute($observerMock); } diff --git a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php index f110635a48f8c..afbb938c740b5 100644 --- a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php +++ b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php @@ -1,7 +1,8 @@ objectManager = Bootstrap::getObjectManager(); $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class); - $this->orderRepository = $this->objectManager->get(Magento\Sales\Model\OrderRepository::class); + $this->orderRepository = $this->objectManager->get(\Magento\Sales\Model\OrderRepository::class); $this->delegateCustomerService = $this->objectManager->get(Order\OrderCustomerDelegate::class); $this->customerRepository = $this->objectManager->get(\Magento\Customer\Api\CustomerRepositoryInterface::class); - $this->ruleCustomerFactory = $this->objectManager->get(Magento\SalesRule\Model\Rule\CustomerFactory::class);; + $this->ruleCustomerFactory = $this->objectManager->get(\Magento\SalesRule\Model\Rule\CustomerFactory::class); $this->assignCouponToCustomerObserver = $this->objectManager->get( - Magento\SalesRule\Observer\AssignCouponDataAfterOrderCustomerAssignObserver::class + \Magento\SalesRule\Observer\AssignCouponDataAfterOrderCustomerAssignObserver::class ); } @@ -116,7 +119,6 @@ protected function tearDown() } /** - * @magentoAppIsolation enabled * @magentoDataFixture Magento/Sales/_files/order.php */ public function testCouponDataHasBeenAssignedTest() @@ -137,7 +139,6 @@ public function testCouponDataHasBeenAssignedTest() } /** - * @magentoAppIsolation enabled * @magentoDataFixture Magento/Sales/_files/order.php */ public function testOrderCancelingDecreasesCouponUsages() @@ -145,7 +146,7 @@ public function testOrderCancelingDecreasesCouponUsages() $this->processOrder($this->order); // Should not throw exception as bux is fixed now - $this->cancelOrder($this->order); + $this->order->cancel(); $ruleCustomer = $this->getSalesruleCustomerUsage($this->customer, $this->salesRule); // Assert, that rule customer model has been created for specific customer @@ -172,20 +173,12 @@ private function processOrder(Order $order) return $this->orderRepository->save($order); } - /** - * @param Order $order - */ - private function cancelOrder(Order $order) - { - $order->cancel(); - } - /** * @param Customer $customer * @param Rule $rule * @return Rule\Customer */ - private function getSalesruleCustomerUsage(Customer $customer, Rule $rule) : Magento\SalesRule\Model\Rule\Customer + private function getSalesruleCustomerUsage(Customer $customer, Rule $rule) : \Magento\SalesRule\Model\Rule\Customer { $ruleCustomer = $this->ruleCustomerFactory->create(); return $ruleCustomer->loadByCustomerRule($customer->getId(), $rule->getRuleId()); @@ -301,4 +294,3 @@ private function registerNewCustomer() : Customer return $customer; } } - From 1e2f3fbd5481b3eee6d7c049703c0d51b849c439 Mon Sep 17 00:00:00 2001 From: Andriy Kravets Date: Fri, 7 Dec 2018 11:14:47 +0200 Subject: [PATCH 4/8] Added supression of static method access. --- .../AssignCouponDataAfterOrderCustomerAssignTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php index afbb938c740b5..47498ef3c51ba 100644 --- a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php +++ b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php @@ -17,6 +17,7 @@ * @magentoAppIsolation enabled * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.StaticAccess) */ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\TestCase { @@ -168,8 +169,8 @@ public function testOrderCancelingDecreasesCouponUsages() */ private function processOrder(Order $order) { - $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING); - $order->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING); + $order->setState(Order::STATE_PROCESSING); + $order->setStatus(Order::STATE_PROCESSING); return $this->orderRepository->save($order); } From 5ea6e7208099123c5e7f8df34607fca9ef03c650 Mon Sep 17 00:00:00 2001 From: Andriy Kravets Date: Fri, 14 Dec 2018 12:39:01 +0200 Subject: [PATCH 5/8] Changed way to get to clean up the conceptions. --- .../AssignCouponDataAfterOrderCustomerAssignTest.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php index 47498ef3c51ba..332e5a254fa22 100644 --- a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php +++ b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php @@ -17,7 +17,6 @@ * @magentoAppIsolation enabled * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - * @SuppressWarnings(PHPMD.StaticAccess) */ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\TestCase { @@ -111,12 +110,10 @@ protected function setUp() */ protected function tearDown() { - unset( - $this->order, - $this->coupon, - $this->customer, - $this->salesRule - ); + $this->salesRule = null; + $this->customer = null; + $this->coupon = null; + $this->order = null; } /** From 9a63dad6a2958d07aa631bb0e971f17e3f6eda3b Mon Sep 17 00:00:00 2001 From: Andriy Kravets Date: Wed, 26 Dec 2018 11:28:08 +0200 Subject: [PATCH 6/8] Moved object manager to setup method. --- .../AssignCouponDataAfterOrderCustomerAssignTest.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php index 332e5a254fa22..06478dc1f1571 100644 --- a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php +++ b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php @@ -75,12 +75,12 @@ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\Te */ private $customer; + /** - * AssignCouponDataAfterOrderCustomerAssignTest constructor. + * @inheritdoc */ - public function __construct($name = null, array $data = [], $dataName = '') + protected function setUp() { - parent::__construct($name, $data, $dataName); $this->objectManager = Bootstrap::getObjectManager(); $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class); $this->orderRepository = $this->objectManager->get(\Magento\Sales\Model\OrderRepository::class); @@ -90,13 +90,7 @@ public function __construct($name = null, array $data = [], $dataName = '') $this->assignCouponToCustomerObserver = $this->objectManager->get( \Magento\SalesRule\Observer\AssignCouponDataAfterOrderCustomerAssignObserver::class ); - } - /** - * @inheritdoc - */ - protected function setUp() - { $this->salesRule = $this->prepareSalesRule(); $this->coupon = $this->attachSalesruleCoupon($this->salesRule); $this->order = $this->makeOrderWithCouponAsGuest($this->coupon); From 36c2329505ca63c821fc8d7a553e6f35a4f3ae40 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Thu, 3 Jan 2019 13:20:49 +0000 Subject: [PATCH 7/8] magento/magento2#19423: Removed extra blank line --- .../Observer/AssignCouponDataAfterOrderCustomerAssignTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php index 06478dc1f1571..840ee1a5607d9 100644 --- a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php +++ b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php @@ -75,7 +75,6 @@ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\Te */ private $customer; - /** * @inheritdoc */ From fdf7da48088ffae823021e5a8be4429afa941404 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Tue, 8 Jan 2019 15:29:29 +0200 Subject: [PATCH 8/8] ENGCOM-3784: Static test fix. --- .../Observer/AssignCouponDataAfterOrderCustomerAssignTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php index 840ee1a5607d9..397650df416e9 100644 --- a/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php +++ b/dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php @@ -1,4 +1,8 @@