Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductsCount.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 use Magento\Catalog\Test\Page\Category\CatalogCategoryView;
12 use Magento\Mtf\Client\BrowserInterface;
13 use Magento\Mtf\Constraint\AbstractConstraint;
14 
18 class AssertProductsCount extends AbstractConstraint
19 {
25  private $browser;
26 
32  private $catalogCategoryView;
33 
43  public function processAssert(
44  CatalogCategoryView $catalogCategoryView,
45  Category $category,
46  BrowserInterface $browser,
47  string $productsCount
48  ) {
49  $this->browser = $browser;
50  $this->catalogCategoryView = $catalogCategoryView;
51  while ($category) {
52  $parentCategory = $category->getDataFieldConfig('parent_id')['source']->getParentCategory();
53  if ($parentCategory && $parentCategory->getData('is_anchor') == 'No') {
54  $this->openCategory($parentCategory);
55  \PHPUnit\Framework\Assert::assertTrue(
56  $this->catalogCategoryView->getLayeredNavigationBlock()->isCategoryVisible(
57  $category,
58  $productsCount
59  ),
60  'Category ' . $category->getName() . ' is absent in Layered Navigation or products count is wrong'
61  );
62  }
63  $category = $parentCategory;
64  }
65  }
66 
73  private function openCategory(Category $category)
74  {
75  $categoryUrlKey = [];
76 
77  while ($category) {
78  $categoryUrlKey[] = $category->hasData('url_key')
79  ? strtolower($category->getUrlKey())
80  : trim(strtolower(preg_replace('#[^0-9a-z%]+#i', '-', $category->getName())), '-');
81 
82  $category = $category->getDataFieldConfig('parent_id')['source']->getParentCategory();
83  if ($category !== null && 1 == $category->getParentId()) {
84  $category = null;
85  }
86  }
87  $categoryUrlKey = $_ENV['app_frontend_url'] . implode('/', array_reverse($categoryUrlKey)) . '.html';
88 
89  $this->browser->open($categoryUrlKey);
90  }
91 
97  public function toString()
98  {
99  return 'Category is present in layered navigation and product is visible in product grid.';
100  }
101 }
processAssert(CatalogCategoryView $catalogCategoryView, Category $category, BrowserInterface $browser, string $productsCount)