|
| 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\Wishlist\Test\Unit\ViewModel; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface; |
| 12 | +use Magento\CatalogInventory\Model\StockRegistry; |
| 13 | +use Magento\Store\Model\Store; |
| 14 | +use Magento\Wishlist\ViewModel\AllowedQuantity; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class AllowedQuantityTest |
| 20 | + */ |
| 21 | +class AllowedQuantityTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var AllowedQuantity |
| 25 | + */ |
| 26 | + private $sut; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var StockRegistry|MockObject |
| 30 | + */ |
| 31 | + private $stockRegistryMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ItemInterface|MockObject |
| 35 | + */ |
| 36 | + private $itemMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Product|MockObject |
| 40 | + */ |
| 41 | + private $productMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Store|MockObject |
| 45 | + */ |
| 46 | + private $storeMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * Set Up |
| 50 | + */ |
| 51 | + protected function setUp() |
| 52 | + { |
| 53 | + $this->stockRegistryMock = $this->createMock(StockRegistry::class); |
| 54 | + $this->itemMock = $this->getMockForAbstractClass( |
| 55 | + ItemInterface::class, |
| 56 | + [], |
| 57 | + '', |
| 58 | + false, |
| 59 | + true, |
| 60 | + true, |
| 61 | + ['getMinSaleQty', 'getMaxSaleQty'] |
| 62 | + ); |
| 63 | + $this->productMock = $this->getMockBuilder(Product::class) |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->getMock(); |
| 66 | + $this->storeMock = $this->getMockBuilder(Store::class) |
| 67 | + ->disableOriginalConstructor() |
| 68 | + ->getMock(); |
| 69 | + |
| 70 | + $this->sut = new AllowedQuantity( |
| 71 | + $this->stockRegistryMock |
| 72 | + ); |
| 73 | + $this->sut->setItem($this->itemMock); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Getting min and max qty test. |
| 78 | + * |
| 79 | + * @dataProvider saleQuantityDataProvider |
| 80 | + * |
| 81 | + * @param int $minSaleQty |
| 82 | + * @param int $maxSaleQty |
| 83 | + * @param array $expectedResult |
| 84 | + */ |
| 85 | + public function testGettingMinMaxQty(int $minSaleQty, int $maxSaleQty, array $expectedResult) |
| 86 | + { |
| 87 | + $this->storeMock->expects($this->atLeastOnce()) |
| 88 | + ->method('getWebsiteId') |
| 89 | + ->willReturn(1); |
| 90 | + $this->productMock->expects($this->atLeastOnce()) |
| 91 | + ->method('getId') |
| 92 | + ->willReturn(1); |
| 93 | + $this->productMock->expects($this->atLeastOnce()) |
| 94 | + ->method('getStore') |
| 95 | + ->willReturn($this->storeMock); |
| 96 | + $this->itemMock->expects($this->any()) |
| 97 | + ->method('getProduct') |
| 98 | + ->willReturn($this->productMock); |
| 99 | + $this->itemMock->expects($this->any()) |
| 100 | + ->method('getMinSaleQty') |
| 101 | + ->willReturn($minSaleQty); |
| 102 | + $this->itemMock->expects($this->any()) |
| 103 | + ->method('getMaxSaleQty') |
| 104 | + ->willReturn($maxSaleQty); |
| 105 | + $this->stockRegistryMock->expects($this->any()) |
| 106 | + ->method('getStockItem') |
| 107 | + ->will($this->returnValue($this->itemMock)); |
| 108 | + |
| 109 | + $result = $this->sut->getMinMaxQty(); |
| 110 | + |
| 111 | + $this->assertEquals($result, $expectedResult); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Sales quantity provider |
| 116 | + * |
| 117 | + * @return array |
| 118 | + */ |
| 119 | + public function saleQuantityDataProvider(): array |
| 120 | + { |
| 121 | + return [ |
| 122 | + [ |
| 123 | + 1, |
| 124 | + 10, |
| 125 | + [ |
| 126 | + 'minAllowed' => 1, |
| 127 | + 'maxAllowed' => 10 |
| 128 | + ] |
| 129 | + ], [ |
| 130 | + 1, |
| 131 | + 0, |
| 132 | + [ |
| 133 | + 'minAllowed' => 1, |
| 134 | + 'maxAllowed' => 99999999 |
| 135 | + ] |
| 136 | + ] |
| 137 | + ]; |
| 138 | + } |
| 139 | +} |
0 commit comments