Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductAssert.php
Go to the documentation of this file.
1 <?php
7 
15 {
20 
24  protected $expressionFactory;
25 
30  public function __construct(
31  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
32  \Magento\Framework\DB\Sql\ColumnValueExpressionFactory $expressionFactory
33  ) {
34  $this->productCollectionFactory = $productCollectionFactory;
35  $this->expressionFactory = $expressionFactory;
36  }
37 
47  public function assertProductsCount($skuPattern, $expectedCount)
48  {
49  $productSkuPattern = str_replace('%s', '[0-9]+', $skuPattern);
50  $productCollection = $this->productCollectionFactory->create();
52  ->getSelect()
53  ->where('sku ?', $this->expressionFactory->create([
54  'expression' => 'REGEXP \'^' . $productSkuPattern . '$\''
55  ]));
56 
57  if ($expectedCount !== count($productCollection)) {
58  throw new \AssertionError(
59  sprintf(
60  'Expected amount of products with sku pattern "%s" not equals actual amount',
61  $skuPattern
62  )
63  );
64  }
65  }
66 
74  public function assertProductType($expectedProductType, $product)
75  {
76  if ($expectedProductType !== $product->getTypeId()) {
77  throw new \AssertionError('Product type is wrong');
78  }
79  }
80 }
__construct(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Framework\DB\Sql\ColumnValueExpressionFactory $expressionFactory)
assertProductType($expectedProductType, $product)