Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.0.6.0
=============
* now it is possible to configure attribute data from a Pimcore. Additional informations kept in 'attr_conf' key in attribute will be merged and override default configuration of an attribute. `\Divante\PimcoreIntegration\Model\Catalog\Product\Attribute\Creator\Strategy\AbstractStrategy::getMergedConfig`

1.0.5.5
=============
* add .gitignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,19 @@ public function __construct(
$this->attrData = $attrData;
$this->code = $code;
}

/**
* @param array $base
*
* @return array
*/
public function getMergedConfig(array $base = []): array
{
return array_merge(self::$defaultAttrConfig, $base, $this->attrData['attr_conf'] ?? []);
}

/**
* @return array
*/
abstract public function getBaseAttrConfig(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,26 @@ class DatetimeStrategy extends AbstractStrategy
public function execute(): int
{
$eavSetup = $this->eavSetupFactory->create();

$eavSetup->addAttribute(
Product::ENTITY,
$this->code,
array_merge(self::$defaultAttrConfig, [
'type' => 'datetime',
'label' => $this->attrData['label'],
'input' => 'date',
'backend' => Datetime::class,
])
$this->getMergedConfig($this->getBaseAttrConfig())
);

return $eavSetup->getAttributeId(Product::ENTITY, $this->code);
}

/**
* @return array
*/
public function getBaseAttrConfig(): array
{
return [
'type' => 'datetime',
'label' => $this->attrData['label'],
'input' => 'date',
'backend' => Datetime::class,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,24 @@ protected function createNewAttribute(array $options)
{
$eavSetup = $this->eavSetupFactory->create();

$data = [
$eavSetup->addAttribute(
Product::ENTITY,
$this->code,
$this->getMergedConfig($this->getBaseAttrConfig())
);
}

/**
* @return array
*/
public function getBaseAttrConfig(): array
{
return [
'type' => 'varchar',
'label' => $this->attrData['label'],
'input' => 'multiselect',
'user_defined' => true,
'backend' => ArrayBackend::class,
];

$eavSetup->addAttribute(
Product::ENTITY,
$this->code,
array_merge(self::$defaultAttrConfig, $data)
);
}
}
34 changes: 21 additions & 13 deletions Model/Catalog/Product/Attribute/Creator/Strategy/SelectStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ protected function createNewAttribute(array $options)
{
$eavSetup = $this->eavSetupFactory->create();

$eavSetup->addAttribute(
Product::ENTITY,
$this->code,
$this->getMergedConfig($this->getBaseAttrConfig())
);
}

/**
* @return bool
*/
private function isConfigurable(): bool
{
return (!empty($this->attrData['is_configurable']) && true === $this->attrData['is_configurable']);
}

/**
* @return array
*/
public function getBaseAttrConfig(): array
{
$data = [
'type' => 'int',
'label' => $this->attrData['label'],
Expand All @@ -93,18 +113,6 @@ protected function createNewAttribute(array $options)
]);
}

$eavSetup->addAttribute(
Product::ENTITY,
$this->code,
array_merge(self::$defaultAttrConfig, $data)
);
}

/**
* @return bool
*/
private function isConfigurable(): bool
{
return (!empty($this->attrData['is_configurable']) && true === $this->attrData['is_configurable']);
return $data;
}
}
18 changes: 13 additions & 5 deletions Model/Catalog/Product/Attribute/Creator/Strategy/TextStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ public function execute(): int
$eavSetup->addAttribute(
Product::ENTITY,
$this->code,
array_merge(self::$defaultAttrConfig, [
'type' => 'varchar',
'label' => $this->attrData['label'],
'input' => 'text',
])
$this->getMergedConfig($this->getBaseAttrConfig())
);

return $eavSetup->getAttributeId(Product::ENTITY, $this->code);
}

/**
* @return array
*/
public function getBaseAttrConfig(): array
{
return [
'type' => 'varchar',
'label' => $this->attrData['label'],
'input' => 'text',
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ public function execute(): int
$eavSetup->addAttribute(
Product::ENTITY,
$this->code,
array_merge(self::$defaultAttrConfig, [
'type' => 'text',
'label' => $this->attrData['label'],
'input' => 'textarea',
'wysiwyg_enabled' => false,
'is_html_allowed_on_front' => false,
])
$this->getMergedConfig($this->getBaseAttrConfig())
);

return $eavSetup->getAttributeId(Product::ENTITY, $this->code);
}

/**
* @return array
*/
public function getBaseAttrConfig(): array
{
return [
'type' => 'text',
'label' => $this->attrData['label'],
'input' => 'textarea',
'wysiwyg_enabled' => false,
'is_html_allowed_on_front' => false,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ public function execute(): int
$eavSetup->addAttribute(
Product::ENTITY,
$this->code,
array_merge(self::$defaultAttrConfig, [
'type' => 'text',
'label' => $this->attrData['label'],
'input' => 'textarea',
'wysiwyg_enabled' => true,
'is_html_allowed_on_front' => true,
])
$this->getMergedConfig($this->getBaseAttrConfig())
);

return $eavSetup->getAttributeId(Product::ENTITY, $this->code);
}

/**
* @return array
*/
public function getBaseAttrConfig(): array
{
return [
'type' => 'text',
'label' => $this->attrData['label'],
'input' => 'textarea',
'wysiwyg_enabled' => true,
'is_html_allowed_on_front' => true,
];
}
}
20 changes: 14 additions & 6 deletions Model/Catalog/Product/Attribute/Creator/Strategy/YesnoStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@ public function execute(): int
$eavSetup->addAttribute(
Product::ENTITY,
$this->code,
array_merge(self::$defaultAttrConfig, [
'type' => 'int',
'label' => $this->attrData['label'],
'input' => 'boolean',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
])
$this->getMergedConfig($this->getBaseAttrConfig())
);

return $eavSetup->getAttributeId(Product::ENTITY, $this->code);
}

/**
* @return array
*/
public function getBaseAttrConfig(): array
{
return [
'type' => 'int',
'label' => $this->attrData['label'],
'input' => 'boolean',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php
/**
* @package Divante\PimcoreIntegration
* @author Bartosz Herba <bherba@divante.pl>
* @copyright 2020 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\PimcoreIntegration\Test\Unit\Model\Catalog\Product\Attribute\Creator\Strategy;

use Divante\PimcoreIntegration\Model\Catalog\Product\Attribute\Creator\Strategy\AbstractStrategy;
use Magento\Catalog\Model\Category\AttributeRepository;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Class TextStrategyTest
*/
class AbstractStrategyTest extends \PHPUnit\Framework\TestCase
{
/**
* @var MockObject|EavSetupFactory\
*/
private $eavSetupFactoryMock;

/**
* @var MockObject|EavSetup
*/
private $eavSetupMock;

/**
* @var MockObject|AttributeRepository
*/
private $attributeRepositoryMock;

/**
* @var array
*/
private $defaultAttrConfig = [
'backend' => '',
'frontend' => '',
'input' => 'text',
'class' => '',
'source' => '',
'global' => ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
];

public function setUp()
{
$this->eavSetupFactoryMock = $this->getMockBuilder(EavSetupFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->eavSetupMock = $this->getMockBuilder(EavSetup::class)
->disableOriginalConstructor()
->setMethods([])
->getMock();

$this->eavSetupMock = $this->getMockBuilder(EavSetup::class)
->disableOriginalConstructor()
->setMethods([])
->getMock();

$this->attributeRepositoryMock = $this->getMockBuilder(AttributeRepositoryInterface::class)
->disableOriginalConstructor()
->setMethods([])
->getMock();
}

/**
* @return array[]
*/
public function attrDataProvider()
{
return [
[['attr_conf' => ['test' => 1]], []],
[['attr_conf' => ['test' => 1]], ['input' => 'select']],
[['attr_conf' => ['test' => 1]], ['input' => 'select']],
[['attr_conf' => ['test' => 1]], ['input' => 'select']],
[['something_else' => [], 'attr_conf' => ['test' => 1]], ['input' => 'select']],
[['missed_key' => ['test' => 1, 'required' => true]], []],
];
}

public function testGetDefaultAttrConfig()
{
$strategy = $this->getAbstractStrategyMockImplementation('test');
$this->assertEquals($this->defaultAttrConfig, $strategy->getDefaultAttrConfig());
}

/**
* @dataProvider attrDataProvider
*/
public function testGetMergedConfig(array $attrData, $base)
{
$strategy = $this->getAbstractStrategyMockImplementation('test', $attrData);
$result = array_merge($this->defaultAttrConfig, $base, $attrData['attr_conf'] ?? []);
$this->assertEquals($result, $strategy->getMergedConfig($base));
}

/**
* @param string $code
* @param array $attrData
*
* @return AbstractStrategy
*/
private function getAbstractStrategyMockImplementation(string $code, array $attrData = [])
{
return new class(
$this->eavSetupFactoryMock,
$this->attributeRepositoryMock,
$attrData,
$code
) extends AbstractStrategy {
public function getBaseAttrConfig(): array
{
return [];
}

public function execute(): int
{
return 1;
}
};
}
}
Loading