Skip to content

Commit

Permalink
[task] magento#115 Refactoring
Browse files Browse the repository at this point in the history
- Add Documentation
- phpcs fixes
- Impelement phpunit test for ValidatorChain
  • Loading branch information
larsroettig committed Oct 3, 2017
1 parent 0e48a30 commit 8dcbe34
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
use Magento\Framework\Validation\ValidationResultFactory;

/**
* Extension point for row validation
*
* @api
* @inheritdoc
*/
class ValidatorChain implements ValidatorInterface
{
Expand Down Expand Up @@ -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)]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Magento\Framework\Validation\ValidationResult;

/**
* Extension point for row validation
* Extension point for row validation (Service Provider Interface - SPI)
*
* @api
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Inventory\Test\Unit\Model;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Validation\ValidationResultFactory;
use Magento\InventoryImportExport\Model\Import\Validator\ValidatorChain;
use Magento\InventoryImportExport\Model\Import\Validator\ValidatorInterface;
use PHPUnit\Framework\TestCase;

class ValidatorChainTest extends TestCase
{

/**
* @var ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $qtyValidator;

/**
* @var ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $skuValidator;

/**
* @var ValidationResultFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $validationResultFactory;

/**
* @var ValidatorChain
*/
private $validatorChain;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->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);
}
}

0 comments on commit 8dcbe34

Please sign in to comment.