Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Refactoring for validate function
- Add expectedErrorData to api test
  • Loading branch information
larsroettig committed Oct 27, 2017
1 parent 773f463 commit 5b557f6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
57 changes: 38 additions & 19 deletions app/code/Magento/Inventory/Model/Source/Validator/CarrierLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Inventory\Model\Source\Validator;

use Magento\Framework\Validation\ValidationResult;
Expand Down Expand Up @@ -42,29 +43,47 @@ public function __construct(ValidationResultFactory $validationResultFactory, Co
public function validate(SourceInterface $source): ValidationResult
{
$carrierLinks = $source->getCarrierLinks();

$errors = [];
if (null !== $carrierLinks) {
if (!is_array($carrierLinks)) {
$errors[] = __('"%field" must be list of SourceCarrierLinkInterface.', [
'field' => SourceInterface::CARRIER_LINKS
]);
} elseif (count($carrierLinks) && $source->isUseDefaultCarrierConfig()) {
$errors[] = __('You can\'t configure "%field" because you have chosen Global Shipping configuration.', [
'field' => SourceInterface::CARRIER_LINKS
]);
}

$availableCarriers = $this->shippingConfig->getAllCarriers();
foreach ($carrierLinks as $carrierLink) {
$carrierCode = $carrierLink->getCarrierCode();
if (array_key_exists($carrierCode, $availableCarriers) === false) {
$errors[] = __('You can\'t configure because carrier with code: "%carrier" don\'t exists.', [
'carrier' => $carrierCode
]);
}
if (null === $carrierLinks) {
return $this->buildValidationResult($errors);
}

if (!is_array($carrierLinks)) {
$errors[] = __('"%field" must be list of SourceCarrierLinkInterface.', [
'field' => SourceInterface::CARRIER_LINKS
]);
return $this->buildValidationResult($errors);
}

if (count($carrierLinks) && $source->isUseDefaultCarrierConfig()) {
$errors[] = __('You can\'t configure "%field" because you have chosen Global Shipping configuration.', [
'field' => SourceInterface::CARRIER_LINKS
]);
return $this->buildValidationResult($errors);
}

$availableCarriers = $this->shippingConfig->getAllCarriers();
foreach ($carrierLinks as $carrierLink) {
$carrierCode = $carrierLink->getCarrierCode();
if (array_key_exists($carrierCode, $availableCarriers) === false) {
$errors[] = __('You can\'t configure because carrier with code: "%carrier" don\'t exists.', [
'carrier' => $carrierCode
]);
}
}

return $this->buildValidationResult($errors);
}

/**
* Build the ValidationResult by given errors.
*
* @param array $errors
* @return ValidationResult
*/
private function buildValidationResult(array $errors): ValidationResult
{
return $this->validationResultFactory->create(['errors' => $errors]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,28 @@ public function testCreateWithWrongCarrierLinks()
],
];

$expectedErrorData = [
'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'
],
]
],
];

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());
}
}
Expand Down

0 comments on commit 5b557f6

Please sign in to comment.