-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from magento-engcom/task-288-add-source-code
WIP MSI: 288 Add Support For SourceCode into the SourceInterface and SourceItemInterface
- Loading branch information
Showing
16 changed files
with
313 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/code/Magento/Inventory/Model/Source/Validator/CodeValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Inventory\Model\Source\Validator; | ||
|
||
use Magento\Framework\Validation\ValidationResult; | ||
use Magento\Framework\Validation\ValidationResultFactory; | ||
use Magento\InventoryApi\Api\Data\SourceInterface; | ||
|
||
/** | ||
* Check that code is valid | ||
*/ | ||
class CodeValidator implements SourceValidatorInterface | ||
{ | ||
/** | ||
* @var ValidationResultFactory | ||
*/ | ||
private $validationResultFactory; | ||
|
||
/** | ||
* @param ValidationResultFactory $validationResultFactory | ||
*/ | ||
public function __construct(ValidationResultFactory $validationResultFactory) | ||
{ | ||
$this->validationResultFactory = $validationResultFactory; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function validate(SourceInterface $source): ValidationResult | ||
{ | ||
$value = (string)$source->getCode(); | ||
|
||
if ('' === trim($value)) { | ||
$errors[] = __('"%field" can not be empty.', ['field' => SourceInterface::CODE]); | ||
} elseif (preg_match('/\s/', $value)) { | ||
$errors[] = __('"%field" can not contain whitespaces.', ['field' => SourceInterface::CODE]); | ||
} else { | ||
$errors = []; | ||
} | ||
|
||
return $this->validationResultFactory->create(['errors' => $errors]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
app/code/Magento/Inventory/Test/Unit/Model/Source/Validator/CodeValidatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Inventory\Test\Unit\Model\Source\Validator; | ||
|
||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\Framework\Validation\ValidationResultFactory; | ||
use Magento\Inventory\Model\Source\Validator\CodeValidator; | ||
use Magento\InventoryApi\Api\Data\SourceInterface; | ||
use Magento\InventoryApi\Api\Data\SourceInterfaceFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CodeValidatorTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @var CodeValidator | ||
*/ | ||
private $codeValidator; | ||
|
||
/** | ||
* @var SourceInterface |\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $source; | ||
|
||
/** | ||
* @var ValidationResultFactory|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $validationResultFactory; | ||
|
||
protected function setUp() | ||
{ | ||
$this->validationResultFactory = $this->getMockBuilder(ValidationResultFactory::class)->getMock(); | ||
$this->source = $this->getMockBuilder(SourceInterface::class)->getMock(); | ||
} | ||
|
||
public function testValidateCodeNotEmpty() | ||
{ | ||
$emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class); | ||
$this->validationResultFactory->expects($this->once()) | ||
->method('create') | ||
->with(['errors' => [__('"%field" can not be empty.', ['field' => SourceInterface::CODE])]]) | ||
->willReturn($emptyValidatorResult); | ||
$this->codeValidator = (new ObjectManager($this))->getObject(CodeValidator::class, [ | ||
'validationResultFactory' => $this->validationResultFactory | ||
]); | ||
|
||
$this->source->expects($this->once()) | ||
->method('getCode') | ||
->willReturn(' '); | ||
$this->codeValidator->validate($this->source); | ||
} | ||
|
||
public function testValidateCodeNotWithWhiteSpaces() | ||
{ | ||
$emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class); | ||
$this->validationResultFactory->expects($this->once()) | ||
->method('create') | ||
->with(['errors' => [__('"%field" can not contain whitespaces.', ['field' => SourceInterface::CODE])]]) | ||
->willReturn($emptyValidatorResult); | ||
$this->codeValidator = (new ObjectManager($this))->getObject(CodeValidator::class, [ | ||
'validationResultFactory' => $this->validationResultFactory | ||
]); | ||
$this->source->expects($this->once()) | ||
->method('getCode') | ||
->willReturn(' source code '); | ||
$this->codeValidator->validate($this->source); | ||
} | ||
|
||
public function testValidateCodeSuccessfully() | ||
{ | ||
$emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class); | ||
$this->validationResultFactory->expects($this->once()) | ||
->method('create') | ||
->willReturn($emptyValidatorResult); | ||
$this->codeValidator = (new ObjectManager($this))->getObject(CodeValidator::class, [ | ||
'validationResultFactory' => $this->validationResultFactory | ||
]); | ||
$this->source->expects($this->once()) | ||
->method('getCode') | ||
->willReturn(' source_code '); | ||
|
||
$result = $this->codeValidator->validate($this->source); | ||
$errors = $result->getErrors(); | ||
$this->assertCount(0, $errors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.