-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathSplitMultipleEntitiesFiles.php
237 lines (214 loc) · 7.51 KB
/
SplitMultipleEntitiesFiles.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Upgrade;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Util\Script\ScriptUtil;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
/**
* Class SplitMultipleEntitiesFiles
* @package Magento\FunctionalTestingFramework\Upgrade
*/
class SplitMultipleEntitiesFiles implements UpgradeInterface
{
const XML_VERSION = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
const XML_COPYRIGHT = '<!--' . PHP_EOL
. ' /**' . PHP_EOL
. ' * Copyright © Magento, Inc. All rights reserved.' . PHP_EOL
. ' * See COPYING.txt for license details.' . PHP_EOL
. ' */' . PHP_EOL
. '-->' . PHP_EOL;
const XML_NAMESPACE = 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . PHP_EOL;
const XML_SCHEMA_LOCATION = "\t" . 'xsi:noNamespaceSchemaLocation="urn:magento:mftf:';
const FILENAME_BASE = 'base';
const FILENAME_SUFFIX = 'type';
/**
* OutputInterface
*
* @var OutputInterface
*/
private $output;
/**
* Total test updated
*
* @var integer
*/
private $testsUpdated = 0;
/**
* Entity categories for the upgrade script
*
* @var array
*/
private $entityCategories = [
'Suite' => 'Suite/etc/suiteSchema.xsd',
'Test' => 'Test/etc/testSchema.xsd',
'ActionGroup' => 'Test/etc/actionGroupSchema.xsd',
'Page' => 'Page/etc/PageObject.xsd',
'Section' => 'Page/etc/SectionObject.xsd',
];
/**
* Scan all xml files and split xml files that contains more than one entities
* for Test, Action Group, Page, Section, Suite types.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string
* @throws TestFrameworkException
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$scriptUtil = new ScriptUtil();
$this->output = $output;
$this->testsUpdated = 0;
$testPaths[] = $input->getArgument('path');
if (empty($testPaths[0])) {
$testPaths = $scriptUtil->getAllModulePaths();
}
// Process module xml files
foreach ($this->entityCategories as $type => $urn) {
$xmlFiles = $scriptUtil->getModuleXmlFilesByScope($testPaths, $type);
$this->processXmlFiles($xmlFiles, $type, $urn);
}
return ("Split multiple entities in {$this->testsUpdated} file(s).");
}
/**
* Split on list of xml files
*
* @param Finder $xmlFiles
* @param string $type
* @param string $urn
* @return void
*/
private function processXmlFiles($xmlFiles, $type, $urn)
{
foreach ($xmlFiles as $file) {
$contents = $file->getContents();
$domDocument = new \DOMDocument();
$domDocument->loadXML($contents);
$entities = $domDocument->getElementsByTagName(lcfirst($type));
if ($entities->length > 1) {
$filename = $file->getRealPath();
if ($this->output->isVerbose()) {
$this->output->writeln('Processing file:' . $filename);
}
foreach ($entities as $entity) {
/** @var \DOMElement $entity */
$entityName = $entity->getAttribute('name');
$entityContent = $entity->ownerDocument->saveXML($entity);
$dir = dirname($file);
$dir .= DIRECTORY_SEPARATOR . ucfirst(basename($file, '.xml'));
$splitFileName = $this->formatName($entityName, $type);
$this->filePutContents(
$dir . DIRECTORY_SEPARATOR . $splitFileName . '.xml',
$type,
$urn,
$entityContent
);
if ($this->output->isVerbose()) {
$this->output->writeln(
'Created file:' . $dir . DIRECTORY_SEPARATOR . $splitFileName . '.xml'
);
}
$this->testsUpdated++;
}
unlink($file);
if ($this->output->isVerbose()) {
$this->output->writeln('Unlinked file:' . $filename . PHP_EOL);
}
}
}
}
/**
* Create file with contents and create dir if needed
*
* @param string $fullPath
* @param string $type
* @param string $urn
* @param string $contents
* @return void
*/
private function filePutContents($fullPath, $type, $urn, $contents)
{
$dir = dirname($fullPath);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
// Make sure not overwriting an existing file
$fullPath = $this->getNonExistingFileFullPath($fullPath, $type);
$fullContents = self::XML_VERSION
. self::XML_COPYRIGHT
. '<' . lcfirst($type) . 's '
. self::XML_NAMESPACE
. self::XML_SCHEMA_LOCATION . $urn . '">' . PHP_EOL
. ' ' . $contents . PHP_EOL
. '</' . lcfirst($type) . 's>' . PHP_EOL;
file_put_contents($fullPath, $fullContents);
}
/**
* Format name to include type if it's Page, Section or Action Group
*
* @param string $name
* @param string $type
* @return string
*/
private function formatName($name, $type)
{
$name = ucfirst($name);
$type = ucfirst($type);
if ($type !== 'Section' && $type !== 'Page' && $type !== 'ActionGroup') {
return $name;
}
$parts = $this->getFileNameParts($name, $type);
if (empty($parts[self::FILENAME_SUFFIX])) {
$name .= $type;
}
return $name;
}
/**
* Vary the input to return a non-existing file name
*
* @param string $fullPath
* @param string $type
* @return string
*/
private function getNonExistingFileFullPath($fullPath, $type)
{
$type = ucfirst($type);
$dir = dirname($fullPath);
$filename = basename($fullPath, '.xml');
$i = 1;
$parts = [];
while (file_exists($fullPath)) {
if (empty($parts)) {
$parts = $this->getFileNameParts($filename, $type);
}
$basename = $parts[self::FILENAME_BASE] . strval(++$i);
$fullPath = $dir . DIRECTORY_SEPARATOR . $basename . $parts[self::FILENAME_SUFFIX] . '.xml';
}
return $fullPath;
}
/**
* Split filename into two parts and return it in an associate array with keys FILENAME_BASE and FILENAME_SUFFIX
*
* @param string $filename
* @param string $type
* @return array
*/
private function getFileNameParts($filename, $type)
{
$type = ucfirst($type);
$fileNameParts = [];
if (substr($filename, -strlen($type)) === $type) {
$fileNameParts[self::FILENAME_BASE] = substr($filename, 0, strlen($filename) - strlen($type));
$fileNameParts[self::FILENAME_SUFFIX] = $type;
} else {
$fileNameParts[self::FILENAME_BASE] = $filename;
$fileNameParts[self::FILENAME_SUFFIX] = '';
}
return $fileNameParts;
}
}