InterfaceTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test for an environment-dependent DB adapter that implements \Magento\Framework\DB\Adapter\AdapterInterface
  8. */
  9. namespace Magento\Framework\DB\Adapter;
  10. class InterfaceTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  14. */
  15. protected $_connection;
  16. /**
  17. * @var string
  18. */
  19. protected $_tableName;
  20. /**
  21. * @var string
  22. */
  23. protected $_oneColumnIdxName;
  24. /**
  25. * @var string
  26. */
  27. protected $_twoColumnIdxName;
  28. protected function setUp()
  29. {
  30. /** @var \Magento\Framework\Setup\ModuleDataSetupInterface $installer */
  31. $installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  32. \Magento\Framework\Setup\ModuleDataSetupInterface::class
  33. );
  34. $this->_connection = $installer->getConnection();
  35. $this->_tableName = $this->_connection->getTableName('table_two_column_idx');
  36. $this->_oneColumnIdxName = $this->_connection->getIndexName($this->_tableName, ['column1']);
  37. $this->_twoColumnIdxName = $this->_connection->getIndexName($this->_tableName, ['column1', 'column2']);
  38. $table = $this->_connection->newTable(
  39. $this->_tableName
  40. )->addColumn(
  41. 'id',
  42. \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
  43. null,
  44. ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
  45. 'Id'
  46. )->addColumn(
  47. 'column1',
  48. \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER
  49. )->addColumn(
  50. 'column2',
  51. \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER
  52. )->addIndex(
  53. $this->_oneColumnIdxName,
  54. ['column1']
  55. )->addIndex(
  56. $this->_twoColumnIdxName,
  57. ['column1', 'column2']
  58. );
  59. $this->_connection->createTable($table);
  60. }
  61. /**
  62. * Cleanup DDL cache for the fixture table
  63. */
  64. protected function tearDown()
  65. {
  66. $this->_connection->dropTable($this->_tableName);
  67. $this->_connection->resetDdlCache($this->_tableName);
  68. $this->_connection = null;
  69. }
  70. protected function assertPreConditions()
  71. {
  72. $this->assertTrue(
  73. $this->_connection->tableColumnExists($this->_tableName, 'column1'),
  74. 'Table column "column1" must be provided by the fixture.'
  75. );
  76. $this->assertTrue(
  77. $this->_connection->tableColumnExists($this->_tableName, 'column2'),
  78. 'Table column "column2" must be provided by the fixture.'
  79. );
  80. $this->assertEquals(
  81. ['column1'],
  82. $this->_getIndexColumns($this->_tableName, $this->_oneColumnIdxName),
  83. 'Single-column index must be provided by the fixture.'
  84. );
  85. $this->assertEquals(
  86. ['column1', 'column2'],
  87. $this->_getIndexColumns($this->_tableName, $this->_twoColumnIdxName),
  88. 'Multiple-column index must be provided by the fixture.'
  89. );
  90. }
  91. /**
  92. * Retrieve list of columns used for an index or return false, if an index with a given name does not exist
  93. *
  94. * @param string $tableName
  95. * @param string $indexName
  96. * @param string|null $schemaName
  97. * @return array|false
  98. */
  99. protected function _getIndexColumns($tableName, $indexName, $schemaName = null)
  100. {
  101. foreach ($this->_connection->getIndexList($tableName, $schemaName) as $idxData) {
  102. if ($idxData['KEY_NAME'] == $indexName) {
  103. return $idxData['COLUMNS_LIST'];
  104. }
  105. }
  106. return false;
  107. }
  108. public function testDropColumn()
  109. {
  110. $this->_connection->dropColumn($this->_tableName, 'column1');
  111. $this->assertFalse(
  112. $this->_connection->tableColumnExists($this->_tableName, 'column1'),
  113. 'Table column must not exist after it has been dropped.'
  114. );
  115. }
  116. /**
  117. * @depends testDropColumn
  118. */
  119. public function testDropColumnRemoveFromIndexes()
  120. {
  121. $this->_connection->dropColumn($this->_tableName, 'column1');
  122. $this->assertFalse(
  123. $this->_getIndexColumns($this->_tableName, $this->_oneColumnIdxName),
  124. 'Column index must be dropped along with the column.'
  125. );
  126. $this->assertEquals(
  127. ['column2'],
  128. $this->_getIndexColumns($this->_tableName, $this->_twoColumnIdxName),
  129. 'References to the dropped column must be removed from the multiple-column indexes.'
  130. );
  131. }
  132. /**
  133. * @depends testDropColumn
  134. */
  135. public function testDropColumnRemoveIndexDuplicate()
  136. {
  137. $this->_connection->dropColumn($this->_tableName, 'column2');
  138. $this->assertEquals(
  139. ['column1'],
  140. $this->_getIndexColumns($this->_tableName, $this->_oneColumnIdxName),
  141. 'Column index must be preserved.'
  142. );
  143. $this->assertFalse(
  144. $this->_getIndexColumns($this->_tableName, $this->_twoColumnIdxName),
  145. 'Multiple-column index must be dropped to not duplicate existing index by indexed columns.'
  146. );
  147. }
  148. /**
  149. * @param array $columns
  150. * @param array $data
  151. * @param array $expected
  152. * @dataProvider insertArrayDataProvider
  153. */
  154. public function testInsertArray(array $columns, array $data, array $expected)
  155. {
  156. $this->_connection->insertArray($this->_tableName, $columns, $data);
  157. $select = $this->_connection->select()->from($this->_tableName, array_keys($expected[0]))->order('column1');
  158. $result = $this->_connection->fetchAll($select);
  159. $this->assertEquals($expected, $result);
  160. }
  161. /**
  162. * Data provider for insertArray() test
  163. *
  164. * @return array
  165. */
  166. public function insertArrayDataProvider()
  167. {
  168. return [
  169. 'one column' => [
  170. ['column1'],
  171. [[1], [2]],
  172. [['column1' => 1, 'column2' => null], ['column1' => 2, 'column2' => null]],
  173. ],
  174. 'one column simple' => [
  175. ['column1'],
  176. [1, 2],
  177. [['column1' => 1, 'column2' => null], ['column1' => 2, 'column2' => null]],
  178. ],
  179. 'two columns' => [
  180. ['column1', 'column2'],
  181. [[1, 2], [3, 4]],
  182. [['column1' => 1, 'column2' => 2], ['column1' => 3, 'column2' => 4]],
  183. ],
  184. 'several columns with identity' => [ // test possibility to insert data with filled identity field
  185. ['id', 'column1', 'column2'],
  186. [[1, 0, 0], [2, 1, 1], [3, 2, 2]],
  187. [
  188. ['id' => 1, 'column1' => 0, 'column2' => 0],
  189. ['id' => 2, 'column1' => 1, 'column2' => 1],
  190. ['id' => 3, 'column1' => 2, 'column2' => 2]
  191. ],
  192. ]
  193. ];
  194. }
  195. /**
  196. * @expectedException \Zend_Db_Exception
  197. */
  198. public function testInsertArrayTwoColumnsWithSimpleData()
  199. {
  200. $this->_connection->insertArray($this->_tableName, ['column1', 'column2'], [1, 2]);
  201. }
  202. /**
  203. * @dataProvider insertDataProvider
  204. */
  205. public function testInsertMultiple($data)
  206. {
  207. $this->_connection->insertMultiple($this->_tableName, $data);
  208. $select = $this->_connection->select()->from($this->_tableName);
  209. $result = $this->_connection->fetchRow($select);
  210. $this->assertEquals($data, $result);
  211. }
  212. /**
  213. * @dataProvider insertDataProvider
  214. */
  215. public function testInsertOnDuplicate($data)
  216. {
  217. $this->_connection->insertOnDuplicate($this->_tableName, $data);
  218. $select = $this->_connection->select()->from($this->_tableName);
  219. $result = $this->_connection->fetchRow($select);
  220. $this->assertEquals($data, $result);
  221. }
  222. /**
  223. * @dataProvider insertDataProvider
  224. */
  225. public function testInsertForce($data)
  226. {
  227. $this->assertEquals(1, $this->_connection->insertForce($this->_tableName, $data));
  228. $select = $this->_connection->select()->from($this->_tableName);
  229. $result = $this->_connection->fetchRow($select);
  230. $this->assertEquals($data, $result);
  231. }
  232. /**
  233. * Data provider for insert() tests
  234. *
  235. * @return array
  236. */
  237. public function insertDataProvider()
  238. {
  239. return ['column with identity field' => [['id' => 1, 'column1' => 10, 'column2' => 20]]];
  240. }
  241. }