|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Multishipping\Controller\Checkout; |
| 9 | + |
| 10 | +use Magento\Customer\Api\AccountManagementInterface; |
| 11 | +use Magento\Customer\Model\Session as CustomerSession; |
| 12 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 13 | +use Magento\Framework\Exception\LocalizedException; |
| 14 | +use Magento\Framework\Message\MessageInterface; |
| 15 | +use Magento\Quote\Api\Data\CartInterface; |
| 16 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 17 | +use Magento\Quote\Api\CartRepositoryInterface as QuoteRepository; |
| 18 | +use Magento\TestFramework\TestCase\AbstractController; |
| 19 | +use Psr\Log\LoggerInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Test class for @see \Magento\Multishipping\Controller\Checkout\AddressesPost. |
| 23 | + * |
| 24 | + * @magentoAppArea frontend |
| 25 | + */ |
| 26 | +class AddressesPostTest extends AbstractController |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var QuoteRepository |
| 30 | + */ |
| 31 | + private $quoteRepository; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritdoc |
| 35 | + */ |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + parent::setUp(); |
| 39 | + $this->quoteRepository = $this->_objectManager->get(QuoteRepository::class); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @magentoDataFixture Magento/Checkout/_files/quote_with_address.php |
| 44 | + */ |
| 45 | + public function testExecute() |
| 46 | + { |
| 47 | + $quote = $this->getQuote('test_order_1'); |
| 48 | + $this->setMultiShippingToQuote($quote); |
| 49 | + $quoteItems = $quote->getItems(); |
| 50 | + $quoteItemId = array_shift($quoteItems)->getItemId(); |
| 51 | + $this->loginCustomer(); |
| 52 | + |
| 53 | + $qty = 3; |
| 54 | + $productPrice = 10; |
| 55 | + $request = [ |
| 56 | + 'ship' => [ |
| 57 | + 1 => [ |
| 58 | + $quoteItemId => [ |
| 59 | + 'qty' => $qty, |
| 60 | + 'address' => 1, |
| 61 | + ], |
| 62 | + ], |
| 63 | + ] |
| 64 | + ]; |
| 65 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST); |
| 66 | + $this->getRequest()->setPostValue($request); |
| 67 | + $this->dispatch('multishipping/checkout/addressesPost'); |
| 68 | + $freshQuote = $this->getQuote('test_order_1'); |
| 69 | + |
| 70 | + $this->assertEquals($qty, $freshQuote->getItemsQty()); |
| 71 | + $this->assertEquals($productPrice * $qty, $freshQuote->getGrandTotal()); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @magentoDataFixture Magento/Checkout/_files/quote_with_address.php |
| 76 | + */ |
| 77 | + public function testExecuteFail() |
| 78 | + { |
| 79 | + $msg = 'Verify the shipping address information and continue.'; |
| 80 | + |
| 81 | + $quote = $this->getQuote('test_order_1'); |
| 82 | + $this->setMultiShippingToQuote($quote); |
| 83 | + $quoteItems = $quote->getItems(); |
| 84 | + $quoteItemId = array_shift($quoteItems)->getItemId(); |
| 85 | + $this->loginCustomer(); |
| 86 | + $request = [ |
| 87 | + 'ship' => [ |
| 88 | + 1 => [ |
| 89 | + $quoteItemId => [ |
| 90 | + 'qty' => 1, |
| 91 | + 'address' => $quoteItemId, |
| 92 | + ], |
| 93 | + ], |
| 94 | + ], |
| 95 | + ]; |
| 96 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST); |
| 97 | + $this->getRequest()->setPostValue($request); |
| 98 | + $this->dispatch('multishipping/checkout/addressesPost'); |
| 99 | + |
| 100 | + $this->assertSessionMessages($this->equalTo([$msg]), MessageInterface::TYPE_ERROR); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param CartInterface $quote |
| 105 | + * @return void |
| 106 | + */ |
| 107 | + private function setMultiShippingToQuote(CartInterface $quote): void |
| 108 | + { |
| 109 | + $quote->setIsMultiShipping(1); |
| 110 | + $this->quoteRepository->save($quote); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Authenticates customer and creates customer session. |
| 115 | + * |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + private function loginCustomer(): void |
| 119 | + { |
| 120 | + $logger = $this->createMock(LoggerInterface::class); |
| 121 | + /** @var AccountManagementInterface $service */ |
| 122 | + $service = $this->_objectManager->create(AccountManagementInterface::class); |
| 123 | + try { |
| 124 | + $customer = $service->authenticate('customer@example.com', 'password'); |
| 125 | + } catch (LocalizedException $e) { |
| 126 | + $this->fail($e->getMessage()); |
| 127 | + } |
| 128 | + /** @var CustomerSession $customerSession */ |
| 129 | + $customerSession = $this->_objectManager->create(CustomerSession::class, [$logger]); |
| 130 | + $customerSession->setCustomerDataAsLoggedIn($customer); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Gets quote by reserved order id. |
| 135 | + * |
| 136 | + * @param string $reservedOrderId |
| 137 | + * @return CartInterface |
| 138 | + */ |
| 139 | + private function getQuote(string $reservedOrderId): CartInterface |
| 140 | + { |
| 141 | + /** @var SearchCriteriaBuilder $searchCriteriaBuilder */ |
| 142 | + $searchCriteriaBuilder = $this->_objectManager->create(SearchCriteriaBuilder::class); |
| 143 | + $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)->create(); |
| 144 | + /** @var QuoteRepository $quoteRepository */ |
| 145 | + $quoteRepository = $this->_objectManager->get(QuoteRepository::class); |
| 146 | + $items = $quoteRepository->getList($searchCriteria)->getItems(); |
| 147 | + |
| 148 | + return array_pop($items); |
| 149 | + } |
| 150 | +} |
0 commit comments