From 4d46f9b14642962ac29b8f818ebd7ab72f8360f1 Mon Sep 17 00:00:00 2001 From: Ji Lu Date: Tue, 27 Aug 2019 15:34:49 -0500 Subject: [PATCH 1/4] MQE-588: CreatedData should throw Error/Warning when undefined data is returned --- .../Handlers/PersistedObjectHandlerTest.php | 104 ++++++++++++++++++ .../Handlers/PersistedObjectHandler.php | 9 +- 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php index 30dc3ec40..fd04ee5fe 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php @@ -11,6 +11,8 @@ 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; @@ -330,6 +332,108 @@ 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"; + + $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); + + $this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestReferenceException::class); + + // Call method + $handler->retrieveEntityField($stepKey, $invalidDataKey, $scope); + } + + /** + * 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 diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php index cc35bbc52..55dd599f1 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php @@ -7,6 +7,7 @@ namespace Magento\FunctionalTestingFramework\DataGenerator\Handlers; use Magento\FunctionalTestingFramework\DataGenerator\Persist\DataPersistenceHandler; +use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException; class PersistedObjectHandler @@ -178,11 +179,15 @@ public function getEntity($key, $scope, $entity, $dependentObjectKeys = [], $sto * @param string $scope * @return string * @throws TestReferenceException - * @throws \Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException + * @throws TestFrameworkException */ public function retrieveEntityField($stepKey, $field, $scope) { - return $this->retrieveEntity($stepKey, $scope)->getCreatedDataByName($field); + $fieldValue = $this->retrieveEntity($stepKey, $scope)->getCreatedDataByName($field); + if ($fieldValue === null) { + throw new TestReferenceException("Undefined field {$field} in entity object with a stepKey of {$stepKey}"); + } + return $fieldValue; } /** From d4a1237b032e3669ee8c4e7e1e1f3744a1978acd Mon Sep 17 00:00:00 2001 From: Ji Lu Date: Fri, 30 Aug 2019 11:05:36 -0500 Subject: [PATCH 2/4] MQE-588: CreatedData should throw Error/Warning when undefined data is returned - changed to print warning message for backward compatibility --- .../Handlers/PersistedObjectHandlerTest.php | 31 +++++++++++++++++-- .../Handlers/PersistedObjectHandler.php | 9 ++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php index fd04ee5fe..c79ad322a 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php @@ -16,12 +16,22 @@ 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 @@ -389,6 +399,8 @@ public function testRetrieveEntityValidField($name, $key, $value, $type, $scope, 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' => [ @@ -416,10 +428,15 @@ public function testRetrieveEntityInValidField($name, $key, $value, $type, $scop $this->mockCurlHandler($jsonReponseOne); $handler->createEntity($stepKey, $scope, $name); - $this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestReferenceException::class); - // Call method $handler->retrieveEntityField($stepKey, $invalidDataKey, $scope); + + // validate log statement + TestLoggingUtil::getInstance()->validateMockLogStatement( + "warning", + $warnMsg, + [] + ); } /** @@ -478,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 55dd599f1..3845e52e9 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php @@ -9,6 +9,7 @@ use Magento\FunctionalTestingFramework\DataGenerator\Persist\DataPersistenceHandler; use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException; +use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil; class PersistedObjectHandler { @@ -178,14 +179,16 @@ public function getEntity($key, $scope, $entity, $dependentObjectKeys = [], $sto * @param string $field * @param string $scope * @return string - * @throws TestReferenceException - * @throws TestFrameworkException */ public function retrieveEntityField($stepKey, $field, $scope) { $fieldValue = $this->retrieveEntity($stepKey, $scope)->getCreatedDataByName($field); if ($fieldValue === null) { - throw new TestReferenceException("Undefined field {$field} in entity object with a stepKey of {$stepKey}"); + $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); + print($warnMsg . PHP_EOL); } return $fieldValue; } From a344d34128305238b5ad2ac51842728e0aac60b8 Mon Sep 17 00:00:00 2001 From: Ji Lu Date: Wed, 4 Sep 2019 10:30:05 -0500 Subject: [PATCH 3/4] MQE-588: CreatedData should throw Error/Warning when undefined data is returned - prevent printing in unit phase --- .../DataGenerator/Handlers/PersistedObjectHandler.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php index 3845e52e9..ea3aa919a 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/PersistedObjectHandler.php @@ -10,6 +10,7 @@ use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException; use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil; +use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; class PersistedObjectHandler { @@ -188,7 +189,9 @@ public function retrieveEntityField($stepKey, $field, $scope) $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); - print($warnMsg . PHP_EOL); + if (MftfApplicationConfig::getConfig()->getPhase() !== MftfApplicationConfig::UNIT_TEST_PHASE) { + print("\n$warnMsg\n"); + } } return $fieldValue; } From 25c57dcbfbce1a9e90cd2521d5be45f7f3240ff3 Mon Sep 17 00:00:00 2001 From: Ji Lu Date: Wed, 4 Sep 2019 16:27:15 -0500 Subject: [PATCH 4/4] MQE-588: CreatedData should throw Error/Warning when undefined data is returned - prevent printing in unit phase --- .../DataGenerator/Handlers/PersistedObjectHandlerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php index c79ad322a..6c3823466 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/PersistedObjectHandlerTest.php @@ -433,7 +433,7 @@ public function testRetrieveEntityInValidField($name, $key, $value, $type, $scop // validate log statement TestLoggingUtil::getInstance()->validateMockLogStatement( - "warning", + 'warning', $warnMsg, [] );