Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReindexRuleProductTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class ReindexRuleProductTest extends \PHPUnit\Framework\TestCase
13 {
17  private $model;
18 
22  private $resourceMock;
23 
27  private $activeTableSwitcherMock;
28 
32  private $tableSwapperMock;
33 
34  protected function setUp()
35  {
36  $this->resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
37  ->disableOriginalConstructor()
38  ->getMock();
39  $this->activeTableSwitcherMock = $this->getMockBuilder(ActiveTableSwitcher::class)
40  ->disableOriginalConstructor()
41  ->getMock();
42  $this->tableSwapperMock = $this->getMockForAbstractClass(
43  IndexerTableSwapperInterface::class
44  );
45  $this->model = new \Magento\CatalogRule\Model\Indexer\ReindexRuleProduct(
46  $this->resourceMock,
47  $this->activeTableSwitcherMock,
48  $this->tableSwapperMock
49  );
50  }
51 
52  public function testExecuteIfRuleInactive()
53  {
54  $ruleMock = $this->getMockBuilder(\Magento\CatalogRule\Model\Rule::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $ruleMock->expects($this->once())->method('getIsActive')->willReturn(false);
58  $this->assertFalse($this->model->execute($ruleMock, 100, true));
59  }
60 
62  {
63  $ruleMock = $this->getMockBuilder(\Magento\CatalogRule\Model\Rule::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66  $ruleMock->expects($this->once())->method('getIsActive')->willReturn(true);
67  $ruleMock->expects($this->once())->method('getWebsiteIds')->willReturn(null);
68  $this->assertFalse($this->model->execute($ruleMock, 100, true));
69  }
70 
71  public function testExecute()
72  {
73  $productIds = [
74  4 => [1 => 1],
75  5 => [1 => 1],
76  6 => [1 => 1],
77  ];
78  $ruleMock = $this->getMockBuilder(\Magento\CatalogRule\Model\Rule::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81  $ruleMock->expects($this->once())->method('getIsActive')->willReturn(true);
82  $ruleMock->expects($this->exactly(2))->method('getWebsiteIds')->willReturn(1);
83  $ruleMock->expects($this->once())->method('getMatchingProductIds')->willReturn($productIds);
84 
85  $this->tableSwapperMock->expects($this->once())
86  ->method('getWorkingTableName')
87  ->with('catalogrule_product')
88  ->willReturn('catalogrule_product_replica');
89 
90  $connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
91  ->disableOriginalConstructor()
92  ->getMock();
93  $this->resourceMock->expects($this->at(0))->method('getConnection')->willReturn($connectionMock);
94  $this->resourceMock->expects($this->at(1))
95  ->method('getTableName')
96  ->with('catalogrule_product')
97  ->willReturn('catalogrule_product');
98  $this->resourceMock->expects($this->at(2))
99  ->method('getTableName')
100  ->with('catalogrule_product_replica')
101  ->willReturn('catalogrule_product_replica');
102 
103  $ruleMock->expects($this->once())->method('getId')->willReturn(100);
104  $ruleMock->expects($this->once())->method('getCustomerGroupIds')->willReturn([10]);
105  $ruleMock->expects($this->once())->method('getFromDate')->willReturn('2017-06-21');
106  $ruleMock->expects($this->once())->method('getToDate')->willReturn('2017-06-30');
107  $ruleMock->expects($this->once())->method('getSortOrder')->willReturn(1);
108  $ruleMock->expects($this->once())->method('getSimpleAction')->willReturn('simple_action');
109  $ruleMock->expects($this->once())->method('getDiscountAmount')->willReturn(43);
110  $ruleMock->expects($this->once())->method('getStopRulesProcessing')->willReturn(true);
111 
112  $batchRows = [
113  [
114  'rule_id' => 100,
115  'from_time' => 1498028400,
116  'to_time' => 1498892399,
117  'website_id' => 1,
118  'customer_group_id' => 10,
119  'product_id' => 4,
120  'action_operator' => 'simple_action',
121  'action_amount' => 43,
122  'action_stop' => true,
123  'sort_order' => 1,
124  ],
125  [
126  'rule_id' => 100,
127  'from_time' => 1498028400,
128  'to_time' => 1498892399,
129  'website_id' => 1,
130  'customer_group_id' => 10,
131  'product_id' => 5,
132  'action_operator' => 'simple_action',
133  'action_amount' => 43,
134  'action_stop' => true,
135  'sort_order' => 1,
136  ]
137  ];
138 
139  $rowsNotInBatch = [
140  [
141  'rule_id' => 100,
142  'from_time' => 1498028400,
143  'to_time' => 1498892399,
144  'website_id' => 1,
145  'customer_group_id' => 10,
146  'product_id' => 6,
147  'action_operator' => 'simple_action',
148  'action_amount' => 43,
149  'action_stop' => true,
150  'sort_order' => 1,
151  ]
152  ];
153 
154  $connectionMock->expects($this->at(0))
155  ->method('insertMultiple')
156  ->with('catalogrule_product_replica', $batchRows);
157  $connectionMock->expects($this->at(1))
158  ->method('insertMultiple')
159  ->with('catalogrule_product_replica', $rowsNotInBatch);
160 
161  $this->assertTrue($this->model->execute($ruleMock, 2, true));
162  }
163 }