Skip to content

Commit 779735f

Browse files
authored
MQE-247
1 parent dbc03a9 commit 779735f

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Exceptions;
8+
9+
/**
10+
* Class TestReferenceException
11+
*/
12+
class TestReferenceException extends \Exception
13+
{
14+
/**
15+
* TestReferenceException constructor.
16+
* @param string $message
17+
*/
18+
public function __construct($message)
19+
{
20+
parent::__construct($message);
21+
}
22+
23+
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,28 @@ public function getElements()
5656
return $this->elements;
5757
}
5858

59+
/**
60+
* Checks to see if this section contains any element by the name of elementName
61+
* @param $elementName
62+
* @return bool
63+
*/
64+
public function hasElement($elementName)
65+
{
66+
return array_key_exists($elementName, $this->elements);
67+
}
68+
5969
/**
6070
* Given the name of an element, returns the element object
6171
*
6272
* @param string $elementName
63-
* @return ElementObject
73+
* @return ElementObject | null
6474
*/
6575
public function getElement($elementName)
6676
{
67-
return $this->elements[$elementName];
77+
if ($this->hasElement($elementName)) {
78+
return $this->elements[$elementName];
79+
}
80+
81+
return null;
6882
}
6983
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\FunctionalTestingFramework\Page\Objects\SectionObject;
1313
use Magento\FunctionalTestingFramework\Page\Handlers\PageObjectHandler;
1414
use Magento\FunctionalTestingFramework\Page\Handlers\SectionObjectHandler;
15+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
1516

1617
/**
1718
* Class ActionObject
@@ -321,6 +322,9 @@ private function findAndReplaceReferences($objectHandler, $inputString)
321322
break;
322323
case SectionObject::class:
323324
list(,$objField) = $this->stripAndSplitReference($match);
325+
if ($obj->getElement($objField) == null) {
326+
throw new TestReferenceException("Could not resolve entity reference " . $inputString);
327+
}
324328
$parameterized = $obj->getElement($objField)->isParameterized();
325329
$replacement = $obj->getElement($objField)->getLocator();
326330
$this->timeout = $obj->getElement($objField)->getTimeout();
@@ -347,7 +351,7 @@ private function findAndReplaceReferences($objectHandler, $inputString)
347351
if ($replacement == null && get_class($objectHandler) != DataObjectHandler::class) {
348352
return $this->findAndReplaceReferences(DataObjectHandler::getInstance(), $outputString);
349353
} elseif ($replacement == null) {
350-
throw new \Exception("Could not resolve entity reference " . $inputString);
354+
throw new TestReferenceException("Could not resolve entity reference " . $inputString);
351355
}
352356

353357
//If Page or Section's Element is has parameterized = true attribute, attempt to do parameter replacement.
@@ -373,12 +377,12 @@ private function matchParameterReferences($reference, $parameters)
373377
{
374378
preg_match_all('/{{[\w.]+}}/', $reference, $varMatches);
375379
if (count($varMatches[0]) > count($parameters)) {
376-
throw new \Exception(
380+
throw new TestReferenceException(
377381
"Parameter Resolution Failed: Not enough parameters given for reference " .
378382
$reference . ". Parameters Given: " . implode(",", $parameters)
379383
);
380384
} elseif (count($varMatches[0]) < count($parameters)) {
381-
throw new \Exception(
385+
throw new TestReferenceException(
382386
"Parameter Resolution Failed: Too many parameters given for reference " .
383387
$reference . ". Parameters Given: " . implode(",", $parameters)
384388
);

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use FilesystemIterator;
1010
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
11+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
1112
use Magento\FunctionalTestingFramework\Test\Handlers\CestObjectHandler;
1213
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
1314
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
@@ -63,7 +64,7 @@ private function rmdirRecursive($directory)
6364
{
6465
$it = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS);
6566

66-
while($it->valid()) {
67+
while ($it->valid()) {
6768
$path = $directory . DIRECTORY_SEPARATOR . $it->getFilename();
6869
if ($it->isDir()) {
6970
$this->rmDirRecursive($path);
@@ -85,7 +86,7 @@ private function rmdirRecursive($directory)
8586
public static function getInstance()
8687
{
8788
if (!self::$testGenerator) {
88-
self::$testGenerator = new TestGenerator(TESTS_MODULE_PATH . DIRECTORY_SEPARATOR . "_generated");
89+
self::$testGenerator = new TestGenerator(TESTS_MODULE_PATH . DIRECTORY_SEPARATOR . "_generated");
8990
}
9091

9192
return self::$testGenerator;
@@ -152,8 +153,12 @@ private function assembleCestPhp($cestObject)
152153
$classAnnotationsPhp = $this->generateClassAnnotationsPhp($cestObject->getAnnotations());
153154
$className = $cestObject->getName();
154155
$className = str_replace(' ', '', $className);
155-
$hookPhp = $this->generateHooksPhp($cestObject->getHooks());
156-
$testsPhp = $this->generateTestsPhp($cestObject->getTests());
156+
try {
157+
$hookPhp = $this->generateHooksPhp($cestObject->getHooks());
158+
$testsPhp = $this->generateTestsPhp($cestObject->getTests());
159+
} catch (TestReferenceException $e) {
160+
throw new TestReferenceException($e->getMessage(). " in Cest \"" . $cestObject->getName() . "\"");
161+
}
157162

158163
$cestPhp = "<?php\n";
159164
$cestPhp .= "namespace Magento\AcceptanceTest\Backend;\n\n";
@@ -376,7 +381,7 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
376381
foreach ($params as $param) {
377382
$paramsWithUniqueness[] = $this->addUniquenessFunctionCall($param);
378383
}
379-
$parameterArray = '[' . implode(',', $paramsWithUniqueness) .']';
384+
$parameterArray = '[' . implode(',', $paramsWithUniqueness) . ']';
380385
}
381386

382387
if (isset($customActionAttributes['requiredAction'])) {
@@ -865,7 +870,15 @@ private function generateHooksPhp($hookObjects)
865870
}
866871
}
867872

868-
$steps = $this->generateStepsPhp($hookObject->getActions(), $hookObject->getCustomData(), $createData);
873+
try {
874+
$steps = $this->generateStepsPhp(
875+
$hookObject->getActions(),
876+
$hookObject->getCustomData(),
877+
$createData
878+
);
879+
} catch (TestReferenceException $e) {
880+
throw new TestReferenceException($e->getMessage() . " in Element \"" . $type . "\"");
881+
}
869882

870883
if ($type == "after") {
871884
$hooks .= sprintf("\tpublic function _after(%s)\n", $dependencies);
@@ -988,7 +1001,11 @@ private function generateTestsPhp($testsObject)
9881001
$testName = str_replace(' ', '', $testName);
9891002
$testAnnotations = $this->generateTestAnnotationsPhp($test->getAnnotations());
9901003
$dependencies = 'AcceptanceTester $I';
991-
$steps = $this->generateStepsPhp($test->getOrderedActions(), $test->getCustomData());
1004+
try {
1005+
$steps = $this->generateStepsPhp($test->getOrderedActions(), $test->getCustomData());
1006+
} catch (TestReferenceException $e) {
1007+
throw new TestReferenceException($e->getMessage() . " in Test \"" . $test->getName() . "\"");
1008+
}
9921009

9931010
$testPhp .= $testAnnotations;
9941011
$testPhp .= sprintf("\tpublic function %s(%s)\n", $testName, $dependencies);
@@ -1014,7 +1031,7 @@ private function addUniquenessFunctionCall($input)
10141031
{
10151032
$output = '';
10161033

1017-
preg_match('/' . EntityDataObject::CEST_UNIQUE_FUNCTION .'\("[\w]+"\)/', $input, $matches);
1034+
preg_match('/' . EntityDataObject::CEST_UNIQUE_FUNCTION . '\("[\w]+"\)/', $input, $matches);
10181035
if (!empty($matches)) {
10191036
$parts = preg_split('/' . EntityDataObject::CEST_UNIQUE_FUNCTION . '\("[\w]+"\)/', $input, -1);
10201037
for ($i = 0; $i < count($parts); $i++) {
@@ -1082,6 +1099,7 @@ private function addDollarSign($input)
10821099
}
10831100

10841101
// @codingStandardsIgnoreStart
1102+
10851103
/**
10861104
* Wrap parameters into a function call.
10871105
*

0 commit comments

Comments
 (0)