Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertAdvancedSearchProductsResult.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\CatalogSearch\Test\Page\AdvancedResult;
11 use Magento\Mtf\Constraint\AbstractConstraint;
12 
16 class AssertAdvancedSearchProductsResult extends AbstractConstraint
17 {
21  const NOTICE_MESSAGE = "Don't see what you're looking for?";
22 
26  const ERROR_MESSAGE = 'We can\'t find any items matching these search criteria.';
27 
33  protected $resultPage;
34 
40  protected $placeholder = [
41  'tax_class_id' => 'tax_class',
42  ];
43 
53  public function processAssert(
54  array $products,
55  AdvancedResult $resultPage,
56  array $productsSearch,
57  CatalogProductSimple $productSearch
58  ) {
59  $this->resultPage = $resultPage;
60  $searchResult = [];
61  foreach ($products as $key => $value) {
62  if ($value === 'Yes') {
64  $searchResult[$productsSearch[$key]->getSku()] = $productsSearch[$key];
65  }
66  }
67 
68  $errors = $this->checkSearchData($searchResult, $productSearch);
69  foreach ($searchResult as $sku => $product) {
71  $name = $product->getName();
72  do {
73  $isProductVisible = $resultPage->getListProductBlock()->getProductItem($product)->isVisible();
74  } while (!$isProductVisible && $resultPage->getBottomToolbar()->nextPage());
75 
76  if (!$isProductVisible) {
77  $errors[] = '- failed to find the product (SKU - "'
78  . $sku . '", name - "' . $name . '") according to the search parameters';
79  }
80  }
81 
82  \PHPUnit\Framework\Assert::assertTrue(
83  empty($errors),
84  "The following errors occurred:\n" . implode("\n", $errors)
85  );
86  }
87 
95  protected function checkSearchData(array $searchResult, CatalogProductSimple $productSearch)
96  {
97  $searchBlock = $this->resultPage->getSearchResultBlock();
98  $errors = [];
99  $textMessage = self::NOTICE_MESSAGE;
100  if (empty($searchResult)) {
101  $textMessage = self::ERROR_MESSAGE;
102  }
103 
104  if (!$searchBlock->isVisibleMessages($textMessage)) {
105  $errors[] = '- message does not match the search script';
106  }
107 
108  $searchData = $searchBlock->getSearchSummaryItems();
109  $productData = $this->prepareFixtureData($productSearch);
110  foreach ($productData as $key => $data) {
111  if (!isset($searchData[$key])) {
112  $errors[] = '- "' . $key . '" not found on the page';
113  } elseif ($searchData[$key] !== $data) {
114  $errors[] = '- "' . $key . '" value does not match the page';
115  }
116  }
117 
118  return $errors;
119  }
120 
127  protected function prepareFixtureData(CatalogProductSimple $productSearch)
128  {
129  $compareData = [];
130  foreach ($productSearch->getData() as $key => $value) {
131  if ($key === 'price') {
132  if (isset($value['price_from'])) {
133  $compareData[$key][] = $value['price_from'];
134  }
135  if (isset($value['price_to'])) {
136  $compareData[$key][] = $value['price_to'];
137  }
138  } else {
139  $index = isset($this->placeholder[$key]) ? $this->placeholder[$key] : $key;
140  $compareData[$index][] = $value;
141  }
142  }
143  unset($compareData['url_key']);
144 
145  return $compareData;
146  }
147 
153  public function toString()
154  {
155  return 'All products are involved in the search were found successfully.';
156  }
157 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
$productData
$index
Definition: list.phtml:44
$errors
Definition: overview.phtml:9
if(!isset($_GET['name'])) $name
Definition: log.php:14