Skip to content

Commit 167c7e7

Browse files
authored
Merge pull request #432 from magento/MQE-558
MQE-588: CreatedData should throw Error/Warning when undefined data i…
2 parents a233e32 + a708d46 commit 167c7e7

File tree

2 files changed

+145
-3
lines changed

2 files changed

+145
-3
lines changed

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

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,27 @@
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;
19+
use tests\unit\Util\TestLoggingUtil;
1720

1821
/**
1922
* Class PersistedObjectHandlerTest
2023
*/
2124
class PersistedObjectHandlerTest extends MagentoTestCase
2225
{
26+
/**
27+
* Before test functionality
28+
* @return void
29+
*/
30+
public function setUp()
31+
{
32+
TestLoggingUtil::getInstance()->setMockLoggingUtil();
33+
}
34+
2335
public function testCreateSimpleEntity()
2436
{
2537
// Test Data and Variables
@@ -330,6 +342,115 @@ public function testRetrieveEntityAcrossScopes()
330342
$this->assertEquals($dataValueThree, $retrievedFromSuite);
331343
}
332344

345+
/**
346+
* @param string $name
347+
* @param string $key
348+
* @param string $value
349+
* @param string $type
350+
* @param string $scope
351+
* @param string $stepKey
352+
* @dataProvider entityDataProvider
353+
*/
354+
public function testRetrieveEntityValidField($name, $key, $value, $type, $scope, $stepKey)
355+
{
356+
$parserOutputOne = [
357+
'entity' => [
358+
$name => [
359+
'type' => $type,
360+
'data' => [
361+
0 => [
362+
'key' => $key,
363+
'value' => $value
364+
]
365+
]
366+
]
367+
]
368+
];
369+
$jsonReponseOne = "
370+
{
371+
\"" . strtolower($key) . "\" : \"{$value}\"
372+
}
373+
";
374+
375+
// Mock Classes and Create Entities
376+
$handler = PersistedObjectHandler::getInstance();
377+
378+
$this->mockDataHandlerWithOutput($parserOutputOne);
379+
$this->mockCurlHandler($jsonReponseOne);
380+
$handler->createEntity($stepKey, $scope, $name);
381+
382+
// Call method
383+
$retrieved = $handler->retrieveEntityField($stepKey, $key, $scope);
384+
385+
$this->assertEquals($value, $retrieved);
386+
}
387+
388+
/**
389+
* @param string $name
390+
* @param string $key
391+
* @param string $value
392+
* @param string $type
393+
* @param string $scope
394+
* @param string $stepKey
395+
* @dataProvider entityDataProvider
396+
* @throws TestReferenceException
397+
* @throws TestFrameworkException
398+
*/
399+
public function testRetrieveEntityInValidField($name, $key, $value, $type, $scope, $stepKey)
400+
{
401+
$invalidDataKey = "invalidDataKey";
402+
$warnMsg = "Undefined field {$invalidDataKey} in entity object with a stepKey of {$stepKey}\n";
403+
$warnMsg .= "Please fix the invalid reference. This will result in fatal error in next major release.";
404+
405+
$parserOutputOne = [
406+
'entity' => [
407+
$name => [
408+
'type' => $type,
409+
'data' => [
410+
0 => [
411+
'key' => $key,
412+
'value' => $value
413+
]
414+
]
415+
]
416+
]
417+
];
418+
$jsonReponseOne = "
419+
{
420+
\"" . strtolower($key) . "\" : \"{$value}\"
421+
}
422+
";
423+
424+
// Mock Classes and Create Entities
425+
$handler = PersistedObjectHandler::getInstance();
426+
427+
$this->mockDataHandlerWithOutput($parserOutputOne);
428+
$this->mockCurlHandler($jsonReponseOne);
429+
$handler->createEntity($stepKey, $scope, $name);
430+
431+
// Call method
432+
$handler->retrieveEntityField($stepKey, $invalidDataKey, $scope);
433+
434+
// validate log statement
435+
TestLoggingUtil::getInstance()->validateMockLogStatement(
436+
'warning',
437+
$warnMsg,
438+
[]
439+
);
440+
}
441+
442+
/**
443+
* Data provider for testRetrieveEntityField
444+
*/
445+
public static function entityDataProvider()
446+
{
447+
return [
448+
['Entity1', 'testKey1', 'testValue1', 'testType', PersistedObjectHandler::HOOK_SCOPE, 'StepKey1'],
449+
['Entity2', 'testKey2', 'testValue2', 'testType', PersistedObjectHandler::SUITE_SCOPE, 'StepKey2'],
450+
['Entity3', 'testKey3', 'testValue3', 'testType', PersistedObjectHandler::TEST_SCOPE, 'StepKey3']
451+
];
452+
}
453+
333454
/**
334455
* Mocks DataObjectHandler to use given output to create
335456
* @param $parserOutput
@@ -374,4 +495,14 @@ public function tearDown()
374495

375496
parent::tearDown(); // TODO: Change the autogenerated stub
376497
}
498+
499+
/**
500+
* After class functionality
501+
* @return void
502+
*/
503+
public static function tearDownAfterClass()
504+
{
505+
TestLoggingUtil::getInstance()->clearMockLoggingUtil();
506+
parent::tearDownAfterClass();
507+
}
377508
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
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;
12+
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
13+
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
1114

1215
class PersistedObjectHandler
1316
{
@@ -177,12 +180,20 @@ public function getEntity($key, $scope, $entity, $dependentObjectKeys = [], $sto
177180
* @param string $field
178181
* @param string $scope
179182
* @return string
180-
* @throws TestReferenceException
181-
* @throws \Magento\FunctionalTestingFramework\Exceptions\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+
$warnMsg = "Undefined field {$field} in entity object with a stepKey of {$stepKey}\n";
189+
$warnMsg .= "Please fix the invalid reference. This will result in fatal error in next major release.";
190+
//TODO: change this to throw an exception in next major release
191+
LoggingUtil::getInstance()->getLogger(PersistedObjectHandler::class)->warn($warnMsg);
192+
if (MftfApplicationConfig::getConfig()->getPhase() !== MftfApplicationConfig::UNIT_TEST_PHASE) {
193+
print("\n$warnMsg\n");
194+
}
195+
}
196+
return $fieldValue;
186197
}
187198

188199
/**

0 commit comments

Comments
 (0)