GridStructureTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Indexer\GridStructure;
  8. use Magento\Framework\DB\Adapter\AdapterInterface;
  9. use Magento\Framework\DB\Ddl\Table;
  10. class GridStructureTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $resource;
  16. /**
  17. * @var \Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $flatScopeResolver;
  20. /**
  21. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $connection;
  24. /**
  25. * @var GridStructure
  26. */
  27. protected $object;
  28. protected function setUp()
  29. {
  30. $this->connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  31. ->getMock();
  32. $this->resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->flatScopeResolver = $this->getMockBuilder(
  36. \Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver::class
  37. )
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->resource->expects($this->any())
  41. ->method('getConnection')
  42. ->with('write')
  43. ->willReturn($this->connection);
  44. $this->object = new GridStructure(
  45. $this->resource,
  46. $this->flatScopeResolver
  47. );
  48. }
  49. public function testDelete()
  50. {
  51. $index = 'index';
  52. $table = 'index_table';
  53. $this->flatScopeResolver->expects($this->once())
  54. ->method('resolve')
  55. ->with($index, [])
  56. ->willReturn($table);
  57. $this->connection->expects($this->once())
  58. ->method('isTableExists')
  59. ->with($table)
  60. ->willReturn(true);
  61. $this->connection->expects($this->once())
  62. ->method('dropTable')
  63. ->with($table);
  64. $this->object->delete($index);
  65. }
  66. public function testCreate()
  67. {
  68. $index = 'index';
  69. $fields = [
  70. [
  71. 'type' => 'searchable',
  72. 'name' => 'field',
  73. 'dataType' => 'int'
  74. ]
  75. ];
  76. $tableName = 'index_table';
  77. $idxName = 'idxName';
  78. $table = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Table::class)
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $this->flatScopeResolver->expects($this->once())
  82. ->method('resolve')
  83. ->with($index, [])
  84. ->willReturn($tableName);
  85. $this->connection->expects($this->once())
  86. ->method('newTable')
  87. ->with($tableName)
  88. ->willReturn($table);
  89. $table->expects($this->any())
  90. ->method('addColumn')
  91. ->willReturnMap(
  92. [
  93. ['entity_id', Table::TYPE_INTEGER, 10, ['unsigned' => true, 'nullable' => false], 'Entity ID'],
  94. ['field', Table::TYPE_INTEGER, null]
  95. ]
  96. );
  97. $this->connection->expects($this->once())
  98. ->method('createTable')
  99. ->with($table);
  100. $this->resource->expects($this->once())
  101. ->method('getIdxName')
  102. ->with($tableName, ['field'], AdapterInterface::INDEX_TYPE_FULLTEXT)
  103. ->willReturn($idxName);
  104. $table->expects($this->once())
  105. ->method('addIndex')
  106. ->with($idxName, ['field'], ['type' => AdapterInterface::INDEX_TYPE_FULLTEXT]);
  107. $this->object->create($index, $fields);
  108. }
  109. }