Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractAssertForm.php
Go to the documentation of this file.
1 <?php
8 
18 abstract class AbstractAssertForm extends AbstractConstraint
19 {
25  protected $notice = "\nForm data not equals to passed from fixture:\n";
26 
32  protected $skippedFields = [];
33 
46  protected function verifyData(array $fixtureData, array $formData, $isStrict = false, $isPrepareError = true)
47  {
48  $errors = [];
49 
50  foreach ($fixtureData as $key => $value) {
51  if (in_array($key, $this->skippedFields)) {
52  continue;
53  }
54  $formValue = isset($formData[$key]) ? $formData[$key] : null;
55  if (is_numeric($formValue)) {
56  $formValue = floatval($formValue);
57  }
58 
59  if (null === $formValue) {
60  $errors[] = '- field "' . $key . '" is absent in form';
61  } elseif (is_array($value) && is_array($formValue)) {
62  $valueErrors = $this->verifyData($value, $formValue, true, false);
63  if (!empty($valueErrors)) {
64  $errors[$key] = $valueErrors;
65  }
66  } elseif ($value != $formValue) {
67  if (is_array($value)) {
68  $value = $this->arrayToString($value);
69  }
70  if (is_array($formValue)) {
71  $formValue = $this->arrayToString($formValue);
72  }
73  $errors[] = sprintf('- %s: "%s" instead of "%s"', $key, $formValue, $value);
74  }
75  }
76 
77  if ($isStrict) {
78  $diffData = array_diff(array_keys($formData), array_keys($fixtureData));
79  if ($diffData) {
80  $errors[] = '- fields ' . implode(', ', $diffData) . ' is absent in fixture';
81  }
82  }
83 
84  if ($isPrepareError) {
85  return $this->prepareErrors($errors);
86  }
87  return $errors;
88  }
89 
96  protected function sortData(array $data)
97  {
98  $scalarValues = [];
99  $arrayValues = [];
100 
101  foreach ($data as $key => $value) {
102  if (is_array($value)) {
103  $arrayValues[$key] = $this->sortData($value);
104  } else {
105  $scalarValues[$key] = $value;
106  }
107  }
108  asort($scalarValues);
109  foreach (array_keys($arrayValues) as $key) {
110  if (!is_numeric($key)) {
111  ksort($arrayValues);
112  break;
113  }
114  }
115 
116  return $scalarValues + $arrayValues;
117  }
118 
159  protected function sortDataByPath(array $data, $path)
160  {
161  $steps = explode('/', $path);
162  $key = array_shift($steps);
163  $order = null;
164  $nextPath = empty($steps) ? null : implode('/', $steps);
165 
166  if (false !== strpos($key, '::')) {
167  list($key, $order) = explode('::', $key);
168  }
169  if ($key && !isset($data[$key])) {
170  return $data;
171  }
172 
173  if ($key) {
174  if ($order) {
175  $data[$key] = $this->sortMultidimensionalArray($data[$key], $order);
176  }
177  if ($nextPath) {
178  $data[$key] = $this->sortDataByPath($data[$key], $nextPath);
179  }
180  } else {
181  $data = $this->sortMultidimensionalArray($data, $order);
182  if ($nextPath) {
183  $data = $this->sortDataByPath($data, $nextPath);
184  }
185  }
186 
187  return $data;
188  }
189 
197  protected function sortMultidimensionalArray(array $data, $orderKey)
198  {
199  $result = [];
200  foreach ($data as $key => $value) {
201  if (isset($value[$orderKey])) {
202  $key = is_numeric($value[$orderKey]) ? (int)$value[$orderKey] : $value[$orderKey];
203  }
204  $result[$key] = $value;
205  }
206  ksort($result);
207  return $result;
208  }
209 
216  protected function arrayToString(array $array)
217  {
218  $result = [];
219  foreach ($array as $key => $value) {
220  $value = is_array($value) ? $this->arrayToString($value) : $value;
221  $result[] = "{$key} => {$value}";
222  }
223 
224  return '[' . implode(', ', $result) . ']';
225  }
226 
235  protected function prepareErrors(array $errors, $notice = null, $indent = '')
236  {
237  if (empty($errors)) {
238  return '';
239  }
240 
241  $result = [];
242  foreach ($errors as $key => $error) {
243  $result[] = is_array($error)
244  ? $this->prepareErrors($error, "{$indent}{$key}:\n", $indent . "\t")
245  : ($indent . $error);
246  }
247 
248  if (null === $notice) {
250  }
251  return $notice . implode("\n", $result);
252  }
253 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$order
Definition: order.php:55
verifyData(array $fixtureData, array $formData, $isStrict=false, $isPrepareError=true)
$value
Definition: gender.phtml:16
prepareErrors(array $errors, $notice=null, $indent='')
$errors
Definition: overview.phtml:9