diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php index 30dc3ec40..6c3823466 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php @@ -11,15 +11,27 @@ use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler; use Magento\FunctionalTestingFramework\DataGenerator\Parsers\DataProfileSchemaParser; use Magento\FunctionalTestingFramework\DataGenerator\Persist\CurlHandler; +use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; +use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException; use Magento\FunctionalTestingFramework\ObjectManager; use Magento\FunctionalTestingFramework\ObjectManagerFactory; use Magento\FunctionalTestingFramework\Util\MagentoTestCase; +use tests\unit\Util\TestLoggingUtil; /** * Class PersistedObjectHandlerTest */ class PersistedObjectHandlerTest extends MagentoTestCase { + /** + * Before test functionality + * @return void + */ + public function setUp() + { + TestLoggingUtil::getInstance()->setMockLoggingUtil(); + } + public function testCreateSimpleEntity() { // Test Data and Variables @@ -330,6 +342,115 @@ public function testRetrieveEntityAcrossScopes() $this->assertEquals($dataValueThree, $retrievedFromSuite); } + /** + * @param string $name + * @param string $key + * @param string $value + * @param string $type + * @param string $scope + * @param string $stepKey + * @dataProvider entityDataProvider + */ + public function testRetrieveEntityValidField($name, $key, $value, $type, $scope, $stepKey) + { + $parserOutputOne = [ + 'entity' => [ + $name => [ + 'type' => $type, + 'data' => [ + 0 => [ + 'key' => $key, + 'value' => $value + ] + ] + ] + ] + ]; + $jsonReponseOne = " + { + \"" . strtolower($key) . "\" : \"{$value}\" + } + "; + + // Mock Classes and Create Entities + $handler = PersistedObjectHandler::getInstance(); + + $this->mockDataHandlerWithOutput($parserOutputOne); + $this->mockCurlHandler($jsonReponseOne); + $handler->createEntity($stepKey, $scope, $name); + + // Call method + $retrieved = $handler->retrieveEntityField($stepKey, $key, $scope); + + $this->assertEquals($value, $retrieved); + } + + /** + * @param string $name + * @param string $key + * @param string $value + * @param string $type + * @param string $scope + * @param string $stepKey + * @dataProvider entityDataProvider + * @throws TestReferenceException + * @throws TestFrameworkException + */ + public function testRetrieveEntityInValidField($name, $key, $value, $type, $scope, $stepKey) + { + $invalidDataKey = "invalidDataKey"; + $warnMsg = "Undefined field {$invalidDataKey} in entity object with a stepKey of {$stepKey}\n"; + $warnMsg .= "Please fix the invalid reference. This will result in fatal error in next major release."; + + $parserOutputOne = [ + 'entity' => [ + $name => [ + 'type' => $type, + 'data' => [ + 0 => [ + 'key' => $key, + 'value' => $value + ] + ] + ] + ] + ]; + $jsonReponseOne = " + { + \"" . strtolower($key) . "\" : \"{$value}\" + } + "; + + // Mock Classes and Create Entities + $handler = PersistedObjectHandler::getInstance(); + + $this->mockDataHandlerWithOutput($parserOutputOne); + $this->mockCurlHandler($jsonReponseOne); + $handler->createEntity($stepKey, $scope, $name); + + // Call method + $handler->retrieveEntityField($stepKey, $invalidDataKey, $scope); + + // validate log statement + TestLoggingUtil::getInstance()->validateMockLogStatement( + 'warning', + $warnMsg, + [] + ); + } + + /** + * Data provider for testRetrieveEntityField + */ + public static function entityDataProvider() + { + return [ + ['Entity1', 'testKey1', 'testValue1', 'testType', PersistedObjectHandler::HOOK_SCOPE, 'StepKey1'], + ['Entity2', 'testKey2', 'testValue2', 'testType', PersistedObjectHandler::SUITE_SCOPE, 'StepKey2'], + ['Entity3', 'testKey3', 'testValue3', 'testType', PersistedObjectHandler::TEST_SCOPE, 'StepKey3'] + ]; + } + /** * Mocks DataObjectHandler to use given output to create * @param $parserOutput @@ -374,4 +495,14 @@ public function tearDown() parent::tearDown(); // TODO: Change the autogenerated stub } + + /** + * After class functionality + * @return void + */ + public static function tearDownAfterClass() + { + TestLoggingUtil::getInstance()->clearMockLoggingUtil(); + parent::tearDownAfterClass(); + } } diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php index cc35bbc52..ea3aa919a 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php @@ -7,7 +7,10 @@ namespace Magento\FunctionalTestingFramework\DataGenerator\Handlers; use Magento\FunctionalTestingFramework\DataGenerator\Persist\DataPersistenceHandler; +use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException; +use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil; +use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; class PersistedObjectHandler { @@ -177,12 +180,20 @@ public function getEntity($key, $scope, $entity, $dependentObjectKeys = [], $sto * @param string $field * @param string $scope * @return string - * @throws TestReferenceException - * @throws \Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException */ public function retrieveEntityField($stepKey, $field, $scope) { - return $this->retrieveEntity($stepKey, $scope)->getCreatedDataByName($field); + $fieldValue = $this->retrieveEntity($stepKey, $scope)->getCreatedDataByName($field); + if ($fieldValue === null) { + $warnMsg = "Undefined field {$field} in entity object with a stepKey of {$stepKey}\n"; + $warnMsg .= "Please fix the invalid reference. This will result in fatal error in next major release."; + //TODO: change this to throw an exception in next major release + LoggingUtil::getInstance()->getLogger(PersistedObjectHandler::class)->warn($warnMsg); + if (MftfApplicationConfig::getConfig()->getPhase() !== MftfApplicationConfig::UNIT_TEST_PHASE) { + print("\n$warnMsg\n"); + } + } + return $fieldValue; } /**