1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
<?php
namespace Doctrine\DBAL\Tests\Schema;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\SqliteSchemaManager;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
class SqliteSchemaManagerTest extends TestCase
{
/** @dataProvider getDataColumnCollation */
public function testParseColumnCollation(?string $collation, string $column, string $sql): void
{
$conn = $this->createMock(Connection::class);
$manager = new SqliteSchemaManager($conn, new SqlitePlatform());
$ref = new ReflectionMethod($manager, 'parseColumnCollationFromSQL');
$ref->setAccessible(true);
self::assertSame($collation, $ref->invoke($manager, $column, $sql));
}
/** @return mixed[][] */
public static function getDataColumnCollation(): iterable
{
return [
[
'RTRIM',
'a',
'CREATE TABLE "a" ("a" text DEFAULT "aa" COLLATE "RTRIM" NOT NULL)',
],
[
'utf-8',
'a',
'CREATE TABLE "a" ("b" text UNIQUE NOT NULL COLLATE NOCASE, '
. '"a" text DEFAULT "aa" COLLATE "utf-8" NOT NULL)',
],
[
'NOCASE',
'a',
'CREATE TABLE "a" ("a" text DEFAULT (lower(ltrim(" a") || rtrim("a ")))'
. ' CHECK ("a") NOT NULL COLLATE NOCASE UNIQUE, "b" text COLLATE RTRIM)',
],
[
null,
'a',
'CREATE TABLE "a" ("a" text CHECK ("a") NOT NULL, "b" text COLLATE RTRIM)',
],
[
'RTRIM',
'a"b',
'CREATE TABLE "a" ("a""b" text COLLATE RTRIM)',
],
[
'BINARY',
'b',
'CREATE TABLE "a" (bb TEXT COLLATE RTRIM, b VARCHAR(42) NOT NULL COLLATE BINARY)',
],
[
'BINARY',
'b',
'CREATE TABLE "a" (bbb TEXT COLLATE NOCASE, bb TEXT COLLATE RTRIM, '
. 'b VARCHAR(42) NOT NULL COLLATE BINARY)',
],
[
'BINARY',
'b',
'CREATE TABLE "a" (b VARCHAR(42) NOT NULL COLLATE BINARY, bb TEXT COLLATE RTRIM)',
],
[
'utf-8',
'bar#',
'CREATE TABLE dummy_table (id INTEGER NOT NULL, foo VARCHAR(255) COLLATE "utf-8" NOT NULL, '
. '"bar#" VARCHAR(255) COLLATE "utf-8" NOT NULL, baz VARCHAR(255) COLLATE "utf-8" NOT NULL, '
. 'PRIMARY KEY(id))',
],
[
null,
'bar#',
'CREATE TABLE dummy_table (id INTEGER NOT NULL, foo VARCHAR(255) NOT NULL,'
. ' "bar#" VARCHAR(255) NOT NULL, baz VARCHAR(255) NOT NULL, PRIMARY KEY(id))',
],
[
'utf-8',
'baz',
'CREATE TABLE dummy_table (id INTEGER NOT NULL, foo VARCHAR(255) COLLATE "utf-8" NOT NULL,'
. ' "bar#" INTEGER NOT NULL, baz VARCHAR(255) COLLATE "utf-8" NOT NULL, PRIMARY KEY(id))',
],
[
null,
'baz',
'CREATE TABLE dummy_table (id INTEGER NOT NULL, foo VARCHAR(255) NOT NULL, "bar#" INTEGER NOT NULL, '
. 'baz VARCHAR(255) NOT NULL, PRIMARY KEY(id))',
],
[
'utf-8',
'bar/',
'CREATE TABLE dummy_table (id INTEGER NOT NULL, foo VARCHAR(255) COLLATE "utf-8" NOT NULL, '
. '"bar/" VARCHAR(255) COLLATE "utf-8" NOT NULL, baz VARCHAR(255) COLLATE "utf-8" NOT NULL,'
. ' PRIMARY KEY(id))',
],
[
null,
'bar/',
'CREATE TABLE dummy_table (id INTEGER NOT NULL, foo VARCHAR(255) NOT NULL, '
. '"bar/" VARCHAR(255) NOT NULL, baz VARCHAR(255) NOT NULL, PRIMARY KEY(id))',
],
[
'utf-8',
'baz',
'CREATE TABLE dummy_table (id INTEGER NOT NULL, foo VARCHAR(255) COLLATE "utf-8" NOT NULL, '
. '"bar/" INTEGER NOT NULL, baz VARCHAR(255) COLLATE "utf-8" NOT NULL, PRIMARY KEY(id))',
],
[
null,
'baz',
'CREATE TABLE dummy_table (id INTEGER NOT NULL, foo VARCHAR(255) NOT NULL,'
. ' "bar/" INTEGER NOT NULL, baz VARCHAR(255) NOT NULL, PRIMARY KEY(id))',
],
];
}
/** @dataProvider getDataColumnComment */
public function testParseColumnCommentFromSQL(?string $comment, string $column, string $sql): void
{
$conn = $this->createMock(Connection::class);
$manager = new SqliteSchemaManager($conn, new SqlitePlatform());
$ref = new ReflectionMethod($manager, 'parseColumnCommentFromSQL');
$ref->setAccessible(true);
self::assertSame($comment, $ref->invoke($manager, $column, $sql));
}
/** @return mixed[][] */
public static function getDataColumnComment(): iterable
{
return [
'Single column with no comment' => [
null,
'a',
'CREATE TABLE "a" ("a" TEXT DEFAULT "a" COLLATE RTRIM)',
],
'Single column with type comment' => [
'(DC2Type:x)',
'a',
'CREATE TABLE "a" ("a" CLOB DEFAULT NULL COLLATE BINARY --(DC2Type:x)
)',
],
'Multiple similar columns with type comment 1' => [
null,
'b',
'CREATE TABLE "a" (a TEXT COLLATE RTRIM, "b" TEXT DEFAULT "a" COLLATE RTRIM, '
. '"bb" CLOB DEFAULT NULL COLLATE BINARY --(DC2Type:x)
)',
],
'Multiple similar columns with type comment 2' => [
'(DC2Type:x)',
'b',
'CREATE TABLE "a" (a TEXT COLLATE RTRIM, "bb" TEXT DEFAULT "a" COLLATE RTRIM, '
. '"b" CLOB DEFAULT NULL COLLATE BINARY --(DC2Type:x)
)',
],
'Multiple similar columns on different lines, with type comment 1' => [
null,
'bb',
'CREATE TABLE "a" (a TEXT COLLATE RTRIM, "b" CLOB DEFAULT NULL COLLATE BINARY --(DC2Type:x)
, "bb" TEXT DEFAULT "a" COLLATE RTRIM',
],
'Multiple similar columns on different lines, with type comment 2' => [
'(DC2Type:x)',
'bb',
'CREATE TABLE "a" (a TEXT COLLATE RTRIM, "bb" CLOB DEFAULT NULL COLLATE BINARY --(DC2Type:x)
, "b" TEXT DEFAULT "a" COLLATE RTRIM',
],
'Column with numeric but no comment 1' => [
null,
'a',
'CREATE TABLE "a" ("a" NUMERIC(10, 0) NOT NULL, "b" CLOB NOT NULL --(DC2Type:array)
, "c" CHAR(36) NOT NULL --(DC2Type:guid)
)',
],
'Column with numeric but no comment 2' => [
null,
'a',
'CREATE TABLE "b" ("a" NUMERIC(10, 0) NOT NULL, "b" CLOB NOT NULL --(DC2Type:array)
, "c" CHAR(36) NOT NULL --(DC2Type:guid)
)',
],
'Column with numeric but no comment 3' => [
'(DC2Type:guid)',
'c',
'CREATE TABLE "b" ("a" NUMERIC(10, 0) NOT NULL, "b" CLOB NOT NULL --(DC2Type:array)
, "c" CHAR(36) NOT NULL --(DC2Type:guid)
)',
],
'Column with numeric but no comment 4' => [
'(DC2Type:array)',
'b',
'CREATE TABLE "b" ("a" NUMERIC(10, 0) NOT NULL,
"b" CLOB NOT NULL, --(DC2Type:array)
"c" CHAR(36) NOT NULL --(DC2Type:guid)
)',
],
'Column "bar", select "bar" with no comment' => [
null,
'bar',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar" VARCHAR(255) COLLATE "utf-8" NOT NULL,
baz VARCHAR(255) COLLATE "utf-8" NOT NULL,
PRIMARY KEY(id)
)',
],
'Column "bar", select "bar" with type comment' => [
'(DC2Type:x)',
'bar',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar" VARCHAR(255) COLLATE "utf-8" NOT NULL, --(DC2Type:x)
baz VARCHAR(255) COLLATE "utf-8" NOT NULL, --(DC2Type:y)
PRIMARY KEY(id)
)',
],
'Column "bar", select "baz" with no comment' => [
null,
'baz',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar" INTEGER NOT NULL,
baz VARCHAR(255) COLLATE "utf-8" NOT NULL,
PRIMARY KEY(id)
)',
],
'Column "bar", select "baz" with type comment' => [
'(DC2Type:y)',
'baz',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar" INTEGER NOT NULL, --(DC2Type:x)
baz VARCHAR(255) COLLATE "utf-8" NOT NULL, --(DC2Type:y)
PRIMARY KEY(id)
)',
],
'Column "bar#", select "bar#" with no comment' => [
null,
'bar#',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar#" VARCHAR(255) COLLATE "utf-8" NOT NULL,
baz VARCHAR(255) COLLATE "utf-8" NOT NULL,
PRIMARY KEY(id)
)',
],
'Column "bar#", select "bar#" with type comment' => [
'(DC2Type:x)',
'bar#',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar#" VARCHAR(255) COLLATE "utf-8" NOT NULL, --(DC2Type:x)
baz VARCHAR(255) COLLATE "utf-8" NOT NULL, --(DC2Type:y)
PRIMARY KEY(id)
)',
],
'Column "bar#", select "baz" with no comment' => [
null,
'baz',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar#" INTEGER NOT NULL,
baz VARCHAR(255) COLLATE "utf-8" NOT NULL,
PRIMARY KEY(id)
)',
],
'Column "bar#", select "baz" with type comment' => [
'(DC2Type:y)',
'baz',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar#" INTEGER NOT NULL, --(DC2Type:x)
baz VARCHAR(255) COLLATE "utf-8" NOT NULL, --(DC2Type:y)
PRIMARY KEY(id)
)',
],
'Column "bar/", select "bar/" with no comment' => [
null,
'bar/',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar/" VARCHAR(255) COLLATE "utf-8" NOT NULL,
baz VARCHAR(255) COLLATE "utf-8" NOT NULL,
PRIMARY KEY(id)
)',
],
'Column "bar/", select "bar/" with type comment' => [
'(DC2Type:x)',
'bar/',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar/" VARCHAR(255) COLLATE "utf-8" NOT NULL, --(DC2Type:x)
baz VARCHAR(255) COLLATE "utf-8" NOT NULL, --(DC2Type:y)
PRIMARY KEY(id)
)',
],
'Column "bar/", select "baz" with no comment' => [
null,
'baz',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar/" INTEGER NOT NULL,
baz VARCHAR(255) COLLATE "utf-8" NOT NULL,
PRIMARY KEY(id)
)',
],
'Column "bar/", select "baz" with type comment' => [
'(DC2Type:y)',
'baz',
'CREATE TABLE dummy_table (
id INTEGER NOT NULL,
foo VARCHAR(255) COLLATE "utf-8" NOT NULL,
"bar/" INTEGER COLLATE "utf-8" NOT NULL, --(DC2Type:x)
baz VARCHAR(255) COLLATE "utf-8" NOT NULL, --(DC2Type:y)
PRIMARY KEY(id)
)',
],
];
}
}
|