9
9
use Magento \AcceptanceTestFramework \DataGenerator \Handlers \JsonDefinitionObjectHandler ;
10
10
use Magento \AcceptanceTestFramework \DataGenerator \Objects \EntityDataObject ;
11
11
use Magento \AcceptanceTestFramework \DataGenerator \Objects \JsonDefinition ;
12
+ use Magento \AcceptanceTestFramework \DataGenerator \Objects \JsonElement ;
13
+ use Magento \AcceptanceTestFramework \DataGenerator \Util \JsonObjectExtractor ;
12
14
use Magento \AcceptanceTestFramework \Util \ApiClientUtil ;
13
15
16
+ /**
17
+ * Class ApiExecutor
18
+ */
14
19
class ApiExecutor
15
20
{
21
+ const PRIMITIVE_TYPES = ['string ' , 'boolean ' , 'integer ' , 'double ' , 'array ' ];
22
+
16
23
/**
17
24
* Describes the operation for the executor ('create','update','delete')
18
25
*
@@ -35,11 +42,9 @@ class ApiExecutor
35
42
/**
36
43
* The array of dependentEntities this class can be given. When finding linked entities, APIExecutor
37
44
* uses this repository before looking for static data.
38
- * @var null
45
+ * @var array
39
46
*/
40
- private $ dependentEntities ;
41
-
42
- const PRIMITIVE_TYPES = ['string ' , 'boolean ' , 'integer ' , 'double ' , 'array ' ];
47
+ private $ dependentEntities = [];
43
48
44
49
/**
45
50
* ApiSubObject constructor.
@@ -128,20 +133,19 @@ private function getAuthorizationHeader($authUrl)
128
133
* recursively forming an array which represents the json structure for the api of the desired type.
129
134
*
130
135
* @param EntityDataObject $entityObject
131
- * @param array $jsonDefMetadata
132
- *
136
+ * @param array $jsonArrayMetadata
133
137
* @return array
134
138
*/
135
- private function getJsonDataArray ($ entityObject , $ jsonDefMetadata = null )
139
+ private function convertJsonArray ($ entityObject , $ jsonArrayMetadata )
136
140
{
137
- $ jsonArrayMetadata = !$ jsonDefMetadata ? JsonDefinitionObjectHandler::getInstance ()->getJsonDefinition (
138
- $ this ->operation ,
139
- $ entityObject ->getType ()
140
- )->getJsonMetadata () : $ jsonDefMetadata ;
141
-
142
141
$ jsonArray = [];
143
142
144
143
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
+
145
149
$ jsonElementType = $ jsonElement ->getValue ();
146
150
147
151
if (in_array ($ jsonElementType , ApiExecutor::PRIMITIVE_TYPES )) {
@@ -167,15 +171,7 @@ private function getJsonDataArray($entityObject, $jsonDefMetadata = null)
167
171
$ entityNamesOfType = $ entityObject ->getLinkedEntitiesOfType ($ jsonElementType );
168
172
169
173
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 );
179
175
180
176
if ($ jsonElement ->getType () == 'array ' ) {
181
177
$ jsonArray [$ jsonElement ->getKey ()][] = $ jsonDataSubArray ;
@@ -189,16 +185,62 @@ private function getJsonDataArray($entityObject, $jsonDefMetadata = null)
189
185
return $ jsonArray ;
190
186
}
191
187
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
+
192
234
/**
193
235
* This function retrieves an array representative of json body for a request and returns it encoded as a string.
194
236
*
195
237
* @return string
196
238
*/
197
239
public function getEncodedJsonString ()
198
240
{
199
- $ jsonArray = $ this ->getJsonDataArray ($ this ->entityObject , $ this ->jsonDefinition ->getJsonMetadata ());
241
+ $ jsonMetadataArray = $ this ->convertJsonArray ($ this ->entityObject , $ this ->jsonDefinition ->getJsonMetadata ());
200
242
201
- return json_encode ([ $ this -> entityObject -> getType () => $ jsonArray ] , JSON_PRETTY_PRINT );
243
+ return json_encode ($ jsonMetadataArray , JSON_PRETTY_PRINT );
202
244
}
203
245
204
246
// @codingStandardsIgnoreStart
0 commit comments