Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PersistedObjectHandlerTest.php
Go to the documentation of this file.
1 <?php
8 
9 use AspectMock\Test as AspectMock;
17 
22 {
23  public function testCreateSimpleEntity()
24  {
25  // Test Data and Variables
26  $entityName = "EntityOne";
27  $entityStepKey = "StepKey";
28  $dataKey = "testKey";
29  $dataValue = "testValue";
31  $parserOutput = [
32  'entity' => [
33  'EntityOne' => [
34  'type' => 'testType',
35  'data' => [
36  0 => [
37  'key' => $dataKey,
38  'value' => $dataValue
39  ]
40  ]
41  ]
42  ]
43  ];
44  $jsonResponse = "
45  {
46  \"" . strtolower($dataKey) . "\" : \"{$dataValue}\"
47  }
48  ";
49 
50  // Mock Classes
51  $this->mockDataHandlerWithOutput($parserOutput);
52  $this->mockCurlHandler($jsonResponse);
54 
55  // Call method
56  $handler->createEntity(
57  $entityStepKey,
58  $scope,
59  $entityName
60  );
61 
62  $persistedValue = $handler->retrieveEntityField($entityStepKey, $dataKey, $scope);
63  $this->assertEquals($dataValue, $persistedValue);
64  }
65 
66  public function testDeleteSimpleEntity()
67  {
68  // Test Data and Variables
69  $entityName = "EntityOne";
70  $entityStepKey = "StepKey";
71  $dataKey = "testKey";
72  $dataValue = "testValue";
74  $parserOutput = [
75  'entity' => [
76  'EntityOne' => [
77  'type' => 'testType',
78  'data' => [
79  0 => [
80  'key' => $dataKey,
81  'value' => $dataValue
82  ]
83  ]
84  ]
85  ]
86  ];
87  $jsonResponse = "
88  {
89  \"" . strtolower($dataKey) . "\" : \"{$dataValue}\"
90  }
91  ";
92 
93  // Mock Classes
94  $this->mockDataHandlerWithOutput($parserOutput);
95  $this->mockCurlHandler($jsonResponse);
97 
98  // Call method
99  $handler->createEntity(
100  $entityStepKey,
101  $scope,
102  $entityName
103  );
104 
105  $handler->deleteEntity(
106  $entityStepKey,
107  $scope
108  );
109 
110  // Handler found and called Delete on existing entity
111  $this->addToAssertionCount(1);
112  }
113 
114  public function testGetSimpleEntity()
115  {
116  // Test Data and Variables
117  $entityName = "EntityOne";
118  $entityStepKey = "StepKey";
119  $dataKey = "testKey";
120  $dataValue = "testValue";
122  $parserOutput = [
123  'entity' => [
124  'EntityOne' => [
125  'type' => 'testType',
126  'data' => [
127  0 => [
128  'key' => $dataKey,
129  'value' => $dataValue
130  ]
131  ]
132  ]
133  ]
134  ];
135  $jsonResponse = "
136  {
137  \"" . strtolower($dataKey) . "\" : \"{$dataValue}\"
138  }
139  ";
140 
141  // Mock Classes
142  $this->mockDataHandlerWithOutput($parserOutput);
143  $this->mockCurlHandler($jsonResponse);
145 
146  // Call method
147  $handler->getEntity(
148  $entityStepKey,
149  $scope,
150  $entityName
151  );
152 
153  $persistedValue = $handler->retrieveEntityField($entityStepKey, $dataKey, $scope);
154  $this->assertEquals($dataValue, $persistedValue);
155  }
156 
157  public function testUpdateSimpleEntity()
158  {
159  $this->markTestSkipped("Potential Bug in DataPersistenceHandler class");
160  // Test Data and Variables
161  $entityName = "EntityOne";
162  $entityStepKey = "StepKey";
163  $dataKey = "testKey";
164  $dataValue = "testValue";
165  $updateName = "EntityTwo";
166  $updateValue = "newValue";
168  $parserOutput = [
169  'entity' => [
170  $entityName => [
171  'type' => 'testType',
172  'data' => [
173  0 => [
174  'key' => $dataKey,
175  'value' => $dataValue
176  ]
177  ]
178  ],
179  $updateName => [
180  'type' => 'testType',
181  'data' => [
182  0 => [
183  'key' => $dataKey,
184  'value' => $updateValue
185  ]
186  ]
187  ]
188  ]
189  ];
190  $jsonResponse = "
191  {
192  \"" . strtolower($dataKey) . "\" : \"{$dataValue}\"
193  }
194  ";
195  $updatedResponse = "
196  {
197  \"" . strtolower($dataKey) . "\" : \"{$updateValue}\"
198  }
199  ";
200 
201  // Mock Classes
202  $this->mockDataHandlerWithOutput($parserOutput);
203  $this->mockCurlHandler($jsonResponse);
205  $handler->createEntity(
206  $entityStepKey,
207  $scope,
208  $entityName
209  );
210  $this->mockCurlHandler($updatedResponse);
211 
212  // Call method
213  $handler->updateEntity(
214  $entityStepKey,
215  $scope,
216  $updateName
217  );
218 
219  $persistedValue = $handler->retrieveEntityField($entityStepKey, $dataKey, $scope);
220  $this->assertEquals($updateValue, $persistedValue);
221  }
222 
224  {
225  // Test Data and Variables
226  $entityNameOne = "EntityOne";
227  $entityStepKeyOne = "StepKeyOne";
228  $dataKeyOne = "testKeyOne";
229  $dataValueOne = "testValueOne";
230  $entityNameTwo = "EntityTwo";
231  $entityStepKeyTwo = "StepKeyTwo";
232  $dataKeyTwo = "testKeyTwo";
233  $dataValueTwo = "testValueTwo";
234  $entityNameThree = "EntityThree";
235  $entityStepKeyThree = "StepKeyThree";
236  $dataKeyThree = "testKeyThree";
237  $dataValueThree = "testValueThree";
238 
239  $parserOutputOne = [
240  'entity' => [
241  $entityNameOne => [
242  'type' => 'testType',
243  'data' => [
244  0 => [
245  'key' => $dataKeyOne,
246  'value' => $dataValueOne
247  ]
248  ]
249  ],
250  $entityNameTwo => [
251  'type' => 'testType',
252  'data' => [
253  0 => [
254  'key' => $dataKeyTwo,
255  'value' => $dataValueTwo
256  ]
257  ]
258  ],
259  $entityNameThree => [
260  'type' => 'testType',
261  'data' => [
262  0 => [
263  'key' => $dataKeyThree,
264  'value' => $dataValueThree
265  ]
266  ]
267  ]
268  ]
269  ];
270  $jsonReponseOne = "
271  {
272  \"" . strtolower($dataKeyOne) . "\" : \"{$dataValueOne}\"
273  }
274  ";
275  $jsonReponseTwo = "
276  {
277  \"" . strtolower($dataKeyTwo) . "\" : \"{$dataValueTwo}\"
278  }
279  ";
280  $jsonReponseThree = "
281  {
282  \"" . strtolower($dataKeyThree) . "\" : \"{$dataValueThree}\"
283  }
284  ";
285 
286  // Mock Classes and Create Entities
288 
289  $this->mockDataHandlerWithOutput($parserOutputOne);
290  $this->mockCurlHandler($jsonReponseOne);
291  $handler->createEntity(
292  $entityStepKeyOne,
294  $entityNameOne
295  );
296 
297  $this->mockCurlHandler($jsonReponseTwo);
298  $handler->createEntity(
299  $entityStepKeyTwo,
301  $entityNameTwo
302  );
303 
304  $this->mockCurlHandler($jsonReponseThree);
305  $handler->createEntity(
306  $entityStepKeyThree,
308  $entityNameThree
309  );
310 
311  // Call method
312  $retrievedFromTest = $handler->retrieveEntityField(
313  $entityStepKeyOne,
314  $dataKeyOne,
316  );
317  $retrievedFromHook = $handler->retrieveEntityField(
318  $entityStepKeyTwo,
319  $dataKeyTwo,
321  );
322  $retrievedFromSuite = $handler->retrieveEntityField(
323  $entityStepKeyThree,
324  $dataKeyThree,
326  );
327 
328  $this->assertEquals($dataValueOne, $retrievedFromTest);
329  $this->assertEquals($dataValueTwo, $retrievedFromHook);
330  $this->assertEquals($dataValueThree, $retrievedFromSuite);
331  }
332 
338  public function mockDataHandlerWithOutput($parserOutput)
339  {
340  // Clear DataObjectHandler singleton if already set
341  $property = new \ReflectionProperty(DataObjectHandler::class, "INSTANCE");
342  $property->setAccessible(true);
343  $property->setValue(null);
344 
345  $mockDataProfileSchemaParser = AspectMock::double(DataProfileSchemaParser::class, [
346  'readDataProfiles' => $parserOutput
347  ])->make();
348 
349  $mockObjectManager = AspectMock::double(ObjectManager::class, [
350  'create' => $mockDataProfileSchemaParser
351  ])->make();
352 
353  AspectMock::double(ObjectManagerFactory::class, [
354  'getObjectManager' => $mockObjectManager
355  ]);
356  }
357 
358  public function mockCurlHandler($response)
359  {
360  AspectMock::double(CurlHandler::class, [
361  "__construct" => null,
362  "executeRequest" => $response,
363  "getRequestDataArray" => [],
364  "isContentTypeJson" => true
365  ]);
366  }
367 
368  public function tearDown()
369  {
370  // Clear out Singleton between tests
371  $property = new \ReflectionProperty(PersistedObjectHandler::class, "INSTANCE");
372  $property->setAccessible(true);
373  $property->setValue(null);
374 
375  parent::tearDown(); // TODO: Change the autogenerated stub
376  }
377 }
$response
Definition: 404.php:11
catch(\Exception $e) $handler
Definition: index.php:30