Skip to content

Commit 4d46f9b

Browse files
committed
MQE-588: CreatedData should throw Error/Warning when undefined data is returned
1 parent ee12a62 commit 4d46f9b

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;
1212
use Magento\FunctionalTestingFramework\DataGenerator\Parsers\DataProfileSchemaParser;
1313
use Magento\FunctionalTestingFramework\DataGenerator\Persist\CurlHandler;
14+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
15+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
1416
use Magento\FunctionalTestingFramework\ObjectManager;
1517
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
1618
use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
@@ -330,6 +332,108 @@ public function testRetrieveEntityAcrossScopes()
330332
$this->assertEquals($dataValueThree, $retrievedFromSuite);
331333
}
332334

335+
/**
336+
* @param string $name
337+
* @param string $key
338+
* @param string $value
339+
* @param string $type
340+
* @param string $scope
341+
* @param string $stepKey
342+
* @dataProvider entityDataProvider
343+
*/
344+
public function testRetrieveEntityValidField($name, $key, $value, $type, $scope, $stepKey)
345+
{
346+
$parserOutputOne = [
347+
'entity' => [
348+
$name => [
349+
'type' => $type,
350+
'data' => [
351+
0 => [
352+
'key' => $key,
353+
'value' => $value
354+
]
355+
]
356+
]
357+
]
358+
];
359+
$jsonReponseOne = "
360+
{
361+
\"" . strtolower($key) . "\" : \"{$value}\"
362+
}
363+
";
364+
365+
// Mock Classes and Create Entities
366+
$handler = PersistedObjectHandler::getInstance();
367+
368+
$this->mockDataHandlerWithOutput($parserOutputOne);
369+
$this->mockCurlHandler($jsonReponseOne);
370+
$handler->createEntity($stepKey, $scope, $name);
371+
372+
// Call method
373+
$retrieved = $handler->retrieveEntityField($stepKey, $key, $scope);
374+
375+
$this->assertEquals($value, $retrieved);
376+
}
377+
378+
/**
379+
* @param string $name
380+
* @param string $key
381+
* @param string $value
382+
* @param string $type
383+
* @param string $scope
384+
* @param string $stepKey
385+
* @dataProvider entityDataProvider
386+
* @throws TestReferenceException
387+
* @throws TestFrameworkException
388+
*/
389+
public function testRetrieveEntityInValidField($name, $key, $value, $type, $scope, $stepKey)
390+
{
391+
$invalidDataKey = "invalidDataKey";
392+
393+
$parserOutputOne = [
394+
'entity' => [
395+
$name => [
396+
'type' => $type,
397+
'data' => [
398+
0 => [
399+
'key' => $key,
400+
'value' => $value
401+
]
402+
]
403+
]
404+
]
405+
];
406+
$jsonReponseOne = "
407+
{
408+
\"" . strtolower($key) . "\" : \"{$value}\"
409+
}
410+
";
411+
412+
// Mock Classes and Create Entities
413+
$handler = PersistedObjectHandler::getInstance();
414+
415+
$this->mockDataHandlerWithOutput($parserOutputOne);
416+
$this->mockCurlHandler($jsonReponseOne);
417+
$handler->createEntity($stepKey, $scope, $name);
418+
419+
$this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestReferenceException::class);
420+
421+
// Call method
422+
$handler->retrieveEntityField($stepKey, $invalidDataKey, $scope);
423+
}
424+
425+
/**
426+
* Data provider for testRetrieveEntityField
427+
*/
428+
public static function entityDataProvider()
429+
{
430+
return [
431+
['Entity1', 'testKey1', 'testValue1', 'testType', PersistedObjectHandler::HOOK_SCOPE, 'StepKey1'],
432+
['Entity2', 'testKey2', 'testValue2', 'testType', PersistedObjectHandler::SUITE_SCOPE, 'StepKey2'],
433+
['Entity3', 'testKey3', 'testValue3', 'testType', PersistedObjectHandler::TEST_SCOPE, 'StepKey3']
434+
];
435+
}
436+
333437
/**
334438
* Mocks DataObjectHandler to use given output to create
335439
* @param $parserOutput

src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\FunctionalTestingFramework\DataGenerator\Handlers;
88

99
use Magento\FunctionalTestingFramework\DataGenerator\Persist\DataPersistenceHandler;
10+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
1011
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
1112

1213
class PersistedObjectHandler
@@ -178,11 +179,15 @@ public function getEntity($key, $scope, $entity, $dependentObjectKeys = [], $sto
178179
* @param string $scope
179180
* @return string
180181
* @throws TestReferenceException
181-
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException
182+
* @throws TestFrameworkException
182183
*/
183184
public function retrieveEntityField($stepKey, $field, $scope)
184185
{
185-
return $this->retrieveEntity($stepKey, $scope)->getCreatedDataByName($field);
186+
$fieldValue = $this->retrieveEntity($stepKey, $scope)->getCreatedDataByName($field);
187+
if ($fieldValue === null) {
188+
throw new TestReferenceException("Undefined field {$field} in entity object with a stepKey of {$stepKey}");
189+
}
190+
return $fieldValue;
186191
}
187192

188193
/**

0 commit comments

Comments
 (0)