ReadHandlerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model\ResourceModel;
  7. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  8. use Magento\Framework\EntityManager\EntityMetadataInterface;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. class ReadHandlerTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $configMock;
  16. /**
  17. * @var \Magento\Framework\EntityManager\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $metadataPoolMock;
  20. /**
  21. * @var EntityMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $metadataMock;
  24. /**
  25. * @var \Magento\Eav\Model\ResourceModel\ReadHandler
  26. */
  27. private $readHandler;
  28. /**
  29. * @var \Magento\Framework\Model\Entity\ScopeResolver|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $scopeResolverMock;
  32. protected function setUp()
  33. {
  34. $objectManager = new ObjectManager($this);
  35. $args = $objectManager->getConstructArguments(\Magento\Eav\Model\ResourceModel\ReadHandler::class);
  36. $this->metadataPoolMock = $args['metadataPool'];
  37. $this->metadataMock = $this->getMockBuilder(EntityMetadataInterface::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->metadataPoolMock->expects($this->any())
  41. ->method('getMetadata')
  42. ->willReturn($this->metadataMock);
  43. $this->configMock = $args['config'];
  44. $this->scopeResolverMock = $args['scopeResolver'];
  45. $this->scopeResolverMock->method('getEntityContext')
  46. ->willReturn([]);
  47. $this->readHandler = $objectManager->getObject(\Magento\Eav\Model\ResourceModel\ReadHandler::class, $args);
  48. }
  49. /**
  50. * @param string $eavEntityType
  51. * @param int $callNum
  52. * @param array $expected
  53. * @param bool $isStatic
  54. * @dataProvider executeDataProvider
  55. */
  56. public function testExecute($eavEntityType, $callNum, array $expected, $isStatic = true)
  57. {
  58. $entityData = ['linkField' => 'theLinkField'];
  59. $this->metadataMock->method('getEavEntityType')
  60. ->willReturn($eavEntityType);
  61. $connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $selectMock->method('from')
  68. ->willReturnSelf();
  69. $selectMock->method('where')
  70. ->willReturnSelf();
  71. $connectionMock->method('select')
  72. ->willReturn($selectMock);
  73. $connectionMock->method('fetchAll')
  74. ->willReturn(
  75. [
  76. [
  77. 'attribute_id' => 'attributeId',
  78. 'value' => 'attributeValue',
  79. ]
  80. ]
  81. );
  82. $this->metadataMock->method('getEntityConnection')
  83. ->willReturn($connectionMock);
  84. $this->metadataMock->method('getLinkField')
  85. ->willReturn('linkField');
  86. $attributeMock = $this->getMockBuilder(AbstractAttribute::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $attributeMock->method('isStatic')
  90. ->willReturn($isStatic);
  91. $backendMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class)
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. $backendMock->method('getTable')
  95. ->willReturn('backendTable');
  96. $attributeMock->method('getBackend')
  97. ->willReturn($backendMock);
  98. $attributeMock->method('getAttributeId')
  99. ->willReturn('attributeId');
  100. $attributeMock->method('getAttributeCode')
  101. ->willReturn('attributeCode');
  102. $this->configMock->expects($this->exactly($callNum))
  103. ->method('getEntityAttributes')
  104. ->willReturn([$attributeMock]);
  105. $this->assertEquals($expected, $this->readHandler->execute('entity_type', $entityData));
  106. }
  107. /**
  108. * @return array
  109. */
  110. public function executeDataProvider()
  111. {
  112. return [
  113. 'null entity type' => [null, 0, ['linkField' => 'theLinkField']],
  114. 'static attribute' => ['env-entity-type', 1, ['linkField' => 'theLinkField']],
  115. 'non-static attribute' => [
  116. 'env-entity-type',
  117. 1,
  118. [
  119. 'linkField' => 'theLinkField',
  120. 'attributeCode' => 'attributeValue'
  121. ],
  122. false
  123. ],
  124. ];
  125. }
  126. /**
  127. * @expectedException \Exception
  128. */
  129. public function testExecuteWithException()
  130. {
  131. $this->metadataPoolMock->expects($this->once())
  132. ->method('getMetadata')
  133. ->willThrowException(new \Exception('Unknown entity type'));
  134. $this->configMock->expects($this->never())
  135. ->method('getAttributes');
  136. $this->readHandler->execute('entity_type', ['linkField' => 'theLinkField']);
  137. }
  138. }