|
2 | 2 | namespace Vimeo\MysqlEngine\Tests;
|
3 | 3 |
|
4 | 4 | use PDOException;
|
| 5 | +use Vimeo\MysqlEngine\Parser\Token; |
| 6 | +use Vimeo\MysqlEngine\Query\Expression\ColumnExpression; |
| 7 | +use Vimeo\MysqlEngine\TokenType; |
5 | 8 |
|
6 | 9 | class EndToEndTest extends \PHPUnit\Framework\TestCase
|
7 | 10 | {
|
@@ -530,6 +533,113 @@ public function testDateArithhmetic()
|
530 | 533 | );
|
531 | 534 | }
|
532 | 535 |
|
| 536 | + /** |
| 537 | + * Test various timestamp differences using the TIMESTAMPDIFF function. |
| 538 | + * |
| 539 | + * This method verifies the calculation of differences in seconds, minutes, |
| 540 | + * hours, days, months, and years. |
| 541 | + */ |
| 542 | + public function testTimestampDiff(): void |
| 543 | + { |
| 544 | + // Get a PDO instance for MySQL. |
| 545 | + $pdo = self::getPdo('mysql:host=localhost;dbname=testdb'); |
| 546 | + $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
| 547 | + |
| 548 | + // Prepare a single query with multiple TIMESTAMPDIFF calls. |
| 549 | + $query = $pdo->prepare( |
| 550 | + 'SELECT |
| 551 | + TIMESTAMPDIFF(SECOND, \'2020-01-01 00:00:00\', \'2020-01-01 00:01:40\') as `second_diff`, |
| 552 | + TIMESTAMPDIFF(MINUTE, \'2020-01-01 00:00:00\', \'2020-01-01 01:30:00\') as `minute_diff`, |
| 553 | + TIMESTAMPDIFF(HOUR, \'2020-01-02 00:00:00\', \'2020-01-01 00:00:00\') as `hour_diff`, |
| 554 | + TIMESTAMPDIFF(DAY, \'2020-01-01\', \'2020-01-10\') as `day_diff`, |
| 555 | + TIMESTAMPDIFF(MONTH, \'2019-01-01\', \'2020-04-01\') as `month_diff`, |
| 556 | + TIMESTAMPDIFF(YEAR, \'2010-05-15\', \'2020-05-15\') as `year_diff`' |
| 557 | + ); |
| 558 | + |
| 559 | + $query->execute(); |
| 560 | + |
| 561 | + $results = $query->fetchAll(\PDO::FETCH_ASSOC); |
| 562 | + $castedResults = array_map(function($row) { |
| 563 | + return array_map('intval', $row); |
| 564 | + }, $results); |
| 565 | + |
| 566 | + $this->assertSame( |
| 567 | + [[ |
| 568 | + 'second_diff' => 100, |
| 569 | + 'minute_diff' => 90, |
| 570 | + 'hour_diff' => -24, |
| 571 | + 'day_diff' => 9, |
| 572 | + 'month_diff' => 15, |
| 573 | + 'year_diff' => 10, |
| 574 | + ]], |
| 575 | + $castedResults |
| 576 | + ); |
| 577 | + } |
| 578 | + |
| 579 | + public function testTimestampDiffThrowsExceptionWithWrongArgumentCount(): void |
| 580 | + { |
| 581 | + $this->expectException(\UnexpectedValueException::class); |
| 582 | + $this->expectExceptionMessage('MySQL TIMESTAMPDIFF() function must be called with three arguments'); |
| 583 | + |
| 584 | + $pdo = self::getPdo('mysql:host=localhost;dbname=testdb'); |
| 585 | + $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
| 586 | + |
| 587 | + $query = $pdo->prepare( |
| 588 | + 'SELECT |
| 589 | + TIMESTAMPDIFF(SECOND, \'2020-01-01 00:00:00\', \'2020-01-01 00:01:40\', \'2020-01-01 00:01:40\')', |
| 590 | + ); |
| 591 | + |
| 592 | + $query->execute(); |
| 593 | + } |
| 594 | + |
| 595 | + public function testTimestampDiffThrowsExceptionIfFirstArgNotColumnExpression(): void |
| 596 | + { |
| 597 | + $this->expectException(\UnexpectedValueException::class); |
| 598 | + $this->expectExceptionMessage('MySQL TIMESTAMPDIFF() function should be called with a unit for interval'); |
| 599 | + |
| 600 | + $pdo = self::getPdo('mysql:host=localhost;dbname=testdb'); |
| 601 | + $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
| 602 | + |
| 603 | + $query = $pdo->prepare( |
| 604 | + 'SELECT |
| 605 | + TIMESTAMPDIFF(\'2020-01-01 00:00:00\', \'2020-01-01 00:01:40\', \'2020-01-01 00:01:40\')', |
| 606 | + ); |
| 607 | + |
| 608 | + $query->execute(); |
| 609 | + } |
| 610 | + |
| 611 | + public function testTimestampDiffThrowsExceptionWithWrongDates(): void |
| 612 | + { |
| 613 | + $this->expectException(\UnexpectedValueException::class); |
| 614 | + $this->expectExceptionMessage('Invalid datetime value passed to TIMESTAMPDIFF()'); |
| 615 | + |
| 616 | + $pdo = self::getPdo('mysql:host=localhost;dbname=testdb'); |
| 617 | + $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
| 618 | + |
| 619 | + $query = $pdo->prepare( |
| 620 | + 'SELECT |
| 621 | + TIMESTAMPDIFF(SECOND, \'2020-01-01 00:0140\', \'2020-01-01 00:01:40\')', |
| 622 | + ); |
| 623 | + |
| 624 | + $query->execute(); |
| 625 | + } |
| 626 | + |
| 627 | + public function testTimestampDiffThrowsExceptionWithWrongInterval(): void |
| 628 | + { |
| 629 | + $this->expectException(\UnexpectedValueException::class); |
| 630 | + $this->expectExceptionMessage('Unsupported unit \'CENTURY\' in TIMESTAMPDIFF()'); |
| 631 | + |
| 632 | + $pdo = self::getPdo('mysql:host=localhost;dbname=testdb'); |
| 633 | + $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
| 634 | + |
| 635 | + $query = $pdo->prepare( |
| 636 | + 'SELECT |
| 637 | + TIMESTAMPDIFF(CENTURY, \'2020-01-01 00:01:40\', \'2020-01-01 00:01:40\')', |
| 638 | + ); |
| 639 | + |
| 640 | + $query->execute(); |
| 641 | + } |
| 642 | + |
533 | 643 | public function testCurDateFunction()
|
534 | 644 | {
|
535 | 645 | $pdo = self::getPdo('mysql:foo');
|
@@ -1221,7 +1331,7 @@ public function testUpdate()
|
1221 | 1331 | $query->execute();
|
1222 | 1332 | $this->assertSame([['type' => 'villain']], $query->fetchAll(\PDO::FETCH_ASSOC));
|
1223 | 1333 | }
|
1224 |
| - |
| 1334 | + |
1225 | 1335 | public function testNegateOperationWithAnd()
|
1226 | 1336 | {
|
1227 | 1337 | // greater than
|
|
0 commit comments