|
| 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\Sales\Test\Unit\Block\Adminhtml\Order\Create; |
| 9 | + |
| 10 | +use Magento\Backend\Block\Template\Context; |
| 11 | +use Magento\Backend\Model\Session\Quote as QuoteSession; |
| 12 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 13 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 14 | +use Magento\Customer\Model\Address\Mapper; |
| 15 | +use Magento\Customer\Model\Metadata\FormFactory; |
| 16 | +use Magento\Framework\Currency; |
| 17 | +use Magento\Framework\Json\EncoderInterface; |
| 18 | +use Magento\Framework\Locale\CurrencyInterface; |
| 19 | +use Magento\Framework\Pricing\PriceCurrencyInterface; |
| 20 | +use Magento\Quote\Model\Quote; |
| 21 | +use Magento\Quote\Model\Quote\Address; |
| 22 | +use Magento\Quote\Model\Quote\Payment; |
| 23 | +use Magento\Sales\Block\Adminhtml\Order\Create\Form; |
| 24 | +use Magento\Sales\Model\AdminOrder\Create; |
| 25 | +use Magento\Store\Model\Store; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
| 28 | + |
| 29 | +/** |
| 30 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 31 | + */ |
| 32 | +class FormTest extends TestCase |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @var QuoteSession|MockObject |
| 36 | + */ |
| 37 | + private $quoteSession; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var CustomerRepositoryInterface|MockObject |
| 41 | + */ |
| 42 | + private $customerRepository; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var CurrencyInterface|MockObject |
| 46 | + */ |
| 47 | + private $localeCurrency; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var Form |
| 51 | + */ |
| 52 | + private $block; |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritdoc |
| 56 | + */ |
| 57 | + protected function setUp() |
| 58 | + { |
| 59 | + /** @var Context|MockObject $context */ |
| 60 | + $context = $this->getMockBuilder(Context::class) |
| 61 | + ->disableOriginalConstructor() |
| 62 | + ->getMock(); |
| 63 | + $this->quoteSession = $this->getMockBuilder(QuoteSession::class) |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->setMethods(['getCustomerId', 'getQuoteId', 'getStoreId', 'getStore', 'getQuote']) |
| 66 | + ->getMock(); |
| 67 | + /** @var Create|MockObject $create */ |
| 68 | + $create = $this->getMockBuilder(Create::class) |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->getMock(); |
| 71 | + /** @var PriceCurrencyInterface|MockObject $priceCurrency */ |
| 72 | + $priceCurrency = $this->getMockForAbstractClass(PriceCurrencyInterface::class); |
| 73 | + /** @var EncoderInterface|MockObject $encoder */ |
| 74 | + $encoder = $this->getMockForAbstractClass(EncoderInterface::class); |
| 75 | + $encoder->method('encode') |
| 76 | + ->willReturnCallback(function ($param) { |
| 77 | + return json_encode($param); |
| 78 | + }); |
| 79 | + /** @var FormFactory|MockObject $formFactory */ |
| 80 | + $formFactory = $this->getMockBuilder(FormFactory::class) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->getMock(); |
| 83 | + $this->customerRepository = $this->getMockForAbstractClass(CustomerRepositoryInterface::class); |
| 84 | + |
| 85 | + $this->localeCurrency = $this->getMockForAbstractClass(CurrencyInterface::class); |
| 86 | + /** @var Mapper|MockObject $addressMapper */ |
| 87 | + $addressMapper = $this->getMockBuilder(Mapper::class) |
| 88 | + ->disableOriginalConstructor() |
| 89 | + ->getMock(); |
| 90 | + |
| 91 | + $this->block = new Form( |
| 92 | + $context, |
| 93 | + $this->quoteSession, |
| 94 | + $create, |
| 95 | + $priceCurrency, |
| 96 | + $encoder, |
| 97 | + $formFactory, |
| 98 | + $this->customerRepository, |
| 99 | + $this->localeCurrency, |
| 100 | + $addressMapper |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Checks if order contains all needed data. |
| 106 | + */ |
| 107 | + public function testGetOrderDataJson() |
| 108 | + { |
| 109 | + $customerId = 1; |
| 110 | + $storeId = 1; |
| 111 | + $quoteId = 2; |
| 112 | + $expected = [ |
| 113 | + 'customer_id' => $customerId, |
| 114 | + 'addresses' => [], |
| 115 | + 'store_id' => $storeId, |
| 116 | + 'currency_symbol' => '$', |
| 117 | + 'shipping_method_reseted' => false, |
| 118 | + 'payment_method' => 'free', |
| 119 | + 'quote_id' => $quoteId |
| 120 | + ]; |
| 121 | + |
| 122 | + $this->quoteSession->method('getCustomerId') |
| 123 | + ->willReturn($customerId); |
| 124 | + $this->quoteSession->method('getStoreId') |
| 125 | + ->willReturn($storeId); |
| 126 | + $this->quoteSession->method('getQuoteId') |
| 127 | + ->willReturn($quoteId); |
| 128 | + |
| 129 | + $customer = $this->getMockBuilder(CustomerInterface::class) |
| 130 | + ->disableOriginalConstructor() |
| 131 | + ->getMock(); |
| 132 | + $customer->method('getAddresses') |
| 133 | + ->willReturn([]); |
| 134 | + $this->customerRepository->method('getById') |
| 135 | + ->with($customerId) |
| 136 | + ->willReturn($customer); |
| 137 | + |
| 138 | + $this->withCurrencySymbol('$'); |
| 139 | + |
| 140 | + $this->withQuote(); |
| 141 | + |
| 142 | + self::assertEquals($expected, json_decode($this->block->getOrderDataJson(), true)); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Configures mock object for currency. |
| 147 | + * |
| 148 | + * @param string $symbol |
| 149 | + */ |
| 150 | + private function withCurrencySymbol(string $symbol) |
| 151 | + { |
| 152 | + $store = $this->getMockBuilder(Store::class) |
| 153 | + ->disableOriginalConstructor() |
| 154 | + ->getMock(); |
| 155 | + $store->method('getCurrentCurrencyCode') |
| 156 | + ->willReturn('USD'); |
| 157 | + $this->quoteSession->method('getStore') |
| 158 | + ->willReturn($store); |
| 159 | + |
| 160 | + $currency = $this->getMockBuilder(Currency::class) |
| 161 | + ->disableOriginalConstructor() |
| 162 | + ->getMock(); |
| 163 | + $currency->method('getSymbol') |
| 164 | + ->willReturn($symbol); |
| 165 | + $this->localeCurrency->method('getCurrency') |
| 166 | + ->with('USD') |
| 167 | + ->willReturn($currency); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Configures shipping and payment mock objects. |
| 172 | + */ |
| 173 | + private function withQuote() |
| 174 | + { |
| 175 | + $quote = $this->getMockBuilder(Quote::class) |
| 176 | + ->disableOriginalConstructor() |
| 177 | + ->getMock(); |
| 178 | + $this->quoteSession->method('getQuote') |
| 179 | + ->willReturn($quote); |
| 180 | + |
| 181 | + $address = $this->getMockBuilder(Address::class) |
| 182 | + ->disableOriginalConstructor() |
| 183 | + ->getMock(); |
| 184 | + $address->method('getShippingMethod') |
| 185 | + ->willReturn('free'); |
| 186 | + $quote->method('getShippingAddress') |
| 187 | + ->willReturn($address); |
| 188 | + |
| 189 | + $payment = $this->getMockBuilder(Payment::class) |
| 190 | + ->disableOriginalConstructor() |
| 191 | + ->getMock(); |
| 192 | + $payment->method('getMethod') |
| 193 | + ->willReturn('free'); |
| 194 | + $quote->method('getPayment') |
| 195 | + ->willReturn($payment); |
| 196 | + } |
| 197 | +} |
0 commit comments