From 669ca5330fdb17dc6f1541c742f6e75eba84f9da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20K=C3=B6cke?= Date: Wed, 28 Aug 2019 13:39:49 +0200 Subject: [PATCH 1/5] Add possibility to include multiple non primitive types in an array If an array is defined for an entity it is currently not possible to include different types of non primitive types in the same array. --- .../Persist/OperationDataArrayResolver.php | 22 ++++++++++++++++++- .../Util/OperationElementExtractor.php | 13 ++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php index 6d1ebc3fc..bd641c2ff 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php @@ -109,6 +109,26 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op $operationElementType, $operationDataArray ); + } else if (is_array($operationElementType)) { + foreach ($operationElementType as $currentElementType) { + if (in_array($currentElementType, self::PRIMITIVE_TYPES)) { + $this->resolvePrimitiveReferenceElement( + $entityObject, + $operationElement, + $currentElementType, + $operationDataArray + ); + } else { + $this->resolveNonPrimitiveReferenceElement( + $entityObject, + $operation, + $fromArray, + $currentElementType, + $operationElement, + $operationDataArray + ); + } + } } else { $this->resolveNonPrimitiveReferenceElement( $entityObject, @@ -236,7 +256,7 @@ private function resolveNonPrimitiveElement($entityName, $operationElement, $ope $linkedEntityObj = $this->resolveLinkedEntityObject($entityName); // in array case - if (!empty($operationElement->getNestedOperationElement($operationElement->getValue())) + if (!is_array($operationElement->getValue()) && !empty($operationElement->getNestedOperationElement($operationElement->getValue())) && $operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY ) { $operationSubArray = $this->resolveOperationDataArray( diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php index 35d188f5b..7467e312b 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php @@ -112,9 +112,16 @@ private function extractOperationField(&$operationElements, $operationFieldArray private function extractOperationArray(&$operationArrayData, $operationArrayArray) { foreach ($operationArrayArray as $operationFieldType) { - $operationElementValue = - $operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE][0] - [OperationElementExtractor::OPERATION_OBJECT_ARRAY_VALUE] ?? null; + $operationElementValue = []; + if (isset($operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE])) { + foreach ($operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE] as $operationFieldValue) { + $operationElementValue[] = $operationFieldValue[OperationElementExtractor::OPERATION_OBJECT_ARRAY_VALUE] ?? null; + } + } + + if (count($operationElementValue) === 1) { + $operationElementValue = array_pop($operationElementValue); + } $nestedOperationElements = []; if (array_key_exists(OperationElementExtractor::OPERATION_OBJECT_OBJ_NAME, $operationFieldType)) { From 938a6aba218504cbcb8a712753afa95286652194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20K=C3=B6cke?= Date: Wed, 28 Aug 2019 14:02:26 +0200 Subject: [PATCH 2/5] Correct formatting to adhere to psr-2 coding standards --- .../DataGenerator/Persist/OperationDataArrayResolver.php | 5 +++-- .../DataGenerator/Util/OperationElementExtractor.php | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php index bd641c2ff..c8e582728 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php @@ -109,7 +109,7 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op $operationElementType, $operationDataArray ); - } else if (is_array($operationElementType)) { + } elseif (is_array($operationElementType)) { foreach ($operationElementType as $currentElementType) { if (in_array($currentElementType, self::PRIMITIVE_TYPES)) { $this->resolvePrimitiveReferenceElement( @@ -256,7 +256,8 @@ private function resolveNonPrimitiveElement($entityName, $operationElement, $ope $linkedEntityObj = $this->resolveLinkedEntityObject($entityName); // in array case - if (!is_array($operationElement->getValue()) && !empty($operationElement->getNestedOperationElement($operationElement->getValue())) + if (!is_array($operationElement->getValue()) + && !empty($operationElement->getNestedOperationElement($operationElement->getValue())) && $operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY ) { $operationSubArray = $this->resolveOperationDataArray( diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php index 7467e312b..8c127046a 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php @@ -114,7 +114,9 @@ private function extractOperationArray(&$operationArrayData, $operationArrayArra foreach ($operationArrayArray as $operationFieldType) { $operationElementValue = []; if (isset($operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE])) { - foreach ($operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE] as $operationFieldValue) { + foreach ($operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE] + as $operationFieldValue + ) { $operationElementValue[] = $operationFieldValue[OperationElementExtractor::OPERATION_OBJECT_ARRAY_VALUE] ?? null; } } From b3f9db9f55e07f99ae7be1f1021a759e8be75308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20K=C3=B6cke?= Date: Wed, 28 Aug 2019 14:12:51 +0200 Subject: [PATCH 3/5] Correct formatting to adhere to psr-2 coding standards --- .../DataGenerator/Util/OperationElementExtractor.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php index 8c127046a..c86e27972 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Util/OperationElementExtractor.php @@ -114,10 +114,10 @@ private function extractOperationArray(&$operationArrayData, $operationArrayArra foreach ($operationArrayArray as $operationFieldType) { $operationElementValue = []; if (isset($operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE])) { - foreach ($operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE] - as $operationFieldValue - ) { - $operationElementValue[] = $operationFieldValue[OperationElementExtractor::OPERATION_OBJECT_ARRAY_VALUE] ?? null; + foreach ($operationFieldType[OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE] as + $operationFieldValue) { + $operationElementValue[] = + $operationFieldValue[OperationElementExtractor::OPERATION_OBJECT_ARRAY_VALUE] ?? null; } } From 070654daed9144ad7aaa38a981be8d7dddbad8a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20K=C3=B6cke?= Date: Tue, 1 Oct 2019 15:06:01 +0200 Subject: [PATCH 4/5] Add unit test for diveres data array --- .../OperationDataArrayResolverTest.php | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Persist/OperationDataArrayResolverTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Persist/OperationDataArrayResolverTest.php index 9e425e020..60cf4bf0f 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Persist/OperationDataArrayResolverTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Persist/OperationDataArrayResolverTest.php @@ -9,6 +9,7 @@ use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler; use Magento\FunctionalTestingFramework\DataGenerator\Handlers\OperationDefinitionObjectHandler; use Magento\FunctionalTestingFramework\DataGenerator\Persist\OperationDataArrayResolver; +use Magento\FunctionalTestingFramework\Util\Iterator\AbstractIterator; use Magento\FunctionalTestingFramework\Util\MagentoTestCase; use tests\unit\Util\EntityDataObjectBuilder; use tests\unit\Util\OperationDefinitionBuilder; @@ -355,6 +356,123 @@ public function testNestedMetadataArrayOfValue() $this->assertEquals(self::NESTED_METADATA_ARRAY_RESULT, $result); } + public function testNestedMetadataArrayOfDiverseObjects() { + + $entityDataObjBuilder = new EntityDataObjectBuilder(); + $parentDataObject = $entityDataObjBuilder + ->withName("parentObject") + ->withType("parentType") + ->withLinkedEntities(['child1Object' => 'childType1','child2Object' => 'childType2']) + ->build(); + + + $child1DataObject = $entityDataObjBuilder + ->withName('child1Object') + ->withType('childType1') + ->withDataFields(['city' => 'Testcity','zip' => 12345]) + ->build(); + + $child2DataObject = $entityDataObjBuilder + ->withName('child2Object') + ->withType('childType2') + ->withDataFields(['city' => 'Testcity 2','zip' => 54321,'state' => 'Teststate']) + ->build(); + + $mockDOHInstance = AspectMock::double(DataObjectHandler::class, + [ + 'getObject' => function ($name) use ($child1DataObject, $child2DataObject) { + switch ($name) { + case 'child1Object': + return $child1DataObject; + case 'child2Object': + return $child2DataObject; + } + } + ])->make(); + AspectMock::double(DataObjectHandler::class,[ + 'getInstance' => $mockDOHInstance + ]); + + $operationDefinitionBuilder = new OperationDefinitionBuilder(); + $child1OperationDefinition = $operationDefinitionBuilder + ->withName('createchildType1') + ->withOperation('create') + ->withType('childType1') + ->withMetadata([ + 'city' => 'string', + 'zip' => 'integer' + ])->build(); + + $child2OperationDefinition = $operationDefinitionBuilder + ->withName('createchildType2') + ->withOperation('create') + ->withType('childType2') + ->withMetadata([ + 'city' => 'string', + 'zip' => 'integer', + 'state' => 'string' + ])->build(); + + $mockODOHInstance = AspectMock::double( + OperationDefinitionObjectHandler::class, + [ + 'getObject' => function($name) use ($child1OperationDefinition, $child2OperationDefinition) { + switch ($name) { + case 'createchildType1': + return $child1OperationDefinition; + case 'createchildType2': + return $child2OperationDefinition; + } + } + ] + )->make(); + AspectMock::double(OperationDefinitionObjectHandler::class, + [ + 'getInstance' => $mockODOHInstance + ]); + + $arrayObElementBuilder = new OperationElementBuilder(); + $arrayElement = $arrayObElementBuilder + ->withKey('address') + ->withType(['childType1','childType2']) + ->withFields([]) + ->withElementType(OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY) + //->withNestedElements(['childType1' => $child1Element, 'childType2' => $child2Element]) + ->build(); + + $parentOpElementBuilder = new OperationElementBuilder(); + $parentElement = $parentOpElementBuilder + ->withKey('parentType') + ->withType('parentType') + ->addElements(['address' => $arrayElement]) + ->build(); + + $operationResolver = new OperationDataArrayResolver(); + $result = $operationResolver->resolveOperationDataArray($parentDataObject,[$parentElement],'create',false); + + $expectedResult = [ + 'parentType' => [ + 'address' => [ + [ + 'city' => 'Testcity', + 'zip' => '12345' + ], + [ + 'city' => 'Testcity 2', + 'zip' => '54321', + 'state' => 'Teststate' + ] + ], + 'name' => 'Hopper', + 'gpa' => '3.5678', + 'phone' => '5555555', + 'isPrimary' => '1' + ] + ]; + + $this->assertEquals($expectedResult, $result); + } + /** * After class functionality * @return void From 491802cf11310732a4046f2d52ea63bf31873fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20K=C3=B6cke?= Date: Tue, 1 Oct 2019 15:19:13 +0200 Subject: [PATCH 5/5] Adhere to code style guide --- .../OperationDataArrayResolverTest.php | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Persist/OperationDataArrayResolverTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Persist/OperationDataArrayResolverTest.php index 60cf4bf0f..fb6dcd865 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Persist/OperationDataArrayResolverTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Persist/OperationDataArrayResolverTest.php @@ -356,7 +356,8 @@ public function testNestedMetadataArrayOfValue() $this->assertEquals(self::NESTED_METADATA_ARRAY_RESULT, $result); } - public function testNestedMetadataArrayOfDiverseObjects() { + public function testNestedMetadataArrayOfDiverseObjects() + { $entityDataObjBuilder = new EntityDataObjectBuilder(); $parentDataObject = $entityDataObjBuilder @@ -365,7 +366,6 @@ public function testNestedMetadataArrayOfDiverseObjects() { ->withLinkedEntities(['child1Object' => 'childType1','child2Object' => 'childType2']) ->build(); - $child1DataObject = $entityDataObjBuilder ->withName('child1Object') ->withType('childType1') @@ -378,18 +378,20 @@ public function testNestedMetadataArrayOfDiverseObjects() { ->withDataFields(['city' => 'Testcity 2','zip' => 54321,'state' => 'Teststate']) ->build(); - $mockDOHInstance = AspectMock::double(DataObjectHandler::class, + $mockDOHInstance = AspectMock::double( + DataObjectHandler::class, [ 'getObject' => function ($name) use ($child1DataObject, $child2DataObject) { - switch ($name) { - case 'child1Object': - return $child1DataObject; - case 'child2Object': - return $child2DataObject; - } + switch ($name) { + case 'child1Object': + return $child1DataObject; + case 'child2Object': + return $child2DataObject; + } } - ])->make(); - AspectMock::double(DataObjectHandler::class,[ + ] + )->make(); + AspectMock::double(DataObjectHandler::class, [ 'getInstance' => $mockDOHInstance ]); @@ -416,7 +418,7 @@ public function testNestedMetadataArrayOfDiverseObjects() { $mockODOHInstance = AspectMock::double( OperationDefinitionObjectHandler::class, [ - 'getObject' => function($name) use ($child1OperationDefinition, $child2OperationDefinition) { + 'getObject' => function ($name) use ($child1OperationDefinition, $child2OperationDefinition) { switch ($name) { case 'createchildType1': return $child1OperationDefinition; @@ -425,11 +427,13 @@ public function testNestedMetadataArrayOfDiverseObjects() { } } ] - )->make(); - AspectMock::double(OperationDefinitionObjectHandler::class, + )->make(); + AspectMock::double( + OperationDefinitionObjectHandler::class, [ 'getInstance' => $mockODOHInstance - ]); + ] + ); $arrayObElementBuilder = new OperationElementBuilder(); $arrayElement = $arrayObElementBuilder @@ -448,7 +452,7 @@ public function testNestedMetadataArrayOfDiverseObjects() { ->build(); $operationResolver = new OperationDataArrayResolver(); - $result = $operationResolver->resolveOperationDataArray($parentDataObject,[$parentElement],'create',false); + $result = $operationResolver->resolveOperationDataArray($parentDataObject, [$parentElement], 'create', false); $expectedResult = [ 'parentType' => [