Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductInCategory.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Test\Page\Category\CatalogCategoryView;
11 use Magento\Cms\Test\Page\CmsIndex;
12 use Magento\Mtf\Constraint\AbstractConstraint;
13 use Magento\Mtf\Fixture\FixtureInterface;
14 
18 class AssertProductInCategory extends AbstractConstraint
19 {
29  public function processAssert(
30  CatalogCategoryView $catalogCategoryView,
31  CmsIndex $cmsIndex,
32  FixtureInterface $product,
33  Category $category
34  ) {
35  // Open category view page and check visible product
36  $categoryName = $category->getName();
37  if ($product->hasData('category_ids')) {
38  $categoryIds = $product->getCategoryIds();
39  $categoryName = is_array($categoryIds) ? reset($categoryIds) : $categoryName;
40  }
41  $cmsIndex->open();
42  $cmsIndex->getTopmenu()->selectCategoryByName($categoryName);
43 
44  $isProductVisible = $catalogCategoryView->getListProductBlock()->getProductItem($product)->isVisible();
45  while (!$isProductVisible && $catalogCategoryView->getBottomToolbar()->nextPage()) {
46  $isProductVisible = $catalogCategoryView->getListProductBlock()->getProductItem($product)->isVisible();
47  }
48 
49  \PHPUnit\Framework\Assert::assertTrue(
50  $isProductVisible,
51  'Product is absent on category page.'
52  );
53 
54  //Process price asserts
55  $this->assertPrice($product, $catalogCategoryView);
56  }
57 
65  protected function assertPrice(FixtureInterface $product, CatalogCategoryView $catalogCategoryView)
66  {
67  $priceBlock = $catalogCategoryView->getListProductBlock()->getProductItem($product)->getPriceBlock();
68 
69  \PHPUnit\Framework\Assert::assertEquals(
70  number_format($product->getPrice(), 2, '.', ''),
71  $priceBlock->isOldPriceVisible() ? $priceBlock->getOldPrice() : $priceBlock->getPrice(),
72  'Product regular price on category page is not correct.'
73  );
74 
75  if ($product->hasData('special_price')) {
76  \PHPUnit\Framework\Assert::assertEquals(
77  number_format($product->getSpecialPrice(), 2, '.', ''),
78  $priceBlock->getSpecialPrice(),
79  'Product special price on category page is not correct.'
80  );
81  }
82  }
83 
89  public function toString()
90  {
91  return 'Product price on category page correct.';
92  }
93 }
assertPrice(FixtureInterface $product, CatalogCategoryView $catalogCategoryView)
processAssert(CatalogCategoryView $catalogCategoryView, CmsIndex $cmsIndex, FixtureInterface $product, Category $category)