ConfigTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model;
  7. use Magento\Framework\DataObject;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Magento\TestFramework\Helper\CacheCleaner;
  10. /**
  11. * @magentoAppIsolation enabled
  12. * @magentoDbIsolation enabled
  13. * @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
  14. */
  15. class ConfigTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var Config
  19. */
  20. private $config;
  21. protected function setUp()
  22. {
  23. $objectManager = Bootstrap::getObjectManager();
  24. $this->config = $objectManager->get(Config::class);
  25. }
  26. public function testGetEntityAttributeCodes()
  27. {
  28. $entityType = 'test';
  29. CacheCleaner::cleanAll();
  30. $entityAttributeCodes1 = $this->config->getEntityAttributeCodes($entityType);
  31. $this->assertEquals(
  32. [
  33. 'attribute_for_search_1',
  34. 'attribute_for_search_2',
  35. 'attribute_for_search_3',
  36. 'attribute_for_search_4',
  37. 'attribute_for_search_5',
  38. ],
  39. $entityAttributeCodes1
  40. );
  41. $entityAttributeCodes2 = $this->config->getEntityAttributeCodes($entityType);
  42. $this->assertEquals($entityAttributeCodes1, $entityAttributeCodes2);
  43. }
  44. public function testGetEntityAttributeCodesWithObject()
  45. {
  46. $entityType = 'test';
  47. /** @var \Magento\Eav\Model\Entity\Type $testEntityType */
  48. $testEntityType = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Entity\Type::class)
  49. ->loadByCode('test');
  50. $attributeSetId = $testEntityType->getDefaultAttributeSetId();
  51. CacheCleaner::cleanAll();
  52. $object = new DataObject(
  53. [
  54. 'attribute_set_id' => $attributeSetId,
  55. 'store_id' => 0
  56. ]
  57. );
  58. $entityAttributeCodes1 = $this->config->getEntityAttributeCodes($entityType, $object);
  59. $this->assertEquals(
  60. [
  61. 'attribute_for_search_1',
  62. 'attribute_for_search_2',
  63. ],
  64. $entityAttributeCodes1
  65. );
  66. $entityAttributeCodes2 = $this->config->getEntityAttributeCodes($entityType, $object);
  67. $this->assertEquals($entityAttributeCodes1, $entityAttributeCodes2);
  68. }
  69. public function testGetAttributes()
  70. {
  71. $entityType = 'test';
  72. CacheCleaner::cleanAll();
  73. $attributes1 = $this->config->getAttributes($entityType);
  74. $expectedAttributeCodes = [
  75. 'attribute_for_search_1',
  76. 'attribute_for_search_2',
  77. 'attribute_for_search_3',
  78. 'attribute_for_search_4',
  79. 'attribute_for_search_5',
  80. ];
  81. $this->assertEquals(count($expectedAttributeCodes), count($attributes1));
  82. $attributeCodes = [];
  83. foreach ($attributes1 as $attribute) {
  84. $attributeCodes[] = $attribute->getAttributeCode();
  85. }
  86. $this->assertEquals($expectedAttributeCodes, $attributeCodes);
  87. $attributes2 = $this->config->getAttributes($entityType);
  88. $this->assertEquals($attributes1, $attributes2);
  89. }
  90. public function testGetAttribute()
  91. {
  92. $entityType = 'test';
  93. CacheCleaner::cleanAll();
  94. $attribute1 = $this->config->getAttribute($entityType, 'attribute_for_search_1');
  95. $this->assertEquals('attribute_for_search_1', $attribute1->getAttributeCode());
  96. $this->assertEquals('varchar', $attribute1->getBackendType());
  97. $this->assertEquals(1, $attribute1->getIsRequired());
  98. $this->assertEquals(1, $attribute1->getIsUserDefined());
  99. $this->assertEquals(0, $attribute1->getIsUnique());
  100. $attribute2 = $this->config->getAttribute($entityType, 'attribute_for_search_1');
  101. $this->assertEquals($attribute1, $attribute2);
  102. }
  103. }