Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Fix API Test
- Add Type Hint
  • Loading branch information
larsroettig committed Oct 27, 2017
1 parent cdf1165 commit 23077d5
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CarrierLinksValidator implements SourceValidatorInterface

/**
* @param ValidationResultFactory $validationResultFactory
* @param Config $shippingConfig
*/
public function __construct(ValidationResultFactory $validationResultFactory, Config $shippingConfig)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\InventoryApi\Test\Api\SourceRepository;

use Magento\Framework\Webapi\Exception;
use Magento\Framework\Webapi\Rest\Request;
use Magento\InventoryApi\Api\Data\SourceCarrierLinkInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
Expand Down Expand Up @@ -49,106 +49,6 @@ public function testCarrierLinksManagement(array $carrierLinks)
self::assertEquals($expectedData[SourceInterface::CARRIER_LINKS], $sourceData[SourceInterface::CARRIER_LINKS]);
}

/**
* @return array
*/
public function dataProviderCarrierLinks(): array
{
return [
'add_carrier_new_links' => [
[
[
SourceCarrierLinkInterface::CARRIER_CODE => 'ups',
SourceCarrierLinkInterface::POSITION => 100,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'usps',
SourceCarrierLinkInterface::POSITION => 200,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'dhl',
SourceCarrierLinkInterface::POSITION => 300,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'fedex',
SourceCarrierLinkInterface::POSITION => 400,
],
],
],
'replace_carrier_links' => [
[
[
SourceCarrierLinkInterface::CARRIER_CODE => 'dhl',
SourceCarrierLinkInterface::POSITION => 100,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'fedex',
SourceCarrierLinkInterface::POSITION => 200,
],
],
],
'delete_carrier_links' => [
[],
],
];
}

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php
*/
public function testAssignCarrierLinksIfUseGlobalConfigurationChosen()
{
$sourceId = 10;
$expectedData = [
SourceInterface::NAME => 'source-name-1',
SourceInterface::POSTCODE => 'source-postcode',
SourceInterface::COUNTRY_ID => 'US',
SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 1,
SourceInterface::CARRIER_LINKS => [
[
SourceCarrierLinkInterface::CARRIER_CODE => 'ups',
SourceCarrierLinkInterface::POSITION => 100,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'usps',
SourceCarrierLinkInterface::POSITION => 200,
],
],
];

$expectedErrorData = [
'message' => 'Validation Failed',
'errors' => [
[
'message' => 'You can\'t configure "%field" because you have chosen Global Shipping configuration.',
'parameters' => [
'field' => SourceInterface::CARRIER_LINKS,
],
],
],
];

try {
$this->saveSource($sourceId, $expectedData);
$this->fail('Expected throwing exception');
} catch (\Exception $e) {
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) {
self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
self::assertEquals(Exception::HTTP_BAD_REQUEST, $e->getCode());
} elseif (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
$this->assertInstanceOf('SoapFault', $e);
// @see \Magento\TestFramework\TestCase\WebapiAbstract::getActualWrappedErrors()
$expectedWrappedErrors = $expectedErrorData['errors'];
$expectedWrappedErrors[0]['params'] = $expectedWrappedErrors[0]['parameters'];
unset($expectedWrappedErrors[0]['parameters']);

$this->checkSoapFault($e, $expectedErrorData['message'], 'env:Sender', [], $expectedWrappedErrors);
} else {
throw $e;
}
}
}

/**
* @param int $sourceId
* @param array $data
Expand Down Expand Up @@ -198,24 +98,13 @@ private function getSourceDataById(int $sourceId): array
return $response;
}

public function testFailedValidationOnCreate()
/**
* @param array $carrierLinks
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php
* @dataProvider dataProviderForValidationFailed
*/
public function testCarrierLinksValidation(array $carrierData, array $expectedErrorData)
{
$expectedData = [
SourceInterface::NAME => 'source-name-1',
SourceInterface::POSTCODE => 'source-postcode',
SourceInterface::COUNTRY_ID => 'US',
SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 0,
SourceInterface::CARRIER_LINKS => [
[
SourceCarrierLinkInterface::CARRIER_CODE => 'no_exists_1',
SourceCarrierLinkInterface::POSITION => 100,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'no_exists_2',
SourceCarrierLinkInterface::POSITION => 200,
],
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
Expand All @@ -227,29 +116,151 @@ public function testFailedValidationOnCreate()
],
];

$expectedErrorData = [
'message' => 'Validation Failed',
'errors' => [
try {
$this->_webApiCall($serviceInfo, ['source' => $carrierData]);
$this->fail('Expected throwing exception');
} catch (\Exception $e) {
self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
self::assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
}
}

/**
* @return array
*/
public function dataProviderCarrierLinks(): array
{
return [
'add_carrier_new_links' => [
[
'message' => 'You can\'t configure because carrier with code: "%carrier" don\'t exists.',
'parameters' => [
'carrier' => 'no_exists_1'
[
SourceCarrierLinkInterface::CARRIER_CODE => 'ups',
SourceCarrierLinkInterface::POSITION => 100,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'usps',
SourceCarrierLinkInterface::POSITION => 200,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'dhl',
SourceCarrierLinkInterface::POSITION => 300,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'fedex',
SourceCarrierLinkInterface::POSITION => 400,
],
],
],
'replace_carrier_links' => [
[
'message' => 'You can\'t configure because carrier with code: "%carrier" don\'t exists.',
'parameters' => [
'carrier' => 'no_exists_2'
[
SourceCarrierLinkInterface::CARRIER_CODE => 'dhl',
SourceCarrierLinkInterface::POSITION => 100,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'fedex',
SourceCarrierLinkInterface::POSITION => 200,
],
]
],
],
'delete_carrier_links' => [
[],
],
];
}

try {
$this->_webApiCall($serviceInfo, ['source' => $expectedData]);
} catch (\Exception $e) {
self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
self::assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
}
/**
* @return array
*/
public function dataProviderForValidationFailed(): array
{
return [
'not_list_of_SourceCarrierLinkInterface' => [
[
SourceInterface::NAME => 'source-name-1',
SourceInterface::POSTCODE => 'source-postcode',
SourceInterface::COUNTRY_ID => 'US',
SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 0,
SourceInterface::CARRIER_LINKS => 1222
],
[
'message' => 'Validation Failed',
'errors' => [
[
'message' => '"%field" must be list of SourceCarrierLinkInterface.',
'parameters' => [
'field' => SourceInterface::CARRIER_LINKS,
],
],
],
],
],
'use_global_configuration_chosen' => [
[
SourceInterface::NAME => 'source-name-1',
SourceInterface::POSTCODE => 'source-postcode',
SourceInterface::COUNTRY_ID => 'US',
SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 1,
SourceInterface::CARRIER_LINKS => [
[
SourceCarrierLinkInterface::CARRIER_CODE => 'ups',
SourceCarrierLinkInterface::POSITION => 100,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'usps',
SourceCarrierLinkInterface::POSITION => 200,
],
],
],
[
'message' => 'Validation Failed',
'errors' => [
[
'message' =>
'You can\'t configure "%field" because you have chosen Global Shipping configuration.',
'parameters' => [
'field' => SourceInterface::CARRIER_LINKS,
],
],
],
],
],
'carrier_codes_not_exits' => [
[
SourceInterface::NAME => 'source-name-1',
SourceInterface::POSTCODE => 'source-postcode',
SourceInterface::COUNTRY_ID => 'US',
SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 0,
SourceInterface::CARRIER_LINKS => [
[
SourceCarrierLinkInterface::CARRIER_CODE => 'no_exists_1',
SourceCarrierLinkInterface::POSITION => 100,
],
[
SourceCarrierLinkInterface::CARRIER_CODE => 'no_exists_2',
SourceCarrierLinkInterface::POSITION => 200,
],
],
],
[
'message' => 'Validation Failed',
'errors' => [
[
'message' => 'You can\'t configure because carrier with code: "%carrier" don\'t exists.',
'parameters' => [
'carrier' => 'no_exists_1'
],
],
[
'message' => 'You can\'t configure because carrier with code: "%carrier" don\'t exists.',
'parameters' => [
'carrier' => 'no_exists_2'
],
]
],
],
]

];
}
}

0 comments on commit 23077d5

Please sign in to comment.