-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathUpdateAssertionSchema.php
167 lines (154 loc) · 6.8 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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Upgrade;
use Magento\FunctionalTestingFramework\Util\Script\ScriptUtil;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
/**
* Class UpdateAssertionSchema
* @package Magento\FunctionalTestingFramework\Upgrade
*/
class UpdateAssertionSchema implements UpgradeInterface
{
/**
* Upgrades all test xml files, changing as many <assert> actions to be nested as possible
* WILL NOT CATCH cases where style is a mix of old and new
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$scriptUtil = new ScriptUtil();
$testPaths[] = $input->getArgument('path');
if (empty($testPaths[0])) {
$testPaths = $scriptUtil->getAllModulePaths();
}
$testsUpdated = 0;
foreach ($testPaths as $testsPath) {
$finder = new Finder();
$finder->files()->in($testsPath)->name("*.xml");
$fileSystem = new Filesystem();
foreach ($finder->files() as $file) {
$contents = $file->getContents();
// Isolate <assert ... /> but never <assert> ... </assert>, stops after finding first />
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).");
}
/**
* Takes given string and attempts to convert it from single line to multi-line
*
* @param string $assertion
* @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
private function convertOldAssertionToNew($assertion)
{
// <assertSomething => assertSomething
$assertType = ltrim(explode(' ', $assertion)[0], '<');
// regex to all attribute=>value pairs
$allAttributes = "stepKey|actual|actualType|expected|expectedType|expectedValue|";
$allAttributes .= "delta|message|selector|attribute|before|after|remove";
$grabValueRegex = '/('. $allAttributes .')=(\'[^\']*\'|"[^"]*")/';
// Makes 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];
}
// Begin trimming values and adding back into new string
$trimmedParts = [];
$newString = "<$assertType";
$subElements = ["actual" => [], "expected" => []];
foreach ($sortedParts as $type => $value) {
// If attribute="'value'", elseif attribute='"value"', new nested format will break if we leave these in
if (strpos($value, '"') === 0) {
$value = rtrim(ltrim($value, '"'), '"');
} elseif (strpos($value, "'") === 0) {
$value = rtrim(ltrim($value, "'"), "'");
}
// If value is empty string (" " or ' '), trim again to become empty
if (str_replace(" ", "", $value) === "''") {
$value = "";
} elseif (str_replace(" ", "", $value) === '""') {
$value = "";
}
// Value is ready for storage/reapply
$trimmedParts[$type] = $value;
if (in_array($type, ["stepKey", "delta", "message", "before", "after", "remove"])) {
// Add back as attribute safely
$newString .= " $type=\"$value\"";
continue;
}
// Store in subtype for child element creation
if ($type === "actual") {
$subElements["actual"]["value"] = $value;
} elseif ($type === "actualType") {
$subElements["actual"]["type"] = $value;
} elseif ($type === "expected" or $type === "expectedValue") {
$subElements["expected"]["value"] = $value;
} elseif ($type === "expectedType") {
$subElements["expected"]["type"] = $value;
}
}
$newString .= ">\n";
// Assert type is very edge-cased, completely different schema
if ($assertType === 'assertElementContainsAttribute') {
// assertElementContainsAttribute type defaulted to string if not present
if (!isset($subElements["expected"]['type'])) {
$subElements["expected"]['type'] = "string";
}
$value = $subElements['expected']['value'] ?? "";
$type = $subElements["expected"]['type'];
$selector = $trimmedParts['selector'];
$attribute = $trimmedParts['attribute'];
// @codingStandardsIgnoreStart
$newString .= "\t\t\t<expectedResult selector=\"$selector\" attribute=\"$attribute\" type=\"$type\">$value</expectedResult>\n";
// @codingStandardsIgnoreEnd
} else {
// Set type to const if it's absent, old default
if (isset($subElements["actual"]['value']) && !isset($subElements["actual"]['type'])) {
$subElements["actual"]['type'] = "const";
}
if (isset($subElements["expected"]['value']) && !isset($subElements["expected"]['type'])) {
$subElements["expected"]['type'] = "const";
}
foreach ($subElements as $type => $subElement) {
if (empty($subElement)) {
continue;
}
$value = $subElement['value'];
$typeValue = $subElement['type'];
$newString .= "\t\t\t<{$type}Result type=\"$typeValue\">$value</{$type}Result>\n";
}
}
$newString .= " </$assertType>";
return $newString;
}
}