Skip to content

Commit 3a59e26

Browse files
committed
MQE-590: Fix PHP Mess Detector Errors
- ActionObject refactor - EntityDataObject refactor around uniqueness - marked OperationDataArray as todo - added getPrioritizedSelector to ElementObject - no more errors
1 parent 6990bcf commit 3a59e26

File tree

4 files changed

+113
-74
lines changed

4 files changed

+113
-74
lines changed

src/Magento/FunctionalTestingFramework/DataGenerator/Objects/EntityDataObject.php

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -147,58 +147,76 @@ public function getDataByName($dataName, $uniDataFormat)
147147
if (null === $uniData || $uniDataFormat == self::NO_UNIQUE_PROCESS) {
148148
return $this->data[$name];
149149
}
150-
151-
switch ($uniDataFormat) {
152-
case self::SUITE_UNIQUE_VALUE:
153-
if (!function_exists(self::SUITE_UNIQUE_FUNCTION)) {
154-
throw new TestFrameworkException(
155-
sprintf(
156-
'Unique data format value: %s can only be used when running cests.\n',
157-
$uniDataFormat
158-
)
159-
);
160-
} elseif ($uniData == 'prefix') {
161-
return msqs($this->getName()) . $this->data[$name];
162-
} else { // $uniData == 'suffix'
163-
return $this->data[$name] . msqs($this->getName());
164-
}
165-
break;
166-
case self::CEST_UNIQUE_VALUE:
167-
if (!function_exists(self::CEST_UNIQUE_FUNCTION)) {
168-
throw new TestFrameworkException(
169-
sprintf(
170-
'Unique data format value: %s can only be used when running cests.\n',
171-
$uniDataFormat
172-
)
173-
);
174-
} elseif ($uniData == 'prefix') {
175-
return msq($this->getName()) . $this->data[$name];
176-
} else { // $uniData == 'suffix'
177-
return $this->data[$name] . msq($this->getName());
178-
}
179-
break;
180-
case self::SUITE_UNIQUE_NOTATION:
181-
if ($uniData == 'prefix') {
182-
return self::SUITE_UNIQUE_FUNCTION . '("' . $this->getName() . '")' . $this->data[$name];
183-
} else { // $uniData == 'suffix'
184-
return $this->data[$name] . self::SUITE_UNIQUE_FUNCTION . '("' . $this->getName() . '")';
185-
}
186-
break;
187-
case self::CEST_UNIQUE_NOTATION:
188-
if ($uniData == 'prefix') {
189-
return self::CEST_UNIQUE_FUNCTION . '("' . $this->getName() . '")' . $this->data[$name];
190-
} else { // $uniData == 'suffix'
191-
return $this->data[$name] . self::CEST_UNIQUE_FUNCTION . '("' . $this->getName() . '")';
192-
}
193-
break;
194-
default:
195-
break;
196-
}
150+
return $this->formatUniqueData($name, $uniData, $uniDataFormat);
197151
}
152+
return null;
153+
}
198154

155+
/**
156+
* Formats and returns data based on given uniqueDataFormat and prefix/suffix.
157+
* @param string $name
158+
* @param string $uniqueData
159+
* @param string $uniqueDataFormat
160+
* @return null|string
161+
*/
162+
private function formatUniqueData($name, $uniqueData, $uniqueDataFormat)
163+
{
164+
switch ($uniqueDataFormat) {
165+
case self::SUITE_UNIQUE_VALUE:
166+
$this->checkUniquenessFunctionExists(self::SUITE_UNIQUE_FUNCTION, $uniqueDataFormat);
167+
if ($uniqueData == 'prefix') {
168+
return msqs($this->getName()) . $this->data[$name];
169+
} else { // $uniData == 'suffix'
170+
return $this->data[$name] . msqs($this->getName());
171+
}
172+
break;
173+
case self::CEST_UNIQUE_VALUE:
174+
$this->checkUniquenessFunctionExists(self::CEST_UNIQUE_FUNCTION, $uniqueDataFormat);
175+
if ($uniqueData == 'prefix') {
176+
return msq($this->getName()) . $this->data[$name];
177+
} else { // $uniqueData == 'suffix'
178+
return $this->data[$name] . msq($this->getName());
179+
}
180+
break;
181+
case self::SUITE_UNIQUE_NOTATION:
182+
if ($uniqueData == 'prefix') {
183+
return self::SUITE_UNIQUE_FUNCTION . '("' . $this->getName() . '")' . $this->data[$name];
184+
} else { // $uniqueData == 'suffix'
185+
return $this->data[$name] . self::SUITE_UNIQUE_FUNCTION . '("' . $this->getName() . '")';
186+
}
187+
break;
188+
case self::CEST_UNIQUE_NOTATION:
189+
if ($uniqueData == 'prefix') {
190+
return self::CEST_UNIQUE_FUNCTION . '("' . $this->getName() . '")' . $this->data[$name];
191+
} else { // $uniqueData == 'suffix'
192+
return $this->data[$name] . self::CEST_UNIQUE_FUNCTION . '("' . $this->getName() . '")';
193+
}
194+
break;
195+
default:
196+
break;
197+
}
199198
return null;
200199
}
201200

201+
/**
202+
* Performs a check that the given uniqueness function exists, throws an exception if it doesn't.
203+
* @param string $function
204+
* @param string $uniqueDataFormat
205+
* @return void
206+
* @throws TestFrameworkException
207+
*/
208+
private function checkUniquenessFunctionExists($function, $uniqueDataFormat)
209+
{
210+
if (!function_exists($function)) {
211+
throw new TestFrameworkException(
212+
sprintf(
213+
'Unique data format value: %s can only be used when running cests.\n',
214+
$uniqueDataFormat
215+
)
216+
);
217+
}
218+
}
219+
202220
/**
203221
* Function which returns a reference to another entity (e.g. a var with entity="category" field="id" returns as
204222
* category->id)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ public function __construct($dependentEntities = null)
6464
* @param bool $fromArray
6565
* @return array
6666
* @throws \Exception
67+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
6768
*/
6869
public function resolveOperationDataArray($entityObject, $operationMetadata, $operation, $fromArray = false)
6970
{
71+
//TODO: Refactor to reduce Cyclomatic Complexity, remove SupressWarning accordingly.
7072
$operationDataArray = [];
7173
self::incrementSequence($entityObject->getName());
7274

src/Magento/FunctionalTestingFramework/Page/Objects/ElementObject.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ public function getLocatorFunction()
125125
return $this->locatorFunction;
126126
}
127127

128+
/**
129+
* Returns selector if not null, otherwise returns locatorFunction
130+
*
131+
* @return string
132+
*/
133+
public function getPrioritizedSelector()
134+
{
135+
return $this->selector ?: $this->locatorFunction;
136+
}
137+
128138
/**
129139
* Returns an integer representing an element's timeout
130140
*

src/Magento/FunctionalTestingFramework/Test/Objects/ActionObject.php

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,6 @@ private function stripAndReturnParameters($reference)
304304
private function findAndReplaceReferences($objectHandler, $inputString)
305305
{
306306
preg_match_all(ActionObject::ACTION_ATTRIBUTE_VARIABLE_REGEX_PATTERN, $inputString, $matches);
307-
if (empty($matches[0])) {
308-
return $inputString;
309-
}
310307

311308
$outputString = $inputString;
312309

@@ -329,27 +326,11 @@ private function findAndReplaceReferences($objectHandler, $inputString)
329326
throw new TestReferenceException("Could not resolve entity reference " . $inputString);
330327
}
331328
$parameterized = $obj->getElement($objField)->isParameterized();
332-
// If no Selector is defined, assume element has LocatorFunction
333-
$replacement = $obj->getElement($objField)->getSelector() ?:
334-
$obj->getElement($objField)->getLocatorFunction();
329+
$replacement = $obj->getElement($objField)->getPrioritizedSelector();
335330
$this->timeout = $obj->getElement($objField)->getTimeout();
336331
break;
337-
case (get_class($obj) == EntityDataObject::class):
338-
list(,$objField) = $this->stripAndSplitReference($match);
339-
340-
if (strpos($objField, '[') == true) {
341-
// Access <array>...</array>
342-
$parts = explode('[', $objField);
343-
$name = $parts[0];
344-
$index = str_replace(']', '', $parts[1]);
345-
$replacement = $obj->getDataByName(
346-
$name,
347-
EntityDataObject::CEST_UNIQUE_NOTATION
348-
)[$index];
349-
} else {
350-
// Access <data></data>
351-
$replacement = $obj->getDataByName($objField, EntityDataObject::CEST_UNIQUE_NOTATION);
352-
}
332+
case EntityDataObject::class:
333+
$replacement = $this->resolveEntityDataObjectReference($obj, $match);
353334
break;
354335
}
355336

@@ -359,16 +340,44 @@ private function findAndReplaceReferences($objectHandler, $inputString)
359340
throw new TestReferenceException("Could not resolve entity reference " . $inputString);
360341
}
361342

362-
// If Page or Section's Element has the parameterized = true attribute, attempt to do parameter replacement
363-
if ($parameterized) {
364-
$parameterList = $this->stripAndReturnParameters($match);
365-
$replacement = $this->matchParameterReferences($replacement, $parameterList);
366-
}
343+
$replacement = $this->resolveParameterization($parameterized, $replacement, $match);
367344
$outputString = str_replace($match, $replacement, $outputString);
368345
}
369346
return $outputString;
370347
}
371348

349+
/**
350+
* Gets the object's dataByName with given $match, differentiating behavior between <array> and <data> nodes.
351+
* @param $obj
352+
* @param $match
353+
* @return string
354+
*/
355+
private function resolveEntityDataObjectReference($obj, $match)
356+
{
357+
list(,$objField) = $this->stripAndSplitReference($match);
358+
359+
if (strpos($objField, '[') == true) {
360+
// Access <array>...</array>
361+
$parts = explode('[', $objField);
362+
$name = $parts[0];
363+
$index = str_replace(']', '', $parts[1]);
364+
return $obj->getDataByName($name, EntityDataObject::CEST_UNIQUE_NOTATION)[$index];
365+
} else {
366+
// Access <data></data>
367+
return $obj->getDataByName($objField, EntityDataObject::CEST_UNIQUE_NOTATION);
368+
}
369+
}
370+
371+
private function resolveParameterization($isParameterized, $replacement, $match)
372+
{
373+
if ($isParameterized) {
374+
$parameterList = $this->stripAndReturnParameters($match);
375+
return $this->matchParameterReferences($replacement, $parameterList);
376+
} else {
377+
return $replacement;
378+
}
379+
}
380+
372381
/**
373382
* Finds all {{var}} occurrences in reference, and replaces them in sequence with parameters list given.
374383
* Parameter list given is also resolved, attempting to match {{data.field}} references.

0 commit comments

Comments
 (0)