@@ -54,6 +54,9 @@ public function execute(InputInterface $input)
54
54
preg_match_all ('/<assert[^>]*\/>/ ' , $ contents , $ potentialAssertions );
55
55
$ newAssertions = [];
56
56
$ index = 0 ;
57
+ if (empty ($ potentialAssertions [0 ])) {
58
+ continue ;
59
+ }
57
60
foreach ($ potentialAssertions [0 ] as $ potentialAssertion ) {
58
61
$ newAssertions [$ index ] = $ this ->convertOldAssertionToNew ($ potentialAssertion );
59
62
$ index ++;
@@ -98,8 +101,7 @@ private function convertOldAssertionToNew($assertion)
98
101
$ stepKey = "" ;
99
102
100
103
// regex to grab values
101
- $ grabValueRegex = '/(stepKey|actual|actualType|expected|expectedType|delta|message)="([^"]*)"/ ' ;
102
-
104
+ $ grabValueRegex = '/(stepKey|actual|actualType|expected|expectedType|delta|message|selector|attribute|expectedValue|before|after|remove)=( \'[^ \']* \'|"[^"]*")/ ' ;
103
105
// Make 3 arrays in $grabbedParts:
104
106
// 0 contains stepKey="value"
105
107
// 1 contains stepKey
@@ -110,40 +112,69 @@ private function convertOldAssertionToNew($assertion)
110
112
$ sortedParts [$ grabbedParts [1 ][$ i ]] = $ grabbedParts [2 ][$ i ];
111
113
}
112
114
113
- // Build new String
114
- $ newString = "< $ assertType " ;
115
+ // Build new String, trim ' and "
116
+ $ trimmedParts = [];
117
+ $ newString = "< $ assertType " ;
115
118
$ subElements = ["actual " => [], "expected " => []];
116
119
foreach ($ sortedParts as $ type => $ value ) {
117
- if (in_array ($ type , ["stepKey " , "delta " , "message " ])) {
120
+ $ value = rtrim (ltrim ($ value , '" ' ), '" ' );
121
+ $ value = rtrim (ltrim ($ value , "' " ), "' " );
122
+ $ trimmedParts [$ type ] = $ value ;
123
+ if (in_array ($ type , ["stepKey " , "delta " , "message " , "before " , "after " , "remove " ])) {
118
124
if ($ type == "stepKey " ) {
119
125
$ stepKey = $ value ;
120
126
}
121
- $ newString .= "$ type= \"$ value \"" ;
127
+ $ newString .= " $ type= \"$ value \"" ;
122
128
continue ;
123
129
}
124
130
if ($ type == "actual " ) {
125
131
$ subElements ["actual " ]["value " ] = $ value ;
126
132
} elseif ($ type == "actualType " ) {
127
133
$ subElements ["actual " ]["type " ] = $ value ;
128
- } elseif ($ type == "expected " ) {
134
+ } elseif ($ type == "expected " || $ type = " expectedValue " ) {
129
135
$ subElements ["expected " ]["value " ] = $ value ;
130
136
} elseif ($ type == "expectedType " ) {
131
137
$ subElements ["expected " ]["type " ] = $ value ;
132
138
}
133
139
}
134
140
$ newString .= "> \n" ;
135
- foreach ($ subElements as $ type => $ subElement ) {
136
- if (!isset ($ subElement ['value ' ]) || !isset ($ subElement ['type ' ])) {
137
- //don't have all the info we need to rebuild
138
- $ this ->errors [] = "UNABLE TO FULLY REBUILD ASSERTION, PLEASE MANUALLY CHECK FORMAT " .
139
- "( $ assertType \"$ stepKey \" in $ this ->currentFile ) " ;
140
- continue ;
141
+ // Guess value type if not set in either case
142
+ if (!isset ($ subElements ["actual " ]['type ' ]) && isset ($ subElements ["actual " ]["value " ])) {
143
+ $ subElements ["actual " ]['type ' ] = $ this ->guessValueType ($ subElements ["actual " ]["value " ]);
144
+ }
145
+ if (!isset ($ subElements ["expected " ]['type ' ]) && isset ($ subElements ["expected " ]["value " ])) {
146
+ $ subElements ["expected " ]['type ' ] = $ this ->guessValueType ($ subElements ["expected " ]["value " ]);
147
+ }
148
+ // Massage subElements with data for edge cases
149
+ if ($ assertType == 'assertElementContainsAttribute ' ) {
150
+ // Assert type is very edge-cased, completely different schema
151
+ $ value = $ subElements ['expected ' ]['value ' ];
152
+ $ selector = $ trimmedParts ['selector ' ];
153
+ $ attribute = $ trimmedParts ['attribute ' ];
154
+ $ newString .= "\t\t\t<expectedResult selector= \"$ selector \" attribute= \"$ attribute \"> $ value</expectedResult> \n" ;
155
+ } else {
156
+ foreach ($ subElements as $ type => $ subElement ) {
157
+ if (empty ($ subElement )) {
158
+ continue ;
159
+ }
160
+ $ value = $ subElement ['value ' ];
161
+ $ typeValue = $ subElement ['type ' ];
162
+ if (empty ($ value )) {
163
+ $ this ->errors [] = "POTENTIAL ANOMALOUS OUPUT DETECTED, PLEASE MANUALLY CHECK OUTPUT " .
164
+ "( $ assertType \"$ stepKey \" in $ this ->currentFile ) " ;
165
+ }
166
+ $ newString .= "\t\t\t< {$ type }Result type= \"$ typeValue \"> $ value</ {$ type }Result> \n" ;
141
167
}
142
- $ value = $ subElement ['value ' ];
143
- $ typeValue = $ subElement ['type ' ];
144
- $ newString .= "< {$ type }Result type= \"$ typeValue \"> $ value</ {$ type }Result> \n" ;
145
168
}
146
- $ newString .= "</ $ assertType> " ;
169
+ $ newString .= " </$ assertType> " ;
147
170
return $ newString ;
148
171
}
172
+
173
+ private function guessValueType ($ string ) {
174
+ preg_match ('/\$[a-zA-Z0-9]*/ ' , $ string , $ matches );
175
+ if (isset ($ matches [0 ]) && $ matches [0 ] == $ string ) {
176
+ return "variable " ;
177
+ }
178
+ return "string " ;
179
+ }
149
180
}
0 commit comments