Skip to content

Commit 303f305

Browse files
committed
MQE-295:[Data Input] Nested persistence definition support
- change to reference new metadata format - update to JsonElement object to reference metadata def within declaration
1 parent 9986002 commit 303f305

File tree

7 files changed

+332
-69
lines changed

7 files changed

+332
-69
lines changed

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
<item name="/config/operation" xsi:type="string">name</item>
201201
<item name="/config/operation/entry" xsi:type="string">key</item>
202202
<item name="/config/operation/array" xsi:type="string">key</item>
203+
<item name="/config/operation/object" xsi:type="string">key</item>
203204
</argument>
204205
<argument name="fileName" xsi:type="string">*-meta.xml</argument>
205206
<argument name="defaultScope" xsi:type="string">Metadata</argument>

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

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@
99
use Magento\AcceptanceTestFramework\DataGenerator\Handlers\JsonDefinitionObjectHandler;
1010
use Magento\AcceptanceTestFramework\DataGenerator\Objects\EntityDataObject;
1111
use Magento\AcceptanceTestFramework\DataGenerator\Objects\JsonDefinition;
12+
use Magento\AcceptanceTestFramework\DataGenerator\Objects\JsonElement;
13+
use Magento\AcceptanceTestFramework\DataGenerator\Util\JsonObjectExtractor;
1214
use Magento\AcceptanceTestFramework\Util\ApiClientUtil;
1315

16+
/**
17+
* Class ApiExecutor
18+
*/
1419
class ApiExecutor
1520
{
21+
const PRIMITIVE_TYPES = ['string', 'boolean', 'integer', 'double', 'array'];
22+
1623
/**
1724
* Describes the operation for the executor ('create','update','delete')
1825
*
@@ -35,11 +42,9 @@ class ApiExecutor
3542
/**
3643
* The array of dependentEntities this class can be given. When finding linked entities, APIExecutor
3744
* uses this repository before looking for static data.
38-
* @var null
45+
* @var array
3946
*/
40-
private $dependentEntities;
41-
42-
const PRIMITIVE_TYPES = ['string', 'boolean', 'integer', 'double', 'array'];
47+
private $dependentEntities = [];
4348

4449
/**
4550
* ApiSubObject constructor.
@@ -128,20 +133,19 @@ private function getAuthorizationHeader($authUrl)
128133
* recursively forming an array which represents the json structure for the api of the desired type.
129134
*
130135
* @param EntityDataObject $entityObject
131-
* @param array $jsonDefMetadata
132-
*
136+
* @param array $jsonArrayMetadata
133137
* @return array
134138
*/
135-
private function getJsonDataArray($entityObject, $jsonDefMetadata = null)
139+
private function convertJsonArray($entityObject, $jsonArrayMetadata)
136140
{
137-
$jsonArrayMetadata = !$jsonDefMetadata ? JsonDefinitionObjectHandler::getInstance()->getJsonDefinition(
138-
$this->operation,
139-
$entityObject->getType()
140-
)->getJsonMetadata() : $jsonDefMetadata;
141-
142141
$jsonArray = [];
143142

144143
foreach ($jsonArrayMetadata as $jsonElement) {
144+
if ($jsonElement->getType() == JsonObjectExtractor::JSON_OBJECT_OBJ_NAME) {
145+
$jsonArray[$jsonElement->getValue()] =
146+
$this->convertJsonArray($entityObject, $jsonElement->getNestedMetadata());
147+
}
148+
145149
$jsonElementType = $jsonElement->getValue();
146150

147151
if (in_array($jsonElementType, ApiExecutor::PRIMITIVE_TYPES)) {
@@ -167,15 +171,7 @@ private function getJsonDataArray($entityObject, $jsonDefMetadata = null)
167171
$entityNamesOfType = $entityObject->getLinkedEntitiesOfType($jsonElementType);
168172

169173
foreach ($entityNamesOfType as $entityName) {
170-
// If this entity's name exists in the dependentEntities (Test-defined data), use that.
171-
// Else go to the DataManager and try and get the entity from the overall repository of data.
172-
if (array_key_exists($entityName, $this->dependentEntities)) {
173-
$linkedEntityObj = $this->dependentEntities[$entityName];
174-
} else {
175-
$linkedEntityObj = DataObjectHandler::getInstance()->getObject($entityName);
176-
}
177-
178-
$jsonDataSubArray = self::getJsonDataArray($linkedEntityObj);
174+
$jsonDataSubArray = $this->resolveNonPrimitiveElement($entityName, $jsonElement);
179175

180176
if ($jsonElement->getType() == 'array') {
181177
$jsonArray[$jsonElement->getKey()][] = $jsonDataSubArray;
@@ -189,16 +185,62 @@ private function getJsonDataArray($entityObject, $jsonDefMetadata = null)
189185
return $jsonArray;
190186
}
191187

188+
/**
189+
* Resolves JsonObjects and pre-defined metadata (in other operation.xml file) referenced by the json metadata
190+
*
191+
* @param string $entityName
192+
* @param JsonElement $jsonElement
193+
* @return array
194+
*/
195+
private function resolveNonPrimitiveElement($entityName, $jsonElement)
196+
{
197+
$linkedEntityObj = $this->resolveLinkedEntityObject($entityName);
198+
199+
if (!empty($jsonElement->getNestedJsonElement($jsonElement->getValue()))
200+
&& $jsonElement->getType() == 'array') {
201+
$jsonSubArray = $this->convertJsonArray(
202+
$linkedEntityObj,
203+
[$jsonElement->getNestedJsonElement($jsonElement->getValue())]
204+
);
205+
206+
return $jsonSubArray[$jsonElement->getValue()];
207+
}
208+
209+
$jsonMetadata = JsonDefinitionObjectHandler::getInstance()->getJsonDefinition(
210+
$this->operation,
211+
$linkedEntityObj->getType()
212+
)->getJsonMetadata();
213+
214+
return $this->convertJsonArray($linkedEntityObj, $jsonMetadata);
215+
}
216+
217+
218+
/**
219+
* Method to wrap entity resolution, checks locally defined dependent entities first
220+
*
221+
* @param string $entityName
222+
* @return EntityDataObject
223+
*/
224+
private function resolveLinkedEntityObject($entityName)
225+
{
226+
// check our dependent entity list to see if we have this defined
227+
if (array_key_exists($entityName, $this->dependentEntities)) {
228+
return $this->dependentEntities[$entityName];
229+
}
230+
231+
return DataObjectHandler::getInstance()->getObject($entityName);
232+
}
233+
192234
/**
193235
* This function retrieves an array representative of json body for a request and returns it encoded as a string.
194236
*
195237
* @return string
196238
*/
197239
public function getEncodedJsonString()
198240
{
199-
$jsonArray = $this->getJsonDataArray($this->entityObject, $this->jsonDefinition->getJsonMetadata());
241+
$jsonMetadataArray = $this->convertJsonArray($this->entityObject, $this->jsonDefinition->getJsonMetadata());
200242

201-
return json_encode([$this->entityObject->getType() => $jsonArray], JSON_PRETTY_PRINT);
243+
return json_encode($jsonMetadataArray, JSON_PRETTY_PRINT);
202244
}
203245

204246
// @codingStandardsIgnoreStart

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php
2-
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
36
namespace Magento\AcceptanceTestFramework\DataGenerator\Handlers;
47

58
use Magento\AcceptanceTestFramework\DataGenerator\Objects\JsonDefinition;
69
use Magento\AcceptanceTestFramework\DataGenerator\Objects\JsonElement;
710
use Magento\AcceptanceTestFramework\DataGenerator\Parsers\OperationMetadataParser;
11+
use Magento\AcceptanceTestFramework\DataGenerator\Util\JsonObjectExtractor;
812
use Magento\AcceptanceTestFramework\ObjectManager\ObjectHandlerInterface;
913
use Magento\AcceptanceTestFramework\ObjectManagerFactory;
1014

@@ -29,6 +33,10 @@ class JsonDefinitionObjectHandler implements ObjectHandlerInterface
2933
const ENTITY_OPERATION_ARRAY = 'array';
3034
const ENTITY_OPERATION_ARRAY_KEY = 'key';
3135
const ENTITY_OPERATION_ARRAY_VALUE = 'value';
36+
const ENTITY_OPERATION_OBJECT = 'object';
37+
const ENTITY_OPERATION_OBJECT_KEY = 'key';
38+
const ENTITY_OPERATION_OBJECT_VALUE = 'value';
39+
const ENTITY_OPERATION_JSON_OBJECT = 'jsonObject';
3240

3341
/**
3442
* Singleton Instance of class
@@ -44,6 +52,13 @@ class JsonDefinitionObjectHandler implements ObjectHandlerInterface
4452
*/
4553
private $jsonDefinitions = [];
4654

55+
/**
56+
* Object used to extract jsonObjects from array into JsonElements
57+
*
58+
* @var JsonObjectExtractor
59+
*/
60+
private $jsonDefExtractor;
61+
4762
/**
4863
* Singleton method to return JsonDefinitionProcessor.
4964
*
@@ -85,7 +100,7 @@ public function getAllObjects()
85100
*/
86101
private function __construct()
87102
{
88-
// private constructor
103+
$this->jsonDefExtractor = new JsonObjectExtractor();
89104
}
90105

91106
/**
@@ -135,6 +150,15 @@ private function initJsonDefinitions()
135150
}
136151
}
137152

153+
// extract relevant jsonObjects as JsonElements
154+
if (array_key_exists(JsonDefinitionObjectHandler::ENTITY_OPERATION_JSON_OBJECT, $jsonDefArray)) {
155+
foreach ($jsonDefArray[JsonDefinitionObjectHandler::ENTITY_OPERATION_JSON_OBJECT] as $jsonObjectArray) {
156+
$jsonMetadata[] = $this->jsonDefExtractor->extractJsonObject($jsonObjectArray);
157+
}
158+
}
159+
160+
//handle loose entries
161+
138162
if (array_key_exists(JsonDefinitionObjectHandler::ENTITY_OPERATION_ENTRY, $jsonDefArray)) {
139163
foreach ($jsonDefArray[JsonDefinitionObjectHandler::ENTITY_OPERATION_ENTRY] as $jsonEntryType) {
140164
$jsonMetadata[] = new JsonElement(
@@ -147,10 +171,28 @@ private function initJsonDefinitions()
147171

148172
if (array_key_exists(JsonDefinitionObjectHandler::ENTITY_OPERATION_ARRAY, $jsonDefArray)) {
149173
foreach ($jsonDefArray[JsonDefinitionObjectHandler::ENTITY_OPERATION_ARRAY] as $jsonEntryType) {
174+
$jsonSubMetadata = [];
175+
$value = null;
176+
$type = null;
177+
178+
if (array_key_exists('jsonObject', $jsonEntryType)) {
179+
$jsonNestedElement = $this->jsonDefExtractor->extractJsonObject(
180+
$jsonEntryType['jsonObject'][0]
181+
);
182+
$jsonSubMetadata[$jsonNestedElement->getKey()] = $jsonNestedElement;
183+
$value = $jsonNestedElement->getValue();
184+
$type = $jsonNestedElement->getKey();
185+
} else {
186+
$value = $jsonEntryType[JsonDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE][0]
187+
[JsonDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE];
188+
$type = [JsonDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE];
189+
}
190+
150191
$jsonMetadata[] = new JsonElement(
151192
$jsonEntryType[JsonDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_KEY],
152-
$jsonEntryType[JsonDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE],
153-
JsonDefinitionObjectHandler::ENTITY_OPERATION_ARRAY
193+
$value,
194+
$type,
195+
$jsonSubMetadata
154196
);
155197
}
156198
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function getDataByName($dataName)
114114
{
115115
$name = strtolower($dataName);
116116

117-
if (!is_null($this->data) && array_key_exists($name, $this->data)) {
117+
if ($this->data != null && array_key_exists($name, $this->data)) {
118118
return $this->data[$name];
119119
}
120120

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,35 @@ class JsonElement
2929
*/
3030
private $type;
3131

32+
/**
33+
* Nested Json Objects defined within the same operation.xml file
34+
*
35+
* @var array
36+
*/
37+
private $nestedElements = [];
38+
39+
/**
40+
* Nested Metadata which must be included for a jsonElement of type jsonObject
41+
*
42+
* @var array|null
43+
*/
44+
private $nestedMetadata = [];
45+
3246
/**
3347
* JsonElement constructor.
3448
* @param string $key
3549
* @param string $value
3650
* @param string $type
51+
* @param array $nestedElements
52+
* @param array $nestedMetadata
3753
*/
38-
public function __construct($key, $value, $type)
54+
public function __construct($key, $value, $type, $nestedElements = [], $nestedMetadata = null)
3955
{
4056
$this->key = $key;
4157
$this->value = $value;
4258
$this->type = $type;
59+
$this->nestedElements = $nestedElements;
60+
$this->nestedMetadata = $nestedMetadata;
4361
}
4462

4563
/**
@@ -71,4 +89,29 @@ public function getType()
7189
{
7290
return $this->type;
7391
}
92+
93+
/**
94+
* Returns the nested json element based on the type of entity passed
95+
*
96+
* @param string $type
97+
* @return array
98+
*/
99+
public function getNestedJsonElement($type)
100+
{
101+
if (array_key_exists($type, $this->nestedElements)) {
102+
return $this->nestedElements[$type];
103+
}
104+
105+
return [];
106+
}
107+
108+
/**
109+
* Returns relevant nested json metadata for a json element which is a json object
110+
*
111+
* @return array|null
112+
*/
113+
public function getNestedMetadata()
114+
{
115+
return $this->nestedMetadata;
116+
}
74117
}

0 commit comments

Comments
 (0)