Skip to content

Commit bdcde5c

Browse files
Manjusha.SManjusha.S
authored andcommitted
ACQE-2580: Wrote unit test
1 parent fcd4967 commit bdcde5c

File tree

2 files changed

+128
-19
lines changed

2 files changed

+128
-19
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+
$this->expectException(TestFrameworkException::class);
350+
$testGeneratorObject->throwExceptionIfDuplicateArgumentsFound($fileContents);
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($fileContents);
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: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,34 @@ public function createAllTestFiles($testManifest = null, $testsToIgnore = null)
247247
$this->createCestFile($testPhpFile[1], $testPhpFile[0]);
248248
}
249249
}
250+
/**
251+
* Throw exception if duplicate arguments found
252+
*
253+
* @param string $fileContents
254+
* @return void
255+
* @throws TestFrameworkException
256+
*/
257+
public function throwExceptionIfDuplicateArgumentsFound(string $fileContents)
258+
{
259+
// Throw exception if duplicate arguments found in helper or actionGroup
260+
$fileToArr = explode("\n", $fileContents);
261+
$argArr = [];
262+
foreach ($fileToArr as $key => $fileVal) {
263+
if (!empty(strpos($fileVal, "<argument name"))) {
264+
$argArr[$key] = explode(" ", trim($fileVal))[1];
265+
}
266+
}
267+
foreach ($argArr as $key => $arrVal) {
268+
if (!empty($argArr[$key + 1]) && $argArr[$key + 1] === $arrVal) {
269+
$err[] = 'Duplicate argument name '.$arrVal.' not allowed in helper or actionGroup';
270+
throw new TestFrameworkException(implode(PHP_EOL, $err));
271+
}
272+
if (!empty($argArr[$key + 2]) && $argArr[$key + 2] === $arrVal) {
273+
$err[] = 'Duplicate argument name '.$arrVal.' not allowed in helper or actionGroup';
274+
throw new TestFrameworkException(implode(PHP_EOL, $err));
275+
}
276+
}
277+
}
250278

251279
/**
252280
* Assemble the entire PHP string for a single Test based on a Test Object.
@@ -259,27 +287,9 @@ public function createAllTestFiles($testManifest = null, $testsToIgnore = null)
259287
*/
260288
public function assembleTestPhp($testObject)
261289
{
262-
// Throw exception if duplicate arguments found in helper or actionGroup
263290
if (!empty($testObject->getFilename()) && file_exists($testObject->getFilename())) {
264-
$fileToArr = explode("\n", file_get_contents($testObject->getFilename()));
265-
$argArr = [];
266-
foreach ($fileToArr as $key => $fileVal) {
267-
if (!empty(strpos($fileVal, "<argument name"))) {
268-
$argArr[$key] = explode(" ", trim($fileVal))[1];
269-
}
270-
}
271-
foreach ($argArr as $key => $arrVal) {
272-
if (!empty($argArr[$key + 1]) && $argArr[$key + 1] === $arrVal) {
273-
$err[] = 'Duplicate argument name '.$arrVal.' not allowed in helper or actionGroup';
274-
throw new TestFrameworkException(implode(PHP_EOL, $err));
275-
}
276-
if (!empty($argArr[$key + 2]) && $argArr[$key + 2] === $arrVal) {
277-
$err[] = 'Duplicate argument name '.$arrVal.' not allowed in helper or actionGroup';
278-
throw new TestFrameworkException(implode(PHP_EOL, $err));
279-
}
280-
}
291+
$this->throwExceptionIfDuplicateArgumentsFound(file_get_contents($testObject->getFilename()));
281292
}
282-
283293
$this->customHelpers = [];
284294
$usePhp = $this->generateUseStatementsPhp();
285295

0 commit comments

Comments
 (0)