Skip to content

Commit c985f72

Browse files
author
Oleksii Korshenko
committed
MAGETWO-67404: Fix to allow Zend_Db_Expr as column default #9131
- added integration test to allow Zend_Db_Expr as column default value
1 parent e4c63c3 commit c985f72

File tree

1 file changed

+67
-0
lines changed
  • dev/tests/integration/testsuite/Magento/Framework/DB/Adapter/Pdo

1 file changed

+67
-0
lines changed

Diff for: dev/tests/integration/testsuite/Magento/Framework/DB/Adapter/Pdo/MysqlTest.php

+67
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\App\ResourceConnection;
99
use Magento\TestFramework\Helper\CacheCleaner;
10+
use Magento\Framework\DB\Ddl\Table;
1011

1112
class MysqlTest extends \PHPUnit_Framework_TestCase
1213
{
@@ -157,4 +158,70 @@ public function testDescribeTable()
157158
$this->getDbAdapter()->describeTable($tableName)
158159
);
159160
}
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+
}
160227
}

0 commit comments

Comments
 (0)