Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockRegistryTest.php
Go to the documentation of this file.
1 <?php
7 
9 
15 class StockRegistryTest extends \PHPUnit\Framework\TestCase
16 {
19 
23  protected $stockRegistry;
24 
29 
33  protected $stock;
34 
38  protected $stockItem;
39 
43  protected $stockStatus;
44 
48  protected $productFactory;
49 
54 
58  protected $product;
59 
60  protected $productId = 111;
61  protected $productSku = 'simple';
62  protected $websiteId = 111;
63 
64  protected function setUp()
65  {
66  $this->objectManagerHelper = new ObjectManagerHelper($this);
67 
68  $this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['__wakeup', 'getIdBySku']);
69  $this->product->expects($this->any())
70  ->method('getIdBySku')
71  ->willReturn($this->productId);
72  //getIdBySku
73  $this->productFactory = $this->createPartialMock(\Magento\Catalog\Model\ProductFactory::class, ['create']);
74  $this->productFactory->expects($this->any())
75  ->method('create')
76  ->will($this->returnValue($this->product));
77 
78  $this->stock = $this->getMockForAbstractClass(
79  \Magento\CatalogInventory\Api\Data\StockInterface::class,
80  ['__wakeup'],
81  '',
82  false
83  );
84  $this->stockItem = $this->getMockBuilder(\Magento\CatalogInventory\Api\Data\StockItemInterface::class)
85  ->setMethods(['setProductId', 'getData', 'addData', 'getItemId', 'getWebsiteId'])
86  ->disableOriginalConstructor()
87  ->getMockForAbstractClass();
88  $this->stockStatus = $this->getMockForAbstractClass(
89  \Magento\CatalogInventory\Api\Data\StockStatusInterface::class,
90  ['__wakeup'],
91  '',
92  false
93  );
94 
95  $this->stockRegistryProvider = $this->getMockForAbstractClass(
96  \Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface::class,
97  ['getStock', 'getStockItem', 'getStockStatus'],
98  '',
99  false
100  );
101  $this->stockRegistryProvider->expects($this->any())
102  ->method('getStock')
103  ->will($this->returnValue($this->stock));
104  $this->stockRegistryProvider->expects($this->any())
105  ->method('getStockItem')
106  ->will($this->returnValue($this->stockItem));
107  $this->stockRegistryProvider->expects($this->any())
108  ->method('getStockStatus')
109  ->will($this->returnValue($this->stockStatus));
110 
111  $this->stockItemRepository = $this->getMockForAbstractClass(
112  \Magento\CatalogInventory\Api\StockItemRepositoryInterface::class,
113  ['save'],
114  '',
115  false
116  );
117  $this->stockItemRepository->expects($this->any())
118  ->method('save')
119  ->will($this->returnValue($this->stockItem));
120 
121  $this->stockRegistry = $this->objectManagerHelper->getObject(
122  \Magento\CatalogInventory\Model\StockRegistry::class,
123  [
124  'stockRegistryProvider' => $this->stockRegistryProvider,
125  'productFactory' => $this->productFactory,
126  'stockItemRepository' => $this->stockItemRepository
127  ]
128  );
129  }
130 
131  protected function tearDown()
132  {
133  $this->stockRegistry = null;
134  }
135 
136  public function testGetStock()
137  {
138  $this->assertEquals($this->stock, $this->stockRegistry->getStock($this->websiteId));
139  }
140 
141  public function testGetStockItem()
142  {
143  $this->assertEquals($this->stockItem, $this->stockRegistry->getStockItem($this->productId, $this->websiteId));
144  }
145 
146  public function testGetStockItemBySku()
147  {
148  $this->assertEquals(
149  $this->stockItem,
150  $this->stockRegistry->getStockItemBySku($this->productSku, $this->websiteId)
151  );
152  }
153 
154  public function testGetStockStatus()
155  {
156  $this->assertEquals(
157  $this->stockStatus,
158  $this->stockRegistry->getStockStatus($this->productId, $this->websiteId)
159  );
160  }
161 
162  public function testGetStockStatusBySku()
163  {
164  $this->assertEquals(
165  $this->stockStatus,
166  $this->stockRegistry->getStockStatus($this->productId, $this->websiteId)
167  );
168  }
169 
170  public function testUpdateStockItemBySku()
171  {
172  $itemId = 1;
173  $this->stockItem->expects($this->once())->method('setProductId')->willReturnSelf();
174  $this->stockItem->expects($this->once())->method('getData')->willReturn([]);
175  $this->stockItem->expects($this->once())->method('addData')->willReturnSelf();
176  $this->stockItem->expects($this->atLeastOnce())->method('getItemId')->willReturn($itemId);
177  $this->assertEquals(
178  $itemId,
179  $this->stockRegistry->updateStockItemBySku($this->productSku, $this->stockItem)
180  );
181  }
182 }