Skip to content

Commit

Permalink
Merge pull request #136 from magento-engcom/source-item-webapi
Browse files Browse the repository at this point in the history
Source item webapi
  • Loading branch information
Valeriy Nayda authored Nov 9, 2017
2 parents 00695bb + 2c3a75e commit 9d4984f
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryApi\Test\Api\SourceItemRepository;

use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Webapi\Rest\Request;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\TestFramework\Assert\AssertArrayContains;
use Magento\TestFramework\TestCase\WebapiAbstract;

class GetListTest extends WebapiAbstract
{
/**#@+
* Service constants
*/
const RESOURCE_PATH = '/V1/inventory/source-item';
const SERVICE_NAME = 'inventoryApiSourceItemRepositoryV1';
/**#@-*/

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
*/
public function testGetList()
{
$requestData = [
'searchCriteria' => [
SearchCriteria::FILTER_GROUPS => [
[
'filters' => [
[
'field' => SourceItemInterface::SKU,
'value' => 'SKU-1',
'condition_type' => 'eq',
],
],
],
],
SearchCriteria::SORT_ORDERS => [
[
'field' => SourceItemInterface::QUANTITY,
'direction' => SortOrder::SORT_DESC,
],
],
SearchCriteria::CURRENT_PAGE => 2,
SearchCriteria::PAGE_SIZE => 2,
],
];
$expectedTotalCount = 4;
$expectedItemsData = [
[
SourceItemInterface::SOURCE_ID => 10,
SourceItemInterface::SKU => 'SKU-1',
SourceItemInterface::QUANTITY => 5.5,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
],
[
SourceItemInterface::SOURCE_ID => 20,
SourceItemInterface::SKU => 'SKU-1',
SourceItemInterface::QUANTITY => 3,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
],
];

$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($requestData),
'httpMethod' => Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'operation' => self::SERVICE_NAME . 'GetList',
],
];
$response = (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
? $this->_webApiCall($serviceInfo)
: $this->_webApiCall($serviceInfo, $requestData);

AssertArrayContains::assert($requestData['searchCriteria'], $response['search_criteria']);
self::assertEquals($expectedTotalCount, $response['total_count']);
AssertArrayContains::assert($expectedItemsData, $response['items']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SourceItemsDeleteTest extends WebapiAbstract
/**#@+
* Service constants
*/
const RESOURCE_PATH = '/V1/inventory/source-items';
const RESOURCE_PATH = '/V1/inventory/source-item';
const SERVICE_NAME = 'inventoryApiSourceItemsDeleteV1';
/**#@-*/

Expand Down
115 changes: 115 additions & 0 deletions app/code/Magento/InventoryApi/Test/Api/SourceItemsSave/SaveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryApi\Test\Api\SourceItemsSave;

use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Webapi\Rest\Request;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\TestFramework\Assert\AssertArrayContains;
use Magento\TestFramework\TestCase\WebapiAbstract;

class SaveTest extends WebapiAbstract
{
/**#@+
* Service constants
*/
const RESOURCE_PATH = '/V1/inventory/source-item';
const SERVICE_NAME = 'inventoryApiSourceItemsSaveV1';
/**#@-*/

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
*/
public function testExecute()
{
$sourceItems = [
[
SourceItemInterface::SOURCE_ID => 10,
SourceItemInterface::SKU => 'SKU-1',
SourceItemInterface::QUANTITY => 5.5,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
],
[
SourceItemInterface::SOURCE_ID => 20,
SourceItemInterface::SKU => 'SKU-1',
SourceItemInterface::QUANTITY => 3,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
],
];

$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'operation' => self::SERVICE_NAME . 'Execute',
],
];
$this->_webApiCall($serviceInfo, ['sourceItems' => $sourceItems]);

$actualData = $this->getSourceItems();

self::assertEquals(2, $actualData['total_count']);
AssertArrayContains::assert($sourceItems, $actualData['items']);
}

protected function tearDown()
{
$sourceItems = [
[
SourceItemInterface::SOURCE_ID => 10,
SourceItemInterface::SKU => 'SKU-1',
],
[
SourceItemInterface::SOURCE_ID => 20,
SourceItemInterface::SKU => 'SKU-1',
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '?'
. http_build_query(['sourceItems' => $sourceItems]),
'httpMethod' => Request::HTTP_METHOD_DELETE,
],
'soap' => [
'service' => self::SERVICE_NAME,
'operation' => self::SERVICE_NAME . 'Execute',
],
];
(TESTS_WEB_API_ADAPTER == self::ADAPTER_REST)
? $this->_webApiCall($serviceInfo)
: $this->_webApiCall($serviceInfo, ['sourceItems' => $sourceItems]);
parent::tearDown();
}

/**
* @return array
*/
private function getSourceItems(): array
{
$requestData = [
'searchCriteria' => [SearchCriteria::PAGE_SIZE => 10],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($requestData),
'httpMethod' => Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => 'inventoryApiSourceItemRepositoryV1',
'operation' => 'inventoryApiSourceItemRepositoryV1GetList',
],
];
return (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
? $this->_webApiCall($serviceInfo)
: $this->_webApiCall($serviceInfo, $requestData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ValidationTest extends WebapiAbstract
/**#@+
* Service constants
*/
const RESOURCE_PATH = '/V1/inventory/source-items';
const RESOURCE_PATH = '/V1/inventory/source-item';
const SERVICE_NAME = 'inventoryApiSourceItemsSaveV1';
/**#@-*/

Expand Down Expand Up @@ -54,7 +54,7 @@ public function testCreateWithMissedRequiredFields(string $field, array $expecte
'operation' => self::SERVICE_NAME . 'Execute',
],
];
$this->webApiCall($serviceInfo, $data, $expectedErrorData);
$this->webApiCall($serviceInfo, [$data], $expectedErrorData);
}

/**
Expand Down Expand Up @@ -145,11 +145,12 @@ public function testFailedValidationOnCreate(string $field, $value, array $expec
'operation' => self::SERVICE_NAME . 'Execute',
],
];
$this->webApiCall($serviceInfo, $data, $expectedErrorData);
$this->webApiCall($serviceInfo, [$data], $expectedErrorData);
}

/**
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function failedValidationDataProvider(): array
{
Expand Down Expand Up @@ -244,6 +245,13 @@ public function failedValidationDataProvider(): array
],
],
],
'not_exists_' . SourceItemInterface::SOURCE_ID => [
SourceItemInterface::SOURCE_ID,
100,
[
'message' => 'Could not save Source Item',
],
],
];
}

Expand Down Expand Up @@ -277,7 +285,7 @@ public function testFailedValidationOnCreateRelatedOnlyForRest(string $field, $v
'operation' => self::SERVICE_NAME . 'Execute',
],
];
$this->webApiCall($serviceInfo, $data, $expectedErrorData);
$this->webApiCall($serviceInfo, [$data], $expectedErrorData);
}

/**
Expand Down Expand Up @@ -321,17 +329,39 @@ public function failedValidationRelatedOnlyForRestDataProvider(): array
];
}

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
*/
public function testCreateWithEmptyData()
{
$sourceItems = [];
$expectedErrorData = ['message' => 'Input data is empty'];

$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'operation' => self::SERVICE_NAME . 'Execute',
],
];
$this->webApiCall($serviceInfo, $sourceItems, $expectedErrorData);
}

/**
* @param array $serviceInfo
* @param array $data
* @param array $sourceItems
* @param array $expectedErrorData
* @return void
* @throws \Exception
*/
private function webApiCall(array $serviceInfo, array $data, array $expectedErrorData)
private function webApiCall(array $serviceInfo, array $sourceItems, array $expectedErrorData)
{
try {
$this->_webApiCall($serviceInfo, ['sourceItems' => [$data]]);
$this->_webApiCall($serviceInfo, ['sourceItems' => $sourceItems]);
$this->fail('Expected throwing exception');
} catch (\Exception $exception) {
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/InventoryApi/etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,19 @@
</resources>
</route>
<!-- SourceItem -->
<route url="/V1/inventory/source-items" method="GET">
<route url="/V1/inventory/source-item" method="GET">
<service class="Magento\InventoryApi\Api\SourceItemRepositoryInterface" method="getList"/>
<resources>
<resource ref="Magento_InventoryApi::source"/>
</resources>
</route>
<route url="/V1/inventory/source-items" method="POST">
<route url="/V1/inventory/source-item" method="POST">
<service class="Magento\InventoryApi\Api\SourceItemsSaveInterface" method="execute"/>
<resources>
<resource ref="Magento_InventoryApi::source"/>
</resources>
</route>
<route url="/V1/inventory/source-items" method="DELETE">
<route url="/V1/inventory/source-item" method="DELETE">
<service class="Magento\InventoryApi\Api\SourceItemsDeleteInterface" method="execute"/>
<resources>
<resource ref="Magento_InventoryApi::source"/>
Expand Down

0 comments on commit 9d4984f

Please sign in to comment.