AttributeLoaderTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel;
  7. use Magento\Framework\EntityManager\MetadataPool;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. /**
  10. * @magentoAppIsolation enabled
  11. * @magentoDbIsolation enabled
  12. * @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
  13. */
  14. class AttributeLoaderTest extends \Magento\TestFramework\Indexer\TestCase
  15. {
  16. /**
  17. * @var AttributeLoader
  18. */
  19. private $attributeLoader;
  20. protected function setUp()
  21. {
  22. $objectManager = Bootstrap::getObjectManager();
  23. $metadataPool = $objectManager->create(
  24. MetadataPool::class,
  25. [
  26. 'metadata' => [
  27. 'Test\Entity\Type' => [
  28. 'entityTableName' => 'test_entity',
  29. 'eavEntityType' => 'test',
  30. 'identifierField' => 'entity_id',
  31. ]
  32. ]
  33. ]
  34. );
  35. $this->attributeLoader = $objectManager->create(AttributeLoader::class, ['metadataPool' => $metadataPool]);
  36. }
  37. /**
  38. * @param string[] $expectedAttributeCodes
  39. * @param int|null $attributeSetId
  40. * @dataProvider getAttributesDataProvider
  41. */
  42. public function testGetAttributes($expectedAttributeCodes, $attributeSetId = null)
  43. {
  44. $attributes = $this->attributeLoader->getAttributes('Test\Entity\Type', $attributeSetId);
  45. $this->assertEquals(count($expectedAttributeCodes), count($attributes));
  46. $attributeCodes = [];
  47. foreach ($attributes as $attribute) {
  48. $attributeCodes[] = $attribute->getAttributeCode();
  49. }
  50. $this->assertEquals($expectedAttributeCodes, $attributeCodes);
  51. $attributes2 = $this->attributeLoader->getAttributes('Test\Entity\Type', $attributeSetId);
  52. $this->assertEquals($attributes, $attributes2);
  53. }
  54. public function getAttributesDataProvider()
  55. {
  56. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  57. $entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
  58. ->loadByCode('order');
  59. $attributeSetId = $entityType->getDefaultAttributeSetId();
  60. return [
  61. 'all' => [
  62. [
  63. 'attribute_for_search_1',
  64. 'attribute_for_search_2',
  65. 'attribute_for_search_3',
  66. ]
  67. ],
  68. "$attributeSetId" => [
  69. [
  70. 'attribute_for_search_1',
  71. 'attribute_for_search_2',
  72. ],
  73. $attributeSetId,
  74. ]
  75. ];
  76. }
  77. }