Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RuleProductPricesPersistorTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class RuleProductPricesPersistorTest 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::class)
42  ->disableOriginalConstructor()
43  ->getMock();
44  $this->resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47  $this->activeTableSwitcherMock = $this->getMockBuilder(ActiveTableSwitcher::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50  $this->tableSwapperMock = $this->getMockForAbstractClass(
51  IndexerTableSwapperInterface::class
52  );
53  $this->model = new \Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor(
54  $this->dateTimeMock,
55  $this->resourceMock,
56  $this->activeTableSwitcherMock,
57  $this->tableSwapperMock
58  );
59  }
60 
62  {
63  $this->assertFalse($this->model->execute([]));
64  }
65 
66  public function testExecute()
67  {
68  $priceData = [
69  [
70  'product_id' => 1,
71  'rule_date' => '2017-05-01',
72  'latest_start_date' => '2017-05-10',
73  'earliest_end_date' => '2017-05-20',
74  ]
75  ];
76  $tableName = 'catalogrule_product_price_replica';
77 
78  $this->tableSwapperMock->expects($this->once())
79  ->method('getWorkingTableName')
80  ->with('catalogrule_product_price')
81  ->willReturn($tableName);
82 
83  $connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($connectionMock);
87  $this->resourceMock->expects($this->at(1))
88  ->method('getTableName')
89  ->with('catalogrule_product_price')
90  ->willReturn('catalogrule_product_price');
91  $this->resourceMock->expects($this->at(2))
92  ->method('getTableName')
93  ->with($tableName)
94  ->willReturn($tableName);
95 
96  $this->dateTimeMock->expects($this->at(0))
97  ->method('formatDate')
98  ->with($priceData[0]['rule_date'], false)
99  ->willReturn($priceData[0]['rule_date']);
100 
101  $this->dateTimeMock->expects($this->at(1))
102  ->method('formatDate')
103  ->with($priceData[0]['latest_start_date'], false)
104  ->willReturn($priceData[0]['latest_start_date']);
105 
106  $this->dateTimeMock->expects($this->at(2))
107  ->method('formatDate')
108  ->with($priceData[0]['earliest_end_date'], false)
109  ->willReturn($priceData[0]['earliest_end_date']);
110 
111  $connectionMock->expects($this->once())
112  ->method('insertOnDuplicate')
113  ->with($tableName, $priceData);
114 
115  $this->assertTrue($this->model->execute($priceData, true));
116  }
117 
122  public function testExecuteWithException()
123  {
124  $priceData = [
125  [
126  'product_id' => 1,
127  'rule_date' => '2017-05-5',
128  'latest_start_date' => '2017-05-10',
129  'earliest_end_date' => '2017-05-22',
130  ]
131  ];
132  $tableName = 'catalogrule_product_price_replica';
133 
134  $this->tableSwapperMock->expects($this->once())
135  ->method('getWorkingTableName')
136  ->with('catalogrule_product_price')
137  ->willReturn($tableName);
138 
139  $this->dateTimeMock->expects($this->at(0))
140  ->method('formatDate')
141  ->with($priceData[0]['rule_date'], false)
142  ->willReturn($priceData[0]['rule_date']);
143 
144  $this->dateTimeMock->expects($this->at(1))
145  ->method('formatDate')
146  ->with($priceData[0]['latest_start_date'], false)
147  ->willReturn($priceData[0]['latest_start_date']);
148 
149  $this->dateTimeMock->expects($this->at(2))
150  ->method('formatDate')
151  ->with($priceData[0]['earliest_end_date'], false)
152  ->willReturn($priceData[0]['earliest_end_date']);
153 
154  $connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
155  ->disableOriginalConstructor()
156  ->getMock();
157  $connectionMock->expects($this->once())
158  ->method('insertOnDuplicate')
159  ->with($tableName, $priceData)
160  ->willThrowException(new \Exception('Insert error.'));
161 
162  $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($connectionMock);
163  $this->resourceMock->expects($this->at(1))
164  ->method('getTableName')
165  ->with('catalogrule_product_price')
166  ->willReturn('catalogrule_product_price');
167  $this->resourceMock->expects($this->at(2))
168  ->method('getTableName')
169  ->with($tableName)
170  ->willReturn($tableName);
171 
172  $this->assertTrue($this->model->execute($priceData, true));
173  }
174 }
$tableName
Definition: trigger.php:13