|
| 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\Controller\Adminhtml\Order\Create; |
| 9 | + |
| 10 | +use Magento\Backend\App\Action\Context; |
| 11 | +use Magento\Backend\Model\Session\Quote; |
| 12 | +use Magento\Backend\Model\View\Result\ForwardFactory; |
| 13 | +use Magento\Framework\App\RequestInterface; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\Sales\Controller\Adminhtml\Order\Create\ConfigureProductToAdd; |
| 17 | +use Magento\Framework\View\Result\Layout; |
| 18 | +use Magento\Store\Model\Store; |
| 19 | +use Magento\Catalog\Helper\Product\Composite; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use Magento\Catalog\Helper\Product; |
| 23 | +use Magento\Framework\Escaper; |
| 24 | +use Magento\Framework\View\Result\PageFactory; |
| 25 | +use Magento\Framework\DataObject; |
| 26 | + |
| 27 | +/** |
| 28 | + * Class ConfigureProductToAddTest |
| 29 | + */ |
| 30 | +class ConfigureProductToAddTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var Context |
| 34 | + */ |
| 35 | + private $contextMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var RequestInterface|MockObject |
| 39 | + */ |
| 40 | + private $requestMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ObjectManagerInterface|MockObject |
| 44 | + */ |
| 45 | + private $objectManagerMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var ForwardFactory|MockObject |
| 49 | + */ |
| 50 | + private $resultForwardFactoryMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var Quote|MockObject |
| 54 | + */ |
| 55 | + private $quoteSessionMock; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var Store|MockObject |
| 59 | + */ |
| 60 | + private $storeMock; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var Composite|MockObject |
| 64 | + */ |
| 65 | + private $compositeHelperMock; |
| 66 | + |
| 67 | + /** |
| 68 | + * @var StoreManagerInterface|MockObject |
| 69 | + */ |
| 70 | + private $storeManagerMock; |
| 71 | + |
| 72 | + /** |
| 73 | + * @var Product|MockObject |
| 74 | + */ |
| 75 | + private $productHelperMock; |
| 76 | + |
| 77 | + /** |
| 78 | + * @var Escaper|MockObject |
| 79 | + */ |
| 80 | + private $escaperMock; |
| 81 | + |
| 82 | + /** |
| 83 | + * @var PageFactory|MockObject |
| 84 | + */ |
| 85 | + private $resultPageFactoryMock; |
| 86 | + |
| 87 | + /** |
| 88 | + * @var Layout|MockObject |
| 89 | + */ |
| 90 | + private $layoutMock; |
| 91 | + |
| 92 | + /** |
| 93 | + * @var ConfigureProductToAdd |
| 94 | + */ |
| 95 | + private $configureProductToAdd; |
| 96 | + |
| 97 | + /** |
| 98 | + * @inheritDoc |
| 99 | + */ |
| 100 | + protected function setUp(): void |
| 101 | + { |
| 102 | + $this->requestMock = $this->getMockBuilder(RequestInterface::class) |
| 103 | + ->onlyMethods(['getParam']) |
| 104 | + ->disableOriginalConstructor() |
| 105 | + ->getMockForAbstractClass(); |
| 106 | + $this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class) |
| 107 | + ->onlyMethods(['get']) |
| 108 | + ->disableOriginalConstructor() |
| 109 | + ->getMockForAbstractClass(); |
| 110 | + $this->contextMock = $this->getMockBuilder(Context::class) |
| 111 | + ->onlyMethods(['getObjectManager', 'getRequest']) |
| 112 | + ->disableOriginalConstructor() |
| 113 | + ->getMock(); |
| 114 | + $this->contextMock->expects($this->once()) |
| 115 | + ->method('getObjectManager') |
| 116 | + ->willReturn($this->objectManagerMock); |
| 117 | + $this->contextMock->expects($this->once()) |
| 118 | + ->method('getRequest') |
| 119 | + ->willReturn($this->requestMock); |
| 120 | + $this->productHelperMock = $this->getMockBuilder(Product::class) |
| 121 | + ->disableOriginalConstructor() |
| 122 | + ->getMock(); |
| 123 | + $this->escaperMock = $this->getMockBuilder(Escaper::class) |
| 124 | + ->disableOriginalConstructor() |
| 125 | + ->getMock(); |
| 126 | + $this->resultPageFactoryMock = $this->getMockBuilder(PageFactory::class) |
| 127 | + ->disableOriginalConstructor() |
| 128 | + ->getMock(); |
| 129 | + $this->resultForwardFactoryMock = $this->getMockBuilder(ForwardFactory::class) |
| 130 | + ->disableOriginalConstructor() |
| 131 | + ->getMock(); |
| 132 | + $this->quoteSessionMock = $this->getMockBuilder(Quote::class) |
| 133 | + ->onlyMethods(['getStore']) |
| 134 | + ->addMethods(['getCustomerId']) |
| 135 | + ->disableOriginalConstructor() |
| 136 | + ->getMock(); |
| 137 | + $this->storeMock = $this->getMockBuilder(Store::class) |
| 138 | + ->onlyMethods(['getCode', 'getId']) |
| 139 | + ->disableOriginalConstructor() |
| 140 | + ->getMock(); |
| 141 | + $this->compositeHelperMock = $this->getMockBuilder(Composite::class) |
| 142 | + ->onlyMethods(['renderConfigureResult']) |
| 143 | + ->disableOriginalConstructor() |
| 144 | + ->getMock(); |
| 145 | + $this->layoutMock = $this->getMockBuilder(Layout::class) |
| 146 | + ->disableOriginalConstructor() |
| 147 | + ->getMock(); |
| 148 | + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) |
| 149 | + ->onlyMethods(['setCurrentStore']) |
| 150 | + ->disableOriginalConstructor() |
| 151 | + ->getMockForAbstractClass(); |
| 152 | + |
| 153 | + $this->configureProductToAdd = new ConfigureProductToAdd( |
| 154 | + $this->contextMock, |
| 155 | + $this->productHelperMock, |
| 156 | + $this->escaperMock, |
| 157 | + $this->resultPageFactoryMock, |
| 158 | + $this->resultForwardFactoryMock, |
| 159 | + $this->storeManagerMock |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @return void |
| 165 | + */ |
| 166 | + public function testExecute(): void |
| 167 | + { |
| 168 | + $productId = 1; |
| 169 | + $customerId = 1; |
| 170 | + $storeCode = 'view2'; |
| 171 | + $storeId = 2; |
| 172 | + $configureResult = new DataObject( |
| 173 | + [ |
| 174 | + 'ok' => true, |
| 175 | + 'product_id' => $productId, |
| 176 | + 'current_store_id' => $storeId, |
| 177 | + 'current_customer_id' => $customerId, |
| 178 | + ] |
| 179 | + ); |
| 180 | + $this->requestMock->expects($this->once()) |
| 181 | + ->method('getParam') |
| 182 | + ->with('id') |
| 183 | + ->willReturn($productId); |
| 184 | + $this->objectManagerMock->expects($this->at(0)) |
| 185 | + ->method('get') |
| 186 | + ->with(Quote::class) |
| 187 | + ->willReturn($this->quoteSessionMock); |
| 188 | + $this->objectManagerMock->expects($this->at(1)) |
| 189 | + ->method('get') |
| 190 | + ->with(Composite::class) |
| 191 | + ->willReturn($this->compositeHelperMock); |
| 192 | + $this->quoteSessionMock->expects($this->any()) |
| 193 | + ->method('getStore') |
| 194 | + ->willReturn($this->storeMock); |
| 195 | + $this->quoteSessionMock->expects($this->once()) |
| 196 | + ->method('getCustomerId') |
| 197 | + ->willReturn($customerId); |
| 198 | + $this->storeMock->expects($this->once()) |
| 199 | + ->method('getCode') |
| 200 | + ->willReturn($storeCode); |
| 201 | + $this->storeMock->expects($this->once()) |
| 202 | + ->method('getId') |
| 203 | + ->willReturn($storeId); |
| 204 | + $this->storeManagerMock->expects($this->once()) |
| 205 | + ->method('setCurrentStore') |
| 206 | + ->with($storeCode) |
| 207 | + ->willReturnSelf(); |
| 208 | + $this->compositeHelperMock->expects($this->once()) |
| 209 | + ->method('renderConfigureResult') |
| 210 | + ->with($configureResult)->willReturn($this->layoutMock); |
| 211 | + $this->assertInstanceOf(Layout::class, $this->configureProductToAdd->execute()); |
| 212 | + } |
| 213 | +} |
0 commit comments