-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathDocGenerator.php
137 lines (123 loc) · 4.87 KB
/
DocGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Util;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler;
use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject;
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
use Magento\FunctionalTestingFramework\Test\Util\ActionGroupAnnotationExtractor;
use Magento\FunctionalTestingFramework\Test\Util\ActionGroupObjectExtractor;
/**
* Class TestGenerator
*/
class DocGenerator
{
const DEFAULT_OUTPUT_DIR =
PROJECT_ROOT .
DIRECTORY_SEPARATOR .
"dev" .
DIRECTORY_SEPARATOR .
"tests" .
DIRECTORY_SEPARATOR .
"docs";
const DOC_NAME = "documentation.md";
# This is the only place FILENAMES is defined as this string
const FILENAMES = "filenames";
/**
* DocGenerator constructor.
*
*/
public function __construct()
{
}
/**
* This creates html documentation for objects passed in
*
* @param ActionGroupObject[]|TestObject[] $annotatedObjects
* @param string $outputDir
* @return void
* @throws TestFrameworkException
*/
public function createDocumentation($annotatedObjects, $outputDir, $clean)
{
if (empty($outputDir)) {
$fullPath = self::DEFAULT_OUTPUT_DIR . DIRECTORY_SEPARATOR;
} else {
$fullPath = $outputDir . DIRECTORY_SEPARATOR;
}
$filePath = $fullPath . self::DOC_NAME;
if (!file_exists($fullPath)) {
mkdir($fullPath, 0755, true);
}
if (file_exists($filePath) and !$clean) {
throw new TestFrameworkException(
"$filePath already exists, please add --clean if you want to overwrite it."
);
}
$pageGroups = [];
foreach ($annotatedObjects as $name => $object) {
$annotations = $object->getAnnotations();
$filenames = explode(',', $object->getFilename());
$arguments = $object->getArguments();
$info = [
actionGroupObject::ACTION_GROUP_DESCRIPTION => $annotations[actionGroupObject::ACTION_GROUP_DESCRIPTION]
?? 'NO_DESCRIPTION_SPECIFIED',
self::FILENAMES => $filenames,
ActionGroupObjectExtractor::ACTION_GROUP_ARGUMENTS => $arguments
];
$pageGroups = array_merge_recursive(
$pageGroups,
[$annotations[ActionGroupObject::ACTION_GROUP_PAGE] ?? 'NO_PAGE_SPECIFIED' => [$name => $info]]
);
}
ksort($pageGroups);
foreach ($pageGroups as $page => $groups) {
ksort($groups);
$pageGroups[$page] = $groups;
}
$markdown = $this->transformToMarkdown($pageGroups);
file_put_contents($filePath, $markdown);
}
/**
* This creates html documentation for objects passed in
*
* @param array $annotationList
* @return string
*/
private function transformToMarkdown($annotationList)
{
$markdown = "#Action Group Information" . PHP_EOL;
$markdown .= "This documentation contains a list of all Action Groups." .
PHP_EOL .
PHP_EOL;
$markdown .= "---" . PHP_EOL;
foreach ($annotationList as $group => $objects) {
foreach ($objects as $name => $annotations) {
$markdown .= "###$name" . PHP_EOL;
$markdown .= "**Description**:" . PHP_EOL;
$markdown .= "- " . $annotations[actionGroupObject::ACTION_GROUP_DESCRIPTION] . PHP_EOL . PHP_EOL;
if (!empty($annotations[ActionGroupObjectExtractor::ACTION_GROUP_ARGUMENTS])) {
$markdown .= "**Action Group Arguments**:" . PHP_EOL . PHP_EOL;
$markdown .= "| Name | Type |" . PHP_EOL;
$markdown .= "| ---- | ---- |" . PHP_EOL;
foreach ($annotations[ActionGroupObjectExtractor::ACTION_GROUP_ARGUMENTS] as $argument) {
$argumentName = $argument->getName();
$argumentType = $argument->getDataType();
$markdown .= "| $argumentName | $argumentType |" . PHP_EOL;
}
$markdown .= PHP_EOL;
}
$markdown .= "**Located In**:" . PHP_EOL;
foreach ($annotations[self::FILENAMES] as $filename) {
$relativeFilename = str_replace(MAGENTO_BP . DIRECTORY_SEPARATOR, "", $filename);
$markdown .= PHP_EOL . "- $relativeFilename";
}
$markdown .= PHP_EOL . "***" . PHP_EOL;
}
}
return $markdown;
}
}