Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EraserTest.php
Go to the documentation of this file.
1 <?php
9 
10 class EraserTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $connection;
16 
20  protected $indexerHelper;
21 
25  protected $storeManager;
26 
30  protected $model;
31 
32  protected function setUp()
33  {
34  $resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
35  $this->connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
36  $resource->expects($this->any())->method('getConnection')->will($this->returnValue($this->connection));
37  $this->indexerHelper = $this->createMock(\Magento\Catalog\Helper\Product\Flat\Indexer::class);
38  $this->indexerHelper->expects($this->any())->method('getTable')->will($this->returnArgument(0));
39  $this->indexerHelper->expects($this->any())->method('getFlatTableName')->will($this->returnValueMap([
40  [1, 'store_1_flat'],
41  [2, 'store_2_flat'],
42  ]));
43 
44  $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
45  $this->model = new \Magento\Catalog\Model\Indexer\Product\Flat\Action\Eraser(
46  $resource,
47  $this->indexerHelper,
48  $this->storeManager
49  );
50  }
51 
52  public function testRemoveDeletedProducts()
53  {
54  $productsToDeleteIds = [1, 2];
55  $select = $this->createMock(\Magento\Framework\DB\Select::class);
56  $select->expects($this->once())->method('from')->with('catalog_product_entity')->will($this->returnSelf());
57  $select->expects($this->once())->method('where')->with('entity_id IN(?)', $productsToDeleteIds)
58  ->will($this->returnSelf());
59  $products = [['entity_id' => 2]];
60  $statement = $this->createMock(\Zend_Db_Statement_Interface::class);
61  $statement->expects($this->once())->method('fetchAll')->will($this->returnValue($products));
62  $this->connection->expects($this->once())->method('query')->with($select)
63  ->will($this->returnValue($statement));
64  $this->connection->expects($this->once())->method('select')->will($this->returnValue($select));
65  $this->connection->expects($this->once())->method('delete')
66  ->with('store_1_flat', ['entity_id IN(?)' => [1]]);
67 
68  $this->model->removeDeletedProducts($productsToDeleteIds, 1);
69  }
70 
72  {
73  $store1 = $this->createMock(\Magento\Store\Model\Store::class);
74  $store1->expects($this->any())->method('getId')->will($this->returnValue(1));
75  $store2 = $this->createMock(\Magento\Store\Model\Store::class);
76  $store2->expects($this->any())->method('getId')->will($this->returnValue(2));
77  $this->storeManager->expects($this->once())->method('getStores')
78  ->will($this->returnValue([$store1, $store2]));
79  $this->connection->expects($this->at(0))->method('delete')
80  ->with('store_1_flat', ['entity_id IN(?)' => [1]]);
81  $this->connection->expects($this->at(1))->method('delete')
82  ->with('store_2_flat', ['entity_id IN(?)' => [1]]);
83 
84  $this->model->deleteProductsFromStore(1);
85  }
86 }
$resource
Definition: bulk.php:12