Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SubscriptionTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\Mview\View\Subscription;
10 
11 class SubscriptionTest extends \PHPUnit\Framework\TestCase
12 {
18  protected $connectionMock;
19 
21  protected $model;
22 
24  protected $resourceMock;
25 
28 
31 
33  protected $viewMock;
34 
36  private $tableName;
37 
38  protected function setUp()
39  {
40  $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
41  $this->resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
42 
43  $this->connectionMock->expects($this->any())
44  ->method('quoteIdentifier')
45  ->will($this->returnArgument(0));
46 
47  $this->resourceMock->expects($this->atLeastOnce())
48  ->method('getConnection')
49  ->willReturn($this->connectionMock);
50 
51  $this->triggerFactoryMock = $this->createMock(\Magento\Framework\DB\Ddl\TriggerFactory::class);
52  $this->viewCollectionMock = $this->getMockForAbstractClass(
53  \Magento\Framework\Mview\View\CollectionInterface::class,
54  [],
55  '',
56  false,
57  false,
58  true,
59  []
60  );
61  $this->viewMock = $this->getMockForAbstractClass(
62  \Magento\Framework\Mview\ViewInterface::class,
63  [],
64  '',
65  false,
66  false,
67  true,
68  []
69  );
70 
71  $this->resourceMock->expects($this->any())
72  ->method('getTableName')
73  ->will($this->returnArgument(0));
74 
75  $this->model = new Subscription(
76  $this->resourceMock,
77  $this->triggerFactoryMock,
78  $this->viewCollectionMock,
79  $this->viewMock,
80  $this->tableName,
81  'columnName'
82  );
83  }
84 
85  public function testGetView()
86  {
87  $this->assertEquals($this->viewMock, $this->model->getView());
88  }
89 
90  public function testGetTableName()
91  {
92  $this->assertEquals($this->tableName, $this->model->getTableName());
93  }
94 
95  public function testGetColumnName()
96  {
97  $this->assertEquals('columnName', $this->model->getColumnName());
98  }
99 
103  public function testCreate()
104  {
105  $triggerName = 'trigger_name';
106  $this->resourceMock->expects($this->atLeastOnce())->method('getTriggerName')->willReturn($triggerName);
107  $triggerMock = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Trigger::class)
108  ->setMethods(['setName', 'getName', 'setTime', 'setEvent', 'setTable', 'addStatement'])
109  ->disableOriginalConstructor()
110  ->getMock();
111  $triggerMock->expects($this->exactly(3))
112  ->method('setName')
113  ->with($triggerName)
114  ->will($this->returnSelf());
115  $triggerMock->expects($this->exactly(3))
116  ->method('getName')
117  ->will($this->returnValue('triggerName'));
118  $triggerMock->expects($this->exactly(3))
119  ->method('setTime')
120  ->with(\Magento\Framework\DB\Ddl\Trigger::TIME_AFTER)
121  ->will($this->returnSelf());
122  $triggerMock->expects($this->exactly(3))
123  ->method('setEvent')
124  ->will($this->returnSelf());
125  $triggerMock->expects($this->exactly(3))
126  ->method('setTable')
127  ->with($this->tableName)
128  ->will($this->returnSelf());
129 
130  $triggerMock->expects($this->at(4))
131  ->method('addStatement')
132  ->with("INSERT IGNORE INTO test_view_cl (entity_id) VALUES (NEW.columnName);")
133  ->will($this->returnSelf());
134 
135  $triggerMock->expects($this->at(5))
136  ->method('addStatement')
137  ->with("INSERT IGNORE INTO other_test_view_cl (entity_id) VALUES (NEW.columnName);")
138  ->will($this->returnSelf());
139 
140  $triggerMock->expects($this->at(11))
141  ->method('addStatement')
142  ->with("INSERT IGNORE INTO test_view_cl (entity_id) VALUES (NEW.columnName);")
143  ->will($this->returnSelf());
144 
145  $triggerMock->expects($this->at(12))
146  ->method('addStatement')
147  ->with("INSERT IGNORE INTO other_test_view_cl (entity_id) VALUES (NEW.columnName);")
148  ->will($this->returnSelf());
149 
150  $triggerMock->expects($this->at(18))
151  ->method('addStatement')
152  ->with("INSERT IGNORE INTO test_view_cl (entity_id) VALUES (OLD.columnName);")
153  ->will($this->returnSelf());
154 
155  $triggerMock->expects($this->at(19))
156  ->method('addStatement')
157  ->with("INSERT IGNORE INTO other_test_view_cl (entity_id) VALUES (OLD.columnName);")
158  ->will($this->returnSelf());
159 
160  $changelogMock = $this->getMockForAbstractClass(
161  \Magento\Framework\Mview\View\ChangelogInterface::class,
162  [],
163  '',
164  false,
165  false,
166  true,
167  []
168  );
169  $changelogMock->expects($this->exactly(3))
170  ->method('getName')
171  ->will($this->returnValue('test_view_cl'));
172  $changelogMock->expects($this->exactly(3))
173  ->method('getColumnName')
174  ->will($this->returnValue('entity_id'));
175 
176  $this->viewMock->expects($this->exactly(3))
177  ->method('getChangelog')
178  ->will($this->returnValue($changelogMock));
179 
180  $this->triggerFactoryMock->expects($this->exactly(3))
181  ->method('create')
182  ->will($this->returnValue($triggerMock));
183 
184  $otherChangelogMock = $this->getMockForAbstractClass(
185  \Magento\Framework\Mview\View\ChangelogInterface::class,
186  [],
187  '',
188  false,
189  false,
190  true,
191  []
192  );
193  $otherChangelogMock->expects($this->exactly(3))
194  ->method('getName')
195  ->will($this->returnValue('other_test_view_cl'));
196  $otherChangelogMock->expects($this->exactly(3))
197  ->method('getColumnName')
198  ->will($this->returnValue('entity_id'));
199 
200  $otherViewMock = $this->getMockForAbstractClass(
201  \Magento\Framework\Mview\ViewInterface::class,
202  [],
203  '',
204  false,
205  false,
206  true,
207  []
208  );
209  $otherViewMock->expects($this->exactly(1))
210  ->method('getId')
211  ->will($this->returnValue('other_id'));
212  $otherViewMock->expects($this->exactly(1))
213  ->method('getSubscriptions')
214  ->will($this->returnValue([['name' => $this->tableName], ['name' => 'otherTableName']]));
215  $otherViewMock->expects($this->exactly(3))
216  ->method('getChangelog')
217  ->will($this->returnValue($otherChangelogMock));
218 
219  $this->viewMock->expects($this->exactly(3))
220  ->method('getId')
221  ->will($this->returnValue('this_id'));
222  $this->viewMock->expects($this->never())
223  ->method('getSubscriptions');
224 
225  $this->viewCollectionMock->expects($this->exactly(1))
226  ->method('getViewsByStateMode')
227  ->with(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)
228  ->will($this->returnValue([$this->viewMock, $otherViewMock]));
229 
230  $this->connectionMock->expects($this->exactly(3))
231  ->method('dropTrigger')
232  ->with('triggerName')
233  ->will($this->returnValue(true));
234  $this->connectionMock->expects($this->exactly(3))
235  ->method('createTrigger')
236  ->with($triggerMock);
237 
238  $this->model->create();
239  }
240 
241  public function testRemove()
242  {
243  $triggerMock = $this->createMock(\Magento\Framework\DB\Ddl\Trigger::class);
244  $triggerMock->expects($this->exactly(3))
245  ->method('setName')
246  ->will($this->returnSelf());
247  $triggerMock->expects($this->exactly(3))
248  ->method('getName')
249  ->will($this->returnValue('triggerName'));
250  $triggerMock->expects($this->exactly(3))
251  ->method('setTime')
252  ->with(\Magento\Framework\DB\Ddl\Trigger::TIME_AFTER)
253  ->will($this->returnSelf());
254  $triggerMock->expects($this->exactly(3))
255  ->method('setEvent')
256  ->will($this->returnSelf());
257  $triggerMock->expects($this->exactly(3))
258  ->method('setTable')
259  ->with($this->tableName)
260  ->will($this->returnSelf());
261  $triggerMock->expects($this->exactly(3))
262  ->method('addStatement')
263  ->will($this->returnSelf());
264 
265  $this->triggerFactoryMock->expects($this->exactly(3))
266  ->method('create')
267  ->will($this->returnValue($triggerMock));
268 
269  $otherChangelogMock = $this->getMockForAbstractClass(
270  \Magento\Framework\Mview\View\ChangelogInterface::class,
271  [],
272  '',
273  false,
274  false,
275  true,
276  []
277  );
278  $otherChangelogMock->expects($this->exactly(3))
279  ->method('getName')
280  ->will($this->returnValue('other_test_view_cl'));
281  $otherChangelogMock->expects($this->exactly(3))
282  ->method('getColumnName')
283  ->will($this->returnValue('entity_id'));
284 
285  $otherViewMock = $this->getMockForAbstractClass(
286  \Magento\Framework\Mview\ViewInterface::class,
287  [],
288  '',
289  false,
290  false,
291  true,
292  []
293  );
294  $otherViewMock->expects($this->exactly(1))
295  ->method('getId')
296  ->will($this->returnValue('other_id'));
297  $otherViewMock->expects($this->exactly(1))
298  ->method('getSubscriptions')
299  ->will($this->returnValue([['name' => $this->tableName], ['name' => 'otherTableName']]));
300  $otherViewMock->expects($this->exactly(3))
301  ->method('getChangelog')
302  ->will($this->returnValue($otherChangelogMock));
303 
304  $this->viewMock->expects($this->exactly(3))
305  ->method('getId')
306  ->will($this->returnValue('this_id'));
307  $this->viewMock->expects($this->never())
308  ->method('getSubscriptions');
309 
310  $this->viewCollectionMock->expects($this->exactly(1))
311  ->method('getViewsByStateMode')
312  ->with(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)
313  ->will($this->returnValue([$this->viewMock, $otherViewMock]));
314 
315  $this->connectionMock->expects($this->exactly(3))
316  ->method('dropTrigger')
317  ->with('triggerName')
318  ->will($this->returnValue(true));
319 
320  $triggerMock->expects($this->exactly(3))
321  ->method('getStatements')
322  ->will($this->returnValue(true));
323 
324  $this->connectionMock->expects($this->exactly(3))
325  ->method('createTrigger')
326  ->with($triggerMock);
327 
328  $this->model->remove();
329  }
330 }