Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web api of stock source links management #84

Merged
merged 1 commit into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Magento\InventoryApi\Api;

/**
* Interface to assign source id list by stock id
* Assign Sources to Stock
*
* Used fully qualified namespaces in annotations for proper work of WebApi request parser
*
Expand All @@ -15,7 +15,9 @@
interface AssignSourcesToStockInterface
{
/**
* Assign source id list by stock id
* Assign Sources to Stock
*
* If one of the Sources or Stock with given id don't exist then exception will be throw
*
* @param int[] $sourceIds
* @param int $stockId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ interface GetAssignedSourcesForStockInterface
/**
* Get Sources assigned to Stock
*
* If Stock with given id doesn't exist then return an empty array
*
* @param int $stockId
* @return \Magento\InventoryApi\Api\Data\SourceInterface[]
* @throws \Magento\Framework\Exception\InputException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Magento\InventoryApi\Api;

/**
* Unassign source from stock
* Unassign Source from Stock
*
* Used fully qualified namespaces in annotations for proper work of WebApi request parser
*
Expand All @@ -17,10 +17,13 @@ interface UnassignSourceFromStockInterface
/**
* Unassign source from stock
*
* If Source or Stock with given id doesn't exist then do nothing
*
* @param int $sourceId
* @param int $stockId
* @return void
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\CouldNotDeleteException
*/
public function execute($sourceId, $stockId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\InventoryApi\Test\Api\StockSourceLink;

use Magento\Framework\Webapi\Exception;
use Magento\Framework\Webapi\Rest\Request;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\TestFramework\TestCase\WebapiAbstract;

class AssignSourcesToStockTest extends WebapiAbstract
{
/**#@+
* Service constants
*/
const RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK = '/V1/inventory/stock/get-assigned-sources';
const SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK = 'inventoryApiGetAssignedSourcesForStockV1';
const RESOURCE_PATH_ASSIGN_SOURCES_TO_STOCK = '/V1/inventory/stock/assign-sources';
const SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK = 'inventoryApiAssignSourcesToStockV1';
/**#@-*/

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock.php
*/
public function testAssignSourcesToStock()
{
$sourceIds = [1, 2];
$stockId = 1;
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH_ASSIGN_SOURCES_TO_STOCK . '/' . $stockId,
'httpMethod' => Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK,
'operation' => self::SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK . 'Execute',
],
];
(TESTS_WEB_API_ADAPTER == self::ADAPTER_REST)
? $this->_webApiCall($serviceInfo, ['sourceIds' => $sourceIds])
: $this->_webApiCall($serviceInfo, ['sourceIds' => $sourceIds, 'stockId' => $stockId]);

$assignedSourcesForStock = $this->getAssignedSourcesForStock($stockId);
self::assertEquals($sourceIds, array_column($assignedSourcesForStock, SourceInterface::SOURCE_ID));
}

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock.php
* @param string|array $sourceIds
* @param string|int $stockId
* @param array $expectedErrorData
* @throws \Exception
* @dataProvider dataProviderWrongParameters
*/
public function testAssignSourcesToStockWithWrongParameters($sourceIds, $stockId, array $expectedErrorData)
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH_ASSIGN_SOURCES_TO_STOCK . '/' . $stockId,
'httpMethod' => Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK,
'operation' => self::SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK . 'Execute',
],
];
try {
(TESTS_WEB_API_ADAPTER == self::ADAPTER_REST)
? $this->_webApiCall($serviceInfo, ['sourceIds' => $sourceIds])
: $this->_webApiCall($serviceInfo, ['sourceIds' => $sourceIds, 'stockId' => $stockId]);
$this->fail('Expected throwing exception');
} catch (\Exception $e) {
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) {
$errorData = $this->processRestExceptionResult($e);
self::assertEquals($expectedErrorData['rest_message'], $errorData['message']);
self::assertEquals(Exception::HTTP_BAD_REQUEST, $e->getCode());
} elseif (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
$this->assertInstanceOf('SoapFault', $e);
$this->checkSoapFault(
$e,
$expectedErrorData['soap_message'],
'env:Sender'
);
} else {
throw $e;
}
}
}

/**
* @return array
*/
public function dataProviderWrongParameters()
{
return [
'not_numeric_stock_id' => [
[1, 2],
'not_numeric',
[
'rest_message' => 'Invalid type for value: "not_numeric". Expected Type: "int".',
// During SOAP stock_id parameter will be converted to zero so error is different
'soap_message' => 'Could not assign Sources to Stock',
],
],
'nonexistent_stock_id' => [
[1, 2],
-1,
[
'rest_message' => 'Could not assign Sources to Stock',
'soap_message' => 'Could not assign Sources to Stock',
],
],
'not_array_source_ids' => [
'not_array',
1,
[
'rest_message' => 'Invalid type for value: "string". Expected Type: "int[]".',
// During SOAP source_ids parameter will be converted to empty array so error is different
'soap_message' => 'Input data is invalid',
],
],
'empty_source_ids' => [
[],
1,
[
'rest_message' => 'Input data is invalid',
'soap_message' => 'Input data is invalid',
],
],
'nonexistent_source_id' => [
[-1, 2],
1,
[
'rest_message' => 'Could not assign Sources to Stock',
'soap_message' => 'Could not assign Sources to Stock',
],
],
];
}

/**
* @param int $stockId
* @return array
*/
private function getAssignedSourcesForStock(int $stockId)
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK . '/' . $stockId,
'httpMethod' => Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK,
'operation' => self::SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK . 'Execute',
],
];
$response = (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST)
? $this->_webApiCall($serviceInfo)
: $this->_webApiCall($serviceInfo, ['stockId' => $stockId]);
return $response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\InventoryApi\Test\Api\StockSourceLink;

use Magento\Framework\Webapi\Exception;
use Magento\Framework\Webapi\Rest\Request;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\TestFramework\TestCase\WebapiAbstract;

class GetAssignedSourcesForStockTest extends WebapiAbstract
{
/**#@+
* Service constants
*/
const RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK = '/V1/inventory/stock/get-assigned-sources';
const SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK = 'inventoryApiGetAssignedSourcesForStockV1';
/**#@-*/

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
*/
public function testGetAssignedSourcesForStock()
{
$stockId = 1;
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK . '/' . $stockId,
'httpMethod' => Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK,
'operation' => self::SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK . 'Execute',
],
];
$response = (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST)
? $this->_webApiCall($serviceInfo)
: $this->_webApiCall($serviceInfo, ['stockId' => $stockId]);
self::assertEquals([1, 2], array_column($response, SourceInterface::SOURCE_ID));
}

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
*/
public function testGetAssignedSourcesWithNotNumericStockId()
{
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
$this->markTestSkipped(
'Test works only for REST adapter because in SOAP one stock_id would be converted'
. ' into zero (zero is allowed input for service ner mind it\'s illigible value as'
. ' there are no Stocks in the system with stock_id given)'
);
}
$stockId = 'not_numeric';
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK . '/' . $stockId,
'httpMethod' => Request::HTTP_METHOD_GET,
],
];
try {
$this->_webApiCall($serviceInfo);
$this->fail('Expected throwing exception');
} catch (\Exception $e) {
$errorData = $this->processRestExceptionResult($e);
self::assertEquals('Invalid type for value: "not_numeric". Expected Type: "int".', $errorData['message']);
self::assertEquals(Exception::HTTP_BAD_REQUEST, $e->getCode());
}
}
}
Loading