From 94f678696e4f6a74bcebb821a7fb051d3d44f733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Mon, 11 Sep 2017 11:31:50 +0200 Subject: [PATCH] Add test to ensure json_array can still be used On 31d01283b0bd9f9b3cafa8c6c06d6a45838ad0dd we made JsonType the default one, which caused some unexpected results with the comparator. This tests validates that if users have a JSON column but are using the JsonArrayType we'll just add a comment to the column. Reference: https://github.com/doctrine/dbal/pull/2782 --- .../SchemaManagerFunctionalTestCase.php | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php index 9b22414e9d1..1a469f79bb4 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -1192,4 +1192,111 @@ public function testDoesNotListIndexesImplicitlyCreatedByForeignKeys() self::assertArrayHasKey('explicit_fk1_idx', $indexes); self::assertArrayHasKey('idx_3d6c147fdc58d6c', $indexes); } + + /** + * @group 2782 + * @group 6654 + */ + public function testComparatorShouldReturnFalseWhenLegacyJsonArrayColumnHasComment() + { + if ( ! $this->_sm->getDatabasePlatform()->hasNativeJsonType()) { + $this->markTestSkipped('This test is only supported on platforms that have native JSON type.'); + } + + $table = new Table('json_array_test'); + $table->addColumn('id', 'integer'); + $table->addColumn('parameters', 'json_array'); + $table->setPrimaryKey(['id']); + + $this->_sm->createTable($table); + + $comparator = new Comparator(); + $tableDiff = $comparator->diffTable($this->_sm->listTableDetails('json_array_test'), $table); + + self::assertFalse($tableDiff); + + $this->_sm->dropTable('json_array_test'); + } + + /** + * @group 2782 + * @group 6654 + */ + public function testComparatorShouldAddCommentToLegacyJsonArrayType() + { + if ( ! $this->_sm->getDatabasePlatform()->hasNativeJsonType()) { + $this->markTestSkipped('This test is only supported on platforms that have native JSON type.'); + } + + $this->_conn->executeQuery( + 'CREATE TABLE json_array_test (id INT NOT NULL, parameters JSON NOT NULL, PRIMARY KEY(id))' + ); + + $table = new Table('json_array_test'); + $table->addColumn('id', 'integer'); + $table->addColumn('parameters', 'json_array'); + $table->setPrimaryKey(['id']); + + $comparator = new Comparator(); + $tableDiff = $comparator->diffTable($this->_sm->listTableDetails('json_array_test'), $table); + + $this->_sm->dropTable('json_array_test'); + + self::assertInstanceOf(TableDiff::class, $tableDiff); + self::assertSame(['comment'], $tableDiff->changedColumns['parameters']->changedProperties); + } + + /** + * @group 2782 + * @group 6654 + */ + public function testComparatorShouldReturnAllChangesWhenUsingLegacyJsonArrayType() + { + if ( ! $this->_sm->getDatabasePlatform()->hasNativeJsonType()) { + $this->markTestSkipped('This test is only supported on platforms that have native JSON type.'); + } + + $this->_conn->executeQuery( + 'CREATE TABLE json_array_test (id INT NOT NULL, parameters JSON DEFAULT NULL, PRIMARY KEY(id))' + ); + + $table = new Table('json_array_test'); + $table->addColumn('id', 'integer'); + $table->addColumn('parameters', 'json_array'); + $table->setPrimaryKey(['id']); + + $comparator = new Comparator(); + $tableDiff = $comparator->diffTable($this->_sm->listTableDetails('json_array_test'), $table); + + $this->_sm->dropTable('json_array_test'); + + self::assertInstanceOf(TableDiff::class, $tableDiff); + self::assertSame(['notnull', 'comment'], $tableDiff->changedColumns['parameters']->changedProperties); + } + + /** + * @group 2782 + * @group 6654 + */ + public function testComparatorShouldNotAddCommentToJsonTypeSinceItIsTheDefaultNow() + { + if ( ! $this->_sm->getDatabasePlatform()->hasNativeJsonType()) { + $this->markTestSkipped('This test is only supported on platforms that have native JSON type.'); + } + + $this->_conn->executeQuery( + 'CREATE TABLE json_test (id INT NOT NULL, parameters JSON NOT NULL, PRIMARY KEY(id))' + ); + + $table = new Table('json_test'); + $table->addColumn('id', 'integer'); + $table->addColumn('parameters', 'json'); + $table->setPrimaryKey(['id']); + + $comparator = new Comparator(); + $tableDiff = $comparator->diffTable($this->_sm->listTableDetails('json_test'), $table); + $this->_sm->dropTable('json_test'); + + self::assertFalse($tableDiff); + } }