Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReindexRuleGroupWebsiteTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class ReindexRuleGroupWebsiteTest extends \PHPUnit\Framework\TestCase
13 {
17  private $model;
18 
22  private $dateTimeMock;
23 
27  private $resourceMock;
28 
32  private $activeTableSwitcherMock;
33 
37  private $tableSwapperMock;
38 
39  protected function setUp()
40  {
41  $this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
42  ->disableOriginalConstructor()
43  ->getMock();
44  $this->resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47  $this->activeTableSwitcherMock =
48  $this->getMockBuilder(ActiveTableSwitcher::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51  $this->tableSwapperMock = $this->getMockForAbstractClass(
52  IndexerTableSwapperInterface::class
53  );
54  $this->model = new \Magento\CatalogRule\Model\Indexer\ReindexRuleGroupWebsite(
55  $this->dateTimeMock,
56  $this->resourceMock,
57  $this->activeTableSwitcherMock,
58  $this->tableSwapperMock
59  );
60  }
61 
62  public function testExecute()
63  {
64  $timeStamp = (int)gmdate('U');
65  $insertString = 'insert_string';
66  $connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)->getMock();
67  $this->resourceMock->expects($this->at(0))->method('getConnection')->willReturn($connectionMock);
68  $this->dateTimeMock->expects($this->once())->method('gmtTimestamp')->willReturn($timeStamp);
69 
70  $this->tableSwapperMock->expects($this->any())
71  ->method('getWorkingTableName')
72  ->willReturnMap(
73  [
74  ['catalogrule_group_website', 'catalogrule_group_website_replica'],
75  ['catalogrule_product', 'catalogrule_product_replica'],
76  ]
77  );
78 
79  $this->resourceMock->expects($this->any())
80  ->method('getTableName')
81  ->willReturnMap(
82  [
83  ['catalogrule_group_website', 'default', 'catalogrule_group_website'],
84  ['catalogrule_product', 'default', 'catalogrule_product'],
85  ['catalogrule_group_website_replica', 'default', 'catalogrule_group_website_replica'],
86  ['catalogrule_product_replica', 'default', 'catalogrule_product_replica'],
87  ]
88  );
89 
90  $selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
91  ->disableOriginalConstructor()
92  ->getMock();
93 
94  $connectionMock->expects($this->once())->method('delete')->with('catalogrule_group_website_replica');
95  $connectionMock->expects($this->once())->method('select')->willReturn($selectMock);
96 
97  $selectMock->expects($this->once())->method('distinct')->with(true)->willReturnSelf();
98  $selectMock->expects($this->once())
99  ->method('from')
100  ->with('catalogrule_product_replica', ['rule_id', 'customer_group_id', 'website_id'])
101  ->willReturnSelf();
102  $selectMock->expects($this->once())
103  ->method('where')
104  ->with("{$timeStamp} >= from_time AND (({$timeStamp} <= to_time AND to_time > 0) OR to_time = 0)")
105  ->willReturnSelf();
106  $selectMock->expects($this->once())
107  ->method('insertFromSelect')
108  ->with('catalogrule_group_website_replica', ['rule_id', 'customer_group_id', 'website_id'])
109  ->willReturn($insertString);
110  $connectionMock->expects($this->once())->method('query')->with($insertString);
111 
112  $this->assertTrue($this->model->execute(true));
113  }
114 }