Skip to content

Commit 3a9b868

Browse files
committed
MQE-1561: CreateData action must be able to parse Data entity which references another data
1 parent 85c14a0 commit 3a9b868

File tree

5 files changed

+231
-5
lines changed

5 files changed

+231
-5
lines changed

src/Magento/FunctionalTestingFramework/DataGenerator/Objects/EntityDataObject.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\FunctionalTestingFramework\DataGenerator\Objects;
88

99
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
10+
use Magento\FunctionalTestingFramework\DataGenerator\Util\GenerationDataReferenceResolver;
1011
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
1112
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
1213

@@ -81,14 +82,14 @@ class EntityDataObject
8182
/**
8283
* Constructor
8384
*
84-
* @param string $name
85-
* @param string $type
85+
* @param string $name
86+
* @param string $type
8687
* @param string[] $data
8788
* @param string[] $linkedEntities
8889
* @param string[] $uniquenessData
8990
* @param string[] $vars
90-
* @param string $parentEntity
91-
* @param string $filename
91+
* @param string $parentEntity
92+
* @param string $filename
9293
*/
9394
public function __construct(
9495
$name,
@@ -181,13 +182,30 @@ public function getDataByName($name, $uniquenessFormat)
181182
return null;
182183
}
183184

185+
$dataReferenceResolver = new GenerationDataReferenceResolver();
184186
if (array_key_exists($name_lower, $this->data)) {
185-
$uniquenessData = $this->getUniquenessDataByName($name_lower);
187+
$uniquenessData = $this->getUniquenessDataByName($name_lower) === null
188+
? $dataReferenceResolver->getDataUniqueness(
189+
$this->data[$name_lower],
190+
$this->name . '.' . $name
191+
)
192+
: $this->getUniquenessDataByName($name_lower);
193+
if ($uniquenessData !== null) {
194+
$this->uniquenessData[$name] = $uniquenessData;
195+
}
196+
$this->data[$name_lower] = $dataReferenceResolver->getDataReference(
197+
$this->data[$name_lower],
198+
$this->name . '.' . $name
199+
);
186200
if (null === $uniquenessData || $uniquenessFormat == self::NO_UNIQUE_PROCESS) {
187201
return $this->data[$name_lower];
188202
}
189203
return $this->formatUniqueData($name_lower, $uniquenessData, $uniquenessFormat);
190204
} elseif (array_key_exists($name, $this->data)) {
205+
$this->data[$name] = $dataReferenceResolver->getDataReference(
206+
$this->data[$name],
207+
$this->name . '.' . $name
208+
);
191209
// Data returned by the API may be camelCase so we need to check for the original $name also.
192210
return $this->data[$name];
193211
} else {

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
1212
use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationElement;
1313
use Magento\FunctionalTestingFramework\DataGenerator\Util\OperationElementExtractor;
14+
use Magento\FunctionalTestingFramework\DataGenerator\Util\RuntimeDataReferenceResolver;
1415
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
1516

1617
class OperationDataArrayResolver
@@ -121,6 +122,17 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op
121122
}
122123
}
123124

125+
$dataReferenceResolver = new RuntimeDataReferenceResolver();
126+
foreach ($operationDataArray as $key => $operationDataValue) {
127+
if (is_array($operationDataValue)){
128+
continue;
129+
}
130+
$operationDataArray[$key] = $dataReferenceResolver->getDataReference(
131+
$operationDataValue,
132+
$entityObject->getName()
133+
);
134+
}
135+
124136
return $operationDataArray;
125137
}
126138

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\DataGenerator\Util;
8+
9+
interface DataReferenceResolverInterface
10+
{
11+
const REFERENCE_REGEX_PATTERN = "/(?<reference>{{[\w]+\.[\w\[\]]+}})/";
12+
13+
/**
14+
* @param string $data
15+
* @param string $originalDataEntity
16+
* @return mixed
17+
*/
18+
public function getDataReference(string $data, string $originalDataEntity);
19+
20+
/**
21+
* @param string $data
22+
* @param string $originalDataEntity
23+
* @return mixed
24+
*/
25+
public function getDataUniqueness(string $data, string $originalDataEntity);
26+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\DataGenerator\Util;
8+
9+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
10+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
11+
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
12+
13+
class GenerationDataReferenceResolver implements DataReferenceResolverInterface
14+
{
15+
/**
16+
* @param string $data
17+
* @param string $originalDataEntity
18+
* @return string|null
19+
* @throws TestReferenceException
20+
*/
21+
public function getDataReference(string $data, string $originalDataEntity)
22+
{
23+
$result = null;
24+
preg_match(self::REFERENCE_REGEX_PATTERN, $data, $matches);
25+
26+
if (empty($matches['reference'])) {
27+
return $data;
28+
}
29+
30+
$strippedReference = str_replace(['{{', '}}'], '', $matches['reference']);
31+
list($entity, $var) = explode('.', $strippedReference);
32+
switch ($entity) {
33+
case ActionObject::__ENV:
34+
case ActionObject::__CREDS:
35+
$result = $data;
36+
break;
37+
default:
38+
$entityObject = DataObjectHandler::getInstance()->getObject($entity);
39+
if ($entityObject === null) {
40+
throw new TestReferenceException(
41+
"Could not resolve entity reference \"{$matches['reference']}\" "
42+
. "in Data entity \"{$originalDataEntity}\""
43+
);
44+
}
45+
$entityData = $entityObject->getAllData();
46+
$result = $entityData[$var];
47+
}
48+
49+
return $result;
50+
}
51+
52+
/**
53+
* @param string $data
54+
* @param string $originalDataEntity
55+
* @return string|null
56+
* @throws TestReferenceException
57+
*/
58+
public function getDataUniqueness(string $data, string $originalDataEntity)
59+
{
60+
preg_match(ActionObject::ACTION_ATTRIBUTE_VARIABLE_REGEX_PATTERN,
61+
$data,
62+
$matches
63+
);
64+
65+
if (empty($matches['reference'])) {
66+
return null;
67+
}
68+
69+
$strippedReference = str_replace(['{{', '}}'], '', $matches['reference']);
70+
list($entity, $var) = explode('.', $strippedReference);
71+
$entityObject = DataObjectHandler::getInstance()->getObject($entity);
72+
if ($entityObject === null) {
73+
throw new TestReferenceException(
74+
"Could not resolve entity reference \"{$matches['reference']}\" "
75+
. "in Data entity \"{$originalDataEntity}\""
76+
);
77+
78+
}
79+
80+
return $entityObject->getUniquenessDataByName($var);
81+
}
82+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\DataGenerator\Util;
8+
9+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\CredentialStore;
10+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
11+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
12+
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
13+
14+
class RuntimeDataReferenceResolver implements DataReferenceResolverInterface
15+
{
16+
/**
17+
* @param string $data
18+
* @param string $originalDataEntity
19+
* @return array|false|string|null
20+
* @throws TestReferenceException
21+
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException
22+
*/
23+
public function getDataReference(string $data, string $originalDataEntity)
24+
{
25+
$result = null;
26+
preg_match(self::REFERENCE_REGEX_PATTERN, $data, $matches);
27+
28+
if (empty($matches['reference'])) {
29+
return $data;
30+
}
31+
32+
$strippedReference = str_replace(['{{', '}}'], '', $matches['reference']);
33+
list($entity, $var) = explode('.', $strippedReference);
34+
switch ($entity) {
35+
case ActionObject::__ENV:
36+
$result = str_replace($matches['reference'], getenv($var), $data);
37+
break;
38+
case ActionObject::__CREDS:
39+
$value = CredentialStore::getInstance()->getSecret($var);
40+
$result = CredentialStore::getInstance()->decryptSecretValue($value);
41+
$result = str_replace($matches['reference'], $result, $data);
42+
break;
43+
default:
44+
$entityObject = DataObjectHandler::getInstance()->getObject($entity);
45+
if ($entityObject === null) {
46+
throw new TestReferenceException(
47+
"Could not resolve entity reference \"{$matches['reference']}\" "
48+
. "in Data entity \"{$originalDataEntity}\""
49+
);
50+
}
51+
$entityData = $entityObject->getAllData();
52+
$result = $entityData[$var];
53+
}
54+
55+
return $result;
56+
}
57+
58+
/**
59+
* @param string $data
60+
* @param string $originalDataEntity
61+
* @return string|null
62+
* @throws TestReferenceException
63+
*/
64+
public function getDataUniqueness(string $data, string $originalDataEntity)
65+
{
66+
preg_match(ActionObject::ACTION_ATTRIBUTE_VARIABLE_REGEX_PATTERN,
67+
$data,
68+
$matches
69+
);
70+
71+
if (empty($matches['reference'])) {
72+
return null;
73+
}
74+
75+
$strippedReference = str_replace(['{{', '}}'], '', $matches['reference']);
76+
list($entity, $var) = explode('.', $strippedReference);
77+
$entityObject = DataObjectHandler::getInstance()->getObject($entity);
78+
if ($entityObject === null) {
79+
throw new TestReferenceException(
80+
"Could not resolve entity reference \"{$matches['reference']}\" "
81+
. "in Data entity \"{$originalDataEntity}\""
82+
);
83+
84+
}
85+
86+
return $entityObject->getUniquenessDataByName($var);
87+
}
88+
}

0 commit comments

Comments
 (0)