Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DbStorageTest.php
Go to the documentation of this file.
1 <?php
8 
13 
14 class DbStorageTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $urlRewriteFactory;
20 
24  protected $dataObjectHelper;
25 
29  protected $connectionMock;
30 
34  protected $select;
35 
39  protected $resource;
40 
44  protected $storage;
45 
46  protected function setUp()
47  {
48  $this->urlRewriteFactory = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory::class)
49  ->setMethods(['create'])
50  ->disableOriginalConstructor()->getMock();
51  $this->dataObjectHelper = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
52  $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
53  $this->select = $this->getMockBuilder(Select::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
57 
58  $this->resource->expects($this->any())
59  ->method('getConnection')
60  ->will($this->returnValue($this->connectionMock));
61  $this->connectionMock->expects($this->any())
62  ->method('select')
63  ->will($this->returnValue($this->select));
64 
65  $this->storage = (new ObjectManager($this))->getObject(
66  \Magento\UrlRewrite\Model\Storage\DbStorage::class,
67  [
68  'urlRewriteFactory' => $this->urlRewriteFactory,
69  'dataObjectHelper' => $this->dataObjectHelper,
70  'resource' => $this->resource,
71  ]
72  );
73  }
74 
75  public function testFindAllByData()
76  {
77  $data = ['col1' => 'val1', 'col2' => 'val2'];
78 
79  $this->select->expects($this->at(1))
80  ->method('where')
81  ->with('col1 IN (?)', 'val1');
82 
83  $this->select->expects($this->at(2))
84  ->method('where')
85  ->with('col2 IN (?)', 'val2');
86 
87  $this->connectionMock->expects($this->any())
88  ->method('quoteIdentifier')
89  ->will($this->returnArgument(0));
90 
91  $this->connectionMock->expects($this->once())
92  ->method('fetchAll')
93  ->with($this->select)
94  ->will($this->returnValue([['row1'], ['row2']]));
95 
96  $this->dataObjectHelper->expects($this->at(0))
97  ->method('populateWithArray')
98  ->with(['urlRewrite1'], ['row1'], \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
99  ->will($this->returnSelf());
100 
101  $this->urlRewriteFactory->expects($this->at(0))
102  ->method('create')
103  ->will($this->returnValue(['urlRewrite1']));
104 
105  $this->dataObjectHelper->expects($this->at(1))
106  ->method('populateWithArray')
107  ->with(['urlRewrite2'], ['row2'], \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
108  ->will($this->returnSelf());
109 
110  $this->urlRewriteFactory->expects($this->at(1))
111  ->method('create')
112  ->will($this->returnValue(['urlRewrite2']));
113 
114  $this->assertEquals([['urlRewrite1'], ['urlRewrite2']], $this->storage->findAllByData($data));
115  }
116 
117  public function testFindOneByData()
118  {
119  $data = ['col1' => 'val1', 'col2' => 'val2'];
120 
121  $this->select->expects($this->at(1))
122  ->method('where')
123  ->with('col1 IN (?)', 'val1');
124 
125  $this->select->expects($this->at(2))
126  ->method('where')
127  ->with('col2 IN (?)', 'val2');
128 
129  $this->connectionMock->expects($this->any())
130  ->method('quoteIdentifier')
131  ->will($this->returnArgument(0));
132 
133  $this->connectionMock->expects($this->once())
134  ->method('fetchRow')
135  ->with($this->select)
136  ->will($this->returnValue(['row1']));
137 
138  $this->connectionMock->expects($this->never())->method('fetchAll');
139 
140  $this->dataObjectHelper->expects($this->at(0))
141  ->method('populateWithArray')
142  ->with(['urlRewrite1'], ['row1'], \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
143  ->will($this->returnSelf());
144 
145  $this->urlRewriteFactory->expects($this->at(0))
146  ->method('create')
147  ->will($this->returnValue(['urlRewrite1']));
148 
149  $this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
150  }
151 
153  {
154  $origRequestPath = 'page-one';
155  $data = [
156  'col1' => 'val1',
157  'col2' => 'val2',
158  UrlRewrite::REQUEST_PATH => $origRequestPath,
159  ];
160 
161  $this->select->expects($this->at(1))
162  ->method('where')
163  ->with('col1 IN (?)', 'val1');
164 
165  $this->select->expects($this->at(2))
166  ->method('where')
167  ->with('col2 IN (?)', 'val2');
168 
169  $this->select->expects($this->at(3))
170  ->method('where')
171  ->with('request_path IN (?)', [$origRequestPath, $origRequestPath . '/']);
172 
173  $this->connectionMock->expects($this->any())
174  ->method('quoteIdentifier')
175  ->will($this->returnArgument(0));
176 
177  $this->connectionMock->expects($this->never())
178  ->method('fetchRow');
179 
180  $urlRewriteRowInDb = [
181  UrlRewrite::REQUEST_PATH => $origRequestPath,
183  ];
184 
185  $this->connectionMock->expects($this->once())
186  ->method('fetchAll')
187  ->with($this->select)
188  ->will($this->returnValue([$urlRewriteRowInDb]));
189 
190  $this->dataObjectHelper->expects($this->at(0))
191  ->method('populateWithArray')
192  ->with(['urlRewrite1'], $urlRewriteRowInDb, \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
193  ->will($this->returnSelf());
194 
195  $this->urlRewriteFactory->expects($this->at(0))
196  ->method('create')
197  ->will($this->returnValue(['urlRewrite1']));
198 
199  $this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
200  }
201 
203  {
204  $origRequestPath = 'page-one';
205  $data = [
206  'col1' => 'val1',
207  'col2' => 'val2',
208  UrlRewrite::REQUEST_PATH => $origRequestPath,
209  ];
210 
211  $this->select->expects($this->at(1))
212  ->method('where')
213  ->with('col1 IN (?)', 'val1');
214 
215  $this->select->expects($this->at(2))
216  ->method('where')
217  ->with('col2 IN (?)', 'val2');
218 
219  $this->select->expects($this->at(3))
220  ->method('where')
221  ->with('request_path IN (?)', [$origRequestPath, $origRequestPath . '/']);
222 
223  $this->connectionMock->expects($this->any())
224  ->method('quoteIdentifier')
225  ->will($this->returnArgument(0));
226 
227  $this->connectionMock->expects($this->never())
228  ->method('fetchRow');
229 
230  $urlRewriteRowInDb = [
231  UrlRewrite::REQUEST_PATH => $origRequestPath . '/',
234  ];
235 
236  $this->connectionMock->expects($this->once())
237  ->method('fetchAll')
238  ->with($this->select)
239  ->will($this->returnValue([$urlRewriteRowInDb]));
240 
241  $urlRewriteRedirect = [
242  'request_path' => $origRequestPath,
243  'redirect_type' => 301,
244  'store_id' => 1,
245  'entity_type' => 'custom',
246  'entity_id' => '0',
247  'target_path' => $origRequestPath . '/',
248  'description' => null,
249  'is_autogenerated' => '0',
250  'metadata' => null,
251  ];
252 
253  $this->dataObjectHelper->expects($this->at(0))
254  ->method('populateWithArray')
255  ->with(['urlRewrite1'], $urlRewriteRedirect, \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
256  ->will($this->returnSelf());
257 
258  $this->urlRewriteFactory->expects($this->at(0))
259  ->method('create')
260  ->will($this->returnValue(['urlRewrite1']));
261 
262  $this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
263  }
264 
266  {
267  $origRequestPath = 'page-one/';
268  $data = [
269  'col1' => 'val1',
270  'col2' => 'val2',
271  UrlRewrite::REQUEST_PATH => $origRequestPath,
272  ];
273 
274  $this->select->expects($this->at(1))
275  ->method('where')
276  ->with('col1 IN (?)', 'val1');
277 
278  $this->select->expects($this->at(2))
279  ->method('where')
280  ->with('col2 IN (?)', 'val2');
281 
282  $this->select->expects($this->at(3))
283  ->method('where')
284  ->with('request_path IN (?)', [rtrim($origRequestPath, '/'), rtrim($origRequestPath, '/') . '/']);
285 
286  $this->connectionMock->expects($this->any())
287  ->method('quoteIdentifier')
288  ->will($this->returnArgument(0));
289 
290  $this->connectionMock->expects($this->never())
291  ->method('fetchRow');
292 
293  $urlRewriteRowInDb = [
294  UrlRewrite::REQUEST_PATH => rtrim($origRequestPath, '/'),
297  ];
298 
299  $this->connectionMock->expects($this->once())
300  ->method('fetchAll')
301  ->with($this->select)
302  ->will($this->returnValue([$urlRewriteRowInDb]));
303 
304  $urlRewriteRedirect = [
305  'request_path' => $origRequestPath,
306  'redirect_type' => 301,
307  'store_id' => 1,
308  'entity_type' => 'custom',
309  'entity_id' => '0',
310  'target_path' => rtrim($origRequestPath, '/'),
311  'description' => null,
312  'is_autogenerated' => '0',
313  'metadata' => null,
314  ];
315 
316  $this->dataObjectHelper->expects($this->at(0))
317  ->method('populateWithArray')
318  ->with(['urlRewrite1'], $urlRewriteRedirect, \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
319  ->will($this->returnSelf());
320 
321  $this->urlRewriteFactory->expects($this->at(0))
322  ->method('create')
323  ->will($this->returnValue(['urlRewrite1']));
324 
325  $this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
326  }
327 
329  {
330  $origRequestPath = 'page-one';
331  $data = [
332  'col1' => 'val1',
333  'col2' => 'val2',
334  UrlRewrite::REQUEST_PATH => $origRequestPath,
335  ];
336 
337  $this->select->expects($this->at(1))
338  ->method('where')
339  ->with('col1 IN (?)', 'val1');
340 
341  $this->select->expects($this->at(2))
342  ->method('where')
343  ->with('col2 IN (?)', 'val2');
344 
345  $this->select->expects($this->at(3))
346  ->method('where')
347  ->with('request_path IN (?)', [$origRequestPath, $origRequestPath . '/']);
348 
349  $this->connectionMock->expects($this->any())
350  ->method('quoteIdentifier')
351  ->will($this->returnArgument(0));
352 
353  $this->connectionMock->expects($this->never())
354  ->method('fetchRow');
355 
356  $urlRewriteRowInDb = [
357  UrlRewrite::REQUEST_PATH => $origRequestPath . '/',
358  UrlRewrite::TARGET_PATH => 'page-A/',
361  ];
362 
363  $this->connectionMock->expects($this->once())
364  ->method('fetchAll')
365  ->with($this->select)
366  ->will($this->returnValue([$urlRewriteRowInDb]));
367 
368  $this->dataObjectHelper->expects($this->at(0))
369  ->method('populateWithArray')
370  ->with(['urlRewrite1'], $urlRewriteRowInDb, \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
371  ->will($this->returnSelf());
372 
373  $this->urlRewriteFactory->expects($this->at(0))
374  ->method('create')
375  ->will($this->returnValue(['urlRewrite1']));
376 
377  $this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
378  }
379 
381  {
382  $origRequestPath = 'page-one';
383  $data = [
384  'col1' => 'val1',
385  'col2' => 'val2',
386  UrlRewrite::REQUEST_PATH => $origRequestPath,
387  ];
388 
389  $this->select->expects($this->at(1))
390  ->method('where')
391  ->with('col1 IN (?)', 'val1');
392 
393  $this->select->expects($this->at(2))
394  ->method('where')
395  ->with('col2 IN (?)', 'val2');
396 
397  $this->select->expects($this->at(3))
398  ->method('where')
399  ->with('request_path IN (?)', [$origRequestPath, $origRequestPath . '/']);
400 
401  $this->connectionMock->expects($this->any())
402  ->method('quoteIdentifier')
403  ->will($this->returnArgument(0));
404 
405  $this->connectionMock->expects($this->never())
406  ->method('fetchRow');
407 
408  $urlRewriteRowInDb = [
409  UrlRewrite::REQUEST_PATH => $origRequestPath . '/',
410  UrlRewrite::TARGET_PATH => 'page-A/',
413  ];
414 
415  $urlRewriteRowInDb2 = [
416  UrlRewrite::REQUEST_PATH => $origRequestPath,
417  UrlRewrite::TARGET_PATH => 'page-B/',
420  ];
421 
422  $this->connectionMock->expects($this->once())
423  ->method('fetchAll')
424  ->with($this->select)
425  ->will($this->returnValue([$urlRewriteRowInDb, $urlRewriteRowInDb2]));
426 
427  $this->dataObjectHelper->expects($this->at(0))
428  ->method('populateWithArray')
429  ->with(['urlRewrite1'], $urlRewriteRowInDb2, \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
430  ->will($this->returnSelf());
431 
432  $this->urlRewriteFactory->expects($this->at(0))
433  ->method('create')
434  ->will($this->returnValue(['urlRewrite1']));
435 
436  $this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
437  }
438 
442  public function testReplace()
443  {
444  $urlFirst = $this->createMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class);
445  $urlSecond = $this->createMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class);
446 
447  // delete
448 
449  $urlFirst->expects($this->any())
450  ->method('getEntityType')
451  ->willReturn('product');
452  $urlFirst->expects($this->any())
453  ->method('getEntityId')
454  ->willReturn('entity_1');
455  $urlFirst->expects($this->any())
456  ->method('getStoreId')
457  ->willReturn('store_id_1');
458 
459  $urlSecond->expects($this->any())
460  ->method('getEntityType')
461  ->willReturn('category');
462  $urlSecond->expects($this->any())
463  ->method('getEntityId')
464  ->willReturn('entity_2');
465  $urlSecond->expects($this->any())
466  ->method('getStoreId')
467  ->willReturn('store_id_2');
468 
469  $this->connectionMock->expects($this->any())
470  ->method('quoteIdentifier')
471  ->will($this->returnArgument(0));
472 
473  $this->select->expects($this->any())
474  ->method($this->anything())
475  ->willReturnSelf();
476 
477  $this->resource->expects($this->any())
478  ->method('getTableName')
479  ->with(DbStorage::TABLE_NAME)
480  ->will($this->returnValue('table_name'));
481 
482  $this->connectionMock->expects($this->any())
483  ->method('query')
484  ->with('sql delete query');
485 
486  // insert
487 
488  $urlFirst->expects($this->any())
489  ->method('toArray')
490  ->will($this->returnValue(['row1']));
491  $urlSecond->expects($this->any())
492  ->method('toArray')
493  ->will($this->returnValue(['row2']));
494 
495  $this->resource->expects($this->any())
496  ->method('getTableName')
497  ->with(DbStorage::TABLE_NAME)
498  ->will($this->returnValue('table_name'));
499 
500  $this->connectionMock->expects($this->once())
501  ->method('insertMultiple')
502  ->with('table_name', [['row1'], ['row2']]);
503 
504  $this->storage->replace([$urlFirst, $urlSecond]);
505  }
506 
511  {
512  $url = $this->createMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class);
513 
514  $url->expects($this->any())
515  ->method('toArray')
516  ->will($this->returnValue(['row1']));
517 
518  $this->connectionMock->expects($this->once())
519  ->method('insertMultiple')
520  ->will(
521  $this->throwException(
522  new \Exception('SQLSTATE[23000]: test: 1062 test', DbStorage::ERROR_CODE_DUPLICATE_ENTRY)
523  )
524  );
525  $conflictingUrl = [
526  UrlRewrite::URL_REWRITE_ID => 'conflicting-url'
527  ];
528  $this->connectionMock->expects($this->any())
529  ->method('fetchRow')
530  ->willReturn($conflictingUrl);
531 
532  $this->storage->replace([$url]);
533  }
534 
544  {
545  $url = $this->createMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class);
546 
547  $url->expects($this->any())
548  ->method('toArray')
549  ->will($this->returnValue(['row1']));
550 
551  $this->connectionMock->expects($this->once())
552  ->method('insertMultiple')
553  ->will(
554  $this->throwException(
555  new \Exception('SQLSTATE[23000]: test: 1062 test', DbStorage::ERROR_CODE_DUPLICATE_ENTRY)
556  )
557  );
558 
559  $this->storage->replace([$url]);
560  }
561 
566  {
567  $url = $this->createMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class);
568 
569  $url->expects($this->any())
570  ->method('toArray')
571  ->will($this->returnValue(['row1']));
572 
573  $this->connectionMock->expects($this->once())
574  ->method('insertMultiple')
575  ->will($this->throwException(new \RuntimeException()));
576 
577  $this->storage->replace([$url]);
578  }
579 
580  public function testDeleteByData()
581  {
582  $data = ['col1' => 'val1', 'col2' => 'val2'];
583 
584  $this->connectionMock->expects($this->any())
585  ->method('quoteIdentifier')
586  ->will($this->returnArgument(0));
587 
588  $this->select->expects($this->at(1))
589  ->method('where')
590  ->with('col1 IN (?)', 'val1');
591 
592  $this->select->expects($this->at(2))
593  ->method('where')
594  ->with('col2 IN (?)', 'val2');
595 
596  $this->select->expects($this->at(3))
597  ->method('deleteFromSelect')
598  ->with('table_name')
599  ->will($this->returnValue('sql delete query'));
600 
601  $this->resource->expects($this->any())
602  ->method('getTableName')
603  ->with(DbStorage::TABLE_NAME)
604  ->will($this->returnValue('table_name'));
605 
606  $this->connectionMock->expects($this->once())
607  ->method('query')
608  ->with('sql delete query');
609 
610  $this->storage->deleteByData($data);
611  }
612 }