Skip to content

Commit 7388bae

Browse files
author
Valeriy Nayda
authored
Merge pull request #84 from magento-engcom/source-stock-link-api
Web api of stock source links management
2 parents 0f55a40 + 90395fe commit 7388bae

File tree

8 files changed

+413
-4
lines changed

8 files changed

+413
-4
lines changed

app/code/Magento/InventoryApi/Api/AssignSourcesToStockInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\InventoryApi\Api;
77

88
/**
9-
* Interface to assign source id list by stock id
9+
* Assign Sources to Stock
1010
*
1111
* Used fully qualified namespaces in annotations for proper work of WebApi request parser
1212
*
@@ -15,7 +15,9 @@
1515
interface AssignSourcesToStockInterface
1616
{
1717
/**
18-
* Assign source id list by stock id
18+
* Assign Sources to Stock
19+
*
20+
* If one of the Sources or Stock with given id don't exist then exception will be throw
1921
*
2022
* @param int[] $sourceIds
2123
* @param int $stockId

app/code/Magento/InventoryApi/Api/GetAssignedSourcesForStockInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ interface GetAssignedSourcesForStockInterface
1717
/**
1818
* Get Sources assigned to Stock
1919
*
20+
* If Stock with given id doesn't exist then return an empty array
21+
*
2022
* @param int $stockId
2123
* @return \Magento\InventoryApi\Api\Data\SourceInterface[]
2224
* @throws \Magento\Framework\Exception\InputException

app/code/Magento/InventoryApi/Api/UnassignSourceFromStockInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\InventoryApi\Api;
77

88
/**
9-
* Unassign source from stock
9+
* Unassign Source from Stock
1010
*
1111
* Used fully qualified namespaces in annotations for proper work of WebApi request parser
1212
*
@@ -17,10 +17,13 @@ interface UnassignSourceFromStockInterface
1717
/**
1818
* Unassign source from stock
1919
*
20+
* If Source or Stock with given id doesn't exist then do nothing
21+
*
2022
* @param int $sourceId
2123
* @param int $stockId
2224
* @return void
2325
* @throws \Magento\Framework\Exception\InputException
26+
* @throws \Magento\Framework\Exception\CouldNotDeleteException
2427
*/
2528
public function execute($sourceId, $stockId);
2629
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 GetAssignedSourcesForStockTest 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+
/**#@-*/
21+
22+
/**
23+
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
24+
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
25+
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
26+
*/
27+
public function testGetAssignedSourcesForStock()
28+
{
29+
$stockId = 1;
30+
$serviceInfo = [
31+
'rest' => [
32+
'resourcePath' => self::RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK . '/' . $stockId,
33+
'httpMethod' => Request::HTTP_METHOD_GET,
34+
],
35+
'soap' => [
36+
'service' => self::SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK,
37+
'operation' => self::SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK . 'Execute',
38+
],
39+
];
40+
$response = (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST)
41+
? $this->_webApiCall($serviceInfo)
42+
: $this->_webApiCall($serviceInfo, ['stockId' => $stockId]);
43+
self::assertEquals([1, 2], array_column($response, SourceInterface::SOURCE_ID));
44+
}
45+
46+
/**
47+
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
48+
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
49+
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
50+
*/
51+
public function testGetAssignedSourcesWithNotNumericStockId()
52+
{
53+
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
54+
$this->markTestSkipped(
55+
'Test works only for REST adapter because in SOAP one stock_id would be converted'
56+
. ' into zero (zero is allowed input for service ner mind it\'s illigible value as'
57+
. ' there are no Stocks in the system with stock_id given)'
58+
);
59+
}
60+
$stockId = 'not_numeric';
61+
$serviceInfo = [
62+
'rest' => [
63+
'resourcePath' => self::RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK . '/' . $stockId,
64+
'httpMethod' => Request::HTTP_METHOD_GET,
65+
],
66+
];
67+
try {
68+
$this->_webApiCall($serviceInfo);
69+
$this->fail('Expected throwing exception');
70+
} catch (\Exception $e) {
71+
$errorData = $this->processRestExceptionResult($e);
72+
self::assertEquals('Invalid type for value: "not_numeric". Expected Type: "int".', $errorData['message']);
73+
self::assertEquals(Exception::HTTP_BAD_REQUEST, $e->getCode());
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)