Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SequenceApplierTest.php
Go to the documentation of this file.
1 <?php
7 
18 
19 class SequenceApplierTest extends \PHPUnit\Framework\TestCase
20 {
24  private $metadataPoolMock;
25 
29  private $typeResolverMock;
30 
34  private $sequenceManagerMock;
35 
39  private $sequenceRegistryMock;
40 
44  private $hydratorPoolMock;
45 
49  private $entityMock;
50 
54  private $hydratorMock;
55 
59  private $metadataMock;
60 
64  private $sequenceMock;
65 
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 
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 
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 }
$helper
Definition: iframe.phtml:13