ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,243 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Eav\Setup;
|
||||
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Eav\Api\AttributeRepositoryInterface;
|
||||
use Magento\Eav\Api\Data\AttributeInterface;
|
||||
use Magento\Eav\Api\Data\AttributeOptionInterface;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests coverage for \Magento\Eav\Setup\AddOptionToAttribute
|
||||
*/
|
||||
class AddOptionToAttributeTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var AddOptionToAttribute
|
||||
*/
|
||||
private $operation;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $attributeId;
|
||||
|
||||
/**
|
||||
* @var EavSetup
|
||||
*/
|
||||
private $eavSetup;
|
||||
|
||||
/**
|
||||
* @var AttributeRepositoryInterface
|
||||
*/
|
||||
private $attrRepo;
|
||||
|
||||
/**
|
||||
* @var ModuleDataSetupInterface
|
||||
*/
|
||||
private $setup;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = ObjectManager::getInstance();
|
||||
|
||||
$this->operation = $objectManager->get(AddOptionToAttribute::class);
|
||||
/** @var ModuleDataSetupInterface $setup */
|
||||
$this->setup = $objectManager->get(ModuleDataSetupInterface::class);
|
||||
/** @var AttributeRepositoryInterface attrRepo */
|
||||
$this->attrRepo = $objectManager->get(AttributeRepositoryInterface::class);
|
||||
/** @var EavSetup $eavSetup */
|
||||
$this->eavSetup = $objectManager->get(EavSetupFactory::class)
|
||||
->create(['setup' => $this->setup]);
|
||||
$this->attributeId = $this->eavSetup->getAttributeId(Product::ENTITY, 'zzz');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $fetchPairs
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getAttributeOptions($fetchPairs = true): array
|
||||
{
|
||||
$optionTable = $this->setup->getTable('eav_attribute_option');
|
||||
$optionValueTable = $this->setup->getTable('eav_attribute_option_value');
|
||||
|
||||
$select = $this->setup
|
||||
->getConnection()
|
||||
->select()
|
||||
->from(['o' => $optionTable])
|
||||
->reset('columns')
|
||||
->columns('sort_order')
|
||||
->join(['ov' => $optionValueTable], 'o.option_id = ov.option_id', 'value')
|
||||
->where(AttributeInterface::ATTRIBUTE_ID . ' = ?', $this->attributeId)
|
||||
->where('store_id = 0');
|
||||
|
||||
return $fetchPairs
|
||||
? $this->setup->getConnection()->fetchPairs($select)
|
||||
: $this->setup->getConnection()->fetchAll($select);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
|
||||
*/
|
||||
public function testAddNewOptions()
|
||||
{
|
||||
$optionsBefore = $this->getAttributeOptions(false);
|
||||
$this->operation->execute(
|
||||
[
|
||||
'values' => ['new1', 'new2'],
|
||||
'attribute_id' => $this->attributeId
|
||||
]
|
||||
);
|
||||
$optionsAfter = $this->getAttributeOptions(false);
|
||||
$this->assertEquals(count($optionsBefore) + 2, count($optionsAfter));
|
||||
foreach ($optionsBefore as $option) {
|
||||
$this->assertContainsEquals($option, $optionsAfter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
|
||||
*/
|
||||
public function testAddExistingOptionsWithTheSameSortOrder()
|
||||
{
|
||||
$optionsBefore = $this->getAttributeOptions();
|
||||
$this->operation->execute(
|
||||
[
|
||||
'values' => ['Black', 'White'],
|
||||
'attribute_id' => $this->attributeId
|
||||
]
|
||||
);
|
||||
$optionsAfter = $this->getAttributeOptions();
|
||||
$this->assertEquals(count($optionsBefore), count($optionsAfter));
|
||||
foreach ($optionsBefore as $option) {
|
||||
$this->assertContainsEquals($option, $optionsAfter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
|
||||
*/
|
||||
public function testAddExistingOptionsWithDifferentSortOrder()
|
||||
{
|
||||
$optionsBefore = $this->getAttributeOptions();
|
||||
$this->operation->execute(
|
||||
[
|
||||
'values' => [666 => 'White', 777 => 'Black'],
|
||||
'attribute_id' => $this->attributeId
|
||||
]
|
||||
);
|
||||
$optionsAfter = $this->getAttributeOptions();
|
||||
$this->assertSameSize($optionsBefore, array_intersect($optionsBefore, $optionsAfter));
|
||||
$this->assertEquals($optionsAfter[777], $optionsBefore[0]);
|
||||
$this->assertEquals($optionsAfter[666], $optionsBefore[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
|
||||
*/
|
||||
public function testAddMixedOptions()
|
||||
{
|
||||
$sizeBefore = count($this->getAttributeOptions());
|
||||
$this->operation->execute(
|
||||
[
|
||||
'values' => [666 => 'Black', 'NewOption'],
|
||||
'attribute_id' => $this->attributeId
|
||||
]
|
||||
);
|
||||
$updatedOptions = $this->getAttributeOptions();
|
||||
$this->assertEquals(count($updatedOptions), $sizeBefore + 1);
|
||||
$this->assertEquals($updatedOptions[666], 'Black');
|
||||
$this->assertEquals($updatedOptions[667], 'NewOption');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
|
||||
*/
|
||||
public function testAddNewOption()
|
||||
{
|
||||
$sizeBefore = count($this->getAttributeOptions());
|
||||
|
||||
$this->operation->execute(
|
||||
[
|
||||
'attribute_id' => $this->attributeId,
|
||||
'order' => [0 => 13],
|
||||
'value' => [
|
||||
[
|
||||
0 => 'NewOption',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
$updatedOptions = $this->getAttributeOptions();
|
||||
$this->assertEquals(count($updatedOptions), $sizeBefore + 1);
|
||||
$this->assertEquals($updatedOptions[13], 'NewOption');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
|
||||
*/
|
||||
public function testDeleteOption()
|
||||
{
|
||||
$optionsBefore = $this->getAttributeOptions();
|
||||
$options = $this->attrRepo->get(Product::ENTITY, $this->attributeId)->getOptions();
|
||||
/** @var AttributeOptionInterface $optionToDelete */
|
||||
$optionToDelete = end($options);
|
||||
$this->operation->execute(
|
||||
[
|
||||
'attribute_id' => $this->attributeId,
|
||||
'delete' => [$optionToDelete->getValue() => true],
|
||||
'value' => [
|
||||
$optionToDelete->getValue() => null,
|
||||
],
|
||||
]
|
||||
);
|
||||
$updatedOptions = $this->getAttributeOptions();
|
||||
$this->assertEquals(count($updatedOptions), count($optionsBefore) - 1);
|
||||
foreach ($updatedOptions as $option) {
|
||||
$this->assertContainsEquals($option, $optionsBefore);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
|
||||
*/
|
||||
public function testUpdateOption()
|
||||
{
|
||||
$optionsBefore = $this->getAttributeOptions();
|
||||
$this->operation->execute(
|
||||
[
|
||||
'attribute_id' => $this->attributeId,
|
||||
'value' => [
|
||||
0 => ['updatedValue'],
|
||||
],
|
||||
]
|
||||
);
|
||||
$optionsAfter = $this->getAttributeOptions();
|
||||
$this->assertEquals($optionsAfter[0], 'updatedValue');
|
||||
$this->assertSame(array_slice($optionsBefore, 1), array_slice($optionsAfter, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
$reflection = new \ReflectionObject($this);
|
||||
foreach ($reflection->getProperties() as $property) {
|
||||
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
|
||||
$property->setAccessible(true);
|
||||
$property->setValue($this, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Eav\Setup;
|
||||
|
||||
/**
|
||||
* Test class for Magento\Eav\Setup\EavSetup.
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
class EavSetupTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Eav setup.
|
||||
*
|
||||
* @var \Magento\Eav\Setup\EavSetup
|
||||
*/
|
||||
private $eavSetup;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->eavSetup = $objectManager->create(\Magento\Eav\Setup\EavSetup::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that add attribute work correct attribute_code.
|
||||
*
|
||||
* @param string $attributeCode
|
||||
*
|
||||
* @dataProvider addAttributeDataProvider
|
||||
*/
|
||||
public function testAddAttribute($attributeCode)
|
||||
{
|
||||
$attributeData = $this->getAttributeData();
|
||||
|
||||
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
|
||||
|
||||
$attribute = $this->eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);
|
||||
|
||||
$this->assertEmpty(array_diff($attributeData, $attribute));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testAddAttributeThrowException().
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function addAttributeDataProvider()
|
||||
{
|
||||
return [
|
||||
['eav_setup_test'],
|
||||
['characters_59_characters_59_characters_59_characters_59_59_'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that add attribute throw exception if attribute_code is not valid.
|
||||
*
|
||||
* @param string|null $attributeCode
|
||||
*
|
||||
* @dataProvider addAttributeThrowExceptionDataProvider
|
||||
*/
|
||||
public function testAddAttributeThrowException($attributeCode)
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
|
||||
$this->expectExceptionMessage('An attribute code must not be less than 1 and more than 60 characters.');
|
||||
|
||||
$attributeData = $this->getAttributeData();
|
||||
|
||||
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testAddAttributeThrowException().
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function addAttributeThrowExceptionDataProvider()
|
||||
{
|
||||
return [
|
||||
[null],
|
||||
[''],
|
||||
[' '],
|
||||
['more_than_60_characters_more_than_more_than_60_characters_more'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that add attribute throw exception if attribute_code is not valid.
|
||||
*
|
||||
* @param string|null $attributeCode
|
||||
*
|
||||
* @dataProvider addInvalidAttributeThrowExceptionDataProvider
|
||||
*/
|
||||
public function testAddInvalidAttributeThrowException($attributeCode)
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
|
||||
$this->expectExceptionMessage('Please use only letters (a-z or A-Z), numbers (0-9) or underscore (_) in this field,');
|
||||
|
||||
$attributeData = $this->getAttributeData();
|
||||
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
|
||||
}
|
||||
/**
|
||||
* Data provider for testAddInvalidAttributeThrowException().
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function addInvalidAttributeThrowExceptionDataProvider()
|
||||
{
|
||||
return [
|
||||
['1first_character_is_not_letter'],
|
||||
['attribute.with.dots'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get simple attribute data.
|
||||
*/
|
||||
private function getAttributeData()
|
||||
{
|
||||
$attributeData = [
|
||||
'type' => 'varchar',
|
||||
'backend' => '',
|
||||
'frontend' => '',
|
||||
'label' => 'Eav Setup Test',
|
||||
'input' => 'text',
|
||||
'class' => '',
|
||||
'source' => '',
|
||||
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
|
||||
'visible' => 0,
|
||||
'required' => 0,
|
||||
'user_defined' => 1,
|
||||
'default' => 'none',
|
||||
'searchable' => 0,
|
||||
'filterable' => 0,
|
||||
'comparable' => 0,
|
||||
'visible_on_front' => 0,
|
||||
'unique' => 0,
|
||||
'apply_to' => 'category',
|
||||
];
|
||||
|
||||
return $attributeData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
$reflection = new \ReflectionObject($this);
|
||||
foreach ($reflection->getProperties() as $property) {
|
||||
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
|
||||
$property->setAccessible(true);
|
||||
$property->setValue($this, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user