AbstractResourceTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Test\Unit\Model\ResourceModel;
  7. class AbstractResourceTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Indexer\Test\Unit\Model\ResourceModel\AbstractResourceStub
  11. */
  12. protected $model;
  13. /**
  14. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_resourceMock;
  17. /**
  18. * @var \Magento\Framework\Indexer\Table\StrategyInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_tableStrategyInterface;
  21. protected function setUp()
  22. {
  23. $this->_resourceMock = $this->getMockBuilder(
  24. \Magento\Framework\App\ResourceConnection::class
  25. )->disableOriginalConstructor()
  26. ->getMock();
  27. $this->_tableStrategyInterface = $this->createMock(\Magento\Framework\Indexer\Table\StrategyInterface::class);
  28. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  29. $arguments = $objectManager->getConstructArguments(
  30. \Magento\Indexer\Test\Unit\Model\ResourceModel\AbstractResourceStub::class,
  31. [
  32. 'resource' => $this->_resourceMock,
  33. 'tableStrategy' => $this->_tableStrategyInterface
  34. ]
  35. );
  36. $this->model = $objectManager->getObject(
  37. \Magento\Indexer\Test\Unit\Model\ResourceModel\AbstractResourceStub::class,
  38. $arguments
  39. );
  40. }
  41. public function testReindexAll()
  42. {
  43. $this->_tableStrategyInterface->expects($this->once())
  44. ->method('setUseIdxTable')
  45. ->with(true);
  46. $this->_tableStrategyInterface->expects($this->once())
  47. ->method('prepareTableName')
  48. ->with('test')
  49. ->will($this->returnValue('test_idx'));
  50. $this->model->reindexAll();
  51. $this->assertEquals('test_idx', $this->model->getIdxTable('test'));
  52. }
  53. public function testClearTemporaryIndexTable()
  54. {
  55. $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  56. $this->_resourceMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock));
  57. $connectionMock->expects($this->once())->method('delete')->will($this->returnSelf());
  58. $this->model->clearTemporaryIndexTable();
  59. }
  60. public function testSyncData()
  61. {
  62. $resultTable = 'catalog_category_flat';
  63. $resultColumns = [0 => 'column'];
  64. $describeTable = ['column' => 'column'];
  65. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  66. $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  67. $connectionMock->expects($this->any())->method('describeTable')->will($this->returnValue($describeTable));
  68. $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock));
  69. $selectMock->expects($this->any())->method('from')->will($this->returnSelf());
  70. $selectMock->expects($this->once())->method('insertFromSelect')->with(
  71. $resultTable,
  72. $resultColumns
  73. )->will($this->returnSelf());
  74. $this->_resourceMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock));
  75. $this->_resourceMock->expects($this->any())->method('getTableName')->will($this->returnArgument(0));
  76. $this->assertInstanceOf(
  77. \Magento\Indexer\Test\Unit\Model\ResourceModel\AbstractResourceStub::class,
  78. $this->model->syncData()
  79. );
  80. }
  81. /**
  82. * @expectedException \Exception
  83. */
  84. public function testSyncDataException()
  85. {
  86. $describeTable = ['column' => 'column'];
  87. $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  88. $connectionMock->expects($this->any())->method('describeTable')->will($this->returnValue($describeTable));
  89. $connectionMock->expects($this->any())->method('select')->will($this->throwException(new \Exception()));
  90. $this->_resourceMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock));
  91. $this->_resourceMock->expects($this->any())->method('getTableName')->will($this->returnArgument(0));
  92. $connectionMock->expects($this->once())->method('rollback');
  93. $this->model->syncData();
  94. }
  95. /**
  96. * @param bool $readToIndex
  97. * @dataProvider insertFromTableData
  98. */
  99. public function testInsertFromTable($readToIndex)
  100. {
  101. $sourceTable = 'catalog_category_flat';
  102. $destTable = 'catalog_category_flat';
  103. $resultColumns = [0 => 'column'];
  104. $tableColumns = ['column' => 'column'];
  105. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  106. $connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $connectionMock->expects($this->any())->method('describeTable')->will($this->returnValue($tableColumns));
  110. $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock));
  111. $selectMock->expects($this->any())->method('from')->will($this->returnSelf());
  112. if ($readToIndex) {
  113. $connectionCustomMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  114. ->setMethods(['describeTable', 'query', 'select', 'insertArray'])
  115. ->getMockForAbstractClass();
  116. $pdoMock = $this->createMock(\Zend_Db_Statement_Pdo::class);
  117. $connectionCustomMock->expects($this->any())->method('query')->will($this->returnValue($selectMock));
  118. $connectionCustomMock->expects($this->any())->method('select')->will($this->returnValue($selectMock));
  119. $connectionCustomMock->expects($this->any())->method('describeTable')->will(
  120. $this->returnValue($tableColumns)
  121. );
  122. $connectionCustomMock->expects($this->any())->method('insertArray')->with(
  123. $destTable,
  124. $resultColumns
  125. )->will($this->returnValue(1));
  126. $connectionMock->expects($this->any())->method('query')->will($this->returnValue($pdoMock));
  127. $pdoMock->expects($this->any())->method('fetch')->will($this->returnValue([$tableColumns]));
  128. $this->model->newIndexAdapter();
  129. $this->_resourceMock->expects($this->any())->method('getConnection')->will(
  130. $this->returnValue($connectionMock)
  131. );
  132. } else {
  133. $selectMock->expects($this->once())->method('insertFromSelect')->with(
  134. $destTable,
  135. $resultColumns
  136. )->will($this->returnSelf());
  137. $this->_resourceMock->expects($this->any())->method('getTableName')->will($this->returnArgument(0));
  138. $this->_resourceMock->expects($this->any())->method('getConnection')->will(
  139. $this->returnValue($connectionMock)
  140. );
  141. }
  142. $this->assertInstanceOf(
  143. \Magento\Indexer\Test\Unit\Model\ResourceModel\AbstractResourceStub::class,
  144. $this->model->insertFromTable($sourceTable, $destTable, $readToIndex)
  145. );
  146. }
  147. /**
  148. * @return array
  149. */
  150. public function insertFromTableData()
  151. {
  152. return [[false], [true]];
  153. }
  154. }