|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\InventoryApi\Test\Api\StockSourceLink; |
| 7 | + |
| 8 | +use Magento\Framework\Webapi\Exception; |
| 9 | +use Magento\Framework\Webapi\Rest\Request; |
| 10 | +use Magento\InventoryApi\Api\Data\SourceInterface; |
| 11 | +use Magento\TestFramework\TestCase\WebapiAbstract; |
| 12 | + |
| 13 | +class AssignSourcesToStockTest extends WebapiAbstract |
| 14 | +{ |
| 15 | + /**#@+ |
| 16 | + * Service constants |
| 17 | + */ |
| 18 | + const RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK = '/V1/inventory/stock/get-assigned-sources'; |
| 19 | + const SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK = 'inventoryApiGetAssignedSourcesForStockV1'; |
| 20 | + const RESOURCE_PATH_ASSIGN_SOURCES_TO_STOCK = '/V1/inventory/stock/assign-sources'; |
| 21 | + const SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK = 'inventoryApiAssignSourcesToStockV1'; |
| 22 | + /**#@-*/ |
| 23 | + |
| 24 | + /** |
| 25 | + * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php |
| 26 | + * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock.php |
| 27 | + */ |
| 28 | + public function testAssignSourcesToStock() |
| 29 | + { |
| 30 | + $sourceIds = [1, 2]; |
| 31 | + $stockId = 1; |
| 32 | + $serviceInfo = [ |
| 33 | + 'rest' => [ |
| 34 | + 'resourcePath' => self::RESOURCE_PATH_ASSIGN_SOURCES_TO_STOCK . '/' . $stockId, |
| 35 | + 'httpMethod' => Request::HTTP_METHOD_POST, |
| 36 | + ], |
| 37 | + 'soap' => [ |
| 38 | + 'service' => self::SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK, |
| 39 | + 'operation' => self::SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK . 'Execute', |
| 40 | + ], |
| 41 | + ]; |
| 42 | + (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) |
| 43 | + ? $this->_webApiCall($serviceInfo, ['sourceIds' => $sourceIds]) |
| 44 | + : $this->_webApiCall($serviceInfo, ['sourceIds' => $sourceIds, 'stockId' => $stockId]); |
| 45 | + |
| 46 | + $assignedSourcesForStock = $this->getAssignedSourcesForStock($stockId); |
| 47 | + self::assertEquals($sourceIds, array_column($assignedSourcesForStock, SourceInterface::SOURCE_ID)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php |
| 52 | + * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock.php |
| 53 | + * @param string|array $sourceIds |
| 54 | + * @param string|int $stockId |
| 55 | + * @param array $expectedErrorData |
| 56 | + * @throws \Exception |
| 57 | + * @dataProvider dataProviderWrongParameters |
| 58 | + */ |
| 59 | + public function testAssignSourcesToStockWithWrongParameters($sourceIds, $stockId, array $expectedErrorData) |
| 60 | + { |
| 61 | + $serviceInfo = [ |
| 62 | + 'rest' => [ |
| 63 | + 'resourcePath' => self::RESOURCE_PATH_ASSIGN_SOURCES_TO_STOCK . '/' . $stockId, |
| 64 | + 'httpMethod' => Request::HTTP_METHOD_POST, |
| 65 | + ], |
| 66 | + 'soap' => [ |
| 67 | + 'service' => self::SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK, |
| 68 | + 'operation' => self::SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK . 'Execute', |
| 69 | + ], |
| 70 | + ]; |
| 71 | + try { |
| 72 | + (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) |
| 73 | + ? $this->_webApiCall($serviceInfo, ['sourceIds' => $sourceIds]) |
| 74 | + : $this->_webApiCall($serviceInfo, ['sourceIds' => $sourceIds, 'stockId' => $stockId]); |
| 75 | + $this->fail('Expected throwing exception'); |
| 76 | + } catch (\Exception $e) { |
| 77 | + if (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) { |
| 78 | + $errorData = $this->processRestExceptionResult($e); |
| 79 | + self::assertEquals($expectedErrorData['rest_message'], $errorData['message']); |
| 80 | + self::assertEquals(Exception::HTTP_BAD_REQUEST, $e->getCode()); |
| 81 | + } elseif (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) { |
| 82 | + $this->assertInstanceOf('SoapFault', $e); |
| 83 | + $this->checkSoapFault( |
| 84 | + $e, |
| 85 | + $expectedErrorData['soap_message'], |
| 86 | + 'env:Sender' |
| 87 | + ); |
| 88 | + } else { |
| 89 | + throw $e; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @return array |
| 96 | + */ |
| 97 | + public function dataProviderWrongParameters() |
| 98 | + { |
| 99 | + return [ |
| 100 | + 'not_numeric_stock_id' => [ |
| 101 | + [1, 2], |
| 102 | + 'not_numeric', |
| 103 | + [ |
| 104 | + 'rest_message' => 'Invalid type for value: "not_numeric". Expected Type: "int".', |
| 105 | + // During SOAP stock_id parameter will be converted to zero so error is different |
| 106 | + 'soap_message' => 'Could not assign Sources to Stock', |
| 107 | + ], |
| 108 | + ], |
| 109 | + 'nonexistent_stock_id' => [ |
| 110 | + [1, 2], |
| 111 | + -1, |
| 112 | + [ |
| 113 | + 'rest_message' => 'Could not assign Sources to Stock', |
| 114 | + 'soap_message' => 'Could not assign Sources to Stock', |
| 115 | + ], |
| 116 | + ], |
| 117 | + 'not_array_source_ids' => [ |
| 118 | + 'not_array', |
| 119 | + 1, |
| 120 | + [ |
| 121 | + 'rest_message' => 'Invalid type for value: "string". Expected Type: "int[]".', |
| 122 | + // During SOAP source_ids parameter will be converted to empty array so error is different |
| 123 | + 'soap_message' => 'Input data is invalid', |
| 124 | + ], |
| 125 | + ], |
| 126 | + 'empty_source_ids' => [ |
| 127 | + [], |
| 128 | + 1, |
| 129 | + [ |
| 130 | + 'rest_message' => 'Input data is invalid', |
| 131 | + 'soap_message' => 'Input data is invalid', |
| 132 | + ], |
| 133 | + ], |
| 134 | + 'nonexistent_source_id' => [ |
| 135 | + [-1, 2], |
| 136 | + 1, |
| 137 | + [ |
| 138 | + 'rest_message' => 'Could not assign Sources to Stock', |
| 139 | + 'soap_message' => 'Could not assign Sources to Stock', |
| 140 | + ], |
| 141 | + ], |
| 142 | + ]; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param int $stockId |
| 147 | + * @return array |
| 148 | + */ |
| 149 | + private function getAssignedSourcesForStock(int $stockId) |
| 150 | + { |
| 151 | + $serviceInfo = [ |
| 152 | + 'rest' => [ |
| 153 | + 'resourcePath' => self::RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK . '/' . $stockId, |
| 154 | + 'httpMethod' => Request::HTTP_METHOD_GET, |
| 155 | + ], |
| 156 | + 'soap' => [ |
| 157 | + 'service' => self::SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK, |
| 158 | + 'operation' => self::SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK . 'Execute', |
| 159 | + ], |
| 160 | + ]; |
| 161 | + $response = (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) |
| 162 | + ? $this->_webApiCall($serviceInfo) |
| 163 | + : $this->_webApiCall($serviceInfo, ['stockId' => $stockId]); |
| 164 | + return $response; |
| 165 | + } |
| 166 | +} |
0 commit comments