forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveModuleFileInSuiteFiles.php
151 lines (138 loc) · 4.34 KB
/
RemoveModuleFileInSuiteFiles.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
<?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\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
/**
* Class RemoveModuleFileInSuiteFiles
* @package Magento\FunctionalTestingFramework\Upgrade
*/
class RemoveModuleFileInSuiteFiles implements UpgradeInterface
{
/**
* OutputInterface
*
* @var OutputInterface
*/
private $output;
/**
* Console output style
*
* @var SymfonyStyle
*/
private $ioStyle = null;
/**
* Indicate if notice is print
*
* @var boolean
*/
private $printNotice = false;
/**
* Number of test being updated
*
* @var integer
*/
private $testsUpdated = 0;
/**
* Indicate if a match and replace has happened
*
* @var boolean
*/
private $replaced = false;
/**
* Scan all suite xml files, remove <module file="".../> node, and print update message
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string
* @throws TestFrameworkException
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$this->setOutputStyle($input, $output);
$this->output = $output;
$testPaths[] = $input->getArgument('path');
if (empty($testPaths[0])) {
$testPaths = ScriptUtil::getAllModulePaths();
}
// Get module suite xml files
$xmlFiles = ScriptUtil::getModuleXmlFilesByScope($testPaths, 'Suite');
$this->processXmlFiles($xmlFiles);
return ("Removed module file reference in {$this->testsUpdated} suite file(s).");
}
/**
* Process on list of xml files
*
* @param Finder $xmlFiles
* @return void
*/
private function processXmlFiles($xmlFiles)
{
foreach ($xmlFiles as $file) {
$contents = $file->getContents();
$filePath = $file->getRealPath();
$this->replaced = false;
$contents = $this->removeModuleFileAttributeInSuite($contents, $filePath);
if ($this->replaced) {
file_put_contents($filePath, $contents);
$this->testsUpdated++;
}
}
}
/**
* Remove module file attribute in Suite xml file
*
* @param string $contents
* @param string $file
* @return string|string[]|null
*/
private function removeModuleFileAttributeInSuite($contents, $file)
{
$pattern = '/<module[^\<\>]+file[\s]*=[\s]*"(?<file>[^"\<\>]*)"[^\>\<]*>/';
$contents = preg_replace_callback(
$pattern,
function ($matches) use ($file) {
if (!$this->printNotice) {
$this->ioStyle->note(
'`file` is not a valid attribute for <module> in Suite XML schema.' . PHP_EOL
. 'The `file`references in the following xml files are commented out. '
. 'Consider using <test> instead.'
);
$this->printNotice = true;
}
$this->output->writeln(
PHP_EOL
. '"' . trim($matches[0]) . '"' . PHP_EOL
. 'is commented out from file: ' . $file . PHP_EOL
);
$result = str_replace('<module', '<!--module', $matches[0]);
$result = str_replace('>', '--> <!-- Please replace with <test name="" -->', $result);
$this->replaced = true;
return $result;
},
$contents
);
return $contents;
}
/**
* Set Symfony Style for output
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
private function setOutputStyle(InputInterface $input, OutputInterface $output)
{
// For output style
if (null === $this->ioStyle) {
$this->ioStyle = new SymfonyStyle($input, $output);
}
}
}