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