-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathUpdateAssertionSchema.php
180 lines (167 loc) · 6.65 KB
/
UpdateAssertionSchema.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Upgrade;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
/**
* Class UpdateAssertionSchema
* @package Magento\FunctionalTestingFramework\Upgrade
*/
class UpdateAssertionSchema implements UpgradeInterface
{
const OLD_ASSERTION_ATTRIBUTES = ["expected", "expectedType", "actual", "actualType"];
/**
* Current file being inspected, for error messaging
* @var string
*/
private $currentFile;
/**
* Potential errors reported during replacement.
* @var array
*/
private $errors = [];
/**
* Upgrades all test xml files, changing <assert> actions to be nested
*
* @param InputInterface $input
* @return string
*/
public function execute(InputInterface $input)
{
$testsPath = $input->getArgument('path');
$finder = new Finder();
$finder->files()->in($testsPath)->name("*.xml");
$fileSystem = new Filesystem();
$testsUpdated = 0;
foreach ($finder->files() as $file) {
if (!$this->detectOldAttributes($file)) {
continue;
}
$this->currentFile = $file->getFilename();
$contents = $file->getContents();
// Isolate <assert ... /> but not <assert> ... </assert>
preg_match_all('/<assert[^>]*\/>/', $contents, $potentialAssertions);
$newAssertions = [];
$index = 0;
if (empty($potentialAssertions[0])) {
continue;
}
foreach ($potentialAssertions[0] as $potentialAssertion) {
$newAssertions[$index] = $this->convertOldAssertionToNew($potentialAssertion);
$index++;
}
foreach ($newAssertions as $currentIndex => $replacements) {
$contents = str_replace($potentialAssertions[0][$currentIndex], $replacements, $contents);
}
$fileSystem->dumpFile($file->getRealPath(), $contents);
$testsUpdated++;
}
return ("Assertion Syntax updated in {$testsUpdated} file(s).\n" . implode("\n\t", $this->errors));
}
/**
* Detects present of attributes in file
*
* @param string $file
* @return boolean
*/
private function detectOldAttributes($file)
{
foreach (self::OLD_ASSERTION_ATTRIBUTES as $OLD_ASSERTION_ATTRIBUTE) {
if (strpos($file->getContents(), $OLD_ASSERTION_ATTRIBUTE) !== false) {
return true;
}
}
return false;
}
/**
* Takes given string and attempts to convert it from single line to multi-line
*
* @param string $assertion
* @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function convertOldAssertionToNew($assertion)
{
// <assertSomething => assertSomething
$assertType = ltrim(explode(' ', $assertion)[0], '<');
$stepKey = "";
// regex to grab values
$grabValueRegex = '/(stepKey|actual|actualType|expected|expectedType|delta|message|selector|attribute|expectedValue|before|after|remove)=(\'[^\']*\'|"[^"]*")/';
// Make 3 arrays in $grabbedParts:
// 0 contains stepKey="value"
// 1 contains stepKey
// 2 contains value
$sortedParts = [];
preg_match_all($grabValueRegex, $assertion, $grabbedParts);
for ($i = 0; $i < count($grabbedParts[0]); $i++) {
$sortedParts[$grabbedParts[1][$i]] = $grabbedParts[2][$i];
}
// Build new String, trim ' and "
$trimmedParts = [];
$newString = "<$assertType";
$subElements = ["actual" => [], "expected" => []];
foreach ($sortedParts as $type => $value) {
$value = rtrim(ltrim($value, '"'), '"');
$value = rtrim(ltrim($value, "'"), "'");
$trimmedParts[$type] = $value;
if (in_array($type, ["stepKey", "delta", "message", "before", "after", "remove"])) {
if ($type == "stepKey") {
$stepKey = $value;
}
$newString .= " $type=\"$value\"";
continue;
}
if ($type == "actual") {
$subElements["actual"]["value"] = $value;
} elseif ($type == "actualType") {
$subElements["actual"]["type"] = $value;
} elseif ($type == "expected" || $type = "expectedValue") {
$subElements["expected"]["value"] = $value;
} elseif ($type == "expectedType") {
$subElements["expected"]["type"] = $value;
}
}
$newString .= ">\n";
// Guess value type if not set in either case
if (!isset($subElements["actual"]['type']) && isset($subElements["actual"]["value"])) {
$subElements["actual"]['type'] = $this->guessValueType($subElements["actual"]["value"]);
}
if (!isset($subElements["expected"]['type']) && isset($subElements["expected"]["value"])) {
$subElements["expected"]['type'] = $this->guessValueType($subElements["expected"]["value"]);
}
// Massage subElements with data for edge cases
if ($assertType == 'assertElementContainsAttribute') {
// Assert type is very edge-cased, completely different schema
$value = $subElements['expected']['value'];
$selector = $trimmedParts['selector'];
$attribute = $trimmedParts['attribute'];
$newString .= "\t\t\t<expectedResult selector=\"$selector\" attribute=\"$attribute\">$value</expectedResult>\n";
} else {
foreach ($subElements as $type => $subElement) {
if (empty($subElement)) {
continue;
}
$value = $subElement['value'];
$typeValue = $subElement['type'];
if (empty($value)) {
$this->errors[] = "POTENTIAL ANOMALOUS OUPUT DETECTED, PLEASE MANUALLY CHECK OUTPUT " .
"($assertType \"$stepKey\" in $this->currentFile)";
}
$newString .= "\t\t\t<{$type}Result type=\"$typeValue\">$value</{$type}Result>\n";
}
}
$newString .= " </$assertType>";
return $newString;
}
private function guessValueType($string) {
preg_match('/\$[a-zA-Z0-9]*/', $string, $matches);
if (isset($matches[0]) && $matches[0] == $string) {
return "variable";
}
return "string";
}
}