Skip to content

Commit 94afc74

Browse files
authored
MQE-1256: "Merge" works different for tests and action groups
- Actiongroup and test dom classes now check for duplicate child <test> and <actionGroup> elements.
1 parent de1ab89 commit 94afc74

File tree

6 files changed

+126
-12
lines changed

6 files changed

+126
-12
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Test/Config/ActionGroupDomTest.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testActionGroupDomStepKeyValidation()
2525
</actionGroups>";
2626

2727
$exceptionCollector = new ExceptionCollector();
28-
$actionDom = new ActionGroupDom($sampleXml, 'dupeStepKeyActionGroup.xml', $exceptionCollector);
28+
new ActionGroupDom($sampleXml, 'dupeStepKeyActionGroup.xml', $exceptionCollector);
2929

3030
$this->expectException(\Exception::class);
3131
$exceptionCollector->throwException();
@@ -47,4 +47,25 @@ public function testActionGroupDomInvalidXmlValidation()
4747
$this->expectExceptionMessage("XML Parse Error: invalid.xml\n");
4848
new ActionGroupDom($sampleXml, 'invalid.xml', $exceptionCollector);
4949
}
50+
51+
/**
52+
* Test detection of two ActionGroups with the same Name in the same file.
53+
*/
54+
public function testActionGroupDomDuplicateActionGroupsValidation()
55+
{
56+
$sampleXml = '<actionGroups>
57+
<actionGroup name="actionGroupName">
58+
<wait time="1" stepKey="key1" />
59+
</actionGroup>
60+
<actionGroup name="actionGroupName">
61+
<wait time="1" stepKey="key1" />
62+
</actionGroup>
63+
</actionGroups>';
64+
65+
$exceptionCollector = new ExceptionCollector();
66+
new ActionGroupDom($sampleXml, 'dupeNameActionGroup.xml', $exceptionCollector);
67+
$this->expectException(\Exception::class);
68+
$this->expectExceptionMessageRegExp("/name: actionGroupName is used more than once./");
69+
$exceptionCollector->throwException();
70+
}
5071
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Tests\unit\Magento\FunctionalTestFramework\Test\Config;
7+
8+
use Magento\FunctionalTestingFramework\Exceptions\Collector\ExceptionCollector;
9+
use Magento\FunctionalTestingFramework\Config\Dom\ValidationException;
10+
use Magento\FunctionalTestingFramework\Test\Config\ActionGroupDom;
11+
use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
12+
13+
class DomTest extends MagentoTestCase
14+
{
15+
/**
16+
* Test Test duplicate step key validation
17+
*/
18+
public function testTestStepKeyDuplicateValidation()
19+
{
20+
$sampleXml = '<tests>
21+
<test name="testName">
22+
<comment userInput="step1" stepKey="key1"/>
23+
<comment userInput="step2" stepKey="key1"/>
24+
</test>
25+
</tests>';
26+
27+
$exceptionCollector = new ExceptionCollector();
28+
new ActionGroupDom($sampleXml, 'dupeStepKeyTest.xml', $exceptionCollector);
29+
30+
$this->expectException(\Exception::class);
31+
$this->expectExceptionMessageRegExp("/stepKey: key1 is used more than once. \(Parent: testName\)/");
32+
$exceptionCollector->throwException();
33+
}
34+
35+
/**
36+
* Test detection of two Tests with the same Name in the same file.
37+
*/
38+
public function testTestNameDuplicateValidation()
39+
{
40+
$sampleXml = '<tests>
41+
<test name="testName">
42+
<comment userInput="step1" stepKey="key1"/>
43+
</test>
44+
<test name="testName">
45+
<comment userInput="step1" stepKey="key1"/>
46+
</test>
47+
</tests>';
48+
49+
$exceptionCollector = new ExceptionCollector();
50+
new ActionGroupDom($sampleXml, 'dupeTestsTest.xml', $exceptionCollector);
51+
$this->expectException(\Exception::class);
52+
$this->expectExceptionMessageRegExp("/name: testName is used more than once./");
53+
$exceptionCollector->throwException();
54+
}
55+
}

src/Magento/FunctionalTestingFramework/Config/MftfDom.php

+14
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,18 @@ public function merge($xml, $filename = null, $exceptionCollector = null)
5959
$dom = $this->initDom($xml, $filename, $exceptionCollector);
6060
$this->mergeNode($dom->documentElement, '');
6161
}
62+
63+
/**
64+
* Checks if the filename given ends with the correct suffix.
65+
* @param string $filename
66+
* @param string $suffix
67+
* @return boolean
68+
*/
69+
public function checkFilenameSuffix($filename, $suffix)
70+
{
71+
if (substr_compare($filename, $suffix, -strlen($suffix)) === 0) {
72+
return true;
73+
}
74+
return false;
75+
}
6276
}

src/Magento/FunctionalTestingFramework/Test/Config/ActionGroupDom.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ public function initDom($xml, $filename = null)
2929
{
3030
$dom = parent::initDom($xml, $filename);
3131

32-
if (strpos($filename, self::ACTION_GROUP_FILE_NAME_ENDING)) {
32+
if ($this->checkFilenameSuffix($filename, self::ACTION_GROUP_FILE_NAME_ENDING)) {
33+
$actionGroupsNode = $dom->getElementsByTagName('actionGroups')[0];
3334
$actionGroupNodes = $dom->getElementsByTagName('actionGroup');
35+
36+
$this->testsValidationUtil->validateChildUniqueness(
37+
$actionGroupsNode,
38+
$filename,
39+
null
40+
);
3441
foreach ($actionGroupNodes as $actionGroupNode) {
3542
/** @var \DOMElement $actionGroupNode */
3643
$actionGroupNode->setAttribute(self::TEST_META_FILENAME_ATTRIBUTE, $filename);
37-
$this->validationUtil->validateChildUniqueness(
44+
$this->actionsValidationUtil->validateChildUniqueness(
3845
$actionGroupNode,
3946
$filename,
4047
$actionGroupNode->getAttribute(self::ACTION_GROUP_META_NAME_ATTRIBUTE)

src/Magento/FunctionalTestingFramework/Test/Config/Dom.php

+21-8
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@
1919
*/
2020
class Dom extends \Magento\FunctionalTestingFramework\Config\MftfDom
2121
{
22-
const TEST_FILE_NAME_ENDING = 'Test';
22+
const TEST_FILE_NAME_ENDING = 'Test.xml';
2323
const TEST_META_FILENAME_ATTRIBUTE = 'filename';
2424
const TEST_META_NAME_ATTRIBUTE = 'name';
2525
const TEST_HOOK_NAMES = ["after", "before"];
2626
const TEST_MERGE_POINTER_BEFORE = "insertBefore";
2727
const TEST_MERGE_POINTER_AFTER = "insertAfter";
2828

2929
/**
30-
* NodeValidationUtil
30+
* NodeValidationUtil for test actions
3131
* @var DuplicateNodeValidationUtil
3232
*/
33-
protected $validationUtil;
33+
protected $actionsValidationUtil;
34+
35+
/**
36+
* NodeValidationUtil for test names
37+
* @var DuplicateNodeValidationUtil
38+
*/
39+
protected $testsValidationUtil;
3440

3541
/**
3642
* ExceptionCollector
@@ -57,7 +63,8 @@ public function __construct(
5763
$schemaFile = null,
5864
$errorFormat = self::ERROR_FORMAT_DEFAULT
5965
) {
60-
$this->validationUtil = new DuplicateNodeValidationUtil('stepKey', $exceptionCollector);
66+
$this->actionsValidationUtil = new DuplicateNodeValidationUtil('stepKey', $exceptionCollector);
67+
$this->testsValidationUtil = new DuplicateNodeValidationUtil('name', $exceptionCollector);
6168
$this->exceptionCollector = $exceptionCollector;
6269
parent::__construct(
6370
$xml,
@@ -81,8 +88,14 @@ public function initDom($xml, $filename = null)
8188
{
8289
$dom = parent::initDom($xml, $filename);
8390

84-
if (strpos($filename, self::TEST_FILE_NAME_ENDING)) {
91+
if ($this->checkFilenameSuffix($filename, self::TEST_FILE_NAME_ENDING)) {
92+
$testsNode = $dom->getElementsByTagName('tests')[0];
8593
$testNodes = $dom->getElementsByTagName('test');
94+
$this->testsValidationUtil->validateChildUniqueness(
95+
$testsNode,
96+
$filename,
97+
null
98+
);
8699
foreach ($testNodes as $testNode) {
87100
/** @var \DOMElement $testNode */
88101
$testNode->setAttribute(self::TEST_META_FILENAME_ATTRIBUTE, $filename);
@@ -102,7 +115,7 @@ public function initDom($xml, $filename = null)
102115
);
103116
}
104117

105-
$this->validationUtil->validateChildUniqueness(
118+
$this->actionsValidationUtil->validateChildUniqueness(
106119
$testNode,
107120
$filename,
108121
$testNode->getAttribute(self::TEST_META_NAME_ATTRIBUTE)
@@ -111,14 +124,14 @@ public function initDom($xml, $filename = null)
111124
$afterNode = $testNode->getElementsByTagName('after')->item(0);
112125

113126
if (isset($beforeNode)) {
114-
$this->validationUtil->validateChildUniqueness(
127+
$this->actionsValidationUtil->validateChildUniqueness(
115128
$beforeNode,
116129
$filename,
117130
$testNode->getAttribute(self::TEST_META_NAME_ATTRIBUTE) . "/before"
118131
);
119132
}
120133
if (isset($afterNode)) {
121-
$this->validationUtil->validateChildUniqueness(
134+
$this->actionsValidationUtil->validateChildUniqueness(
122135
$afterNode,
123136
$filename,
124137
$testNode->getAttribute(self::TEST_META_NAME_ATTRIBUTE) . "/after"

src/Magento/FunctionalTestingFramework/Util/Validation/DuplicateNodeValidationUtil.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ public function validateChildUniqueness(\DOMElement $parentNode, $filename, $par
6969
$duplicates = array_diff_assoc($keyValues, $withoutDuplicates);
7070
$keyError = "";
7171
foreach ($duplicates as $duplicateKey => $duplicateValue) {
72-
$keyError .= "\t{$this->uniqueKey}: {$duplicateValue} is used more than once. (Parent: {$parentKey})\n";
72+
$keyError .= "\t{$this->uniqueKey}: {$duplicateValue} is used more than once.";
73+
if ($parentKey !== null) {
74+
$keyError .=" (Parent: {$parentKey})";
75+
}
76+
$keyError .= "\n";
7377
}
7478

7579
$errorMsg = "{$type} cannot use {$this->uniqueKey}s more than once.\t\n{$keyError}\tin file: {$filename}";

0 commit comments

Comments
 (0)