SequenceApplierTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\EntityManager\Test\Unit\Sequence;
  7. use Magento\Framework\EntityManager\HydratorPool;
  8. use Magento\Framework\EntityManager\MetadataPool;
  9. use Magento\Framework\EntityManager\Sequence\SequenceApplier;
  10. use Magento\Framework\EntityManager\Sequence\SequenceManager;
  11. use Magento\Framework\EntityManager\Sequence\SequenceRegistry;
  12. use Magento\Framework\EntityManager\TypeResolver;
  13. use Magento\Framework\DataObject;
  14. use Magento\Framework\EntityManager\HydratorInterface;
  15. use Magento\Framework\EntityManager\EntityMetadataInterface;
  16. use Magento\Framework\DB\Sequence\SequenceInterface;
  17. class SequenceApplierTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $metadataPoolMock;
  23. /**
  24. * @var TypeResolver|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $typeResolverMock;
  27. /**
  28. * @var SequenceManager|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $sequenceManagerMock;
  31. /**
  32. * @var SequenceRegistry|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $sequenceRegistryMock;
  35. /**
  36. * @var HydratorPool|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $hydratorPoolMock;
  39. /**
  40. * @var DataObject|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $entityMock;
  43. /**
  44. * @var HydratorInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $hydratorMock;
  47. /**
  48. * @var EntityMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. private $metadataMock;
  51. /**
  52. * @var SequenceInterface|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. private $sequenceMock;
  55. /**
  56. * @var SequenceApplier
  57. */
  58. private $sequenceApplier;
  59. public function setUp()
  60. {
  61. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  62. $this->metadataPoolMock = $this->getMockBuilder(MetadataPool::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->typeResolverMock = $this->getMockBuilder(TypeResolver::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->sequenceManagerMock = $this->getMockBuilder(SequenceManager::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->sequenceRegistryMock = $this->getMockBuilder(SequenceRegistry::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->hydratorPoolMock = $this->getMockBuilder(HydratorPool::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->entityMock = $this->getMockBuilder(DataObject::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->hydratorMock = $this->getMockBuilder(HydratorInterface::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->metadataMock = $this->getMockBuilder(EntityMetadataInterface::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->sequenceMock = $this->getMockBuilder(SequenceInterface::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $this->sequenceApplier = $helper->getObject(
  90. SequenceApplier::class,
  91. [
  92. 'metadataPool' => $this->metadataPoolMock,
  93. 'typeResolver' => $this->typeResolverMock,
  94. 'sequenceManager' => $this->sequenceManagerMock,
  95. 'sequenceRegistry' => $this->sequenceRegistryMock,
  96. 'hydratorPool' => $this->hydratorPoolMock
  97. ]
  98. );
  99. }
  100. public function testApplySequenceIsNull()
  101. {
  102. $entityType = 'entity_type';
  103. $this->typeResolverMock->expects($this->once())
  104. ->method('resolve')
  105. ->with($this->entityMock)
  106. ->willReturn($entityType);
  107. $this->sequenceRegistryMock->expects($this->once())->method('retrieve')->with($entityType)->willReturn(null);
  108. $this->assertEquals($this->entityMock, $this->sequenceApplier->apply($this->entityMock));
  109. }
  110. public function testApplyEntityHasIdentifier()
  111. {
  112. $entityType = 'entity_type';
  113. $identifierField = 'identifier_field';
  114. $entityData = [$identifierField => 'data'];
  115. $sequenceInfo = ['sequence' => $this->sequenceMock];
  116. $this->typeResolverMock->expects($this->once())
  117. ->method('resolve')
  118. ->with($this->entityMock)
  119. ->willReturn($entityType);
  120. $this->sequenceRegistryMock->expects($this->once())
  121. ->method('retrieve')
  122. ->with($entityType)
  123. ->willReturn($sequenceInfo);
  124. $this->metadataPoolMock->expects($this->any())
  125. ->method('getMetadata')
  126. ->with($entityType)
  127. ->willReturn($this->metadataMock);
  128. $this->hydratorPoolMock->expects($this->once())
  129. ->method('getHydrator')
  130. ->with($entityType)
  131. ->willReturn($this->hydratorMock);
  132. $this->hydratorMock->expects($this->once())
  133. ->method('extract')
  134. ->with($this->entityMock)
  135. ->willReturn($entityData);
  136. $this->metadataMock->expects($this->any())->method('getIdentifierField')->willReturn($identifierField);
  137. $this->sequenceManagerMock->expects($this->once())
  138. ->method('force')
  139. ->with(
  140. $entityType,
  141. $entityData[$identifierField]
  142. );
  143. $this->assertEquals($this->entityMock, $this->sequenceApplier->apply($this->entityMock));
  144. }
  145. public function testApplyEntityDoesNotHaveIdentifier()
  146. {
  147. $entityType = 'entity_type';
  148. $identifierField = 'identifier_field';
  149. $identifierFieldEmptyValue = '';
  150. $entityData = [$identifierField => $identifierFieldEmptyValue];
  151. $sequenceInfo = ['sequence' => $this->sequenceMock];
  152. $this->typeResolverMock->expects($this->once())
  153. ->method('resolve')
  154. ->with($this->entityMock)
  155. ->willReturn($entityType);
  156. $this->sequenceRegistryMock->expects($this->once())
  157. ->method('retrieve')
  158. ->with($entityType)
  159. ->willReturn($sequenceInfo);
  160. $this->metadataPoolMock->expects($this->any())
  161. ->method('getMetadata')
  162. ->with($entityType)
  163. ->willReturn($this->metadataMock);
  164. $this->hydratorPoolMock->expects($this->once())
  165. ->method('getHydrator')
  166. ->with($entityType)
  167. ->willReturn($this->hydratorMock);
  168. $this->hydratorMock->expects($this->once())
  169. ->method('extract')
  170. ->with($this->entityMock)
  171. ->willReturn($entityData);
  172. $this->metadataMock->expects($this->any())
  173. ->method('getIdentifierField')
  174. ->willReturn($identifierFieldEmptyValue);
  175. $nextValue = 'next_value_data';
  176. $this->sequenceMock->expects($this->once())->method('getNextValue')->willReturn($nextValue);
  177. $entityData[''] = $nextValue;
  178. $this->hydratorMock->expects($this->once())
  179. ->method('hydrate')
  180. ->with($this->entityMock, $entityData)
  181. ->willReturn($this->entityMock);
  182. $this->assertEquals($this->entityMock, $this->sequenceApplier->apply($this->entityMock));
  183. }
  184. }