Skip to content

Commit

Permalink
Merge pull request #298 from magento-engcom/task-288-add-source-code
Browse files Browse the repository at this point in the history
WIP MSI: 288 Add Support For SourceCode into the SourceInterface and SourceItemInterface
  • Loading branch information
maghamed authored Dec 13, 2017
2 parents 5ad2300 + 6c9800b commit 37aa67d
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 8 deletions.
16 changes: 16 additions & 0 deletions app/code/Magento/Inventory/Model/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ public function setSourceId($sourceId)
$this->setData(self::SOURCE_ID, $sourceId);
}

/**
* @inheritdoc
*/
public function getCode()
{
return $this->getData(self::CODE);
}

/**
* @inheritdoc
*/
public function setCode($code)
{
return $this->setData(self::CODE, $code);
}

/**
* @inheritdoc
*/
Expand Down
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]);
}
}
27 changes: 27 additions & 0 deletions app/code/Magento/Inventory/Setup/Operation/CreateSourceTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\Inventory\Setup\Operation;

use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Inventory\Model\ResourceModel\Source as SourceResourceModel;
Expand Down Expand Up @@ -37,6 +38,7 @@ public function execute(SchemaSetupInterface $setup)
$sourceTable = $this->addAddressFields($sourceTable);
$sourceTable = $this->addContactInfoFields($sourceTable);
$sourceTable = $this->addSourceCarrierFields($sourceTable);
$sourceTable = $this->addIndex($sourceTable, $setup);

$setup->getConnection()->createTable($sourceTable);
}
Expand All @@ -58,6 +60,14 @@ private function addBaseFields(Table $sourceTable): Table
Table::OPTION_PRIMARY => true,
],
'Source ID'
)->addColumn(
SourceInterface::CODE,
Table::TYPE_TEXT,
255,
[
Table::OPTION_NULLABLE => false,
],
'Source Code'
)->addColumn(
SourceInterface::NAME,
Table::TYPE_TEXT,
Expand Down Expand Up @@ -238,4 +248,21 @@ private function addSourceCarrierFields(Table $sourceTable)
);
return $sourceTable;
}

private function addIndex(Table $sourceTable, SchemaSetupInterface $setup)
{
return $sourceTable->addIndex(
$setup->getIdxName(
$setup->getTable(SourceResourceModel::TABLE_NAME_SOURCE),
[
SourceInterface::CODE
],
AdapterInterface::INDEX_TYPE_UNIQUE
),
[
SourceInterface::CODE
],
['type' => AdapterInterface::INDEX_TYPE_UNIQUE]
);
}
}
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);
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Inventory/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<arguments>
<argument name="validators" xsi:type="array">
<item name="name" xsi:type="object">Magento\Inventory\Model\Source\Validator\NameValidator</item>
<item name="code" xsi:type="object">Magento\Inventory\Model\Source\Validator\CodeValidator</item>
<item name="postcode" xsi:type="object">Magento\Inventory\Model\Source\Validator\PostcodeValidator</item>
<item name="country" xsi:type="object">Magento\Inventory\Model\Source\Validator\CountryValidator</item>
<item name="carrier_links" xsi:type="object">Magento\Inventory\Model\Source\Validator\CarrierLinksValidator</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
<opened>true</opened>
<dataScope>general</dataScope>
</settings>
<field name="code" formElement="input" sortOrder="20">
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
<dataType>text</dataType>
<label translate="true">Code</label>
</settings>
</field>
<field name="name" formElement="input" sortOrder="20">
<settings>
<validation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@
<sorting>asc</sorting>
</settings>
</column>
<column name="code" sortOrder="25">
<settings>
<filter>text</filter>
<label translate="true">Code</label>
<editor>
<editorType>text</editorType>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
</editor>
</settings>
</column>
<column name="name" sortOrder="30">
<settings>
<filter>text</filter>
Expand Down Expand Up @@ -269,7 +281,7 @@
<label translate="true">Priority</label>
</settings>
</column>
<actionsColumn name="actions" class="Magento\Backend\Ui\Component\Listing\Column\EditAction" sortOrder="100">
<actionsColumn name="actions" class="Magento\Backend\Ui\Component\Listing\Column\EditAction" sortOrder="200">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="editUrlPath" xsi:type="string">inventory/source/edit</item>
Expand Down
17 changes: 16 additions & 1 deletion app/code/Magento/InventoryApi/Api/Data/SourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface SourceInterface extends ExtensibleDataInterface
* Constants for keys of data array. Identical to the name of the getter in snake case
*/
const SOURCE_ID = 'source_id';
const CODE = 'code';
const NAME = 'name';
const CONTACT_NAME = 'contact_name';
const EMAIL = 'email';
Expand All @@ -40,7 +41,6 @@ interface SourceInterface extends ExtensibleDataInterface
const PRIORITY = 'priority';
const USE_DEFAULT_CARRIER_CONFIG = 'use_default_carrier_config';
const CARRIER_LINKS = 'carrier_links';
/**#@-*/

/**
* Get source id
Expand All @@ -57,6 +57,21 @@ public function getSourceId();
*/
public function setSourceId($sourceId);

/**
* Get source code
*
* @return string|null
*/
public function getCode();

/**
* Set source code
*
* @param string|null $code
* @return void
*/
public function setCode($code);

/**
* Get source name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function testCarrierLinksManagement(array $carrierLinks)
{
$sourceId = 10;
$expectedData = [
SourceInterface::CODE => 'source-code-1',
SourceInterface::NAME => 'source-name-1',
SourceInterface::POSTCODE => 'source-postcode',
SourceInterface::COUNTRY_ID => 'US',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CreateTest extends WebapiAbstract
public function testCreate()
{
$expectedData = [
SourceInterface::CODE => 'source-code-1',
SourceInterface::NAME => 'source-name-1',
SourceInterface::CONTACT_NAME => 'source-contact-name',
SourceInterface::EMAIL => 'source-email',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function testUpdate()
{
$sourceId = 10;
$expectedData = [
SourceInterface::CODE => 'source-code-1-updated',
SourceInterface::NAME => 'source-name-1-updated',
SourceInterface::CONTACT_NAME => 'source-contact-name-updated',
SourceInterface::EMAIL => 'source-email-updated',
Expand Down
Loading

0 comments on commit 37aa67d

Please sign in to comment.