Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertCartItemsOptions.php
Go to the documentation of this file.
1 <?php
8 
12 use Magento\Checkout\Test\Page\CheckoutCart;
14 use Magento\Mtf\Fixture\FixtureInterface;
15 
23 {
29  protected $errorMessage = '- %s: "%s" instead of "%s"';
30 
34  protected $skippedFields = ['sku'];
35 
44  public function processAssert(CheckoutCart $checkoutCart, Cart $cart)
45  {
46  $checkoutCart->open();
48  $sourceProducts = $cart->getDataFieldConfig('items')['source'];
49  $products = $sourceProducts->getProducts();
50  $items = $cart->getItems();
51  $productsData = [];
52  $cartData = [];
53 
54  foreach ($items as $key => $item) {
56  $product = $products[$key];
57  $productName = $product->getName();
59  $checkoutItem = $item->getData();
60  $cartItem = $checkoutCart->getCartBlock()->getCartItem($product);
61 
62  $productsData[$productName] = [
63  'options' => $this->sortDataByPath($checkoutItem['options'], '::title'),
64  ];
65  $cartData[$productName] = [
66  'options' => $this->sortDataByPath($cartItem->getOptions(), '::title'),
67  ];
68  }
69 
70  $error = $this->verifyContainsData($productsData, $cartData, true);
71  \PHPUnit\Framework\Assert::assertEmpty($error, $error);
72  }
73 
83  protected function verifyContainsData(
84  array $fixtureData,
85  array $formData,
86  $isStrict = false,
87  $isPrepareError = true
88  ) {
89  $errors = [];
90 
91  foreach ($fixtureData as $key => $value) {
92  if (in_array($key, $this->skippedFields)) {
93  continue;
94  }
95 
96  $formValue = isset($formData[$key]) ? $formData[$key] : null;
97  if ($formValue && !is_array($formValue)) {
98  $formValue = trim($formValue, '. ');
99  }
100 
101  if (null === $formValue) {
102  $errors[] = '- field "' . $key . '" is absent in form';
103  } elseif (is_array($value) && is_array($formValue)) {
104  $valueErrors = $this->verifyContainsData($value, $formValue, true, false);
105  if (!empty($valueErrors)) {
106  $errors[$key] = $valueErrors;
107  }
108  } elseif (($key == 'value') && $this->equals($fixtureData['value'], $formData['value'])) {
109  $errors[] = $this->errorFormat($value, $formValue, $key);
110  } elseif (null === strpos($value, $formValue)) {
111  $errors[] = $this->errorFormat($value, $formValue, $key);
112  }
113  }
114 
115  if ($isStrict) {
116  $diffData = array_diff(array_keys($formData), array_keys($fixtureData));
117  if ($diffData) {
118  $errors[] = '- fields ' . implode(', ', $diffData) . ' is absent in fixture';
119  }
120  }
121 
122  if ($isPrepareError) {
123  return $this->prepareErrors($errors);
124  }
125  return $errors;
126  }
127 
135  protected function equals($expected, $actual)
136  {
137  return (null === strpos($expected, $actual));
138  }
139 
148  protected function errorFormat($value, $formValue, $key)
149  {
150  if (is_array($value)) {
151  $value = $this->arrayToString($value);
152  }
153  if (is_array($formValue)) {
154  $formValue = $this->arrayToString($formValue);
155  }
156 
157  return sprintf($this->errorMessage, $key, $formValue, $value);
158  }
159 
165  public function toString()
166  {
167  return 'Product options on the page match.';
168  }
169 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$productsData
Definition: products.php:19
$value
Definition: gender.phtml:16
verifyContainsData(array $fixtureData, array $formData, $isStrict=false, $isPrepareError=true)
prepareErrors(array $errors, $notice=null, $indent='')
$errors
Definition: overview.phtml:9
$items