Skip to content

Commit a22bd04

Browse files
committed
MQE-1376: [SPIKE] Investigate Self-Documentation for MFTF
- Added unit tests - Updated functions to facilitate unit testing
1 parent e0713ef commit a22bd04

File tree

11 files changed

+369
-45
lines changed

11 files changed

+369
-45
lines changed

dev/tests/_bootstrap.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@
6060
defined('TESTS_BP') || define('TESTS_BP', __DIR__);
6161
defined('TESTS_MODULE_PATH') || define('TESTS_MODULE_PATH', TESTS_BP . $RELATIVE_TESTS_MODULE_PATH);
6262
defined('MAGENTO_BP') || define('MAGENTO_BP', __DIR__);
63+
define('OUTPUT_DIR',
64+
FW_BP .
65+
DIRECTORY_SEPARATOR .
66+
"dev" .
67+
DIRECTORY_SEPARATOR .
68+
"tests" .
69+
DIRECTORY_SEPARATOR .
70+
"unit" .
71+
DIRECTORY_SEPARATOR .
72+
"_output"
73+
);
74+
define('RESOURCE_DIR',
75+
FW_BP .
76+
DIRECTORY_SEPARATOR .
77+
"dev" .
78+
DIRECTORY_SEPARATOR .
79+
"tests" .
80+
DIRECTORY_SEPARATOR .
81+
"unit" .
82+
DIRECTORY_SEPARATOR .
83+
"Resources"
84+
);
6385

6486
$utilDir = DIRECTORY_SEPARATOR . 'Util'. DIRECTORY_SEPARATOR . '*.php';
6587

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\Util;
7+
8+
use AspectMock\Test as AspectMock;
9+
use Magento\FunctionalTestingFramework\Test\Util\ActionGroupAnnotationExtractor;
10+
use PHPUnit\Framework\TestCase;
11+
use tests\unit\Util\TestLoggingUtil;
12+
13+
class ActionGroupAnnotationExtractorTest extends TestCase
14+
{
15+
/**
16+
* Before test functionality
17+
* @return void
18+
*/
19+
public function setUp()
20+
{
21+
TestLoggingUtil::getInstance()->setMockLoggingUtil();
22+
}
23+
24+
/**
25+
* Annotation extractor takes in raw array and condenses it to expected format
26+
*
27+
* @throws \Exception
28+
*/
29+
public function testActionGroupExtractAnnotations()
30+
{
31+
// Test Data
32+
$actionGroupAnnotations = [
33+
"nodeName" => "annotations",
34+
"description" => [
35+
"nodeName" => "description",
36+
"value" => "someDescription"
37+
],
38+
"page" => [
39+
"nodeName" => "page",
40+
"value" => "somePage"
41+
]
42+
];
43+
// Perform Test
44+
$extractor = new ActionGroupAnnotationExtractor();
45+
$returnedAnnotations = $extractor->extractAnnotations($actionGroupAnnotations, "fileName");
46+
47+
// Asserts
48+
49+
$this->assertEquals("somePage", $returnedAnnotations['page']);
50+
$this->assertEquals("someDescription", $returnedAnnotations['description']);
51+
}
52+
53+
/**
54+
* Annotation extractor should throw warning when required annotations are missing
55+
*
56+
* @throws \Exception
57+
*/
58+
public function testActionGroupMissingAnnotations()
59+
{
60+
// Action Group Data, missing page and description
61+
$testAnnotations = [];
62+
// Perform Test
63+
$extractor = new ActionGroupAnnotationExtractor();
64+
AspectMock::double($extractor, ['isCommandDefined' => true]);
65+
$extractor->extractAnnotations($testAnnotations, "fileName");
66+
67+
// Asserts
68+
TestLoggingUtil::getInstance()->validateMockLogStatement(
69+
'warning',
70+
'DEPRECATION: Action Group File fileName is missing required annotations.',
71+
[
72+
'actionGroup' => 'fileName',
73+
'missingAnnotations' => "description, page"
74+
]
75+
);
76+
}
77+
78+
/**
79+
* Annotation extractor should not throw warning when required
80+
* annotations are missing if command is not generate:docs
81+
*
82+
* @throws \Exception
83+
*/
84+
public function testActionGroupMissingAnnotationsNoWarning()
85+
{
86+
// Action Group Data, missing page and description
87+
$testAnnotations = [];
88+
// Perform Test
89+
$extractor = new ActionGroupAnnotationExtractor();
90+
$extractor->extractAnnotations($testAnnotations, "fileName");
91+
92+
// Asserts
93+
TestLoggingUtil::getInstance()->validateMockLogEmpty();
94+
}
95+
96+
/**
97+
* After class functionality
98+
* @return void
99+
*/
100+
public static function tearDownAfterClass()
101+
{
102+
TestLoggingUtil::getInstance()->clearMockLoggingUtil();
103+
}
104+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Tests\unit\Magento\FunctionalTestFramework\Test\Handlers;
8+
9+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
10+
use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
11+
use Magento\FunctionalTestingFramework\Util\DocGenerator;
12+
use tests\unit\Util\ActionGroupObjectBuilder;
13+
14+
class DocGeneratorTest extends MagentoTestCase
15+
{
16+
const DOC_FILENAME = "documentation";
17+
18+
/**
19+
* Basic test to check creation of documentation
20+
*
21+
* @throws \Exception
22+
*/
23+
public function testBasicCreateDocumentation()
24+
{
25+
$annotations = [
26+
"page" => "somePage",
27+
"description" => "someDescription"
28+
];
29+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
30+
->withAnnotations($annotations)
31+
->withFilenames(["filename"])
32+
->build();
33+
DocGenerator::getInstance()->createDocumentation(
34+
[$actionGroupUnderTest->getName() => $actionGroupUnderTest],
35+
OUTPUT_DIR,
36+
true
37+
);
38+
39+
$docFile = OUTPUT_DIR . DIRECTORY_SEPARATOR . self::DOC_FILENAME . ".md";
40+
41+
$this->assertTrue(file_exists($docFile));
42+
43+
$this->assertFileEquals(
44+
RESOURCE_DIR . DIRECTORY_SEPARATOR . "basicDocumentation.txt",
45+
$docFile
46+
);
47+
}
48+
49+
/**
50+
* Test to check creation of documentation when overwriting previous
51+
*
52+
* @throws \Exception
53+
*/
54+
public function testCreateDocumentationWithOverwrite()
55+
{
56+
$annotations = [
57+
"page" => "somePage",
58+
"description" => "someDescription"
59+
];
60+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
61+
->withAnnotations($annotations)
62+
->withFilenames(["filename"])
63+
->build();
64+
DocGenerator::getInstance()->createDocumentation(
65+
[$actionGroupUnderTest->getName() => $actionGroupUnderTest],
66+
OUTPUT_DIR,
67+
true
68+
);
69+
70+
$annotations = [
71+
"page" => "alteredPage",
72+
"description" => "alteredDescription"
73+
];
74+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
75+
->withAnnotations($annotations)
76+
->withFilenames(["filename"])
77+
->build();
78+
DocGenerator::getInstance()->createDocumentation(
79+
[$actionGroupUnderTest->getName() => $actionGroupUnderTest],
80+
OUTPUT_DIR,
81+
true
82+
);
83+
84+
$docFile = OUTPUT_DIR . DIRECTORY_SEPARATOR . self::DOC_FILENAME . ".md";
85+
86+
$this->assertTrue(file_exists($docFile));
87+
88+
$this->assertFileEquals(
89+
RESOURCE_DIR . DIRECTORY_SEPARATOR . "alteredDocumentation.txt",
90+
$docFile
91+
);
92+
}
93+
94+
/**
95+
* Test for existing documentation without clean
96+
*
97+
* @throws \Exception
98+
*/
99+
public function testCreateDocumentationNotCleanException()
100+
{
101+
$annotations = [
102+
"page" => "somePage",
103+
"description" => "someDescription"
104+
];
105+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
106+
->withAnnotations($annotations)
107+
->withFilenames(["filename"])
108+
->build();
109+
DocGenerator::getInstance()->createDocumentation(
110+
[$actionGroupUnderTest->getName() => $actionGroupUnderTest],
111+
OUTPUT_DIR,
112+
true
113+
);
114+
115+
$docFile = OUTPUT_DIR . DIRECTORY_SEPARATOR . self::DOC_FILENAME . ".md";
116+
117+
$this->expectException(TestFrameworkException::class);
118+
$this->expectExceptionMessage("$docFile already exists, please add --clean if you want to overwrite it.");
119+
120+
DocGenerator::getInstance()->createDocumentation(
121+
[$actionGroupUnderTest->getName() => $actionGroupUnderTest],
122+
OUTPUT_DIR,
123+
false
124+
);
125+
}
126+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#Action Group Information
2+
This documentation contains a list of all action groups on the pages on which they start
3+
4+
##List of Pages
5+
- [ alteredPage ](#alteredPage)
6+
---
7+
<a name="alteredPage"></a>
8+
##alteredPage
9+
10+
###testActionGroupObject
11+
alteredDescription
12+
13+
Located in:
14+
15+
***
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#Action Group Information
2+
This documentation contains a list of all action groups on the pages on which they start
3+
4+
##List of Pages
5+
- [ somePage ](#somePage)
6+
---
7+
<a name="somePage"></a>
8+
##somePage
9+
10+
###testActionGroupObject
11+
someDescription
12+
13+
Located in:
14+
15+
***

dev/tests/unit/Util/ActionGroupObjectBuilder.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class ActionGroupObjectBuilder
4848
*/
4949
private $extends = null;
5050

51+
/**
52+
* Action Group Object Builder default filenames
53+
*
54+
* @var string
55+
*/
56+
private $filenames = [];
57+
5158
/**
5259
* Setter for the Action Group Object name
5360
*
@@ -60,6 +67,18 @@ public function withName($name)
6067
return $this;
6168
}
6269

70+
/**
71+
* Setter for the Action Group Object annotations
72+
*
73+
* @param array $annotations
74+
* @return ActionGroupObjectBuilder
75+
*/
76+
public function withAnnotations($annotations)
77+
{
78+
$this->annotations = $annotations;
79+
return $this;
80+
}
81+
6382
/**
6483
* Setter for the Action Group Object arguments
6584
*
@@ -96,6 +115,18 @@ public function withExtendedAction($extendedActionGroup)
96115
return $this;
97116
}
98117

118+
/**
119+
* Setter for the Action Group Object filenames
120+
*
121+
* @param array $filenames
122+
* @return ActionGroupObjectBuilder
123+
*/
124+
public function withFilenames($filenames)
125+
{
126+
$this->filenames = $filenames;
127+
return $this;
128+
}
129+
99130
/**
100131
* ActionGroupObjectBuilder constructor.
101132
*/
@@ -118,7 +149,8 @@ public function build()
118149
$this->annotations,
119150
$this->arguments,
120151
$this->actionObjects,
121-
$this->extends
152+
$this->extends,
153+
$this->filenames
122154
);
123155
}
124156
}

dev/tests/unit/Util/MagentoTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,24 @@
1414
*/
1515
class MagentoTestCase extends TestCase
1616
{
17+
public static function setUpBeforeClass()
18+
{
19+
if (!self::fileExists(OUTPUT_DIR)) {
20+
mkdir(OUTPUT_DIR, 0755, true);
21+
}
22+
parent::setUpBeforeClass();
23+
}
24+
1725
/**
1826
* Teardown for removing AspectMock Double References
1927
* @return void
2028
*/
2129
public static function tearDownAfterClass()
2230
{
2331
AspectMock::clean();
32+
array_map('unlink', glob(OUTPUT_DIR . DIRECTORY_SEPARATOR . "*"));
33+
if (file_exists(OUTPUT_DIR)) {
34+
rmdir(OUTPUT_DIR);
35+
}
2436
}
2537
}

0 commit comments

Comments
 (0)