Skip to content

Commit 96e4d9a

Browse files
authored
Merge pull request #314 from magento-gl/ACQE-2580
Acqe 2580
2 parents 7b09e76 + 8506c64 commit 96e4d9a

File tree

2 files changed

+170
-1
lines changed

2 files changed

+170
-1
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use ReflectionProperty;
2525
use tests\unit\Util\MagentoTestCase;
2626
use tests\unit\Util\TestLoggingUtil;
27+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
2728

2829
class TestGeneratorTest extends MagentoTestCase
2930
{
@@ -299,6 +300,104 @@ function ($filename) use (&$generatedTests) {
299300
$this->assertArrayNotHasKey('test2Cest', $generatedTests);
300301
}
301302

303+
/**
304+
* Test for exception thrown when duplicate arguments found
305+
*
306+
* @return void
307+
* @throws TestFrameworkException
308+
*/
309+
public function testIfExceptionThrownWhenDuplicateArgumentsFound()
310+
{
311+
$fileContents = '<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
312+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
313+
<actionGroup name="ActionGroupReturningValueActionGroup">
314+
<arguments>
315+
<argument name="count" type="string"/>
316+
<argument name="count" type="string"/>
317+
</arguments>
318+
<grabMultiple selector="selector" stepKey="grabProducts1"/>
319+
<assertCount stepKey="assertCount">
320+
<expectedResult type="int">{{count}}</expectedResult>
321+
<actualResult type="variable">grabProducts1</actualResult>
322+
</assertCount>
323+
<return value="{$grabProducts1}" stepKey="returnProducts1"/>
324+
</actionGroup>
325+
</actionGroups>';
326+
$actionInput = 'fakeInput';
327+
$actionObject = new ActionObject('fakeAction', 'comment', [
328+
'userInput' => $actionInput
329+
]);
330+
$annotation1 = ['group' => ['someGroupValue']];
331+
332+
$test1 = new TestObject(
333+
'test1',
334+
['fakeAction' => $actionObject],
335+
$annotation1,
336+
[],
337+
'filename'
338+
);
339+
$annotation2 = ['group' => ['someOtherGroupValue']];
340+
341+
$test2 = new TestObject(
342+
'test2',
343+
['fakeAction' => $actionObject],
344+
$annotation2,
345+
[],
346+
'filename'
347+
);
348+
$testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $test1, 'test2' => $test2]);
349+
$result = $testGeneratorObject->throwExceptionIfDuplicateArgumentsFound($testGeneratorObject);
350+
$this->assertEquals($result, "");
351+
}
352+
353+
/**
354+
* Test for exception not thrown when duplicate arguments not found
355+
*
356+
* @return void
357+
*/
358+
public function testIfExceptionNotThrownWhenDuplicateArgumentsNotFound()
359+
{
360+
$fileContents = '<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
361+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
362+
<actionGroup name="ActionGroupReturningValueActionGroup">
363+
<arguments>
364+
<argument name="count" type="string"/>
365+
</arguments>
366+
<grabMultiple selector="selector" stepKey="grabProducts1"/>
367+
<assertCount stepKey="assertCount">
368+
<expectedResult type="int">{{count}}</expectedResult>
369+
<actualResult type="variable">grabProducts1</actualResult>
370+
</assertCount>
371+
<return value="{$grabProducts1}" stepKey="returnProducts1"/>
372+
</actionGroup>
373+
</actionGroups>';
374+
$actionInput = 'fakeInput';
375+
$actionObject = new ActionObject('fakeAction', 'comment', [
376+
'userInput' => $actionInput
377+
]);
378+
$annotation1 = ['group' => ['someGroupValue']];
379+
380+
$test1 = new TestObject(
381+
'test1',
382+
['fakeAction' => $actionObject],
383+
$annotation1,
384+
[],
385+
'filename'
386+
);
387+
$annotation2 = ['group' => ['someOtherGroupValue']];
388+
389+
$test2 = new TestObject(
390+
'test2',
391+
['fakeAction' => $actionObject],
392+
$annotation2,
393+
[],
394+
'filename'
395+
);
396+
$testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $test1, 'test2' => $test2]);
397+
$result = $testGeneratorObject->throwExceptionIfDuplicateArgumentsFound($testGeneratorObject);
398+
$this->assertEquals($result, "");
399+
}
400+
302401
/**
303402
* Tests that TestGenerator createAllTestFiles correctly filters based on group.
304403
*

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,73 @@ public function createAllTestFiles($testManifest = null, $testsToIgnore = null)
247247
$this->createCestFile($testPhpFile[1], $testPhpFile[0]);
248248
}
249249
}
250+
251+
/**
252+
* Throw exception if duplicate arguments found
253+
* @param TestObject $testObject
254+
* @return void
255+
* @throws TestFrameworkException
256+
*/
257+
public function throwExceptionIfDuplicateArgumentsFound($testObject): void
258+
{
259+
if (!($testObject instanceof TestObject)) {
260+
return;
261+
}
262+
$fileName = $testObject->getFilename();
263+
if (!empty($fileName) && file_exists($fileName)) {
264+
return;
265+
}
266+
$fileContents = file_get_contents($fileName);
267+
$parsedSteps = $testObject->getUnresolvedSteps();
268+
foreach ($parsedSteps as $parsedStep) {
269+
if ($parsedStep->getType() !== 'actionGroup' && $parsedStep->getType() !== 'helper') {
270+
continue;
271+
}
272+
$attributesActions = $parsedStep->getCustomActionAttributes();
273+
if (!key_exists('arguments', $attributesActions)) {
274+
continue;
275+
}
276+
$arguments = $attributesActions['arguments'];
277+
$stepKey = $parsedStep->getStepKey();
278+
279+
$fileToArr = explode("\n", $fileContents);
280+
$actionGroupStart = false;
281+
$argumentArray = [];
282+
foreach ($fileToArr as $fileVal) {
283+
$fileVal = trim($fileVal);
284+
if ((str_contains($fileVal, '<actionGroup') || str_contains($fileVal, '<helper')) &&
285+
str_contains($fileVal, $stepKey)) {
286+
$actionGroupStart = true;
287+
continue;
288+
}
289+
if (str_contains($fileVal, '</actionGroup') || str_contains($fileVal, '</helper')) {
290+
foreach ($arguments as $argumentName => $argumentValue) {
291+
$argumentCounter = 0;
292+
foreach ($argumentArray as $rawArgument) {
293+
if (str_contains($rawArgument, '<argument') &&
294+
str_contains($rawArgument, 'name="'.$argumentName.'"')) {
295+
$argumentCounter++;
296+
}
297+
if ($argumentCounter > 1) {
298+
$err[] = sprintf(
299+
'Duplicate argument(%s) for stepKey: %s in test file: %s',
300+
$argumentName,
301+
$stepKey,
302+
$testObject->getFileName()
303+
);
304+
throw new TestFrameworkException(implode(PHP_EOL, $err));
305+
}
306+
}
307+
$actionGroupStart = false;
308+
$argumentArray = [];
309+
}
310+
}
311+
if ($actionGroupStart) {
312+
$argumentArray[] = $fileVal;
313+
}
314+
}
315+
}
316+
}
250317

251318
/**
252319
* Assemble the entire PHP string for a single Test based on a Test Object.
@@ -259,6 +326,10 @@ public function createAllTestFiles($testManifest = null, $testsToIgnore = null)
259326
*/
260327
public function assembleTestPhp($testObject)
261328
{
329+
if (!empty($testObject->getFilename()) && file_exists($testObject->getFilename())) {
330+
$fileContents = file_get_contents($testObject->getFilename());
331+
$this->throwExceptionIfDuplicateArgumentsFound($fileContents, $testObject->getFilename());
332+
}
262333
$this->customHelpers = [];
263334
$usePhp = $this->generateUseStatementsPhp();
264335

@@ -342,7 +413,6 @@ private function assembleAllTestPhp($testManifest, array $testsToIgnore)
342413
foreach ($filters as $filter) {
343414
$filter->filter($testObjects);
344415
}
345-
346416
foreach ($testObjects as $test) {
347417
try {
348418
// Reset flag for new test

0 commit comments

Comments
 (0)