Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InterfaceTest.php
Go to the documentation of this file.
1 <?php
11 
12 class InterfaceTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $_connection;
18 
22  protected $_tableName;
23 
27  protected $_oneColumnIdxName;
28 
32  protected $_twoColumnIdxName;
33 
34  protected function setUp()
35  {
38  \Magento\Framework\Setup\ModuleDataSetupInterface::class
39  );
40  $this->_connection = $installer->getConnection();
41  $this->_tableName = $this->_connection->getTableName('table_two_column_idx');
42  $this->_oneColumnIdxName = $this->_connection->getIndexName($this->_tableName, ['column1']);
43  $this->_twoColumnIdxName = $this->_connection->getIndexName($this->_tableName, ['column1', 'column2']);
44 
45  $table = $this->_connection->newTable(
46  $this->_tableName
47  )->addColumn(
48  'id',
49  \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
50  null,
51  ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
52  'Id'
53  )->addColumn(
54  'column1',
55  \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER
56  )->addColumn(
57  'column2',
58  \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER
59  )->addIndex(
60  $this->_oneColumnIdxName,
61  ['column1']
62  )->addIndex(
63  $this->_twoColumnIdxName,
64  ['column1', 'column2']
65  );
66  $this->_connection->createTable($table);
67  }
68 
72  protected function tearDown()
73  {
74  $this->_connection->dropTable($this->_tableName);
75  $this->_connection->resetDdlCache($this->_tableName);
76  $this->_connection = null;
77  }
78 
79  protected function assertPreConditions()
80  {
81  $this->assertTrue(
82  $this->_connection->tableColumnExists($this->_tableName, 'column1'),
83  'Table column "column1" must be provided by the fixture.'
84  );
85  $this->assertTrue(
86  $this->_connection->tableColumnExists($this->_tableName, 'column2'),
87  'Table column "column2" must be provided by the fixture.'
88  );
89  $this->assertEquals(
90  ['column1'],
91  $this->_getIndexColumns($this->_tableName, $this->_oneColumnIdxName),
92  'Single-column index must be provided by the fixture.'
93  );
94  $this->assertEquals(
95  ['column1', 'column2'],
96  $this->_getIndexColumns($this->_tableName, $this->_twoColumnIdxName),
97  'Multiple-column index must be provided by the fixture.'
98  );
99  }
100 
109  protected function _getIndexColumns($tableName, $indexName, $schemaName = null)
110  {
111  foreach ($this->_connection->getIndexList($tableName, $schemaName) as $idxData) {
112  if ($idxData['KEY_NAME'] == $indexName) {
113  return $idxData['COLUMNS_LIST'];
114  }
115  }
116  return false;
117  }
118 
119  public function testDropColumn()
120  {
121  $this->_connection->dropColumn($this->_tableName, 'column1');
122  $this->assertFalse(
123  $this->_connection->tableColumnExists($this->_tableName, 'column1'),
124  'Table column must not exist after it has been dropped.'
125  );
126  }
127 
132  {
133  $this->_connection->dropColumn($this->_tableName, 'column1');
134  $this->assertFalse(
135  $this->_getIndexColumns($this->_tableName, $this->_oneColumnIdxName),
136  'Column index must be dropped along with the column.'
137  );
138  $this->assertEquals(
139  ['column2'],
140  $this->_getIndexColumns($this->_tableName, $this->_twoColumnIdxName),
141  'References to the dropped column must be removed from the multiple-column indexes.'
142  );
143  }
144 
149  {
150  $this->_connection->dropColumn($this->_tableName, 'column2');
151  $this->assertEquals(
152  ['column1'],
153  $this->_getIndexColumns($this->_tableName, $this->_oneColumnIdxName),
154  'Column index must be preserved.'
155  );
156  $this->assertFalse(
157  $this->_getIndexColumns($this->_tableName, $this->_twoColumnIdxName),
158  'Multiple-column index must be dropped to not duplicate existing index by indexed columns.'
159  );
160  }
161 
168  public function testInsertArray(array $columns, array $data, array $expected)
169  {
170  $this->_connection->insertArray($this->_tableName, $columns, $data);
171  $select = $this->_connection->select()->from($this->_tableName, array_keys($expected[0]))->order('column1');
172  $result = $this->_connection->fetchAll($select);
173  $this->assertEquals($expected, $result);
174  }
175 
181  public function insertArrayDataProvider()
182  {
183  return [
184  'one column' => [
185  ['column1'],
186  [[1], [2]],
187  [['column1' => 1, 'column2' => null], ['column1' => 2, 'column2' => null]],
188  ],
189  'one column simple' => [
190  ['column1'],
191  [1, 2],
192  [['column1' => 1, 'column2' => null], ['column1' => 2, 'column2' => null]],
193  ],
194  'two columns' => [
195  ['column1', 'column2'],
196  [[1, 2], [3, 4]],
197  [['column1' => 1, 'column2' => 2], ['column1' => 3, 'column2' => 4]],
198  ],
199  'several columns with identity' => [ // test possibility to insert data with filled identity field
200  ['id', 'column1', 'column2'],
201  [[1, 0, 0], [2, 1, 1], [3, 2, 2]],
202  [
203  ['id' => 1, 'column1' => 0, 'column2' => 0],
204  ['id' => 2, 'column1' => 1, 'column2' => 1],
205  ['id' => 3, 'column1' => 2, 'column2' => 2]
206  ],
207  ]
208  ];
209  }
210 
215  {
216  $this->_connection->insertArray($this->_tableName, ['column1', 'column2'], [1, 2]);
217  }
218 
222  public function testInsertMultiple($data)
223  {
224  $this->_connection->insertMultiple($this->_tableName, $data);
225 
226  $select = $this->_connection->select()->from($this->_tableName);
227  $result = $this->_connection->fetchRow($select);
228 
229  $this->assertEquals($data, $result);
230  }
231 
235  public function testInsertOnDuplicate($data)
236  {
237  $this->_connection->insertOnDuplicate($this->_tableName, $data);
238 
239  $select = $this->_connection->select()->from($this->_tableName);
240  $result = $this->_connection->fetchRow($select);
241 
242  $this->assertEquals($data, $result);
243  }
244 
248  public function testInsertForce($data)
249  {
250  $this->assertEquals(1, $this->_connection->insertForce($this->_tableName, $data));
251 
252  $select = $this->_connection->select()->from($this->_tableName);
253  $result = $this->_connection->fetchRow($select);
254 
255  $this->assertEquals($data, $result);
256  }
257 
263  public function insertDataProvider()
264  {
265  return ['column with identity field' => [['id' => 1, 'column1' => 10, 'column2' => 20]]];
266  }
267 }
_getIndexColumns($tableName, $indexName, $schemaName=null)
$tableName
Definition: trigger.php:13
$columns
Definition: default.phtml:15
$table
Definition: trigger.php:14
testInsertArray(array $columns, array $data, array $expected)