Skip to content

MQE-685: References to ENV file must be done at test run-time #35

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 10 commits into from
Feb 8, 2018
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
3 changes: 2 additions & 1 deletion dev/tests/verification/Resources/PageReplacementTest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class PageReplacementTestCest
$I->amOnPage("/John/StringLiteral2.html");
$I->amOnPage("/John/" . $datakey->getCreatedDataByName('firstname') . ".html");
$I->amOnPage("/" . $datakey->getCreatedDataByName('firstname') . "/StringLiteral2.html");
$I->amOnPage("/admin/backend");
$I->amOnPage("/" . getenv("MAGENTO_BACKEND_NAME") . "//backend");
$I->amOnPage("/" . getenv("MAGENTO_BACKEND_NAME") . "//StringLiteral/page.html");
$I->amOnUrl("http://myFullUrl.com/");
}
}
3 changes: 3 additions & 0 deletions dev/tests/verification/TestModule/Page/SamplePage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<page name="AdminPage" url="/backend" area="admin" module="SampleTests">
<section name="SampleSection"/>
</page>
<page name="AdminOneParamPage" url="/{{var1}}/page.html" area="admin" module="SampleTests" parameterized="true">
<section name="SampleSection"/>
</page>
<page name="ExternalPage" url="http://myFullUrl.com/" area="external" module="SampleTests">
<section name="SampleSection"/>
</page>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<amOnPage stepKey="twoParamPageDataPersist" url="{{TwoParamPage.url(simpleData.firstname, $datakey.firstname$)}}"/>
<amOnPage stepKey="twoParamPagePersistString" url="{{TwoParamPage.url($datakey.firstname$, 'StringLiteral2')}}"/>
<amOnPage stepKey="onAdminPage" url="{{AdminPage.url}}"/>
<amOnPage stepKey="oneParamAdminPageString" url="{{AdminOneParamPage.url('StringLiteral')}}"/>
<amOnUrl stepKey="onExternalPage" url="{{ExternalPage.url}}"/>
</test>
<test name="ExternalPageTestBadReference">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

class DataObjectHandler implements ObjectHandlerInterface
{
const __ENV = '_ENV';
const _ENTITY = 'entity';
const _NAME = 'name';
const _TYPE = 'type';
Expand Down Expand Up @@ -56,7 +55,6 @@ private function __construct()
return;
}
$this->entityDataObjects = $this->processParserOutput($parserOutput);
$this->entityDataObjects[self::__ENV] = $this->processEnvFile();
}

/**
Expand Down Expand Up @@ -100,37 +98,6 @@ public function getAllObjects()
return $this->entityDataObjects;
}

/**
* Convert the contents of the .env file into a single EntityDataObject so that the values can be accessed like
* normal data.
*
* @return EntityDataObject|null
*/
private function processEnvFile()
{
// These constants are defined in the bootstrap file
$path = PROJECT_ROOT . DIRECTORY_SEPARATOR . '.env';

if (file_exists($path)) {
$vars = [];
$lines = file($path);

foreach ($lines as $line) {
$parts = explode("=", $line);
if (count($parts) != 2) {
continue;
}
$key = strtolower(trim($parts[0]));
$value = trim($parts[1]);
$vars[$key] = $value;
}

return new EntityDataObject(self::__ENV, 'environment', $vars, null, null);
}

return null;
}

/**
* Convert the parser output into a collection of EntityDataObjects
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ public function getName()
*/
public function getUrl()
{
if ($this->getArea() == self::ADMIN_AREA) {
$url = ltrim($this->url, '/');
return "/" . getenv('MAGENTO_BACKEND_NAME') . "/{$url}";
}

return $this->url;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
class ActionObject
{
const __ENV = "_ENV";
const DATA_ENABLED_ATTRIBUTES = ["userInput", "parameterArray", "expected", "actual"];
const SELECTOR_ENABLED_ATTRIBUTES = ['selector', 'dependentSelector', "selector1", "selector2", "function"];
const OLD_ASSERTION_ATTRIBUTES = ["expected", "expectedType", "actual", "actualType"];
Expand Down Expand Up @@ -377,6 +378,11 @@ private function findAndReplaceReferences($objectHandler, $inputString)

$obj = $objectHandler->getObject($objName);

// Leave {{_ENV.VARIABLE}} references to be replaced in TestGenerator with getenv("VARIABLE")
if ($objName === ActionObject::__ENV) {
continue;
}

// specify behavior depending on field
switch (get_class($obj)) {
case PageObject::class:
Expand All @@ -398,13 +404,16 @@ private function findAndReplaceReferences($objectHandler, $inputString)
break;
}

if ($replacement == null && get_class($objectHandler) != DataObjectHandler::class) {
return $this->findAndReplaceReferences(DataObjectHandler::getInstance(), $outputString);
} elseif ($replacement == null) {
throw new TestReferenceException("Could not resolve entity reference " . $inputString);
if ($replacement == null) {
if (get_class($objectHandler) != DataObjectHandler::class) {
return $this->findAndReplaceReferences(DataObjectHandler::getInstance(), $outputString);
} else {
throw new TestReferenceException("Could not resolve entity reference " . $inputString);
}
}

$replacement = $this->resolveParameterization($parameterized, $replacement, $match);
$replacement = $this->resolveParameterization($parameterized, $replacement, $match, $obj);

$outputString = str_replace($match, $replacement, $outputString);
}
return $outputString;
Expand Down Expand Up @@ -468,16 +477,21 @@ private function resolveEntityDataObjectReference($obj, $match)
* @param boolean $isParameterized
* @param string $replacement
* @param string $match
* @param object $object
* @return string
*/
private function resolveParameterization($isParameterized, $replacement, $match)
private function resolveParameterization($isParameterized, $replacement, $match, $object)
{
if ($isParameterized) {
$parameterList = $this->stripAndReturnParameters($match);
return $this->matchParameterReferences($replacement, $parameterList);
$resolvedReplacement = $this->matchParameterReferences($replacement, $parameterList);
} else {
return $replacement;
$resolvedReplacement = $replacement;
}
if (get_class($object) == PageObject::class && $object->getArea() == PageObject::ADMIN_AREA) {
$resolvedReplacement = "/{{_ENV.MAGENTO_BACKEND_NAME}}/" . $resolvedReplacement;
}
return $resolvedReplacement;
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/Magento/FunctionalTestingFramework/Util/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,8 @@ private function wrapFunctionCall($actor, $action, ...$args)
}
$output .= ");\n";

$output = $this->resolveEnvReferences($output, $args);

return $this->resolveTestVariable($output, $args);
}

Expand Down Expand Up @@ -1530,10 +1532,40 @@ private function wrapFunctionCallWithReturnValue($returnVariable, $actor, $actio
}
$output .= ");\n";

$output = $this->resolveEnvReferences($output, $args);

return $this->resolveTestVariable($output, $args);
}
// @codingStandardsIgnoreEnd

/**
* Resolves {{_ENV.variable}} into getenv("variable") for test-runtime ENV referencing.
* @param string $inputString
* @param array $args
* @return string
*/
private function resolveEnvReferences($inputString, $args)
{
$envRegex = "/{{_ENV\.([\w]+)}}/";

$outputString = $inputString;

foreach ($args as $arg) {
preg_match_all($envRegex, $arg, $matches);
if (!empty($matches[0])) {
$fullMatch = $matches[0][0];
$envVariable = $matches[1][0];
unset($matches);
$replacement = "getenv(\"{$envVariable}\")";

$outputArg = $this->processQuoteBreaks($fullMatch, $arg, $replacement);
$outputString = str_replace($arg, $outputArg, $outputString);
}
}

return $outputString;
}

/**
* Validates parameter array format, making sure user has enclosed string with square brackets.
*
Expand Down