Skip to content

Commit ac6daba

Browse files
authored
Merge branch 'develop' into MQE-1644
2 parents 39d987a + e175381 commit ac6daba

File tree

5 files changed

+265
-46
lines changed

5 files changed

+265
-46
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Persist/OperationDataArrayResolverTest.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
1010
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\OperationDefinitionObjectHandler;
1111
use Magento\FunctionalTestingFramework\DataGenerator\Persist\OperationDataArrayResolver;
12+
use Magento\FunctionalTestingFramework\Util\Iterator\AbstractIterator;
1213
use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
1314
use tests\unit\Util\EntityDataObjectBuilder;
1415
use tests\unit\Util\OperationDefinitionBuilder;
@@ -355,6 +356,127 @@ public function testNestedMetadataArrayOfValue()
355356
$this->assertEquals(self::NESTED_METADATA_ARRAY_RESULT, $result);
356357
}
357358

359+
public function testNestedMetadataArrayOfDiverseObjects()
360+
{
361+
362+
$entityDataObjBuilder = new EntityDataObjectBuilder();
363+
$parentDataObject = $entityDataObjBuilder
364+
->withName("parentObject")
365+
->withType("parentType")
366+
->withLinkedEntities(['child1Object' => 'childType1','child2Object' => 'childType2'])
367+
->build();
368+
369+
$child1DataObject = $entityDataObjBuilder
370+
->withName('child1Object')
371+
->withType('childType1')
372+
->withDataFields(['city' => 'Testcity','zip' => 12345])
373+
->build();
374+
375+
$child2DataObject = $entityDataObjBuilder
376+
->withName('child2Object')
377+
->withType('childType2')
378+
->withDataFields(['city' => 'Testcity 2','zip' => 54321,'state' => 'Teststate'])
379+
->build();
380+
381+
$mockDOHInstance = AspectMock::double(
382+
DataObjectHandler::class,
383+
[
384+
'getObject' => function ($name) use ($child1DataObject, $child2DataObject) {
385+
switch ($name) {
386+
case 'child1Object':
387+
return $child1DataObject;
388+
case 'child2Object':
389+
return $child2DataObject;
390+
}
391+
}
392+
]
393+
)->make();
394+
AspectMock::double(DataObjectHandler::class, [
395+
'getInstance' => $mockDOHInstance
396+
]);
397+
398+
$operationDefinitionBuilder = new OperationDefinitionBuilder();
399+
$child1OperationDefinition = $operationDefinitionBuilder
400+
->withName('createchildType1')
401+
->withOperation('create')
402+
->withType('childType1')
403+
->withMetadata([
404+
'city' => 'string',
405+
'zip' => 'integer'
406+
])->build();
407+
408+
$child2OperationDefinition = $operationDefinitionBuilder
409+
->withName('createchildType2')
410+
->withOperation('create')
411+
->withType('childType2')
412+
->withMetadata([
413+
'city' => 'string',
414+
'zip' => 'integer',
415+
'state' => 'string'
416+
])->build();
417+
418+
$mockODOHInstance = AspectMock::double(
419+
OperationDefinitionObjectHandler::class,
420+
[
421+
'getObject' => function ($name) use ($child1OperationDefinition, $child2OperationDefinition) {
422+
switch ($name) {
423+
case 'createchildType1':
424+
return $child1OperationDefinition;
425+
case 'createchildType2':
426+
return $child2OperationDefinition;
427+
}
428+
}
429+
]
430+
)->make();
431+
AspectMock::double(
432+
OperationDefinitionObjectHandler::class,
433+
[
434+
'getInstance' => $mockODOHInstance
435+
]
436+
);
437+
438+
$arrayObElementBuilder = new OperationElementBuilder();
439+
$arrayElement = $arrayObElementBuilder
440+
->withKey('address')
441+
->withType(['childType1','childType2'])
442+
->withFields([])
443+
->withElementType(OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY)
444+
//->withNestedElements(['childType1' => $child1Element, 'childType2' => $child2Element])
445+
->build();
446+
447+
$parentOpElementBuilder = new OperationElementBuilder();
448+
$parentElement = $parentOpElementBuilder
449+
->withKey('parentType')
450+
->withType('parentType')
451+
->addElements(['address' => $arrayElement])
452+
->build();
453+
454+
$operationResolver = new OperationDataArrayResolver();
455+
$result = $operationResolver->resolveOperationDataArray($parentDataObject, [$parentElement], 'create', false);
456+
457+
$expectedResult = [
458+
'parentType' => [
459+
'address' => [
460+
[
461+
'city' => 'Testcity',
462+
'zip' => '12345'
463+
],
464+
[
465+
'city' => 'Testcity 2',
466+
'zip' => '54321',
467+
'state' => 'Teststate'
468+
]
469+
],
470+
'name' => 'Hopper',
471+
'gpa' => '3.5678',
472+
'phone' => '5555555',
473+
'isPrimary' => '1'
474+
]
475+
];
476+
477+
$this->assertEquals($expectedResult, $result);
478+
}
479+
358480
/**
359481
* After class functionality
360482
* @return void

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public function __construct($dependentEntities = null)
6767
* @param boolean $fromArray
6868
* @return array
6969
* @throws \Exception
70+
*
71+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
72+
* I suppressed this warning because I was in a hurry to deliver a community PR. That PR modified this function and
73+
* introduced a new conditional, bumping the complexity to 11.
7074
*/
7175
public function resolveOperationDataArray($entityObject, $operationMetadata, $operation, $fromArray = false)
7276
{
@@ -109,6 +113,26 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op
109113
$operationElementType,
110114
$operationDataArray
111115
);
116+
} elseif (is_array($operationElementType)) {
117+
foreach ($operationElementType as $currentElementType) {
118+
if (in_array($currentElementType, self::PRIMITIVE_TYPES)) {
119+
$this->resolvePrimitiveReferenceElement(
120+
$entityObject,
121+
$operationElement,
122+
$currentElementType,
123+
$operationDataArray
124+
);
125+
} else {
126+
$this->resolveNonPrimitiveReferenceElement(
127+
$entityObject,
128+
$operation,
129+
$fromArray,
130+
$currentElementType,
131+
$operationElement,
132+
$operationDataArray
133+
);
134+
}
135+
}
112136
} else {
113137
$this->resolveNonPrimitiveReferenceElement(
114138
$entityObject,
@@ -248,7 +272,8 @@ private function resolveNonPrimitiveElement($entityName, $operationElement, $ope
248272
$linkedEntityObj = $this->resolveLinkedEntityObject($entityName);
249273

250274
// in array case
251-
if (!empty($operationElement->getNestedOperationElement($operationElement->getValue()))
275+
if (!is_array($operationElement->getValue())
276+
&& !empty($operationElement->getNestedOperationElement($operationElement->getValue()))
252277
&& $operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY
253278
) {
254279
$operationSubArray = $this->resolveOperationDataArray(

src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,18 @@ private function extractOperationField(&$operationElements, $operationFieldArray
112112
private function extractOperationArray(&$operationArrayData, $operationArrayArray)
113113
{
114114
foreach ($operationArrayArray as $operationFieldType) {
115-
$operationElementValue =
116-
$operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE][0]
117-
[OperationElementExtractor::OPERATION_OBJECT_ARRAY_VALUE] ?? null;
115+
$operationElementValue = [];
116+
if (isset($operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE])) {
117+
foreach ($operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE] as
118+
$operationFieldValue) {
119+
$operationElementValue[] =
120+
$operationFieldValue[OperationElementExtractor::OPERATION_OBJECT_ARRAY_VALUE] ?? null;
121+
}
122+
}
123+
124+
if (count($operationElementValue) === 1) {
125+
$operationElementValue = array_pop($operationElementValue);
126+
}
118127

119128
$nestedOperationElements = [];
120129
if (array_key_exists(OperationElementExtractor::OPERATION_OBJECT_OBJ_NAME, $operationFieldType)) {

0 commit comments

Comments
 (0)