-
Notifications
You must be signed in to change notification settings - Fork 133
/
OperationDataArrayResolver.php
375 lines (334 loc) · 13.7 KB
/
OperationDataArrayResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\DataGenerator\Persist;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\OperationDefinitionObjectHandler;
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationElement;
use Magento\FunctionalTestingFramework\DataGenerator\Util\OperationElementExtractor;
class OperationDataArrayResolver
{
const PRIMITIVE_TYPES = [
'string',
'boolean',
'integer',
'number'
];
const EXCEPTION_REQUIRED_DATA = "%s of key \" %s\" in \"%s\" is required by metadata, but was not provided.";
/**
* The array of entity name and number of objects being created,
* we don't need to track objects in update and delete operations.
*
* @var array
*/
private static $entitySequences = [];
/**
* The array of dependentEntities this class can be given. When finding linked entities, APIExecutor
* uses this repository before looking for static data.
*
* @var array
*/
private $dependentEntities = [];
/**
* OperationDataArrayResolver constructor.
*
* @param array $dependentEntities
*/
public function __construct($dependentEntities = null)
{
if ($dependentEntities !== null) {
foreach ($dependentEntities as $entity) {
$this->dependentEntities[$entity->getName()] = $entity;
}
}
}
/**
* This function returns an array which is structurally equal to the data which is needed by the magento web api,
* magento backend / frontend requests for entity creation. The function retrieves an array describing the entity's
* operation metadata and traverses any dependencies recursively forming an array which represents the data
* structure for the request of the desired entity type.
*
* @param EntityDataObject $entityObject
* @param array $operationMetadata
* @param string $operation
* @param bool $fromArray
* @return array
* @throws \Exception
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function resolveOperationDataArray($entityObject, $operationMetadata, $operation, $fromArray = false)
{
//TODO: Refactor to reduce Cyclomatic Complexity, remove SupressWarning accordingly.
$operationDataArray = [];
self::incrementSequence($entityObject->getName());
foreach ($operationMetadata as $operationElement) {
if ($operationElement->getType() == OperationElementExtractor::OPERATION_OBJECT_OBJ_NAME) {
$entityObj = $this->resolveOperationObjectAndEntityData($entityObject, $operationElement->getValue());
if (null === $entityObj && $operationElement->isRequired()) {
throw new \Exception(sprintf(
self::EXCEPTION_REQUIRED_DATA,
$operationElement->getType(),
$operationElement->getKey(),
$entityObject->getName()
));
} elseif (null === $entityObj) {
continue;
}
$operationData = $this->resolveOperationDataArray(
$entityObj,
$operationElement->getNestedMetadata(),
$operation,
$fromArray
);
if (!$fromArray) {
$operationDataArray[$operationElement->getKey()] = $operationData;
} else {
$operationDataArray = $operationData;
}
continue;
}
$operationElementType = $operationElement->getValue();
if (in_array($operationElementType, self::PRIMITIVE_TYPES)) {
$elementData = $this->resolvePrimitiveReference(
$entityObject,
$operationElement->getKey(),
$operationElement->getType()
);
// If data was defined at all, attempt to put it into operation data array
// If data was not defined, and element is required, throw exception
// If no data is defined, don't input defaults per primitive into operation data array
if ($elementData != null) {
if (array_key_exists($operationElement->getKey(), $entityObject->getUniquenessData())) {
$uniqueData = $entityObject->getUniquenessDataByName($operationElement->getKey());
if ($uniqueData === 'suffix') {
$elementData .= (string)self::getSequence($entityObject->getName());
} else {
$elementData = (string)self::getSequence($entityObject->getName()) . $elementData;
}
}
$operationDataArray[$operationElement->getKey()] = $this->castValue(
$operationElementType,
$elementData
);
} elseif ($operationElement->isRequired()) {
throw new \Exception(sprintf(
self::EXCEPTION_REQUIRED_DATA,
$operationElement->getType(),
$operationElement->getKey(),
$entityObject->getName()
));
}
} else {
$entityNamesOfType = $entityObject->getLinkedEntitiesOfType($operationElementType);
// If an element is required by metadata, but was not provided in the entity, throw an exception
if ($operationElement->isRequired() && $entityNamesOfType == null) {
throw new \Exception(sprintf(
self::EXCEPTION_REQUIRED_DATA,
$operationElement->getType(),
$operationElement->getKey(),
$entityObject->getName()
));
}
foreach ($entityNamesOfType as $entityName) {
$operationDataSubArray = $this->resolveNonPrimitiveElement(
$entityName,
$operationElement,
$operation,
$fromArray
);
if ($operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY) {
$operationDataArray[$operationElement->getKey()][] = $operationDataSubArray;
} else {
$operationDataArray[$operationElement->getKey()] = $operationDataSubArray;
}
}
}
}
return $operationDataArray;
}
/**
* Resolves a reference for a primitive piece of data, if the data cannot be found as a defined field, the method
* looks to see if any vars have been declared with the same operationKey and resolves based on defined dependent
* entities.
*
* @param EntityDataObject $entityObject
* @param string $operationKey
* @param string $operationElementType
* @return array|string
*/
private function resolvePrimitiveReference($entityObject, $operationKey, $operationElementType)
{
$elementData = $entityObject->getDataByName(
$operationKey,
EntityDataObject::CEST_UNIQUE_VALUE
);
if ($elementData == null && $entityObject->getVarReference($operationKey) != null) {
list($type, $field) = explode(
DataObjectHandler::_SEPARATOR,
$entityObject->getVarReference($operationKey)
);
if ($operationElementType == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY) {
$elementDatas = [];
$entities = $this->getDependentEntitiesOfType($type);
foreach ($entities as $entity) {
$elementDatas[] = $entity->getDataByName($field, EntityDataObject::CEST_UNIQUE_VALUE);
}
return $elementDatas;
}
$entity = $this->getDependentEntitiesOfType($type)[0];
$elementData = $entity->getDataByName($field, EntityDataObject::CEST_UNIQUE_VALUE);
}
return $elementData;
}
/**
* Returns all dependent entities of the type passed in as an arg (the dependent entities are given at runtime,
* and are not statically defined).
*
* @param string $type
* @return array
*/
private function getDependentEntitiesOfType($type)
{
$entitiesOfType = [];
foreach ($this->dependentEntities as $dependentEntity) {
if ($dependentEntity->getType() == $type) {
$entitiesOfType[] = $dependentEntity;
}
}
return $entitiesOfType;
}
/**
* This function does a comparison of the entity object being matched to the operation element. If there is a
* mismatch in type we attempt to use a nested entity, if the entities are properly matched, we simply return
* the object.
*
* @param EntityDataObject $entityObject
* @param string $operationElementValue
* @return EntityDataObject|null
*/
private function resolveOperationObjectAndEntityData($entityObject, $operationElementValue)
{
if ($operationElementValue != $entityObject->getType()) {
// if we have a mismatch attempt to retrieve linked data and return just the first linkage
$linkName = $entityObject->getLinkedEntitiesOfType($operationElementValue);
if (!empty($linkName)) {
$linkName = $linkName[0];
return DataObjectHandler::getInstance()->getObject($linkName);
}
return null;
}
return $entityObject;
}
/**
* Resolves DataObjects and pre-defined metadata (in other operation.xml file) referenced by the operation
*
* @param string $entityName
* @param OperationElement $operationElement
* @param string $operation
* @param bool $fromArray
* @return array
*/
private function resolveNonPrimitiveElement($entityName, $operationElement, $operation, $fromArray = false)
{
$linkedEntityObj = $this->resolveLinkedEntityObject($entityName);
// in array case
if (!empty($operationElement->getNestedOperationElement($operationElement->getValue()))
&& $operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY
) {
$operationSubArray = $this->resolveOperationDataArray(
$linkedEntityObj,
[$operationElement->getNestedOperationElement($operationElement->getValue())],
$operation,
true
);
return $operationSubArray;
}
$operationMetadata = OperationDefinitionObjectHandler::getInstance()->getOperationDefinition(
$operation,
$linkedEntityObj->getType()
)->getOperationMetadata();
return $this->resolveOperationDataArray($linkedEntityObj, $operationMetadata, $operation, $fromArray);
}
/**
* Method to wrap entity resolution, checks locally defined dependent entities first
*
* @param string $entityName
* @return EntityDataObject
*/
private function resolveLinkedEntityObject($entityName)
{
// check our dependent entity list to see if we have this defined
if (array_key_exists($entityName, $this->dependentEntities)) {
return $this->dependentEntities[$entityName];
}
return DataObjectHandler::getInstance()->getObject($entityName);
}
/**
* Increment an entity's sequence number by 1.
*
* @param string $entityName
* @return void
*/
private static function incrementSequence($entityName)
{
if (array_key_exists($entityName, self::$entitySequences)) {
self::$entitySequences[$entityName]++;
} else {
self::$entitySequences[$entityName] = 1;
}
}
/**
* Get the current sequence number for an entity.
*
* @param string $entityName
* @return int
*/
private static function getSequence($entityName)
{
if (array_key_exists($entityName, self::$entitySequences)) {
return self::$entitySequences[$entityName];
}
return 0;
}
// @codingStandardsIgnoreStart
/**
* This function takes a string value and its corresponding type and returns the string cast
* into its the type passed.
*
* @param string $type
* @param string $value
* @return mixed
*/
private function castValue($type, $value)
{
$newVal = $value;
if (is_array($value)) {
$newVals = [];
foreach($value as $val) {
$newVals[] = $this->castValue($type, $val);
}
return $newVals;
}
switch ($type) {
case 'string':
break;
case 'integer':
$newVal = (integer)$value;
break;
case 'boolean':
if (strtolower($newVal) === 'false') {
return false;
}
$newVal = (boolean)$value;
break;
case 'number':
$newVal = (float)$value;
break;
}
return $newVal;
}
// @codingStandardsIgnoreEnd
}