Skip to content

MQE-588: CreatedData should throw Error/Warning when undefined data i… #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}

/**
Expand Down