Skip to content

Commit 5b557f6

Browse files
author
larsroettig
committed
- Refactoring for validate function - Add expectedErrorData to api test
1 parent 773f463 commit 5b557f6

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

app/code/Magento/Inventory/Model/Source/Validator/CarrierLinks.php

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Inventory\Model\Source\Validator;
78

89
use Magento\Framework\Validation\ValidationResult;
@@ -42,29 +43,47 @@ public function __construct(ValidationResultFactory $validationResultFactory, Co
4243
public function validate(SourceInterface $source): ValidationResult
4344
{
4445
$carrierLinks = $source->getCarrierLinks();
45-
4646
$errors = [];
47-
if (null !== $carrierLinks) {
48-
if (!is_array($carrierLinks)) {
49-
$errors[] = __('"%field" must be list of SourceCarrierLinkInterface.', [
50-
'field' => SourceInterface::CARRIER_LINKS
51-
]);
52-
} elseif (count($carrierLinks) && $source->isUseDefaultCarrierConfig()) {
53-
$errors[] = __('You can\'t configure "%field" because you have chosen Global Shipping configuration.', [
54-
'field' => SourceInterface::CARRIER_LINKS
55-
]);
56-
}
5747

58-
$availableCarriers = $this->shippingConfig->getAllCarriers();
59-
foreach ($carrierLinks as $carrierLink) {
60-
$carrierCode = $carrierLink->getCarrierCode();
61-
if (array_key_exists($carrierCode, $availableCarriers) === false) {
62-
$errors[] = __('You can\'t configure because carrier with code: "%carrier" don\'t exists.', [
63-
'carrier' => $carrierCode
64-
]);
65-
}
48+
if (null === $carrierLinks) {
49+
return $this->buildValidationResult($errors);
50+
}
51+
52+
if (!is_array($carrierLinks)) {
53+
$errors[] = __('"%field" must be list of SourceCarrierLinkInterface.', [
54+
'field' => SourceInterface::CARRIER_LINKS
55+
]);
56+
return $this->buildValidationResult($errors);
57+
}
58+
59+
if (count($carrierLinks) && $source->isUseDefaultCarrierConfig()) {
60+
$errors[] = __('You can\'t configure "%field" because you have chosen Global Shipping configuration.', [
61+
'field' => SourceInterface::CARRIER_LINKS
62+
]);
63+
return $this->buildValidationResult($errors);
64+
}
65+
66+
$availableCarriers = $this->shippingConfig->getAllCarriers();
67+
foreach ($carrierLinks as $carrierLink) {
68+
$carrierCode = $carrierLink->getCarrierCode();
69+
if (array_key_exists($carrierCode, $availableCarriers) === false) {
70+
$errors[] = __('You can\'t configure because carrier with code: "%carrier" don\'t exists.', [
71+
'carrier' => $carrierCode
72+
]);
6673
}
6774
}
75+
76+
return $this->buildValidationResult($errors);
77+
}
78+
79+
/**
80+
* Build the ValidationResult by given errors.
81+
*
82+
* @param array $errors
83+
* @return ValidationResult
84+
*/
85+
private function buildValidationResult(array $errors): ValidationResult
86+
{
6887
return $this->validationResultFactory->create(['errors' => $errors]);
6988
}
7089
}

app/code/Magento/InventoryApi/Test/Api/SourceRepository/CreateTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,28 @@ public function testCreateWithWrongCarrierLinks()
109109
],
110110
];
111111

112+
$expectedErrorData = [
113+
'message' => 'Validation Failed',
114+
'errors' => [
115+
[
116+
'message' => 'You can\'t configure because carrier with code: "%carrier" don\'t exists.',
117+
'parameters' => [
118+
'carrier' => 'no_exists_1'
119+
],
120+
],
121+
[
122+
'message' => 'You can\'t configure because carrier with code: "%carrier" don\'t exists.',
123+
'parameters' => [
124+
'carrier' => 'no_exists_2'
125+
],
126+
]
127+
],
128+
];
129+
112130
try {
113131
$this->_webApiCall($serviceInfo, ['source' => $expectedData]);
114132
} catch (\Exception $e) {
133+
self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
115134
self::assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
116135
}
117136
}

0 commit comments

Comments
 (0)