|
| 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\Customer\Test\Unit\Controller\Adminhtml\Cart\Product\Composite\Cart; |
| 9 | + |
| 10 | +use Magento\Backend\App\Action\Context; |
| 11 | +use Magento\Catalog\Helper\Product\Composite; |
| 12 | +use Magento\Customer\Controller\Adminhtml\Cart\Product\Composite\Cart\Configure; |
| 13 | +use Magento\Framework\App\Request\Http; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 16 | +use Magento\Quote\Model\Quote; |
| 17 | +use Magento\Quote\Model\Quote\Item; |
| 18 | +use Magento\Quote\Model\Quote\Item\Option; |
| 19 | +use Magento\Quote\Model\QuoteFactory; |
| 20 | +use Magento\Quote\Model\ResourceModel\Quote\Item\Option\Collection; |
| 21 | +use Magento\Quote\Model\ResourceModel\QuoteItemRetriever; |
| 22 | +use Magento\Store\Model\StoreManagerInterface; |
| 23 | +use PHPUnit\Framework\MockObject\MockObject; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 26 | + |
| 27 | +/** |
| 28 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 29 | + */ |
| 30 | +class ConfigureTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var int |
| 34 | + */ |
| 35 | + private $quoteItemId; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var int |
| 39 | + */ |
| 40 | + private $websiteId; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var StoreManagerInterface|MockObject |
| 44 | + */ |
| 45 | + private $storeManager; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Option|MockObject |
| 49 | + */ |
| 50 | + private $option; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var Composite|MockObject |
| 54 | + */ |
| 55 | + private $composite; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var CartRepositoryInterface|MockObject |
| 59 | + */ |
| 60 | + private $cartRepository; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var QuoteItemRetriever|MockObject |
| 64 | + */ |
| 65 | + private $quoteItemRetriever; |
| 66 | + |
| 67 | + /** |
| 68 | + * @var Configure |
| 69 | + */ |
| 70 | + private $subject; |
| 71 | + |
| 72 | + protected function setUp(): void |
| 73 | + { |
| 74 | + $customerId = 10; |
| 75 | + $this->quoteItemId = 20; |
| 76 | + $this->websiteId = 1; |
| 77 | + $request = $this->getMockBuilder(Http::class) |
| 78 | + ->disableOriginalConstructor() |
| 79 | + ->getMock(); |
| 80 | + |
| 81 | + $request->expects($this->exactly(3)) |
| 82 | + ->method('getParam') |
| 83 | + ->withConsecutive(['customer_id'], ['id'], ['website_id']) |
| 84 | + ->willReturnOnConsecutiveCalls($customerId, $this->quoteItemId, $this->websiteId); |
| 85 | + |
| 86 | + $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class) |
| 87 | + ->disableOriginalConstructor() |
| 88 | + ->getMock(); |
| 89 | + $this->option = $this->getMockBuilder(Option::class) |
| 90 | + ->disableOriginalConstructor() |
| 91 | + ->getMock(); |
| 92 | + $this->composite = $this->getMockBuilder(Composite::class) |
| 93 | + ->disableOriginalConstructor() |
| 94 | + ->getMock(); |
| 95 | + $objectManager = $this->getMockBuilder(ObjectManagerInterface::class) |
| 96 | + ->disableOriginalConstructor() |
| 97 | + ->getMock(); |
| 98 | + $objectManager->expects($this->any()) |
| 99 | + ->method('get') |
| 100 | + ->willReturnOnConsecutiveCalls($this->storeManager, $this->composite); |
| 101 | + |
| 102 | + $objectManager->expects($this->any()) |
| 103 | + ->method('create') |
| 104 | + ->willReturn($this->option); |
| 105 | + |
| 106 | + $context = $this->getMockBuilder(Context::class) |
| 107 | + ->setMethods(['getRequest', 'getObjectManager']) |
| 108 | + ->disableOriginalConstructor() |
| 109 | + ->getMock(); |
| 110 | + $context->expects($this->any()) |
| 111 | + ->method('getRequest') |
| 112 | + ->willReturn($request); |
| 113 | + $context->expects($this->any()) |
| 114 | + ->method('getObjectManager') |
| 115 | + ->willReturn($objectManager); |
| 116 | + |
| 117 | + $this->cartRepository = $this->getMockBuilder(CartRepositoryInterface::class) |
| 118 | + ->disableOriginalConstructor() |
| 119 | + ->getMock(); |
| 120 | + |
| 121 | + $quoteFactory = $this->getMockBuilder(QuoteFactory::class) |
| 122 | + ->disableOriginalConstructor() |
| 123 | + ->getMock(); |
| 124 | + |
| 125 | + $this->quoteItemRetriever = $this->getMockBuilder(QuoteItemRetriever::class) |
| 126 | + ->setMethods(['getById']) |
| 127 | + ->disableOriginalConstructor() |
| 128 | + ->getMock(); |
| 129 | + |
| 130 | + $objectManagerHelper = new ObjectManagerHelper($this); |
| 131 | + $this->subject = $objectManagerHelper->getObject( |
| 132 | + Configure::class, |
| 133 | + [ |
| 134 | + 'context' => $context, |
| 135 | + 'quoteRepository' => $this->cartRepository, |
| 136 | + 'quoteFactory' => $quoteFactory, |
| 137 | + 'quoteItemRetriever' => $this->quoteItemRetriever |
| 138 | + ] |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Test Execute method |
| 144 | + */ |
| 145 | + public function testExecute() |
| 146 | + { |
| 147 | + $quoteItem = $this->getMockBuilder(Item::class) |
| 148 | + ->disableOriginalConstructor() |
| 149 | + ->getMock(); |
| 150 | + |
| 151 | + $quote = $this->getMockBuilder(Quote::class) |
| 152 | + ->setMethods(['setWebsite', 'getItemById']) |
| 153 | + ->disableOriginalConstructor() |
| 154 | + ->getMock(); |
| 155 | + $quote->expects($this->once()) |
| 156 | + ->method('setWebsite') |
| 157 | + ->willReturnSelf(); |
| 158 | + $quote->expects($this->once()) |
| 159 | + ->method('getItemById') |
| 160 | + ->willReturn($quoteItem); |
| 161 | + |
| 162 | + $this->storeManager->expects($this->once()) |
| 163 | + ->method('getWebsite') |
| 164 | + ->with($this->websiteId) |
| 165 | + ->willReturnSelf(); |
| 166 | + |
| 167 | + $this->cartRepository->expects($this->once()) |
| 168 | + ->method('getForCustomer') |
| 169 | + ->willReturn($quote); |
| 170 | + |
| 171 | + $this->quoteItemRetriever->expects($this->once()) |
| 172 | + ->method('getById') |
| 173 | + ->with($this->quoteItemId) |
| 174 | + ->willReturn($quoteItem); |
| 175 | + |
| 176 | + $collection = $this->getMockBuilder(Collection::class) |
| 177 | + ->disableOriginalConstructor() |
| 178 | + ->getMock(); |
| 179 | + $collection->expects($this->once()) |
| 180 | + ->method('addItemFilter') |
| 181 | + ->willReturnSelf(); |
| 182 | + |
| 183 | + $this->option->expects($this->once()) |
| 184 | + ->method('getCollection') |
| 185 | + ->willReturn($collection); |
| 186 | + |
| 187 | + $this->composite->expects($this->once()) |
| 188 | + ->method('renderConfigureResult') |
| 189 | + ->willReturnSelf(); |
| 190 | + |
| 191 | + $this->subject->execute(); |
| 192 | + } |
| 193 | +} |
0 commit comments