SourceSelectionServiceTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\InventorySourceSelectionApi\Test\Api;
  8. use Magento\Framework\Webapi\Rest\Request;
  9. use Magento\TestFramework\TestCase\WebapiAbstract;
  10. use Magento\TestFramework\Assert\AssertArrayContains;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. use Magento\InventorySourceSelectionApi\Api\GetDefaultSourceSelectionAlgorithmCodeInterface;
  13. class SourceSelectionServiceTest extends WebapiAbstract
  14. {
  15. /**#@+
  16. * Service constants
  17. */
  18. const RESOURCE_PATH = '/V1/inventory/source-selection-algorithm-result';
  19. const SERVICE_NAME = 'inventorySourceSelectionApiSourceSelectionServiceV1';
  20. /**#@-*/
  21. /**
  22. * @var GetDefaultSourceSelectionAlgorithmCodeInterface
  23. */
  24. private $defaultAlgorithmCode;
  25. protected function setUp()
  26. {
  27. parent::setUp();
  28. $this->defaultAlgorithmCode = Bootstrap::getObjectManager()->get(
  29. GetDefaultSourceSelectionAlgorithmCodeInterface::class
  30. );
  31. }
  32. /**
  33. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  34. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  35. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  36. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  37. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  38. */
  39. public function testSourceSelectionService()
  40. {
  41. $inventoryRequest = [
  42. 'stockId' => 10,
  43. 'items' => [
  44. [
  45. 'sku' => 'SKU-1',
  46. 'qty' => 8
  47. ],
  48. [
  49. 'sku' => 'SKU-4',
  50. 'qty' => 4
  51. ]
  52. ]
  53. ];
  54. $expectedResultData = [
  55. 'source_selection_items' => [
  56. [
  57. 'source_code' => 'eu-1',
  58. 'sku' => 'SKU-1',
  59. 'qty_to_deduct' => 5.5,
  60. 'qty_available' => 5.5
  61. ],
  62. [
  63. 'source_code' => 'eu-2',
  64. 'sku' => 'SKU-1',
  65. 'qty_to_deduct' => 2.5,
  66. 'qty_available' => 3
  67. ],
  68. [
  69. 'source_code' => 'eu-2',
  70. 'sku' => 'SKU-4',
  71. 'qty_to_deduct' => 4,
  72. 'qty_available' => 6
  73. ],
  74. ],
  75. 'shippable' => 1
  76. ];
  77. $algorithmCode = $this->defaultAlgorithmCode->execute();
  78. $requestData = [
  79. 'inventoryRequest' => $inventoryRequest,
  80. 'algorithmCode' => $algorithmCode
  81. ];
  82. $serviceInfo = [
  83. 'rest' => [
  84. 'resourcePath' => self::RESOURCE_PATH,
  85. 'httpMethod' => Request::HTTP_METHOD_POST,
  86. ],
  87. 'soap' => [
  88. 'service' => self::SERVICE_NAME,
  89. 'operation' => self::SERVICE_NAME . 'Execute',
  90. ],
  91. ];
  92. $sourceSelectionAlgorithmResult = (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
  93. ? $this->_webApiCall($serviceInfo, $requestData)
  94. : $this->_webApiCall($serviceInfo, $requestData);
  95. self::assertInternalType('array', $sourceSelectionAlgorithmResult);
  96. self::assertNotEmpty($sourceSelectionAlgorithmResult);
  97. AssertArrayContains::assert($expectedResultData, $sourceSelectionAlgorithmResult);
  98. }
  99. }