diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/OperationDefinitionObjectHandler.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/OperationDefinitionObjectHandler.php index 66ca3f594..78deef0ef 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/OperationDefinitionObjectHandler.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/OperationDefinitionObjectHandler.php @@ -9,7 +9,6 @@ use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationElement; use Magento\FunctionalTestingFramework\DataGenerator\Parsers\OperationDefinitionParser; use Magento\FunctionalTestingFramework\DataGenerator\Util\OperationElementExtractor; -use Magento\FunctionalTestingFramework\Exceptions\XmlException; use Magento\FunctionalTestingFramework\ObjectManager\ObjectHandlerInterface; use Magento\FunctionalTestingFramework\ObjectManagerFactory; @@ -128,14 +127,12 @@ public function getOperationDefinition($operation, $dataType) * @return void * @throws \Exception * - * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.UnusedPrivateMethod) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ private function initialize() { - //TODO: Reduce CyclomaticComplexity/NPathComplexity/Length of method, remove warning suppression. $objectManager = ObjectManagerFactory::getObjectManager(); $parser = $objectManager->create(OperationDefinitionParser::class); $parserOutput = $parser->readOperationMetadata()[OperationDefinitionObjectHandler::ENTITY_OPERATION_ROOT_TAG]; @@ -149,29 +146,11 @@ private function initialize() $returnRegex = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_RETURN_REGEX] ?? null; $contentType = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_CONTENT_TYPE][0]['value'] ?? null; - $headers = []; - $params = []; + $headers = $this->initializeHeaders($opDefArray); + $params = $this->initializeParams($opDefArray); $operationElements = []; $removeBackend = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_BACKEND_REMOVE] ?? false; - if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER, $opDefArray)) { - foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER] as $headerEntry) { - if (isset($headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE]) - && $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE] !== 'none') { - $headers[] = $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_PARAM] - . ': ' - . $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE]; - } - } - } - - if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM, $opDefArray)) { - foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM] as $paramEntry) { - $params[$paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_KEY]] = - $paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_VALUE]; - } - } - // extract relevant OperationObjects as OperationElements if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT, $opDefArray)) { foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT] as $opElementArray) { @@ -241,4 +220,44 @@ private function initialize() ); } } + + /** + * Convert headers metadata into an array of objects for further use in. + * + * @param array $opDefArray + * @return array + */ + private function initializeHeaders(array $opDefArray): array + { + $headers = []; + if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER, $opDefArray)) { + foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER] as $headerEntry) { + if (isset($headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE]) + && $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE] !== 'none') { + $headers[] = $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_PARAM] + . ': ' + . $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE]; + } + } + } + return $headers; + } + + /** + * Convert params metadata into an array of objects. + * + * @param array $opDefArray + * @return array + */ + private function initializeParams(array $opDefArray): array + { + $params = []; + if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM, $opDefArray)) { + foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM] as $paramEntry) { + $params[$paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_KEY]] = + $paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_VALUE]; + } + } + return $params; + } } diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php index 95bcd1bab..4e7915b87 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php @@ -65,13 +65,11 @@ public function __construct($dependentEntities = null) * @param boolean $fromArray * @return array * @throws \Exception - * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function resolveOperationDataArray($entityObject, $operationMetadata, $operation, $fromArray = false) { - //TODO: Refactor to reduce Cyclomatic Complexity, remove SupressWarning accordingly. $operationDataArray = []; self::incrementSequence($entityObject->getName()); @@ -105,80 +103,21 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op $operationElementType = $operationElement->getValue(); if (in_array($operationElementType, self::PRIMITIVE_TYPES)) { - $elementData = $this->resolvePrimitiveReference( + $this->resolvePrimitiveReferenceElement( $entityObject, - $operationElement->getKey(), - $operationElement->getType() + $operationElement, + $operationElementType, + $operationDataArray ); - - // 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 { - $operationElementProperty = null; - if (strpos($operationElementType, '.') !== false) { - $operationElementComponents = explode('.', $operationElementType); - $operationElementType = $operationElementComponents[0]; - $operationElementProperty = $operationElementComponents[1]; - } - - $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) { - if ($operationElementProperty === null) { - $operationDataSubArray = $this->resolveNonPrimitiveElement( - $entityName, - $operationElement, - $operation, - $fromArray - ); - } else { - $linkedEntityObj = $this->resolveLinkedEntityObject($entityName); - $operationDataSubArray = $linkedEntityObj->getDataByName($operationElementProperty, 0); - - if ($operationDataSubArray === null) { - throw new \Exception( - sprintf('Property %s not found in entity %s \n', $operationElementProperty, $entityName) - ); - } - } - - if ($operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY) { - $operationDataArray[$operationElement->getKey()][] = $operationDataSubArray; - } else { - $operationDataArray[$operationElement->getKey()] = $operationDataSubArray; - } - } + $this->resolveNonPrimitiveReferenceElement( + $entityObject, + $operation, + $fromArray, + $operationElementType, + $operationElement, + $operationDataArray + ); } } @@ -394,5 +333,106 @@ private function castValue($type, $value) return $newVal; } + + /** + * Resolve a reference for a primitive piece of data + * + * @param EntityDataObject $entityObject + * @param $operationElement + * @param $operationElementType + * @param array $operationDataArray + * @throws TestFrameworkException + */ + private function resolvePrimitiveReferenceElement($entityObject, $operationElement, $operationElementType, array &$operationDataArray) + { + $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() + )); + } + } + + /** + * Resolves DataObjects referenced by the operation + * + * @param $entityObject + * @param $operation + * @param $fromArray + * @param $operationElementType + * @param $operationElement + * @param array $operationDataArray + * @throws TestFrameworkException + */ + private function resolveNonPrimitiveReferenceElement($entityObject, $operation, $fromArray, &$operationElementType, $operationElement, array &$operationDataArray) + { + $operationElementProperty = null; + if (strpos($operationElementType, '.') !== false) { + $operationElementComponents = explode('.', $operationElementType); + $operationElementType = $operationElementComponents[0]; + $operationElementProperty = $operationElementComponents[1]; + } + + $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) { + if ($operationElementProperty === null) { + $operationDataSubArray = $this->resolveNonPrimitiveElement( + $entityName, + $operationElement, + $operation, + $fromArray + ); + } else { + $linkedEntityObj = $this->resolveLinkedEntityObject($entityName); + $operationDataSubArray = $linkedEntityObj->getDataByName($operationElementProperty, 0); + + if ($operationDataSubArray === null) { + throw new \Exception( + sprintf('Property %s not found in entity %s \n', $operationElementProperty, $entityName) + ); + } + } + + if ($operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY) { + $operationDataArray[$operationElement->getKey()][] = $operationDataSubArray; + } else { + $operationDataArray[$operationElement->getKey()] = $operationDataSubArray; + } + } + } // @codingStandardsIgnoreEnd }