|
7 | 7 |
|
8 | 8 | use Magento\Framework\App\ResourceConnection;
|
9 | 9 | use Magento\TestFramework\Helper\CacheCleaner;
|
| 10 | +use Magento\Framework\DB\Ddl\Table; |
10 | 11 |
|
11 | 12 | class MysqlTest extends \PHPUnit_Framework_TestCase
|
12 | 13 | {
|
@@ -157,4 +158,70 @@ public function testDescribeTable()
|
157 | 158 | $this->getDbAdapter()->describeTable($tableName)
|
158 | 159 | );
|
159 | 160 | }
|
| 161 | + |
| 162 | + /** |
| 163 | + * Test that Zend_Db_Expr can be used as a column default value. |
| 164 | + * @see https://github.com/magento/magento2/pull/9131 |
| 165 | + */ |
| 166 | + public function testCreateTableColumnWithExpressionAsColumnDefaultValue() |
| 167 | + { |
| 168 | + $adapter = $this->getDbAdapter(); |
| 169 | + $tableName = 'table_column_with_expression_as_column_default_value'; |
| 170 | + |
| 171 | + $table = $adapter |
| 172 | + ->newTable($tableName) |
| 173 | + ->addColumn( |
| 174 | + 'row_id', |
| 175 | + Table::TYPE_INTEGER, |
| 176 | + null, |
| 177 | + ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], |
| 178 | + 'Row Id' |
| 179 | + ) |
| 180 | + ->addColumn( |
| 181 | + 'created_at', |
| 182 | + Table::TYPE_DATETIME, |
| 183 | + null, |
| 184 | + ['default' => new \Zend_Db_Expr('CURRENT_TIMESTAMP')] |
| 185 | + ) |
| 186 | + ->addColumn( |
| 187 | + 'integer_column', |
| 188 | + Table::TYPE_INTEGER, |
| 189 | + 11, |
| 190 | + ['default' => 123456] |
| 191 | + )->addColumn( |
| 192 | + 'string_column', |
| 193 | + Table::TYPE_TEXT, |
| 194 | + 255, |
| 195 | + ['default' => 'default test text'] |
| 196 | + ) |
| 197 | + ->setComment('Test table column with expression as column default value'); |
| 198 | + $adapter->createTable($table); |
| 199 | + |
| 200 | + $tableDescription = $adapter->describeTable($tableName); |
| 201 | + |
| 202 | + //clean up database from test table |
| 203 | + $adapter->dropTable($tableName); |
| 204 | + |
| 205 | + $this->assertArrayHasKey('created_at', $tableDescription, 'Column created_at does not exists'); |
| 206 | + $this->assertArrayHasKey('integer_column', $tableDescription, 'Column integer_column does not exists'); |
| 207 | + $this->assertArrayHasKey('string_column', $tableDescription, 'Column string_column does not exists'); |
| 208 | + $dateColumn = $tableDescription['created_at']; |
| 209 | + $intColumn = $tableDescription['integer_column']; |
| 210 | + $stringColumn = $tableDescription['string_column']; |
| 211 | + |
| 212 | + //Test default value with expression |
| 213 | + $this->assertEquals('created_at', $dateColumn['COLUMN_NAME'], 'Incorrect column name'); |
| 214 | + $this->assertEquals(Table::TYPE_DATETIME, $dateColumn['DATA_TYPE'], 'Incorrect column type'); |
| 215 | + $this->assertEquals('CURRENT_TIMESTAMP', $dateColumn['DEFAULT'], 'Incorrect column default expression value'); |
| 216 | + |
| 217 | + //Test default value with integer value |
| 218 | + $this->assertEquals('integer_column', $intColumn['COLUMN_NAME'], 'Incorrect column name'); |
| 219 | + $this->assertEquals('int', $intColumn['DATA_TYPE'], 'Incorrect column type'); |
| 220 | + $this->assertEquals(123456, $intColumn['DEFAULT'], 'Incorrect column default integer value'); |
| 221 | + |
| 222 | + //Test default value with string value |
| 223 | + $this->assertEquals('string_column', $stringColumn['COLUMN_NAME'], 'Incorrect column name'); |
| 224 | + $this->assertEquals('varchar', $stringColumn['DATA_TYPE'], 'Incorrect column type'); |
| 225 | + $this->assertEquals('default test text', $stringColumn['DEFAULT'], 'Incorrect column default string value'); |
| 226 | + } |
160 | 227 | }
|
0 commit comments