AttributeTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Weee\Model\ResourceModel;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\Eav\Model\Entity\Attribute as EavAttribute;
  10. use Magento\Framework\EntityManager\MetadataPool;
  11. /**
  12. * Test class for Magento\Catalog\Model\ResourceModel\Attribute class
  13. * with backend model Magento\Weee\Model\Attribute\Backend\Weee\Tax.
  14. *
  15. * @see Magento\Catalog\Model\ResourceModel\Attribute
  16. */
  17. class AttributeTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var \Magento\Catalog\Model\ResourceModel\Attribute
  21. */
  22. private $model;
  23. /**
  24. * @var \Magento\Catalog\Model\ResourceModel\Product
  25. */
  26. protected $productResource;
  27. /**
  28. * @var ProductRepositoryInterface
  29. */
  30. private $productRepository;
  31. /**
  32. * @var MetadataPool
  33. */
  34. private $metadataPool;
  35. /**
  36. * @var \Magento\Framework\ObjectManagerInterface
  37. */
  38. protected $objectManager;
  39. /**
  40. * @inheritdoc
  41. */
  42. protected function setUp()
  43. {
  44. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  45. $this->model = $this->objectManager->get(
  46. \Magento\Catalog\Model\ResourceModel\Attribute::class
  47. );
  48. $this->productResource = $this->objectManager->get(
  49. \Magento\Catalog\Model\ResourceModel\Product::class
  50. );
  51. $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
  52. $this->metadataPool = $this->objectManager->get(MetadataPool::class);
  53. }
  54. /**
  55. * Retrieve eav attribute row.
  56. *
  57. * @param int $entityTypeId
  58. * @param int $attributeSetId
  59. * @param int $attributeId
  60. * @return array|false
  61. */
  62. private function getEavEntityAttributeRow(int $entityTypeId, int $attributeSetId, int $attributeId)
  63. {
  64. $connection = $this->productResource->getConnection();
  65. $select = $connection->select()
  66. ->from($this->productResource->getTable('eav_entity_attribute'))
  67. ->where('attribute_set_id=?', $attributeSetId)
  68. ->where('attribute_id=?', $attributeId)
  69. ->where('entity_type_id=?', $entityTypeId);
  70. return $connection->fetchRow($select);
  71. }
  72. /**
  73. * Test to delete entity attribute with type "Fixed Product Tax".
  74. *
  75. * @magentoDataFixture Magento/Weee/_files/fixed_product_attribute.php
  76. * @return void
  77. */
  78. public function testDeleteEntityFixedTax() : void
  79. {
  80. /* @var EavAttribute $attribute */
  81. $attribute = $this->objectManager->get(EavAttribute::class);
  82. $attribute->loadByCode(\Magento\Catalog\Model\Product::ENTITY, 'fixed_product_attribute');
  83. $entityEavAttributeRow = $this->getEavEntityAttributeRow(
  84. (int)$attribute->getEntityTypeId(),
  85. 4,
  86. (int)$attribute->getId()
  87. );
  88. $this->assertNotEmpty(
  89. $entityEavAttributeRow['entity_attribute_id'],
  90. 'The record is absent in table `eav_entity_attribute` for `fixed_product_attribute`'
  91. );
  92. $attribute->setData('entity_attribute_id', $entityEavAttributeRow['entity_attribute_id']);
  93. $this->model->deleteEntity($attribute);
  94. $entityEavAttributeRow = $this->getEavEntityAttributeRow(
  95. (int)$attribute->getEntityTypeId(),
  96. 4,
  97. (int)$attribute->getId()
  98. );
  99. $this->assertEmpty(
  100. $entityEavAttributeRow,
  101. 'The record was not removed from table `eav_entity_attribute` for `fixed_product_attribute`'
  102. );
  103. }
  104. }