@@ -85,7 +85,6 @@ public function processAssert(
85
85
BrowserInterface $ browser
86
86
) {
87
87
$ browser ->open ($ _ENV ['app_frontend_url ' ] . $ product ->getUrlKey () . '.html ' );
88
-
89
88
$ actualPrice = null ;
90
89
if ($ this ->isPrice ) {
91
90
$ priceBlock = $ catalogProductView ->getViewBlock ()->getPriceBlock ();
@@ -95,7 +94,6 @@ public function processAssert(
95
94
}
96
95
$ fixtureCustomOptions = $ this ->prepareOptions ($ product , $ actualPrice );
97
96
$ formCustomOptions = $ catalogProductView ->getViewBlock ()->getOptions ($ product )['custom_options ' ];
98
-
99
97
$ error = $ this ->verifyData ($ fixtureCustomOptions , $ formCustomOptions );
100
98
\PHPUnit_Framework_Assert::assertEmpty ($ error , $ error );
101
99
}
@@ -106,33 +104,143 @@ public function processAssert(
106
104
* @param FixtureInterface $product
107
105
* @param int|null $actualPrice
108
106
* @return array
109
- *
110
- * @SuppressWarnings(PHPMD.NPathComplexity)
111
107
*/
112
108
protected function prepareOptions (FixtureInterface $ product , $ actualPrice = null )
113
109
{
114
110
$ result = [];
115
111
$ customOptions = $ product ->hasData ('custom_options ' )
116
112
? $ product ->getDataFieldConfig ('custom_options ' )['source ' ]->getCustomOptions ()
117
- : null ;
118
- $ actualPrice = $ actualPrice ? $ actualPrice : $ product ->getPrice ();
113
+ : [] ;
114
+ $ actualPrice = $ actualPrice ?: $ product ->getPrice ();
119
115
foreach ($ customOptions as $ customOption ) {
120
- $ skippedField = isset ($ this ->skippedFieldOptions [$ customOption ['type ' ]])
121
- ? $ this ->skippedFieldOptions [$ customOption ['type ' ]]
122
- : [];
123
- foreach ($ customOption ['options ' ] as &$ option ) {
124
- // recalculate percent price
125
- if ('Percent ' == $ option ['price_type ' ]) {
126
- $ option ['price ' ] = ($ actualPrice * $ option ['price ' ]) / 100 ;
127
- $ option ['price ' ] = round ($ option ['price ' ], 2 );
128
- }
116
+ $ result = $ this ->prepareEachCustomOption ($ actualPrice , $ customOption , $ result );
117
+ }
118
+ return $ result ;
119
+ }
129
120
130
- $ option = array_diff_key ($ option , array_flip ($ skippedField ));
121
+ /**
122
+ * Verify fixture and form data
123
+ *
124
+ * @param array $fixtureData
125
+ * @param array $formData
126
+ * @param bool $isStrict
127
+ * @param bool $isPrepareError
128
+ * @return array|string
129
+ */
130
+ protected function verifyData (array $ fixtureData , array $ formData , $ isStrict = false , $ isPrepareError = true )
131
+ {
132
+ $ errors = [];
133
+ foreach ($ fixtureData as $ key => $ value ) {
134
+ if (in_array ($ key , $ this ->skippedFields , true )) {
135
+ continue ;
131
136
}
137
+ $ formValue = isset ($ formData [$ key ]) ? $ formData [$ key ] : null ;
138
+ $ errors = $ this ->verifyDataForErrors ($ formValue , $ key , $ errors , $ value );
139
+ }
140
+ return $ this ->prepareErrorsForOutput ($ fixtureData , $ formData , $ isStrict , $ isPrepareError , $ errors );
141
+ }
132
142
133
- $ result [$ customOption ['title ' ]] = $ customOption ;
143
+ /**
144
+ * Checks data for not equal values error
145
+ *
146
+ * @param array|string $value
147
+ * @param array|string $formValue
148
+ * @param string $key
149
+ * @return string
150
+ */
151
+ private function checkNotEqualValuesErrors ($ value , $ formValue , $ key )
152
+ {
153
+ /**
154
+ * It is needed because sorting in db begins from 1, but when selenium driver gets value from form it starts
155
+ * calculate from 0. So this operation checks this case
156
+ */
157
+ if ((int )$ value === (int )$ formValue + 1 ) {
158
+ return '' ;
159
+ }
160
+ if (is_array ($ value )) {
161
+ $ value = $ this ->arrayToString ($ value );
134
162
}
163
+ if (is_array ($ formValue )) {
164
+ $ formValue = $ this ->arrayToString ($ formValue );
165
+ }
166
+ return sprintf ('- %s: "%s" instead of "%s" ' , $ key , $ formValue , $ value );
167
+ }
135
168
169
+ /**
170
+ * Prepare errors data to output
171
+ *
172
+ * @param array $fixtureData
173
+ * @param array $formData
174
+ * @param $isStrict
175
+ * @param $isPrepareError
176
+ * @param $errors
177
+ * @return array|string
178
+ */
179
+ private function prepareErrorsForOutput (array $ fixtureData , array $ formData , $ isStrict , $ isPrepareError , $ errors )
180
+ {
181
+ if ($ isStrict ) {
182
+ $ diffData = array_diff (array_keys ($ formData ), array_keys ($ fixtureData ));
183
+ if ($ diffData ) {
184
+ $ errors [] = '- fields ' . implode (', ' , $ diffData ) . ' is absent in fixture ' ;
185
+ }
186
+ }
187
+ if ($ isPrepareError ) {
188
+ return $ this ->prepareErrors ($ errors );
189
+ }
190
+ return $ errors ;
191
+ }
192
+
193
+ /**
194
+ * Checks data for errors
195
+ *
196
+ * @param array|string $formValue
197
+ * @param string $key
198
+ * @param array $errors
199
+ * @param string $value
200
+ * @return array
201
+ */
202
+ private function verifyDataForErrors ($ formValue , $ key , $ errors , $ value )
203
+ {
204
+ if (is_numeric ($ formValue )) {
205
+ $ formValue = (float )$ formValue ;
206
+ }
207
+ if (null === $ formValue ) {
208
+ $ errors [] = '- field " ' . $ key . '" is absent in form ' ;
209
+ } elseif (is_array ($ value ) && is_array ($ formValue )) {
210
+ $ valueErrors = $ this ->verifyData ($ value , $ formValue , true , false );
211
+ if (!empty ($ valueErrors )) {
212
+ $ errors [$ key ] = $ valueErrors ;
213
+ }
214
+ } elseif ($ value != $ formValue ) {
215
+ $ notEqualValuesErrors = $ this ->checkNotEqualValuesErrors ($ value , $ formValue , $ key );
216
+ if ($ notEqualValuesErrors ) {
217
+ $ errors [] = $ notEqualValuesErrors ;
218
+ }
219
+ }
220
+ return $ errors ;
221
+ }
222
+
223
+ /**
224
+ * @param $actualPrice
225
+ * @param $customOption
226
+ * @param $result
227
+ * @return array
228
+ */
229
+ private function prepareEachCustomOption ($ actualPrice , $ customOption , $ result )
230
+ {
231
+ $ skippedField = isset ($ this ->skippedFieldOptions [$ customOption ['type ' ]])
232
+ ? $ this ->skippedFieldOptions [$ customOption ['type ' ]]
233
+ : [];
234
+ foreach ($ customOption ['options ' ] as &$ option ) {
235
+ // recalculate percent price
236
+ if ('Percent ' == $ option ['price_type ' ]) {
237
+ $ option ['price ' ] = ($ actualPrice * $ option ['price ' ]) / 100 ;
238
+ $ option ['price ' ] = round ($ option ['price ' ], 2 );
239
+ }
240
+
241
+ $ option = array_diff_key ($ option , array_flip ($ skippedField ));
242
+ }
243
+ $ result [$ customOption ['title ' ]] = $ customOption ;
136
244
return $ result ;
137
245
}
138
246
0 commit comments