|
6 | 6 |
|
7 | 7 | use PhpMyAdmin\Current; |
8 | 8 | use PhpMyAdmin\Dbal\DatabaseInterface; |
| 9 | +use PhpMyAdmin\Import\AnalysedColumn; |
9 | 10 | use PhpMyAdmin\Import\ColumnType; |
10 | 11 | use PhpMyAdmin\Import\DecimalSize; |
11 | 12 | use PhpMyAdmin\Import\Import; |
12 | 13 | use PhpMyAdmin\Import\ImportSettings; |
| 14 | +use PhpMyAdmin\Import\ImportTable; |
13 | 15 | use PhpMyAdmin\Tests\AbstractTestCase; |
14 | 16 | use PHPUnit\Framework\Attributes\CoversClass; |
15 | 17 | use PHPUnit\Framework\Attributes\DataProvider; |
16 | 18 |
|
| 19 | +use function array_map; |
17 | 20 | use function time; |
18 | 21 |
|
19 | 22 | use const PHP_INT_MAX; |
20 | 23 |
|
21 | 24 | #[CoversClass(Import::class)] |
22 | | -class ImportTest extends AbstractTestCase |
| 25 | +#[CoversClass(ImportTable::class)] |
| 26 | +#[CoversClass(AnalysedColumn::class)] |
| 27 | +final class ImportTest extends AbstractTestCase |
23 | 28 | { |
24 | 29 | private Import $import; |
25 | 30 |
|
@@ -349,4 +354,224 @@ public function testRunQuery(): void |
349 | 354 | self::assertSame('SELECT 1;SELECT 2;', Current::$completeQuery); |
350 | 355 | self::assertSame('SELECT 1;SELECT 2;', Current::$displayQuery); |
351 | 356 | } |
| 357 | + |
| 358 | + /** |
| 359 | + * @param list<string> $columns |
| 360 | + * @param list<list<mixed>> $rows |
| 361 | + * @param list<array{ColumnType, DecimalSize|int}> $expected |
| 362 | + */ |
| 363 | + #[DataProvider('providerForTestAnalyzeTable')] |
| 364 | + public function testAnalyzeTable(array $columns, array $rows, array $expected): void |
| 365 | + { |
| 366 | + $import = new Import(); |
| 367 | + self::assertEquals( |
| 368 | + array_map(static fn (array $column): AnalysedColumn => new AnalysedColumn(...$column), $expected), |
| 369 | + $import->analyzeTable(new ImportTable('test_table', $columns, $rows)), |
| 370 | + ); |
| 371 | + } |
| 372 | + |
| 373 | + /** @return iterable<array-key, array{list<string>, list<list<mixed>>, list<array{ColumnType, DecimalSize|int}>}> */ |
| 374 | + public static function providerForTestAnalyzeTable(): iterable |
| 375 | + { |
| 376 | + yield [ |
| 377 | + ['empty', 'null', 'varchar', 'int', 'decimal', 'big decimal', 'emoji'], |
| 378 | + [['', 'NULL', 'varchar', '123', '123.123', '2147483647.2147483647', '⛵']], |
| 379 | + [ |
| 380 | + [ColumnType::Varchar, 0], |
| 381 | + [ColumnType::Varchar, 10], |
| 382 | + [ColumnType::Varchar, 7], |
| 383 | + [ColumnType::Int, 3], |
| 384 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(6, 3)], |
| 385 | + [ColumnType::Varchar, 21], |
| 386 | + [ColumnType::Varchar, 1], |
| 387 | + ], |
| 388 | + ]; |
| 389 | + |
| 390 | + if (PHP_INT_MAX > 2147483647) { |
| 391 | + yield [['bigint'], [['2222222222']], [[ColumnType::BigInt, 10]]]; |
| 392 | + |
| 393 | + yield [ |
| 394 | + ['col1', 'col2', 'col3', 'col4'], |
| 395 | + [['2147483646', '2147483647', '2147483648', '2147483649']], |
| 396 | + [[ColumnType::Int, 10], [ColumnType::Int, 10], [ColumnType::BigInt, 10], [ColumnType::BigInt, 10]], |
| 397 | + ]; |
| 398 | + |
| 399 | + yield [ |
| 400 | + ['less', 'equal', 'greater'], |
| 401 | + [['2147483648', '2147483648', '2147483648'], ['1.1', '214748364.1', '2147483648.1']], |
| 402 | + [ |
| 403 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(10, 1)], |
| 404 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(10, 1)], |
| 405 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(11, 1)], |
| 406 | + ], |
| 407 | + ]; |
| 408 | + |
| 409 | + yield [ |
| 410 | + ['less', 'equal', 'greater'], |
| 411 | + [['21474836480.1', '21474836480.1', '21474836480.1'], ['2147483648', '21474836480', '214748364800']], |
| 412 | + [ |
| 413 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(12, 1)], |
| 414 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(12, 1)], |
| 415 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(13, 1)], |
| 416 | + ], |
| 417 | + ]; |
| 418 | + |
| 419 | + yield [ |
| 420 | + ['less', 'equal', 'greater'], |
| 421 | + [['21474836480', '21474836480', '21474836480'], ['2147483648', '21474836480', '214748364800']], |
| 422 | + [[ColumnType::BigInt, 11], [ColumnType::BigInt, 11], [ColumnType::BigInt, 12]], |
| 423 | + ]; |
| 424 | + |
| 425 | + yield [ |
| 426 | + ['equal', 'greater'], |
| 427 | + [['2147483647', '2147483647'], ['2147483648', '21474836480']], |
| 428 | + [[ColumnType::BigInt, 10], [ColumnType::BigInt, 11]], |
| 429 | + ]; |
| 430 | + |
| 431 | + yield [ |
| 432 | + ['less', 'equal'], |
| 433 | + [['2147483648', '2147483648'], ['214748364', '2147483647']], |
| 434 | + [[ColumnType::BigInt, 10], [ColumnType::BigInt, 10]], |
| 435 | + ]; |
| 436 | + } else { |
| 437 | + // Can not detect a BIGINT since the value is over PHP_INT_MAX |
| 438 | + yield [['bigint'], [['2222222222']], [[ColumnType::Varchar, 10]]]; |
| 439 | + |
| 440 | + yield [ |
| 441 | + ['col1', 'col2', 'col3', 'col4'], |
| 442 | + [['2147483646', '2147483647', '2147483648', '2147483649']], |
| 443 | + [[ColumnType::Int, 10], [ColumnType::Int, 10], [ColumnType::Varchar, 10], [ColumnType::Varchar, 10]], |
| 444 | + ]; |
| 445 | + |
| 446 | + yield [ |
| 447 | + ['less', 'equal', 'greater'], |
| 448 | + [['2147483648', '2147483648', '2147483648'], ['1.1', '214748364.1', '2147483648.1']], |
| 449 | + [[ColumnType::Varchar, 10], [ColumnType::Varchar, 10], [ColumnType::Varchar, 11]], |
| 450 | + ]; |
| 451 | + |
| 452 | + yield [ |
| 453 | + ['less', 'equal', 'greater'], |
| 454 | + [['21474836480.1', '21474836480.1', '21474836480.1'], ['2147483648', '21474836480', '214748364800']], |
| 455 | + [[ColumnType::Varchar, 12], [ColumnType::Varchar, 12], [ColumnType::Varchar, 12]], |
| 456 | + ]; |
| 457 | + |
| 458 | + yield [ |
| 459 | + ['less', 'equal', 'greater'], |
| 460 | + [['21474836480', '21474836480', '21474836480'], ['2147483648', '21474836480', '214748364800']], |
| 461 | + [[ColumnType::Varchar, 11], [ColumnType::Varchar, 11], [ColumnType::Varchar, 12]], |
| 462 | + ]; |
| 463 | + |
| 464 | + yield [ |
| 465 | + ['equal', 'greater'], |
| 466 | + [['2147483647', '2147483647'], ['2147483648', '21474836480']], |
| 467 | + [[ColumnType::Varchar, 10], [ColumnType::Varchar, 11]], |
| 468 | + ]; |
| 469 | + |
| 470 | + yield [ |
| 471 | + ['less', 'equal'], |
| 472 | + [['2147483648', '2147483648'], ['214748364', '2147483647']], |
| 473 | + [[ColumnType::Varchar, 10], [ColumnType::Varchar, 10]], |
| 474 | + ]; |
| 475 | + } |
| 476 | + |
| 477 | + yield [ |
| 478 | + ['col1', 'col2', 'col3', 'col4', 'col5'], |
| 479 | + [['1.1', '12.12', '123.123', '1.0', '1.2.3']], |
| 480 | + [ |
| 481 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(2, 1)], |
| 482 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(4, 2)], |
| 483 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(6, 3)], |
| 484 | + [ColumnType::Varchar, 3], |
| 485 | + [ColumnType::Varchar, 5], |
| 486 | + ], |
| 487 | + ]; |
| 488 | + |
| 489 | + yield [ |
| 490 | + ['less', 'equal', 'greater'], |
| 491 | + [['aa', 'aa', 'aa'], ['a', 'aa', 'aaa']], |
| 492 | + [[ColumnType::Varchar, 2], [ColumnType::Varchar, 2], [ColumnType::Varchar, 3]], |
| 493 | + ]; |
| 494 | + |
| 495 | + yield [ |
| 496 | + ['less', 'equal', 'greater'], |
| 497 | + [['1.1', '1.1', '1.1'], ['a', 'aa', 'aaa']], |
| 498 | + [[ColumnType::Varchar, 2], [ColumnType::Varchar, 2], [ColumnType::Varchar, 3]], |
| 499 | + ]; |
| 500 | + |
| 501 | + yield [ |
| 502 | + ['less', 'equal', 'greater'], |
| 503 | + [['11', '11', '11'], ['a', 'aa', 'aaa']], |
| 504 | + [[ColumnType::Varchar, 2], [ColumnType::Varchar, 2], [ColumnType::Varchar, 3]], |
| 505 | + ]; |
| 506 | + |
| 507 | + yield [ |
| 508 | + ['less', 'equal', 'greater'], |
| 509 | + [['2147483648', '2147483648', '2147483648'], ['aaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaaa']], |
| 510 | + [[ColumnType::Varchar, 10], [ColumnType::Varchar, 10], [ColumnType::Varchar, 11]], |
| 511 | + ]; |
| 512 | + |
| 513 | + yield [ |
| 514 | + ['less', 'equal', 'greater'], |
| 515 | + [['aaa', 'aaa', 'aaa'], ['1.1', '12.1', '123.1']], |
| 516 | + [[ColumnType::Varchar, 3], [ColumnType::Varchar, 3], [ColumnType::Varchar, 4]], |
| 517 | + ]; |
| 518 | + |
| 519 | + yield [ |
| 520 | + ['less', 'equal', 'greater'], |
| 521 | + [['12.1', '12.1', '12.1'], ['1.1', '12.1', '123.1']], |
| 522 | + [ |
| 523 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(3, 1)], |
| 524 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(3, 1)], |
| 525 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(4, 1)], |
| 526 | + ], |
| 527 | + ]; |
| 528 | + |
| 529 | + yield [ |
| 530 | + ['less', 'equal', 'greater'], |
| 531 | + [['1.12', '1.12', '12.12'], ['12.1', '1.12', '1.123']], |
| 532 | + [ |
| 533 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(3, 2)], |
| 534 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(3, 2)], |
| 535 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(4, 3)], |
| 536 | + ], |
| 537 | + ]; |
| 538 | + |
| 539 | + yield [ |
| 540 | + ['less', 'equal', 'greater'], |
| 541 | + [['123', '123', '123'], ['1.1', '12.1', '123.1']], |
| 542 | + [ |
| 543 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(3, 1)], |
| 544 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(3, 1)], |
| 545 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(4, 1)], |
| 546 | + ], |
| 547 | + ]; |
| 548 | + |
| 549 | + yield [ |
| 550 | + ['less', 'equal', 'greater'], |
| 551 | + [['aaaaaaaaaaa', 'aaaaaaaaaaa', 'aaaaaaaaaaa'], ['2147483648', '21474836480', '214748364800']], |
| 552 | + [[ColumnType::Varchar, 11], [ColumnType::Varchar, 11], [ColumnType::Varchar, 12]], |
| 553 | + ]; |
| 554 | + |
| 555 | + yield [ |
| 556 | + ['less', 'equal', 'greater'], |
| 557 | + [['aa', 'aa', 'aa'], ['1', '12', '123']], |
| 558 | + [[ColumnType::Varchar, 2], [ColumnType::Varchar, 2], [ColumnType::Varchar, 3]], |
| 559 | + ]; |
| 560 | + |
| 561 | + yield [ |
| 562 | + ['less', 'equal', 'greater'], |
| 563 | + [['12.1', '12.1', '12.1'], ['1', '12', '123']], |
| 564 | + [ |
| 565 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(3, 1)], |
| 566 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(3, 1)], |
| 567 | + [ColumnType::Decimal, DecimalSize::fromPrecisionAndScale(4, 1)], |
| 568 | + ], |
| 569 | + ]; |
| 570 | + |
| 571 | + yield [ |
| 572 | + ['less', 'equal', 'greater'], |
| 573 | + [['12', '12', '12'], ['1', '12', '123']], |
| 574 | + [[ColumnType::Int, 2], [ColumnType::Int, 2], [ColumnType::Int, 3]], |
| 575 | + ]; |
| 576 | + } |
352 | 577 | } |
0 commit comments