|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Sales\Controller\Adminhtml\Order\Create; |
| 7 | + |
| 8 | +use Magento\Backend\Model\Session\Quote; |
| 9 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 10 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\Message\MessageInterface; |
| 13 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 14 | +use Magento\Sales\Model\Service\OrderService; |
| 15 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 16 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
| 17 | + |
| 18 | +class SaveTest extends AbstractBackendController |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Checks a case when order creation is failed on payment method processing but new customer already created |
| 22 | + * in the database and after new controller dispatching the customer should be already loaded in session |
| 23 | + * to prevent invalid validation. |
| 24 | + * |
| 25 | + * @magentoAppArea adminhtml |
| 26 | + * @magentoDataFixture Magento/Sales/_files/quote_with_new_customer.php |
| 27 | + */ |
| 28 | + public function testExecuteWithPaymentOperation() |
| 29 | + { |
| 30 | + $quote = $this->getQuote('2000000001'); |
| 31 | + $session = $this->_objectManager->get(Quote::class); |
| 32 | + $session->setQuoteId($quote->getId()); |
| 33 | + $session->setCustomerId(0); |
| 34 | + |
| 35 | + $email = 'john.doe001@test.com'; |
| 36 | + $data = [ |
| 37 | + 'account' => [ |
| 38 | + 'email' => $email |
| 39 | + ] |
| 40 | + ]; |
| 41 | + $this->getRequest()->setPostValue(['order' => $data]); |
| 42 | + |
| 43 | + /** @var OrderService|MockObject $orderService */ |
| 44 | + $orderService = $this->getMockBuilder(OrderService::class) |
| 45 | + ->disableOriginalConstructor() |
| 46 | + ->getMock(); |
| 47 | + $orderService->method('place') |
| 48 | + ->willThrowException(new LocalizedException(__('Transaction has been declined.'))); |
| 49 | + $this->_objectManager->addSharedInstance($orderService, OrderService::class); |
| 50 | + |
| 51 | + $this->dispatch('backend/sales/order_create/save'); |
| 52 | + $this->assertSessionMessages( |
| 53 | + self::equalTo(['Transaction has been declined.']), |
| 54 | + MessageInterface::TYPE_ERROR |
| 55 | + ); |
| 56 | + |
| 57 | + /** @var CustomerRepositoryInterface $customerRepository */ |
| 58 | + $customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class); |
| 59 | + $customer = $customerRepository->get($email); |
| 60 | + |
| 61 | + self::assertNotEmpty($session->getCustomerId()); |
| 62 | + self::assertEquals($customer->getId(), $session->getCustomerId()); |
| 63 | + |
| 64 | + $this->_objectManager->removeSharedInstance(OrderService::class); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Gets quote by reserved order id. |
| 69 | + * |
| 70 | + * @param string $reservedOrderId |
| 71 | + * @return \Magento\Quote\Api\Data\CartInterface |
| 72 | + */ |
| 73 | + private function getQuote($reservedOrderId) |
| 74 | + { |
| 75 | + /** @var SearchCriteriaBuilder $searchCriteriaBuilder */ |
| 76 | + $searchCriteriaBuilder = $this->_objectManager->get(SearchCriteriaBuilder::class); |
| 77 | + $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId) |
| 78 | + ->create(); |
| 79 | + |
| 80 | + /** @var CartRepositoryInterface $quoteRepository */ |
| 81 | + $quoteRepository = $this->_objectManager->get(CartRepositoryInterface::class); |
| 82 | + $items = $quoteRepository->getList($searchCriteria)->getItems(); |
| 83 | + return array_pop($items); |
| 84 | + } |
| 85 | +} |
0 commit comments