StockResolverTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySalesApi\Test\Api;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Magento\Framework\Webapi\Rest\Request;
  10. use Magento\InventoryApi\Api\Data\StockInterface;
  11. use Magento\InventoryApi\Api\StockRepositoryInterface;
  12. use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
  13. use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
  14. use Magento\TestFramework\Helper\Bootstrap;
  15. use Magento\TestFramework\TestCase\WebapiAbstract;
  16. /**
  17. * Stock resolver api-functional test.
  18. */
  19. class StockResolverTest extends WebapiAbstract
  20. {
  21. /**
  22. * Path for REST request
  23. */
  24. const API_PATH = '/V1/inventory/stock-resolver';
  25. /**
  26. * Path for SOAP request
  27. */
  28. const SERVICE_NAME = 'inventorySalesApiStockResolverV1';
  29. /**
  30. * @var ObjectManagerInterface
  31. */
  32. private $objectManager;
  33. /**
  34. * @var StockRepositoryInterface
  35. */
  36. private $stockRepository;
  37. /**
  38. * @var SalesChannelInterfaceFactory
  39. */
  40. private $salesChannelFactory;
  41. /**
  42. * Create objects.
  43. */
  44. protected function setUp()
  45. {
  46. $this->objectManager = Bootstrap::getObjectManager();
  47. $this->stockRepository = $this->objectManager->get(StockRepositoryInterface::class);
  48. $this->salesChannelFactory = $this->objectManager->get(SalesChannelInterfaceFactory::class);
  49. }
  50. /**
  51. * Resolve stock and check data.
  52. *
  53. * @magentoApiDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  54. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  55. * @magentoApiDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  56. */
  57. public function testResolveCustomStock()
  58. {
  59. $serviceInfo = [
  60. 'rest' => [
  61. 'resourcePath' => self::API_PATH . '/website/eu_website',
  62. 'httpMethod' => Request::HTTP_METHOD_GET,
  63. ],
  64. 'soap' => [
  65. 'service' => self::SERVICE_NAME,
  66. 'operation' => self::SERVICE_NAME . 'Execute',
  67. ],
  68. ];
  69. $stockData = (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
  70. ? $this->_webApiCall($serviceInfo)
  71. : $this->_webApiCall($serviceInfo, [
  72. 'type' => 'website',
  73. 'code' => 'eu_website',
  74. ]);
  75. $this->assertEquals(10, $stockData['stock_id']);
  76. $this->assertEquals('EU-stock', $stockData['name']);
  77. $this->assertEquals(1, count($stockData['extension_attributes']['sales_channels']));
  78. $this->assertEquals(
  79. [
  80. 'type' => 'website',
  81. 'code' => 'eu_website'
  82. ],
  83. $stockData['extension_attributes']['sales_channels'][0]
  84. );
  85. }
  86. /**
  87. * Resolve stock and check data after sales channels was changed.
  88. *
  89. * @magentoApiDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  90. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  91. * @magentoApiDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  92. */
  93. public function testResolveCustomStockAfterChangeSalesChannelsTest()
  94. {
  95. $stockId = 20;
  96. $serviceInfo = [
  97. 'rest' => [
  98. 'resourcePath' => self::API_PATH . '/website/us_website',
  99. 'httpMethod' => Request::HTTP_METHOD_GET,
  100. ],
  101. 'soap' => [
  102. 'service' => self::SERVICE_NAME,
  103. 'operation' => self::SERVICE_NAME . 'Execute',
  104. ],
  105. ];
  106. $stockData = (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
  107. ? $this->_webApiCall($serviceInfo)
  108. : $this->_webApiCall($serviceInfo, [
  109. 'type' => 'website',
  110. 'code' => 'us_website',
  111. ]);
  112. $this->assertEquals($stockId, $stockData['stock_id']);
  113. $this->assertEquals('US-stock', $stockData['name']);
  114. $this->assertEquals(1, count($stockData['extension_attributes']['sales_channels']));
  115. $this->assertEquals(
  116. [
  117. 'type' => 'website',
  118. 'code' => 'us_website'
  119. ],
  120. $stockData['extension_attributes']['sales_channels'][0]
  121. );
  122. /** @var StockInterface $stock */
  123. $stock = $this->stockRepository->get($stockId);
  124. $salesChannel = $this->salesChannelFactory->create();
  125. $salesChannel->setCode('global_website');
  126. $salesChannel->setType(SalesChannelInterface::TYPE_WEBSITE);
  127. $stock->getExtensionAttributes()->setSalesChannels([$salesChannel]);
  128. $this->stockRepository->save($stock);
  129. $serviceInfo = [
  130. 'rest' => [
  131. 'resourcePath' => self::API_PATH . '/website/global_website',
  132. 'httpMethod' => Request::HTTP_METHOD_GET,
  133. ],
  134. 'soap' => [
  135. 'service' => self::SERVICE_NAME,
  136. 'operation' => self::SERVICE_NAME . 'Execute',
  137. ],
  138. ];
  139. $stockData = (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
  140. ? $this->_webApiCall($serviceInfo)
  141. : $this->_webApiCall($serviceInfo, [
  142. 'type' => 'website',
  143. 'code' => 'global_website',
  144. ]);
  145. $this->assertEquals($stockId, $stockData['stock_id']);
  146. $this->assertEquals('US-stock', $stockData['name']);
  147. $this->assertEquals(1, count($stockData['extension_attributes']['sales_channels']));
  148. $this->assertEquals(
  149. [
  150. 'type' => 'website',
  151. 'code' => 'global_website'
  152. ],
  153. $stockData['extension_attributes']['sales_channels'][0]
  154. );
  155. }
  156. /**
  157. * Get error when try resolve stock.
  158. */
  159. public function testResolveStockStockError()
  160. {
  161. $expectedMessage = 'No linked stock found';
  162. $serviceInfo = [
  163. 'rest' => [
  164. 'resourcePath' => self::API_PATH . '/website/test_stock_resolver_error',
  165. 'httpMethod' => Request::HTTP_METHOD_GET,
  166. ],
  167. 'soap' => [
  168. 'service' => self::SERVICE_NAME,
  169. 'operation' => self::SERVICE_NAME . 'Execute',
  170. ],
  171. ];
  172. try {
  173. TESTS_WEB_API_ADAPTER === self::ADAPTER_REST ? $this->_webApiCall($serviceInfo)
  174. : $this->_webApiCall($serviceInfo, ['type' => 'website', 'code' => 'test_stock_resolver_error']);
  175. $this->fail('Expected throwing exception');
  176. } catch (\Exception $e) {
  177. if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
  178. $this->assertEquals($expectedMessage, $this->processRestExceptionResult($e)['message']);
  179. } elseif (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
  180. $this->assertInstanceOf('SoapFault', $e);
  181. $this->assertEquals($expectedMessage, $e->getMessage());
  182. } else {
  183. throw $e;
  184. }
  185. }
  186. }
  187. }