|
9 | 9 | namespace Propel\Tests\Generator\Reverse; |
10 | 10 |
|
11 | 11 | use PDO; |
| 12 | +use Propel\Generator\Config\QuickGeneratorConfig; |
12 | 13 | use Propel\Generator\Model\Column; |
13 | 14 | use Propel\Generator\Model\Table; |
14 | 15 | use Propel\Generator\Model\ColumnDefaultValue; |
| 16 | +use Propel\Generator\Model\Database; |
| 17 | +use Propel\Generator\Platform\DefaultPlatform; |
15 | 18 | use Propel\Generator\Reverse\MysqlSchemaParser; |
16 | 19 | use Propel\Tests\Bookstore\Map\BookTableMap; |
17 | 20 |
|
@@ -150,4 +153,112 @@ public function testOnUpdateIsImported(): void |
150 | 153 | $this->assertEquals(ColumnDefaultValue::TYPE_EXPR, $updatedAtColumn->getDefaultValue()->getType()); |
151 | 154 | $this->assertEquals('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', $updatedAtColumn->getDefaultValue()->getValue()); |
152 | 155 | } |
| 156 | + |
| 157 | + /** |
| 158 | + * @return string[][] |
| 159 | + */ |
| 160 | + public function TextColumnDefaultValueDataProvider(): array |
| 161 | + { |
| 162 | + return [ |
| 163 | + ["_latin1\'Foo\'", 'Foo'], |
| 164 | + ["_utf8mb3\'Foo\'", 'Foo'], |
| 165 | + ["_utf8mb3\'Saying \\\'Foo\\\' means nothing\'", "Saying 'Foo' means nothing"], |
| 166 | + ["_utf8mb3\'Saying \"Foo\" means nothing\'", 'Saying "Foo" means nothing'], |
| 167 | + ["_utf8mb4\'\'", ''], |
| 168 | + ["'Saying \'Foo\' means nothing'", "Saying 'Foo' means nothing"], |
| 169 | + ["'Foo'", 'Foo'], |
| 170 | + ["''", ''], |
| 171 | + ['', ''], |
| 172 | + ]; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @dataProvider TextColumnDefaultValueDataProvider |
| 177 | + * |
| 178 | + * @param string $defaultValue |
| 179 | + * @param string $expected |
| 180 | + * |
| 181 | + * @return void |
| 182 | + */ |
| 183 | + public function testTextColumnDefaultValue(string $defaultValue, string $expected): void |
| 184 | + { |
| 185 | + $parser = new MysqlSchemaParser(); |
| 186 | + $actual = $this->callMethod($parser, 'unwrapDefaultValueString', [$defaultValue]); |
| 187 | + |
| 188 | + $this->assertSame($expected, $actual); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @return void |
| 193 | + */ |
| 194 | + public function testTextDefaultValues(): void |
| 195 | + { |
| 196 | + $serverVersion = $this->con->getAttribute(PDO::ATTR_SERVER_VERSION); |
| 197 | + $isMariaDb = stripos($serverVersion, 'mariadb') !== false; |
| 198 | + |
| 199 | + if ($isMariaDb) { |
| 200 | + if (version_compare(preg_replace('/^.*?(\d+\.\d+\.\d+).*$/', '$1', $serverVersion), '10.2.1', '<')) { |
| 201 | + $this->markTestSkipped('TEXT columns with DEFAULT values require MariaDB 10.2.1+'); |
| 202 | + } |
| 203 | + } else { |
| 204 | + if (version_compare($serverVersion, '8.0.13', '<')) { |
| 205 | + $this->markTestSkipped('TEXT columns with DEFAULT values require MySQL 8.0.13+'); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + $this->con->exec('DROP TABLE IF EXISTS test_text_defaults'); |
| 210 | + $this->con->exec("CREATE TABLE test_text_defaults ( |
| 211 | + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, |
| 212 | + content_text TEXT DEFAULT ('hello text'), |
| 213 | + content_text_escaped TEXT DEFAULT ('foo says \'bar\''), |
| 214 | + content_text_empty TEXT DEFAULT (''), |
| 215 | + content_text_not_null TEXT NOT NULL, |
| 216 | + content_text_none TEXT |
| 217 | + )"); |
| 218 | + |
| 219 | + try { |
| 220 | + $parser = new MysqlSchemaParser($this->con); |
| 221 | + $parser->setGeneratorConfig(new QuickGeneratorConfig()); |
| 222 | + |
| 223 | + $database = new Database(); |
| 224 | + $database->setPlatform(new DefaultPlatform()); |
| 225 | + $parser->parse($database); |
| 226 | + |
| 227 | + $table = $database->getTable('test_text_defaults'); |
| 228 | + $this->assertNotNull($table, 'Table test_text_defaults should be parsed'); |
| 229 | + |
| 230 | + // TEXT with default value |
| 231 | + $contentText = $table->getColumn('content_text'); |
| 232 | + $this->assertNotNull($contentText, 'Column content_text should exist'); |
| 233 | + $this->assertNotNull($contentText->getDefaultValue(), 'TEXT column default value should be preserved'); |
| 234 | + $this->assertEquals(ColumnDefaultValue::TYPE_VALUE, $contentText->getDefaultValue()->getType()); |
| 235 | + $this->assertEquals('hello text', $contentText->getDefaultValue()->getValue()); |
| 236 | + |
| 237 | + // TEXT with default value |
| 238 | + $contentText = $table->getColumn('content_text_escaped'); |
| 239 | + $this->assertNotNull($contentText, 'Column content_text should exist'); |
| 240 | + $this->assertNotNull($contentText->getDefaultValue(), 'TEXT column default value should be preserved'); |
| 241 | + $this->assertEquals(ColumnDefaultValue::TYPE_VALUE, $contentText->getDefaultValue()->getType()); |
| 242 | + $this->assertEquals("foo says 'bar'", $contentText->getDefaultValue()->getValue()); |
| 243 | + |
| 244 | + // TEXT with empty string default |
| 245 | + $contentTextEmpty = $table->getColumn('content_text_empty'); |
| 246 | + $this->assertNotNull($contentTextEmpty, 'Column content_text_empty should exist'); |
| 247 | + $this->assertNotNull($contentTextEmpty->getDefaultValue(), 'TEXT column with empty default should be preserved'); |
| 248 | + $this->assertEquals(ColumnDefaultValue::TYPE_VALUE, $contentTextEmpty->getDefaultValue()->getType()); |
| 249 | + $this->assertEquals('', $contentTextEmpty->getDefaultValue()->getValue()); |
| 250 | + |
| 251 | + // TEXT NOT NULL without default |
| 252 | + $contentTextNotNull = $table->getColumn('content_text_not_null'); |
| 253 | + $this->assertNotNull($contentTextNotNull, 'Column content_text_not_null should exist'); |
| 254 | + $this->assertNull($contentTextNotNull->getDefaultValue(), 'TEXT NOT NULL column without default should have no default'); |
| 255 | + |
| 256 | + // TEXT without default (nullable, implicit DEFAULT NULL) |
| 257 | + $contentTextNone = $table->getColumn('content_text_none'); |
| 258 | + $this->assertNotNull($contentTextNone, 'Column content_text_none should exist'); |
| 259 | + $this->assertNull($contentTextNone->getDefaultValue(), 'TEXT column without explicit default should have no default'); |
| 260 | + } finally { |
| 261 | + $this->con->exec('DROP TABLE IF EXISTS test_text_defaults'); |
| 262 | + } |
| 263 | + } |
153 | 264 | } |
0 commit comments