|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Framework\Setup\Declaration\Schema\Db\MySQL; |
| 7 | + |
| 8 | +use Magento\Framework\App\ResourceConnection; |
| 9 | +use Magento\Framework\DB\Ddl\Table; |
| 10 | +use Magento\TestFramework\Helper\Bootstrap; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test DB schema writer |
| 14 | + * |
| 15 | + * @magentoDbIsolation disabled |
| 16 | + */ |
| 17 | +class DbSchemaWriterTest extends \PHPUnit\Framework\TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var ResourceConnection |
| 21 | + */ |
| 22 | + private $resourceConnection; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var DbSchemaWriter |
| 26 | + */ |
| 27 | + private $dbSchemaWriter; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + set_error_handler(null); |
| 32 | + $this->resourceConnection = Bootstrap::getObjectManager()->get(ResourceConnection::class); |
| 33 | + $this->dbSchemaWriter = Bootstrap::getObjectManager()->get(DbSchemaWriter::class); |
| 34 | + } |
| 35 | + |
| 36 | + protected function tearDown(): void |
| 37 | + { |
| 38 | + restore_error_handler(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Retrieve database adapter instance |
| 43 | + * |
| 44 | + * @return \Magento\Framework\DB\Adapter\Pdo\Mysql |
| 45 | + */ |
| 46 | + private function getDbAdapter() |
| 47 | + { |
| 48 | + return $this->resourceConnection->getConnection(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test reset auto increment |
| 53 | + * |
| 54 | + * @param array $options |
| 55 | + * @param string|bool $expected |
| 56 | + * @throws \Zend_Db_Exception |
| 57 | + * @dataProvider getAutoIncrementFieldDataProvider |
| 58 | + */ |
| 59 | + public function testResetAutoIncrement(array $options, $expected) |
| 60 | + { |
| 61 | + $adapter = $this->getDbAdapter(); |
| 62 | + $tableName = 'table_auto_increment_field'; |
| 63 | + |
| 64 | + $table = $adapter |
| 65 | + ->newTable($tableName) |
| 66 | + ->addColumn( |
| 67 | + 'row_id', |
| 68 | + Table::TYPE_INTEGER, |
| 69 | + null, |
| 70 | + $options, |
| 71 | + 'Row Id' |
| 72 | + ) |
| 73 | + ->addColumn( |
| 74 | + 'created_at', |
| 75 | + Table::TYPE_DATETIME, |
| 76 | + null, |
| 77 | + ['default' => new \Zend_Db_Expr('CURRENT_TIMESTAMP')] |
| 78 | + ) |
| 79 | + ->addColumn( |
| 80 | + 'integer_column', |
| 81 | + Table::TYPE_INTEGER, |
| 82 | + 11, |
| 83 | + ['default' => 123456] |
| 84 | + )->addColumn( |
| 85 | + 'string_column', |
| 86 | + Table::TYPE_TEXT, |
| 87 | + 255, |
| 88 | + ['default' => 'default test text'] |
| 89 | + ) |
| 90 | + ->setComment('Test table column with expression as column default value'); |
| 91 | + $adapter->createTable($table); |
| 92 | + |
| 93 | + $dbStatement = $this->dbSchemaWriter->resetAutoIncrement($tableName, 'default'); |
| 94 | + $this->assertEquals($expected, $dbStatement->getStatement()); |
| 95 | + |
| 96 | + //clean up database from test table |
| 97 | + $adapter->dropTable($tableName); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + public function getAutoIncrementFieldDataProvider() |
| 102 | + { |
| 103 | + return [ |
| 104 | + 'auto increment field' => [ |
| 105 | + 'field options' => ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], |
| 106 | + 'expected result' => 'AUTO_INCREMENT = 0', |
| 107 | + ], |
| 108 | + 'non auto increment field' => [ |
| 109 | + 'field options' => ['unsigned' => true, 'nullable' => false,], |
| 110 | + 'expected result' => 'AUTO_INCREMENT = 1', |
| 111 | + ] |
| 112 | + ]; |
| 113 | + } |
| 114 | +} |
0 commit comments