Skip to content

Commit 803c744

Browse files
committed
MQE-307: Added ability to persist same entity data multiple times.
1 parent bc1c77a commit 803c744

File tree

7 files changed

+244
-81
lines changed

7 files changed

+244
-81
lines changed

src/Magento/AcceptanceTestFramework/DataGenerator/Api/ApiExecutor.php

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,34 @@ class ApiExecutor
2929

3030
/**
3131
* The entity object data being created, updated, or deleted.
32+
*
3233
* @var EntityDataObject $entityObject
3334
*/
3435
private $entityObject;
3536

3637
/**
3738
* The json definitions used to map the operation.
39+
*
3840
* @var JsonDefinition $jsonDefinition
3941
*/
4042
private $jsonDefinition;
4143

4244
/**
4345
* The array of dependentEntities this class can be given. When finding linked entities, APIExecutor
4446
* uses this repository before looking for static data.
47+
*
4548
* @var array
4649
*/
4750
private $dependentEntities = [];
4851

52+
/**
53+
* The array of entity name and number of objects being created,
54+
* we don't need to track objects in update and delete operations.
55+
*
56+
* @var array
57+
*/
58+
private static $entitySequences = [];
59+
4960
/**
5061
* ApiSubObject constructor.
5162
* @param string $operation
@@ -82,7 +93,10 @@ public function executeRequest()
8293

8394
if (!empty($matchedParams)) {
8495
foreach ($matchedParams[0] as $paramKey => $paramValue) {
85-
$param = $this->entityObject->getDataByName($matchedParams[1][$paramKey]);
96+
$param = $this->entityObject->getDataByName(
97+
$matchedParams[1][$paramKey],
98+
EntityDataObject::CEST_UNIQUE_VALUE
99+
);
86100
$apiClientUrl = str_replace($paramValue, $param, $apiClientUrl);
87101
}
88102
}
@@ -139,6 +153,7 @@ private function getAuthorizationHeader($authUrl)
139153
private function convertJsonArray($entityObject, $jsonArrayMetadata)
140154
{
141155
$jsonArray = [];
156+
self::incrementSequence($entityObject->getName());
142157

143158
foreach ($jsonArrayMetadata as $jsonElement) {
144159
if ($jsonElement->getType() == JsonObjectExtractor::JSON_OBJECT_OBJ_NAME) {
@@ -149,23 +164,21 @@ private function convertJsonArray($entityObject, $jsonArrayMetadata)
149164
$jsonElementType = $jsonElement->getValue();
150165

151166
if (in_array($jsonElementType, ApiExecutor::PRIMITIVE_TYPES)) {
152-
$elementData = $entityObject->getDataByName($jsonElement->getKey());
153-
$elementUniquenessData = $entityObject->getUniquenessDataByName($jsonElement->getKey());
154-
if ($elementUniquenessData) {
155-
if ($elementUniquenessData == 'prefix') {
156-
if (DataObjectHandler::UNIQUENESS_FUNCTION == 'msq') {
157-
$elementData = msq($entityObject->getName().'.' . $jsonElement->getKey()).$elementData;
158-
} elseif (DataObjectHandler::UNIQUENESS_FUNCTION == 'msqs') {
159-
$elementData = msqs($entityObject->getName().'.' . $jsonElement->getKey()).$elementData;
160-
}
161-
} elseif ($elementUniquenessData == 'suffix') {
162-
if (DataObjectHandler::UNIQUENESS_FUNCTION == 'msq') {
163-
$elementData .= msq($entityObject->getName() . '.'. $jsonElement->getKey());
164-
} elseif (DataObjectHandler::UNIQUENESS_FUNCTION == 'msqs') {
165-
$elementData .= msqs($entityObject->getName() . '.'. $jsonElement->getKey());
166-
}
167+
$elementData = $entityObject->getDataByName(
168+
$jsonElement->getKey(),
169+
EntityDataObject::CEST_UNIQUE_VALUE
170+
);
171+
172+
if (array_key_exists($jsonElement->getKey(), $entityObject->getUniquenessData())) {
173+
$uniqueData = $entityObject->getUniquenessDataByName($jsonElement->getKey());
174+
if ($uniqueData === 'suffix') {
175+
$elementData .= (string)self::getSequence($entityObject->getName());
176+
} else {
177+
$elementData = (string)self::getSequence($entityObject->getName())
178+
. $elementData;
167179
}
168180
}
181+
169182
$jsonArray[$jsonElement->getKey()] = $this->castValue($jsonElementType, $elementData);
170183
} else {
171184
$entityNamesOfType = $entityObject->getLinkedEntitiesOfType($jsonElementType);
@@ -242,6 +255,35 @@ public function getEncodedJsonString()
242255
return json_encode($jsonMetadataArray, JSON_PRETTY_PRINT);
243256
}
244257

258+
/**
259+
* Increment an entity's sequence number by 1.
260+
*
261+
* @param string $entityName
262+
* @return void
263+
*/
264+
private static function incrementSequence($entityName)
265+
{
266+
if (array_key_exists($entityName, self::$entitySequences)) {
267+
self::$entitySequences[$entityName]++;
268+
} else {
269+
self::$entitySequences[$entityName] = 1;
270+
}
271+
}
272+
273+
/**
274+
* Get the current sequence number for an entity.
275+
*
276+
* @param string $entityName
277+
* @return int
278+
*/
279+
private static function getSequence($entityName)
280+
{
281+
if (array_key_exists($entityName, self::$entitySequences)) {
282+
return self::$entitySequences[$entityName];
283+
}
284+
return 0;
285+
}
286+
245287
// @codingStandardsIgnoreStart
246288
/**
247289
* This function takes a string value and its corresponding type and returns the string cast

src/Magento/AcceptanceTestFramework/DataGenerator/Api/EntityApiHandler.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66

77
namespace Magento\AcceptanceTestFramework\DataGenerator\Api;
88

9-
use Codeception\Test\Cest;
109
use Magento\AcceptanceTestFramework\Config\Data;
1110
use Magento\AcceptanceTestFramework\DataGenerator\Handlers\DataObjectHandler;
1211
use Magento\AcceptanceTestFramework\DataGenerator\Objects\EntityDataObject;
1312
use Magento\AcceptanceTestFramework\Test\Handlers\CestObjectHandler;
14-
use Magento\AcceptanceTestFramework\Test\Managers\CestArrayProcessor;
1513
use Magento\AcceptanceTestFramework\Util\TestGenerator;
1614

1715
class EntityApiHandler
@@ -57,10 +55,11 @@ public function createEntity()
5755
$result = $apiExecutor->executeRequest();
5856

5957
$this->createdObject = new EntityDataObject(
60-
'__created' . $this->entityObject->getName(),
58+
$this->entityObject->getName(),
6159
$this->entityObject->getType(),
6260
json_decode($result, true),
63-
null
61+
null,
62+
null // No uniqueness data is needed to be further processed.
6463
);
6564
}
6665

@@ -93,7 +92,11 @@ public function getCreatedObject()
9392
*/
9493
public function getCreatedDataByName($dataName)
9594
{
96-
return $this->createdObject->getDataByName($dataName);
95+
$data = $this->createdObject->getDataByName($dataName, EntityDataObject::NO_UNIQUE_PROCESS);
96+
if (empty($data)) {
97+
$data = $this->entityObject->getDataByName($dataName, EntityDataObject::CEST_UNIQUE_VALUE);
98+
}
99+
return $data;
97100
}
98101

99102
// TODO add update function

src/Magento/AcceptanceTestFramework/DataGenerator/Handlers/DataObjectHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class DataObjectHandler implements ObjectHandlerInterface
3737
const DATA_ELEMENT_UNIQUENESS_ATTR = 'unique';
3838
const DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_PREFIX = 'prefix';
3939
const DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_SUFFIX = 'suffix';
40-
const UNIQUENESS_FUNCTION = 'msq';
4140

4241
const ARRAY_VALUES = 'array';
4342
const ARRAY_ELEMENT_KEY = 'key';
@@ -130,6 +129,7 @@ private function parseEnvVariables()
130129
self::ENV_DATA_OBJECT_NAME,
131130
'environment',
132131
$envData,
132+
null,
133133
null
134134
);
135135
$this->data[$envDataObject->getName()] = $envDataObject;

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

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\AcceptanceTestFramework\DataGenerator\Objects;
88

9+
use Magento\AcceptanceTestFramework\Exceptions\TestFrameworkException;
10+
911
/**
1012
* Class EntityDataObject
1113
*/
@@ -39,6 +41,14 @@ class EntityDataObject
3941
*/
4042
private $data = [];
4143

44+
const NO_UNIQUE_PROCESS = 0;
45+
const SUITE_UNIQUE_VALUE = 1;
46+
const CEST_UNIQUE_VALUE = 2;
47+
const SUITE_UNIQUE_NOTATION = 3;
48+
const CEST_UNIQUE_NOTATION = 4;
49+
const SUITE_UNIQUE_FUNCTION = 'msqs';
50+
const CEST_UNIQUE_FUNCTION = 'msq';
51+
4252
/**
4353
* Array of data name and its uniqueness attribute value.
4454
*
@@ -54,7 +64,7 @@ class EntityDataObject
5464
* @param array $linkedEntities
5565
* @param array $uniquenessData
5666
*/
57-
public function __construct($entityName, $entityType, $data, $linkedEntities, $uniquenessData = null)
67+
public function __construct($entityName, $entityType, $data, $linkedEntities, $uniquenessData)
5868
{
5969
$this->name = $entityName;
6070
$this->type = $entityType;
@@ -108,14 +118,72 @@ public function getData()
108118
* This function retrieves data from an entity defined in xml.
109119
*
110120
* @param string $dataName
121+
* @param int $uniDataFormat
111122
* @return string|null
123+
* @throws TestFrameworkException
112124
*/
113-
public function getDataByName($dataName)
125+
public function getDataByName($dataName, $uniDataFormat)
114126
{
127+
if (!$this->isValidUniqueDataFormat($uniDataFormat)) {
128+
throw new TestFrameworkException(
129+
sprintf('Invalid unique data format value: %s \n', $uniDataFormat)
130+
);
131+
}
132+
115133
$name = strtolower($dataName);
116134

117-
if ($this->data != null && array_key_exists($name, $this->data)) {
118-
return $this->data[$name];
135+
if ($this->data !== null && array_key_exists($name, $this->data)) {
136+
$uniData = $this->getUniquenessDataByName($dataName);
137+
if (null === $uniData || $uniDataFormat == self::NO_UNIQUE_PROCESS) {
138+
return $this->data[$name];
139+
}
140+
141+
switch ($uniDataFormat) {
142+
case self::SUITE_UNIQUE_VALUE:
143+
if (!function_exists(self::SUITE_UNIQUE_FUNCTION)) {
144+
throw new TestFrameworkException(
145+
sprintf(
146+
'Unique data format value: %s can only be used when running cests.\n',
147+
$uniDataFormat
148+
)
149+
);
150+
} elseif ($uniData == 'prefix') {
151+
return msqs($this->getName()) . $this->data[$name];
152+
} else { // $uniData == 'suffix'
153+
return $this->data[$name] . msqs($this->getName());
154+
}
155+
break;
156+
case self::CEST_UNIQUE_VALUE:
157+
if (!function_exists(self::CEST_UNIQUE_FUNCTION)) {
158+
throw new TestFrameworkException(
159+
sprintf(
160+
'Unique data format value: %s can only be used when running cests.\n',
161+
$uniDataFormat
162+
)
163+
);
164+
} elseif ($uniData == 'prefix') {
165+
return msq($this->getName()) . $this->data[$name];
166+
} else { // $uniData == 'suffix'
167+
return $this->data[$name] . msq($this->getName());
168+
}
169+
break;
170+
case self::SUITE_UNIQUE_NOTATION:
171+
if ($uniData == 'prefix') {
172+
return self::SUITE_UNIQUE_FUNCTION . '("' . $this->getName() . '")' . $this->data[$name];
173+
} else { // $uniData == 'suffix'
174+
return $this->data[$name] . self::SUITE_UNIQUE_FUNCTION . '("' . $this->getName() . '")';
175+
}
176+
break;
177+
case self::CEST_UNIQUE_NOTATION:
178+
if ($uniData == 'prefix') {
179+
return self::CEST_UNIQUE_FUNCTION . '("' . $this->getName() . '")' . $this->data[$name];
180+
} else { // $uniData == 'suffix'
181+
return $this->data[$name] . self::CEST_UNIQUE_FUNCTION . '("' . $this->getName() . '")';
182+
}
183+
break;
184+
default:
185+
break;
186+
}
119187
}
120188

121189
return null;
@@ -157,4 +225,35 @@ public function getUniquenessDataByName($dataName)
157225

158226
return null;
159227
}
228+
229+
/**
230+
* This function retrieves uniqueness data.
231+
*
232+
* @return array|null
233+
*/
234+
public function getUniquenessData()
235+
{
236+
return $this->uniquenessData;
237+
}
238+
239+
/**
240+
* Validate if input value is a valid unique data format.
241+
*
242+
* @param int $uniDataFormat
243+
* @return bool
244+
*/
245+
private function isValidUniqueDataFormat($uniDataFormat)
246+
{
247+
return in_array(
248+
$uniDataFormat,
249+
[
250+
self::NO_UNIQUE_PROCESS,
251+
self::SUITE_UNIQUE_VALUE,
252+
self::CEST_UNIQUE_VALUE,
253+
self::SUITE_UNIQUE_NOTATION,
254+
self::CEST_UNIQUE_NOTATION
255+
],
256+
true
257+
);
258+
}
160259
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\AcceptanceTestFramework\Exceptions;
8+
9+
/**
10+
* Class TestFrameworkException
11+
*/
12+
class TestFrameworkException extends \Exception
13+
{
14+
/**
15+
* TestFrameworkException constructor.
16+
* @param string $message
17+
*/
18+
public function __construct($message)
19+
{
20+
parent::__construct($message);
21+
}
22+
}

0 commit comments

Comments
 (0)