|
| 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\CatalogInventoryGraphQl\Test\Unit\Model\Resolver; |
| 9 | + |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Magento\CatalogInventoryGraphQl\Model\Resolver\OnlyXLeftInStockResolver; |
| 12 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 13 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 14 | +use Magento\GraphQl\Model\Query\ContextInterface; |
| 15 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 16 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 17 | +use Magento\CatalogInventory\Api\StockRegistryInterface; |
| 18 | +use Magento\Catalog\Model\Product; |
| 19 | +use Magento\Store\Api\Data\StoreInterface; |
| 20 | +use Magento\CatalogInventory\Api\Data\StockItemInterface; |
| 21 | +use Magento\CatalogInventory\Api\Data\StockStatusInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test class for \Magento\CatalogInventoryGraphQl\Model\Resolver\OnlyXLeftInStockResolver |
| 25 | + */ |
| 26 | +class OnlyXLeftInStockResolverTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Object Manager Instance |
| 30 | + * |
| 31 | + * @var ObjectManager |
| 32 | + */ |
| 33 | + private $objectManager; |
| 34 | + |
| 35 | + /** |
| 36 | + * Testable Object |
| 37 | + * |
| 38 | + * @var RevokeCustomerToken |
| 39 | + */ |
| 40 | + private $resolver; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ContextInterface|MockObject |
| 44 | + */ |
| 45 | + private $contextMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Field|MockObject |
| 49 | + */ |
| 50 | + private $fieldMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var ResolveInfo|MockObject |
| 54 | + */ |
| 55 | + private $resolveInfoMock; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var ScopeConfigInterface|MockObject |
| 59 | + */ |
| 60 | + private $scopeConfigMock; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var StockRegistryInterface|MockObject |
| 64 | + */ |
| 65 | + private $stockRegistryMock; |
| 66 | + |
| 67 | + /** |
| 68 | + * @var Product|MockObject |
| 69 | + */ |
| 70 | + private $productModelMock; |
| 71 | + |
| 72 | + /** |
| 73 | + * @var StoreInterface|MockObject |
| 74 | + */ |
| 75 | + private $storeMock; |
| 76 | + |
| 77 | + /** |
| 78 | + * @var StockItemInterface|MockObject |
| 79 | + */ |
| 80 | + private $stockItemMock; |
| 81 | + |
| 82 | + /** |
| 83 | + * @var StockStatusInterface|MockObject |
| 84 | + */ |
| 85 | + private $stockStatusMock; |
| 86 | + |
| 87 | + /** |
| 88 | + * @inheritdoc |
| 89 | + */ |
| 90 | + |
| 91 | + protected function setUp(): void |
| 92 | + { |
| 93 | + $this->objectManager = new ObjectManager($this); |
| 94 | + |
| 95 | + $this->contextMock = $this->getMockBuilder(ContextInterface::class) |
| 96 | + ->disableOriginalConstructor() |
| 97 | + ->getMockForAbstractClass(); |
| 98 | + |
| 99 | + $this->fieldMock = $this->getMockBuilder(Field::class) |
| 100 | + ->disableOriginalConstructor() |
| 101 | + ->getMock(); |
| 102 | + |
| 103 | + $this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class) |
| 104 | + ->disableOriginalConstructor() |
| 105 | + ->getMock(); |
| 106 | + |
| 107 | + $this->productModelMock = $this->getMockBuilder(Product::class) |
| 108 | + ->disableOriginalConstructor() |
| 109 | + ->getMock(); |
| 110 | + |
| 111 | + $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)->getMock(); |
| 112 | + $this->stockRegistryMock = $this->getMockBuilder(StockRegistryInterface::class)->getMock(); |
| 113 | + $this->storeMock = $this->getMockBuilder(StoreInterface::class)->getMock(); |
| 114 | + $this->stockItemMock = $this->getMockBuilder(StockItemInterface::class)->getMock(); |
| 115 | + $this->stockStatusMock = $this->getMockBuilder(StockStatusInterface::class)->getMock(); |
| 116 | + $this->productModelMock->expects($this->any())->method('getId') |
| 117 | + ->willReturn(1); |
| 118 | + $this->productModelMock->expects($this->once())->method('getStore') |
| 119 | + ->willReturn($this->storeMock); |
| 120 | + $this->stockRegistryMock->expects($this->once())->method('getStockStatus') |
| 121 | + ->willReturn($this->stockStatusMock); |
| 122 | + $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); |
| 123 | + |
| 124 | + $this->resolver = $this->objectManager->getObject( |
| 125 | + OnlyXLeftInStockResolver::class, |
| 126 | + [ |
| 127 | + 'scopeConfig' => $this->scopeConfigMock, |
| 128 | + 'stockRegistry' => $this->stockRegistryMock |
| 129 | + ] |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + public function testResolve() |
| 134 | + { |
| 135 | + $stockCurrentQty = 3; |
| 136 | + $minQty = 2; |
| 137 | + $thresholdQty = 1; |
| 138 | + |
| 139 | + $this->stockItemMock->expects($this->once())->method('getMinQty') |
| 140 | + ->willReturn($minQty); |
| 141 | + $this->stockStatusMock->expects($this->once())->method('getQty') |
| 142 | + ->willReturn($stockCurrentQty); |
| 143 | + $this->stockRegistryMock->expects($this->once())->method('getStockItem') |
| 144 | + ->willReturn($this->stockItemMock); |
| 145 | + $this->scopeConfigMock->method('getValue')->willReturn($thresholdQty); |
| 146 | + |
| 147 | + $this->assertEquals( |
| 148 | + $stockCurrentQty, |
| 149 | + $this->resolver->resolve( |
| 150 | + $this->fieldMock, |
| 151 | + $this->contextMock, |
| 152 | + $this->resolveInfoMock, |
| 153 | + ['model' => $this->productModelMock] |
| 154 | + ) |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + public function testResolveOutStock() |
| 159 | + { |
| 160 | + $stockCurrentQty = 0; |
| 161 | + $minQty = 2; |
| 162 | + $thresholdQty = 1; |
| 163 | + $this->stockItemMock->expects($this->once())->method('getMinQty') |
| 164 | + ->willReturn($minQty); |
| 165 | + $this->stockStatusMock->expects($this->once())->method('getQty') |
| 166 | + ->willReturn($stockCurrentQty); |
| 167 | + $this->stockRegistryMock->expects($this->once())->method('getStockItem') |
| 168 | + ->willReturn($this->stockItemMock); |
| 169 | + $this->scopeConfigMock->method('getValue')->willReturn($thresholdQty); |
| 170 | + |
| 171 | + $this->assertEquals( |
| 172 | + 0, |
| 173 | + $this->resolver->resolve( |
| 174 | + $this->fieldMock, |
| 175 | + $this->contextMock, |
| 176 | + $this->resolveInfoMock, |
| 177 | + ['model' => $this->productModelMock] |
| 178 | + ) |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + public function testResolveNoThresholdQty() |
| 183 | + { |
| 184 | + $stockCurrentQty = 3; |
| 185 | + $minQty = 2; |
| 186 | + $thresholdQty = null; |
| 187 | + $this->stockItemMock->expects($this->once())->method('getMinQty') |
| 188 | + ->willReturn($minQty); |
| 189 | + $this->stockStatusMock->expects($this->once())->method('getQty') |
| 190 | + ->willReturn($stockCurrentQty); |
| 191 | + $this->stockRegistryMock->expects($this->once())->method('getStockItem') |
| 192 | + ->willReturn($this->stockItemMock); |
| 193 | + $this->scopeConfigMock->method('getValue')->willReturn($thresholdQty); |
| 194 | + |
| 195 | + $this->assertEquals( |
| 196 | + null, |
| 197 | + $this->resolver->resolve( |
| 198 | + $this->fieldMock, |
| 199 | + $this->contextMock, |
| 200 | + $this->resolveInfoMock, |
| 201 | + ['model' => $this->productModelMock] |
| 202 | + ) |
| 203 | + ); |
| 204 | + } |
| 205 | +} |
0 commit comments