IndexStructureTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Indexer\Test\Unit;
  7. use Magento\Framework\DB\Adapter\AdapterInterface;
  8. use Magento\Framework\DB\Ddl\Table;
  9. use \Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. /**
  11. * Test for \Magento\Framework\Indexer\IndexStructure
  12. */
  13. class IndexStructureTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $indexScopeResolver;
  19. /**
  20. * @var \Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $flatScopeResolver;
  23. /**
  24. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $resource;
  27. /**
  28. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $connectionInterface;
  31. /**
  32. * @var \Magento\Framework\Indexer\IndexStructure
  33. */
  34. private $target;
  35. protected function setUp()
  36. {
  37. $this->connectionInterface = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  38. ->disableOriginalConstructor()
  39. ->getMockForAbstractClass();
  40. $this->resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  41. ->setMethods(['getConnection'])
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->resource->expects($this->atLeastOnce())
  45. ->method('getConnection')
  46. ->willReturn($this->connectionInterface);
  47. $this->indexScopeResolver = $this->getMockBuilder(
  48. \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver::class
  49. )
  50. ->setMethods(['resolve'])
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->flatScopeResolver = $this->getMockBuilder(
  54. \Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver::class
  55. )
  56. ->setMethods(['resolve'])
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $objectManager = new ObjectManager($this);
  60. $this->target = $objectManager->getObject(
  61. \Magento\Framework\Indexer\IndexStructure::class,
  62. [
  63. 'resource' => $this->resource,
  64. 'indexScopeResolver' => $this->indexScopeResolver,
  65. 'flatScopeResolver' => $this->flatScopeResolver
  66. ]
  67. );
  68. }
  69. /**
  70. * @param string $table
  71. * @param array $dimensions
  72. * @param bool $isTableExist
  73. */
  74. public function testDelete()
  75. {
  76. $index = 'index_name';
  77. $dimensions = [
  78. 'index_name_scope_3' => $this->createDimensionMock('scope', 3),
  79. 'index_name_scope_5' => $this->createDimensionMock('scope', 5),
  80. 'index_name_scope_1' => $this->createDimensionMock('scope', 1),
  81. ];
  82. $expectedTable = 'index_name_scope3_scope5_scope1';
  83. $this->indexScopeResolver->expects($this->once())
  84. ->method('resolve')
  85. ->with($index, $dimensions)
  86. ->willReturn($expectedTable);
  87. $this->flatScopeResolver->expects($this->once())
  88. ->method('resolve')
  89. ->with($index, $dimensions)
  90. ->willReturn($index . '_flat');
  91. $position = 0;
  92. $position = $this->mockDropTable($position, $expectedTable, true);
  93. $this->mockDropTable($position, $index . '_flat', true);
  94. $this->target->delete($index, $dimensions);
  95. }
  96. public function testCreateWithEmptyFields()
  97. {
  98. $fields = [
  99. [
  100. 'name' => 'fieldName1',
  101. 'type' => 'fieldType1',
  102. 'size' => 'fieldSize1',
  103. ],
  104. [
  105. 'name' => 'fieldName2',
  106. 'type' => 'fieldType2',
  107. 'size' => 'fieldSize2',
  108. ],
  109. [
  110. 'name' => 'fieldName3',
  111. 'type' => 'fieldType3',
  112. 'size' => 'fieldSize3',
  113. ],
  114. [
  115. 'name' => 'fieldName3',
  116. 'dataType' => 'varchar',
  117. 'type' => 'text',
  118. 'size' => '255',
  119. ],
  120. [
  121. 'name' => 'fieldName3',
  122. 'dataType' => 'mediumtext',
  123. 'type' => 'text',
  124. 'size' => '16777216',
  125. ],
  126. [
  127. 'name' => 'fieldName3',
  128. 'dataType' => 'text',
  129. 'type' => 'text',
  130. 'size' => '65536',
  131. ]
  132. ];
  133. $index = 'index_name';
  134. $expectedTable = 'index_name_scope3_scope5_scope1';
  135. $dimensions = [
  136. 'index_name_scope_3' => $this->createDimensionMock('scope', 3),
  137. 'index_name_scope_5' => $this->createDimensionMock('scope', 5),
  138. 'index_name_scope_1' => $this->createDimensionMock('scope', 1),
  139. ];
  140. $position = 0;
  141. $this->indexScopeResolver->expects($this->once())
  142. ->method('resolve')
  143. ->with($index, $dimensions)
  144. ->willReturn($expectedTable);
  145. $this->flatScopeResolver->expects($this->once())
  146. ->method('resolve')
  147. ->with($index, $dimensions)
  148. ->willReturn($index . '_flat');
  149. $position = $this->mockFulltextTable($position, $expectedTable, true);
  150. $this->mockFlatTable($position, $index . '_flat');
  151. $this->target->create($index, $fields, $dimensions);
  152. }
  153. /**
  154. * @param string $name
  155. * @param string $value
  156. * @return \PHPUnit_Framework_MockObject_MockObject
  157. */
  158. private function createDimensionMock($name, $value)
  159. {
  160. $dimension = $this->getMockBuilder(\Magento\Framework\Search\Request\Dimension::class)
  161. ->setMethods(['getName', 'getValue'])
  162. ->disableOriginalConstructor()
  163. ->getMock();
  164. $dimension->expects($this->any())
  165. ->method('getName')
  166. ->willReturn($name);
  167. $dimension->expects($this->any())
  168. ->method('getValue')
  169. ->willReturn($value);
  170. return $dimension;
  171. }
  172. /**
  173. * @param $callNumber
  174. * @param $tableName
  175. * @param $isTableExist
  176. * @return mixed
  177. */
  178. private function mockDropTable($callNumber, $tableName, $isTableExist)
  179. {
  180. $this->connectionInterface->expects($this->at($callNumber++))
  181. ->method('isTableExists')
  182. ->with($tableName)
  183. ->willReturn($isTableExist);
  184. if ($isTableExist) {
  185. $this->connectionInterface->expects($this->at($callNumber++))
  186. ->method('dropTable')
  187. ->with($tableName)
  188. ->willReturn(true);
  189. }
  190. return $callNumber;
  191. }
  192. /**
  193. * @param $callNumber
  194. * @param $tableName
  195. * @return mixed
  196. */
  197. private function mockFlatTable($callNumber, $tableName)
  198. {
  199. $table = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Table::class)
  200. ->setMethods(['addColumn', 'getColumns'])
  201. ->disableOriginalConstructor()
  202. ->getMock();
  203. $table->expects($this->any())
  204. ->method('addColumn')
  205. ->willReturnSelf();
  206. $this->connectionInterface->expects($this->at($callNumber++))
  207. ->method('newTable')
  208. ->with($tableName)
  209. ->willReturn($table);
  210. $this->connectionInterface->expects($this->at($callNumber++))
  211. ->method('createTable')
  212. ->with($table)
  213. ->willReturnSelf();
  214. return $callNumber;
  215. }
  216. /**
  217. * @param $callNumber
  218. * @param $tableName
  219. * @return mixed
  220. */
  221. private function mockFulltextTable($callNumber, $tableName)
  222. {
  223. $table = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Table::class)
  224. ->setMethods(['addColumn', 'addIndex'])
  225. ->disableOriginalConstructor()
  226. ->getMock();
  227. $table->expects($this->at(0))
  228. ->method('addColumn')
  229. ->with(
  230. 'entity_id',
  231. Table::TYPE_INTEGER,
  232. 10,
  233. ['unsigned' => true, 'nullable' => false],
  234. 'Entity ID'
  235. )->willReturnSelf();
  236. $table->expects($this->at(1))
  237. ->method('addColumn')
  238. ->with(
  239. 'attribute_id',
  240. Table::TYPE_TEXT,
  241. 255,
  242. ['unsigned' => true, 'nullable' => true]
  243. )->willReturnSelf();
  244. $table->expects($this->at(2))
  245. ->method('addColumn')
  246. ->with(
  247. 'data_index',
  248. Table::TYPE_TEXT,
  249. '4g',
  250. ['nullable' => true],
  251. 'Data index'
  252. )->willReturnSelf();
  253. $table->expects($this->at(3))
  254. ->method('addIndex')
  255. ->with(
  256. 'idx_primary',
  257. ['entity_id', 'attribute_id'],
  258. ['type' => AdapterInterface::INDEX_TYPE_PRIMARY]
  259. )->willReturnSelf();
  260. $table->expects($this->at(4))
  261. ->method('addIndex')
  262. ->with(
  263. 'FTI_FULLTEXT_DATA_INDEX',
  264. ['data_index'],
  265. ['type' => AdapterInterface::INDEX_TYPE_FULLTEXT]
  266. )->willReturnSelf();
  267. $this->connectionInterface->expects($this->at($callNumber++))
  268. ->method('newTable')
  269. ->with($tableName)
  270. ->willReturn($table);
  271. $this->connectionInterface->expects($this->at($callNumber++))
  272. ->method('createTable')
  273. ->with($table)
  274. ->willReturnSelf();
  275. return $callNumber;
  276. }
  277. }