Skip to content

Commit 6a5f52a

Browse files
committed
Merge branch 'fDiverseArray' of https://github.com/ochnygosch/magento2-functional-testing-framework into ochnygosch-fDiverseArray
2 parents acf280b + 491802c commit 6a5f52a

File tree

3 files changed

+156
-4
lines changed

3 files changed

+156
-4
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: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op
109109
$operationElementType,
110110
$operationDataArray
111111
);
112+
} elseif (is_array($operationElementType)) {
113+
foreach ($operationElementType as $currentElementType) {
114+
if (in_array($currentElementType, self::PRIMITIVE_TYPES)) {
115+
$this->resolvePrimitiveReferenceElement(
116+
$entityObject,
117+
$operationElement,
118+
$currentElementType,
119+
$operationDataArray
120+
);
121+
} else {
122+
$this->resolveNonPrimitiveReferenceElement(
123+
$entityObject,
124+
$operation,
125+
$fromArray,
126+
$currentElementType,
127+
$operationElement,
128+
$operationDataArray
129+
);
130+
}
131+
}
112132
} else {
113133
$this->resolveNonPrimitiveReferenceElement(
114134
$entityObject,
@@ -248,7 +268,8 @@ private function resolveNonPrimitiveElement($entityName, $operationElement, $ope
248268
$linkedEntityObj = $this->resolveLinkedEntityObject($entityName);
249269

250270
// in array case
251-
if (!empty($operationElement->getNestedOperationElement($operationElement->getValue()))
271+
if (!is_array($operationElement->getValue())
272+
&& !empty($operationElement->getNestedOperationElement($operationElement->getValue()))
252273
&& $operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY
253274
) {
254275
$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)