|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Wishlist\Test\Unit\Controller\Index; |
| 7 | + |
| 8 | +use Magento\Backend\Model\View\Result\Redirect; |
| 9 | +use Magento\Framework\App\Action\Context; |
| 10 | +use Magento\Framework\App\RequestInterface; |
| 11 | +use Magento\Framework\Controller\ResultFactory; |
| 12 | +use Magento\Framework\Data\Form\FormKey\Validator; |
| 13 | +use Magento\Framework\Exception\NotFoundException; |
| 14 | +use Magento\Framework\Message\ManagerInterface; |
| 15 | +use Magento\Framework\ObjectManagerInterface; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 17 | +use Magento\Wishlist\Controller\Index\Update; |
| 18 | +use Magento\Wishlist\Controller\WishlistProviderInterface; |
| 19 | +use Magento\Wishlist\Helper\Data; |
| 20 | +use Magento\Wishlist\Model\Item; |
| 21 | +use Magento\Wishlist\Model\LocaleQuantityProcessor; |
| 22 | +use Magento\Wishlist\Model\Wishlist; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
| 25 | + |
| 26 | +/** |
| 27 | + * Test for upate controller wishlist |
| 28 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 29 | + */ |
| 30 | +class UpdateTest extends TestCase |
| 31 | +{ |
| 32 | + private const STUB_ITEM_ID = 1; |
| 33 | + |
| 34 | + private const STUB_WISHLIST_PRODUCT_QTY = 21; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var MockObject|Validator $formKeyValidatorMock |
| 38 | + */ |
| 39 | + private $formKeyValidatorMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var MockObject|WishlistProviderInterface $wishlistProviderMock |
| 43 | + */ |
| 44 | + private $wishlistProviderMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var MockObject|LocaleQuantityProcessor $quantityProcessorMock |
| 48 | + */ |
| 49 | + private $quantityProcessorMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Update $updateController |
| 53 | + */ |
| 54 | + private $updateController; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var MockObject|Context$contextMock |
| 58 | + */ |
| 59 | + private $contextMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var MockObject|Redirect $resultRedirectMock |
| 63 | + */ |
| 64 | + private $resultRedirectMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var MockObject|ResultFactory $resultFatoryMock |
| 68 | + */ |
| 69 | + private $resultFactoryMock; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var MockObject|RequestInterface $requestMock |
| 73 | + */ |
| 74 | + private $requestMock; |
| 75 | + |
| 76 | + /** |
| 77 | + * @var MockObject|ObjectManagerInterface $objectManagerMock |
| 78 | + */ |
| 79 | + private $objectManagerMock; |
| 80 | + |
| 81 | + /** |
| 82 | + * @var MockObject|ManagerInterface $messageManagerMock |
| 83 | + */ |
| 84 | + private $messageManagerMock; |
| 85 | + |
| 86 | + /** |
| 87 | + * @inheritdoc |
| 88 | + */ |
| 89 | + protected function setUp() |
| 90 | + { |
| 91 | + $this->formKeyValidatorMock = $this->createMock(Validator::class); |
| 92 | + $this->wishlistProviderMock = $this->createMock(WishlistProviderInterface::class); |
| 93 | + $this->quantityProcessorMock = $this->createMock(LocaleQuantityProcessor::class); |
| 94 | + $this->contextMock = $this->createMock(Context::class); |
| 95 | + $this->resultRedirectMock = $this->createMock(Redirect::class); |
| 96 | + $this->resultFactoryMock = $this->createPartialMock(ResultFactory::class, ['create']); |
| 97 | + $this->messageManagerMock = $this->createMock(ManagerInterface::class); |
| 98 | + $this->objectManagerMock = $this->createMock(ObjectManagerInterface::class); |
| 99 | + $this->requestMock = $this->getMockBuilder(RequestInterface::class) |
| 100 | + ->setMethods(['getPostValue']) |
| 101 | + ->getMockForAbstractClass(); |
| 102 | + |
| 103 | + $this->resultFactoryMock->expects($this->any()) |
| 104 | + ->method('create') |
| 105 | + ->willReturn($this->resultRedirectMock); |
| 106 | + $this->contextMock->expects($this->once()) |
| 107 | + ->method('getResultFactory') |
| 108 | + ->willReturn($this->resultFactoryMock); |
| 109 | + $this->contextMock->expects($this->once()) |
| 110 | + ->method('getObjectManager') |
| 111 | + ->willReturn($this->objectManagerMock); |
| 112 | + $this->contextMock->expects($this->any()) |
| 113 | + ->method('getRequest') |
| 114 | + ->willReturn($this->requestMock); |
| 115 | + $this->contextMock->expects($this->any()) |
| 116 | + ->method('getMessageManager') |
| 117 | + ->willReturn($this->messageManagerMock); |
| 118 | + |
| 119 | + $objectManager = new ObjectManagerHelper($this); |
| 120 | + |
| 121 | + $this->updateController = $objectManager->getObject( |
| 122 | + Update::class, |
| 123 | + [ |
| 124 | + 'context' => $this->contextMock, |
| 125 | + '_formKeyValidator' => $this->formKeyValidatorMock, |
| 126 | + 'wishlistProvider' => $this->wishlistProviderMock, |
| 127 | + 'quantityProcessor' => $this->quantityProcessorMock |
| 128 | + ] |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Test for update method Wishlist controller. |
| 134 | + * |
| 135 | + * @dataProvider getWishlistDataProvider |
| 136 | + * @param array $wishlistDataProvider |
| 137 | + * @param array $postData |
| 138 | + * @return void |
| 139 | + */ |
| 140 | + public function testUpdate(array $wishlistDataProvider, array $postData): void |
| 141 | + { |
| 142 | + $wishlist = $this->createMock(Wishlist::class); |
| 143 | + $itemMock = $this->getMockBuilder(Item::class) |
| 144 | + ->disableOriginalConstructor() |
| 145 | + ->setMethods( |
| 146 | + [ |
| 147 | + 'load', |
| 148 | + 'getId', |
| 149 | + 'getWishlistId', |
| 150 | + 'setQty', |
| 151 | + 'save', |
| 152 | + 'getDescription', |
| 153 | + 'setDescription', |
| 154 | + 'getProduct', |
| 155 | + 'getName' |
| 156 | + ] |
| 157 | + )->getMock(); |
| 158 | + $dataMock = $this->createMock(Data::class); |
| 159 | + $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class) |
| 160 | + ->disableOriginalConstructor() |
| 161 | + ->getMock(); |
| 162 | + |
| 163 | + $this->formKeyValidatorMock->expects($this->once()) |
| 164 | + ->method('validate') |
| 165 | + ->with($this->requestMock) |
| 166 | + ->willReturn(true); |
| 167 | + $this->wishlistProviderMock->expects($this->once()) |
| 168 | + ->method('getWishlist') |
| 169 | + ->willReturn($wishlist); |
| 170 | + $wishlist->expects($this->exactly(2)) |
| 171 | + ->method('getId') |
| 172 | + ->willReturn($wishlistDataProvider['id']); |
| 173 | + $this->requestMock->expects($this->once()) |
| 174 | + ->method('getPostValue') |
| 175 | + ->willReturn($postData); |
| 176 | + $this->resultRedirectMock->expects($this->once()) |
| 177 | + ->method('setPath') |
| 178 | + ->with('*', ['wishlist_id' => $wishlistDataProvider['id']]); |
| 179 | + $this->objectManagerMock->expects($this->once()) |
| 180 | + ->method('create') |
| 181 | + ->with(Item::class) |
| 182 | + ->willReturn($itemMock); |
| 183 | + $itemMock->expects($this->once()) |
| 184 | + ->method('load') |
| 185 | + ->with(1) |
| 186 | + ->willReturnSelf(); |
| 187 | + $itemMock->expects($this->once()) |
| 188 | + ->method('getWishLIstId') |
| 189 | + ->willReturn($wishlistDataProvider['id']); |
| 190 | + $itemMock->expects($this->once()) |
| 191 | + ->method('getDescription') |
| 192 | + ->willReturn(''); |
| 193 | + $itemMock->expects($this->once()) |
| 194 | + ->method('setDescription') |
| 195 | + ->willReturnSelf(); |
| 196 | + $itemMock->expects($this->once()) |
| 197 | + ->method('setQty') |
| 198 | + ->willReturnSelf(); |
| 199 | + $this->objectManagerMock->expects($this->exactly(2)) |
| 200 | + ->method('get') |
| 201 | + ->with(Data::class) |
| 202 | + ->willReturn($dataMock); |
| 203 | + $dataMock->expects($this->once()) |
| 204 | + ->method('defaultCommentString') |
| 205 | + ->willReturn(''); |
| 206 | + $dataMock->expects($this->once()) |
| 207 | + ->method('calculate'); |
| 208 | + $this->quantityProcessorMock->expects($this->once()) |
| 209 | + ->method('process') |
| 210 | + ->willReturn($postData['qty']); |
| 211 | + $itemMock->expects($this->once()) |
| 212 | + ->method('getProduct') |
| 213 | + ->willReturn($productMock); |
| 214 | + $productMock->expects($this->once()) |
| 215 | + ->method('getName') |
| 216 | + ->willReturn('product'); |
| 217 | + $this->messageManagerMock->expects($this->once()) |
| 218 | + ->method('addSuccessMessage'); |
| 219 | + |
| 220 | + $this->assertEquals($this->resultRedirectMock, $this->updateController->execute()); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Verify update method if post data not available |
| 225 | + * |
| 226 | + * @dataProvider getWishlistDataProvider |
| 227 | + * @param array $wishlistDataProvider |
| 228 | + * @return void |
| 229 | + */ |
| 230 | + public function testUpdateRedirectWhenNoPostData(array $wishlistDataProvider): void |
| 231 | + { |
| 232 | + $wishlist = $this->createMock(Wishlist::class); |
| 233 | + |
| 234 | + $this->formKeyValidatorMock->expects($this->once()) |
| 235 | + ->method('validate') |
| 236 | + ->willReturn(true); |
| 237 | + $this->wishlistProviderMock->expects($this->once()) |
| 238 | + ->method('getWishlist') |
| 239 | + ->willReturn($wishlist); |
| 240 | + $wishlist->expects($this->exactly(1)) |
| 241 | + ->method('getId') |
| 242 | + ->willReturn($wishlistDataProvider['id']); |
| 243 | + $this->resultRedirectMock->expects($this->once()) |
| 244 | + ->method('setPath') |
| 245 | + ->with('*', ['wishlist_id' => $wishlistDataProvider['id']]); |
| 246 | + $this->requestMock->expects($this->once()) |
| 247 | + ->method('getPostValue') |
| 248 | + ->willReturn(null); |
| 249 | + |
| 250 | + $this->assertEquals($this->resultRedirectMock, $this->updateController->execute()); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Check if wishlist not availbale, and exception is shown |
| 255 | + * |
| 256 | + * @return void |
| 257 | + */ |
| 258 | + public function testUpdateThrowsNotFoundExceptionWhenWishlistDoNotExist(): void |
| 259 | + { |
| 260 | + $this->formKeyValidatorMock->expects($this->once()) |
| 261 | + ->method('validate') |
| 262 | + ->willReturn(true); |
| 263 | + $this->wishlistProviderMock->expects($this->once()) |
| 264 | + ->method('getWishlist') |
| 265 | + ->willReturn(null); |
| 266 | + $this->expectException(NotFoundException::class); |
| 267 | + $this->updateController->execute(); |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Dataprovider for Update test |
| 272 | + * |
| 273 | + * @return array |
| 274 | + */ |
| 275 | + public function getWishlistDataProvider(): array |
| 276 | + { |
| 277 | + return |
| 278 | + [ |
| 279 | + [ |
| 280 | + [ |
| 281 | + 'id' => self::STUB_ITEM_ID |
| 282 | + ], |
| 283 | + [ |
| 284 | + 'qty' => [self::STUB_ITEM_ID => self::STUB_WISHLIST_PRODUCT_QTY], |
| 285 | + 'description' => [self::STUB_ITEM_ID => 'Description for item_id 1'] |
| 286 | + ] |
| 287 | + ] |
| 288 | + ]; |
| 289 | + } |
| 290 | +} |
0 commit comments