diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php index 51ae14440a591..530fcce22a68a 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/Append.php @@ -5,6 +5,7 @@ */ namespace Magento\InventoryImportExport\Model\Import\Command; + use Magento\InventoryApi\Api\Data\SourceItemInterface; use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory; use Magento\InventoryApi\Api\SourceItemsSaveInterface; diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php index efc9b5cb05b56..7bb5a73bed0a4 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Command/CommandInterface.php @@ -7,11 +7,15 @@ namespace Magento\InventoryImportExport\Model\Import\Command; /** + * It is extension point to implement import/export functionality (Service Provider Interface - SPI) + * * @api */ interface CommandInterface { /** + * Executes the current command. + * * @param array $bunch * @return void * @throws CommandException diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php index 2a9b52bf8a39d..0a56563488e89 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Serializer/Json.php @@ -22,9 +22,8 @@ class Json implements SerializerInterface, LegacyJsonHelperInterface * Json constructor. * @param SerializerInterface $serializer */ - public function __construct( - SerializerInterface $serializer - ) { + public function __construct(SerializerInterface $serializer) + { $this->serializer = $serializer; } diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php index c8644464d4e8a..efbce923cddcd 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/SourceValidator.php @@ -6,7 +6,6 @@ namespace Magento\InventoryImportExport\Model\Import\Validator; -use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Validation\ValidationResultFactory; use Magento\InventoryApi\Api\SourceRepositoryInterface; use Magento\InventoryImportExport\Model\Import\Sources; @@ -63,16 +62,19 @@ public function validate(array $rowData, $rowNumber) } /** + * Returns exits already the source in sources. + * * @param int $sourceId * @return bool */ - private function isExistingSource($sourceId) + private function isExistingSource($sourceId): bool { return isset($this->sourceIds[$sourceId]); } /** * Loads all existing source ids + * * @return void */ private function loadSourceIds() diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php index 537f4abed6794..d93fb69bc5a22 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorChain.php @@ -10,9 +10,7 @@ use Magento\Framework\Validation\ValidationResultFactory; /** - * Extension point for row validation - * - * @api + * @inheritdoc */ class ValidatorChain implements ValidatorInterface { @@ -52,14 +50,16 @@ public function __construct( */ public function validate(array $rowData, $rowNumber) { - $errors = []; + /* the inner empty array covers cases when no loops were made */ + $errors = [[]]; foreach ($this->validators as $validator) { $validationResult = $validator->validate($rowData, $rowNumber); if (!$validationResult->isValid()) { - $errors = array_merge($errors, $validationResult->getErrors()); + $errors[] = $validationResult->getErrors(); } } - return $this->validationResultFactory->create(['errors' => $errors]); + + return $this->validationResultFactory->create(['errors' => array_merge(...$errors)]); } } diff --git a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php index defc1f64c1dba..0e52ccddac444 100644 --- a/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php +++ b/app/code/Magento/InventoryImportExport/Model/Import/Validator/ValidatorInterface.php @@ -9,7 +9,7 @@ use Magento\Framework\Validation\ValidationResult; /** - * Extension point for row validation + * Extension point for row validation (Service Provider Interface - SPI) * * @api */ diff --git a/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php b/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php new file mode 100644 index 0000000000000..fe739c5deaef1 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Test/Unit/Model/ValidatorChainTest.php @@ -0,0 +1,127 @@ +validationResultFactory = $this->getMockBuilder(ValidationResultFactory::class)->getMock(); + $this->qtyValidator = $this->getMockBuilder(ValidatorInterface::class)->getMock(); + $this->skuValidator = $this->getMockBuilder(ValidatorInterface::class)->getMock(); + } + + public function testValidateWithOutValidators() + { + $emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class); + $this->validationResultFactory->expects($this->once()) + ->method('create') + ->with(['errors' =>[]]) + ->willReturn($emptyValidatorResult); + + $this->validatorChain = (new ObjectManager($this))->getObject( + ValidatorChain::class, + [ + 'validationResultFactory' => $this->validationResultFactory, + 'validators' => [] + ] + ); + + + $result = $this->validatorChain->validate([], 1); + $this->assertEquals($emptyValidatorResult, $result); + } + + public function testValidateWithOutErros() + { + $emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class); + $emptyValidatorResult->expects($this->once())->method('isValid') + ->willReturn(true); + + $this->validationResultFactory->expects($this->once()) + ->method('create') + ->with(['errors' => []]) + ->willReturn($emptyValidatorResult); + + $this->qtyValidator->method('validate') + ->willReturn($emptyValidatorResult); + + $this->validatorChain = (new ObjectManager($this))->getObject( + ValidatorChain::class, + [ + 'validationResultFactory' => $this->validationResultFactory, + 'validators' => [$this->qtyValidator] + ] + ); + + $result = $this->validatorChain->validate([], 1); + $this->assertEquals($emptyValidatorResult, $result); + } + + + public function testValidateWithErros() + { + $validatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class); + + $validatorResult->expects($this->once())->method('isValid') + ->willReturn(false); + + $validatorResult->expects($this->once()) + ->method('getErrors') + ->willReturn(['Qty can not negative', 'Additional error']); + + $this->qtyValidator->expects($this->once())->method('validate') + ->willReturn($validatorResult); + + $this->validationResultFactory->expects($this->once()) + ->method('create') + ->with(['errors' => ['Qty can not negative', 'Additional error']]) + ->willReturn($validatorResult); + + + $this->validatorChain = (new ObjectManager($this))->getObject( + ValidatorChain::class, + [ + 'validationResultFactory' => $this->validationResultFactory, + 'validators' => [$this->qtyValidator] + ] + ); + + $result = $this->validatorChain->validate([-1], 1); + $this->assertEquals($validatorResult, $result); + } +}