AttributeManagementTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model;
  7. class AttributeManagementTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Eav\Api\AttributeManagementInterface
  11. */
  12. private $model;
  13. /**
  14. * @var \Magento\Framework\ObjectManagerInterface
  15. */
  16. private $objectManager;
  17. protected function setUp()
  18. {
  19. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  20. $this->model = $this->objectManager->create(\Magento\Eav\Api\AttributeManagementInterface::class);
  21. }
  22. /**
  23. * Verify that collection in service used correctly
  24. */
  25. public function testGetList()
  26. {
  27. $productAttributeSetId = $this->getAttributeSetId(
  28. \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE
  29. );
  30. $productAttributes = $this->model->getAttributes(
  31. \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
  32. $productAttributeSetId
  33. );
  34. // Verify that result contains only product attributes
  35. $this->verifyAttributeSetIds($productAttributes, $productAttributeSetId);
  36. $categoryAttributeSetId = $this->getAttributeSetId(
  37. \Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE
  38. );
  39. $categoryAttributes = $this->model->getAttributes(
  40. \Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE,
  41. $categoryAttributeSetId
  42. );
  43. // Verify that result contains only category attributes
  44. $this->verifyAttributeSetIds($categoryAttributes, $categoryAttributeSetId);
  45. }
  46. /**
  47. * @param string $entityTypeCode
  48. * @return int
  49. */
  50. private function getAttributeSetId($entityTypeCode)
  51. {
  52. /** @var \Magento\Eav\Model\Config $eavConfig */
  53. $eavConfig = $this->objectManager->create(\Magento\Eav\Model\Config::class);
  54. return $eavConfig->getEntityType($entityTypeCode)->getDefaultAttributeSetId();
  55. }
  56. /**
  57. * @param array $items
  58. * @param string $attributeSetId
  59. * @return void
  60. */
  61. private function verifyAttributeSetIds(array $items, $attributeSetId)
  62. {
  63. /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $item */
  64. foreach ($items as $item) {
  65. $this->assertEquals($attributeSetId, $item->getAttributeSetId());
  66. }
  67. }
  68. }