AttributeLoaderTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Entity;
  7. use Magento\Framework\DataObject;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Magento\TestFramework\Helper\CacheCleaner;
  10. /**
  11. * @magentoAppIsolation enabled
  12. * @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
  13. */
  14. class AttributeLoaderTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var AttributeLoader
  18. */
  19. private $attributeLoader;
  20. /**
  21. * @var \Magento\Framework\ObjectManagerInterface
  22. */
  23. private $objectManager;
  24. /**
  25. * @var \Magento\Eav\Model\Entity\AbstractEntity
  26. */
  27. private $resource;
  28. protected function setUp()
  29. {
  30. CacheCleaner::cleanAll();
  31. $this->objectManager = Bootstrap::getObjectManager();
  32. $this->attributeLoader = $this->objectManager->get(AttributeLoader::class);
  33. $entityType = $this->objectManager->create(\Magento\Eav\Model\Entity\Type::class)
  34. ->loadByCode('test');
  35. $context = $this->objectManager->get(\Magento\Eav\Model\Entity\Context::class);
  36. $this->resource = $this->getMockBuilder(\Magento\Eav\Model\Entity\AbstractEntity::class)
  37. ->setConstructorArgs([$context])
  38. ->setMethods(['getEntityType', 'getLinkField'])
  39. ->getMock();
  40. $this->resource->method('getEntityType')
  41. ->willReturn($entityType);
  42. $this->resource->method('getLinkField')
  43. ->willReturn('link_field');
  44. }
  45. /**
  46. * @param int $expectedNumOfAttributesByCode
  47. * @param int $expectedNumOfAttributesByTable
  48. * @param DataObject|null $object
  49. * @dataProvider loadAllAttributesDataProvider
  50. */
  51. public function testLoadAllAttributesTheFirstTime(
  52. $expectedNumOfAttributesByCode,
  53. $expectedNumOfAttributesByTable,
  54. $object
  55. ) {
  56. // Before load all attributes
  57. $attributesByCode = $this->resource->getAttributesByCode();
  58. $attributesByTable = $this->resource->getAttributesByTable();
  59. $this->assertEquals(0, count($attributesByCode));
  60. $this->assertEquals(0, count($attributesByTable));
  61. // Load all attributes
  62. $resource2 = $this->attributeLoader->loadAllAttributes(
  63. $this->resource,
  64. $object
  65. );
  66. $attributesByCode2 = $resource2->getAttributesByCode();
  67. $attributesByTable2 = $resource2->getAttributesByTable();
  68. $this->assertEquals($expectedNumOfAttributesByCode, count($attributesByCode2));
  69. $this->assertEquals($expectedNumOfAttributesByTable, count($attributesByTable2));
  70. }
  71. public function loadAllAttributesDataProvider()
  72. {
  73. /** @var \Magento\Eav\Model\Entity\Type $entityType */
  74. $entityType = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Entity\Type::class)
  75. ->loadByCode('order');
  76. $attributeSetId = $entityType->getDefaultAttributeSetId();
  77. return [
  78. [
  79. 13,
  80. 2,
  81. null
  82. ],
  83. [
  84. 10,
  85. 1,
  86. new DataObject(
  87. [
  88. 'attribute_set_id' => $attributeSetId,
  89. 'store_id' => 0
  90. ]
  91. ),
  92. ],
  93. [
  94. 10,
  95. 1,
  96. new DataObject(
  97. [
  98. 'attribute_set_id' => $attributeSetId,
  99. 'store_id' => 10
  100. ]
  101. ),
  102. ],
  103. ];
  104. }
  105. }