Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductInventoryThreshold.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Catalog\Test\Page\Product\CatalogProductView;
10 use Magento\Mtf\Client\BrowserInterface;
11 use Magento\Mtf\Constraint\AbstractConstraint;
12 use Magento\Mtf\Fixture\FixtureInterface;
13 use Magento\Mtf\Fixture\FixtureFactory;
14 
18 class AssertProductInventoryThreshold extends AbstractConstraint
19 {
23  const SUCCESS_MESSAGE = 'Only %s left';
24 
35  public function processAssert(
36  BrowserInterface $browser,
37  FixtureInterface $product,
38  FixtureFactory $fixtureFactory,
39  CatalogProductView $catalogProductView,
40  array $threshold
41  ) {
42  foreach ($threshold as $thresholdItem) {
43  $this->buyProduct($fixtureFactory, $product, $thresholdItem['qty']);
44  $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
45  $isThresholdMessageDisplayed = $catalogProductView->getViewBlock()
46  ->isThresholdMessageDisplayed($browser, $product);
47  \PHPUnit\Framework\Assert::assertEquals(
48  $isThresholdMessageDisplayed,
49  $thresholdItem['is_message_displayed'],
50  'Product inventory threshold message display is not correct.'
51  );
52  if ($thresholdItem['is_message_displayed']) {
53  \PHPUnit\Framework\Assert::assertEquals(
54  sprintf(self::SUCCESS_MESSAGE, $thresholdItem['expected']),
55  $catalogProductView->getViewBlock()->getThresholdMessage(),
56  'Product inventory success message is not displayed.'
57  );
58  }
59  }
60  }
61 
70  private function buyProduct(FixtureFactory $fixtureFactory, FixtureInterface $product, $qty)
71  {
72  $newProduct = $fixtureFactory->createByCode(
73  'catalogProductSimple',
74  ['data' => ['sku' => $product->getSku(), 'checkout_data' => ['qty' => $qty]]]
75  );
76  $order = $fixtureFactory->createByCode('orderInjectable', [
77  'dataset' => 'guest',
78  'data' => [
79  'entity_id' => [
80  'products' => [$newProduct]
81  ]
82  ]
83  ]);
84  $order->persist();
85  }
86 
92  public function toString()
93  {
94  return 'Product inventory threshold message display is correct.';
95  }
96 }
processAssert(BrowserInterface $browser, FixtureInterface $product, FixtureFactory $fixtureFactory, CatalogProductView $catalogProductView, array $threshold)
$order
Definition: order.php:55