AttributeProviderTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Model\Test\Unit\EntitySnapshot;
  7. use Magento\Framework\DB\Adapter\AdapterInterface;
  8. use Magento\Framework\EntityManager\EntityMetadata;
  9. use Magento\Framework\Model\EntitySnapshot\AttributeProvider;
  10. use Magento\Framework\Model\EntitySnapshot\AttributeProviderInterface;
  11. use Magento\Framework\EntityManager\MetadataPool;
  12. use Magento\Framework\ObjectManagerInterface as ObjectManager;
  13. /**
  14. * Class AttributeProviderTest
  15. */
  16. class AttributeProviderTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $concreteAttributeProviderMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $metadataPoolMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $objectManagerMock;
  30. /**
  31. * @var AttributeProvider
  32. */
  33. protected $attributeProvider;
  34. protected function setUp()
  35. {
  36. $this->concreteAttributeProviderMock = $this->getMockBuilder(
  37. AttributeProviderInterface::class
  38. )->getMockForAbstractClass();
  39. $this->metadataPoolMock = $this->getMockBuilder(MetadataPool::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->objectManagerMock = $this->getMockBuilder(ObjectManager::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->attributeProvider = new AttributeProvider(
  46. $this->metadataPoolMock,
  47. $this->objectManagerMock,
  48. ['default' => ['eav' => AttributeProviderInterface::class]]
  49. );
  50. }
  51. public function testGetAttributes()
  52. {
  53. $entityType = 'Product';
  54. $entityTable = 'entity_table';
  55. $linkField = 'some_id';
  56. $identifierField = 'id';
  57. $metadata = $this->getMockBuilder(EntityMetadata::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $attributes = ['test' => 1];
  61. $this->metadataPoolMock->expects($this->atLeastOnce())->method('getMetadata')->willReturn($metadata);
  62. $connection = $this->getMockBuilder(AdapterInterface::class)->getMockForAbstractClass();
  63. $metadata->expects($this->once())->method('getEntityConnection')->willReturn($connection);
  64. $metadata->expects($this->once())->method('getEntityTable')->willReturn($entityTable);
  65. $metadata->expects($this->exactly(2))->method('getLinkField')->willReturn($linkField);
  66. $metadata->expects($this->once())->method('getIdentifierField')->willReturn($identifierField);
  67. $connection->expects($this->once())->method('describeTable')->with($entityTable)->willReturn([]);
  68. $this->objectManagerMock->expects($this->once())
  69. ->method('get')
  70. ->with(AttributeProviderInterface::class)
  71. ->willReturn($this->concreteAttributeProviderMock);
  72. $this->concreteAttributeProviderMock->expects($this->once())->method('getAttributes')
  73. ->with($entityType)
  74. ->willReturn($attributes);
  75. $this->assertEquals($attributes, $this->attributeProvider->getAttributes($entityType));
  76. }
  77. }