|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Ray\MediaQuery; |
| 6 | + |
| 7 | +use Aura\Sql\ExtendedPdo; |
| 8 | +use Pagerfanta\View\DefaultView; |
| 9 | +use PDO; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Ray\AuraSqlModule\Pagerfanta\AuraSqlPager; |
| 12 | +use Ray\AuraSqlModule\Pagerfanta\AuraSqlPagerFactory; |
| 13 | +use Ray\Di\Injector; |
| 14 | +use Ray\InputQuery\ToArray; |
| 15 | + |
| 16 | +use function file_get_contents; |
| 17 | + |
| 18 | +class SqlCommentTest extends TestCase |
| 19 | +{ |
| 20 | + private SqlQuery $sqlQuery; |
| 21 | + |
| 22 | + /** @var array<string, mixed> */ |
| 23 | + private array $insertData = ['id' => '1', 'title' => 'test']; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + $sqlDir = __DIR__ . '/sql'; |
| 28 | + $pdo = new ExtendedPdo('sqlite::memory:', '', '', [PDO::ATTR_STRINGIFY_FETCHES => true]); |
| 29 | + $pdo->query((string) file_get_contents($sqlDir . '/create_todo.sql')); |
| 30 | + $pdo->perform((string) file_get_contents($sqlDir . '/todo_add.sql'), $this->insertData); |
| 31 | + |
| 32 | + $this->sqlQuery = new SqlQuery( |
| 33 | + $pdo, |
| 34 | + __DIR__ . '/sql', |
| 35 | + new MediaQueryLogger(), |
| 36 | + new AuraSqlPagerFactory(new AuraSqlPager(new DefaultView(), [])), |
| 37 | + new ParamConverter(new ToArray()), |
| 38 | + new Injector(), |
| 39 | + new PerformTemplatedSql('{{ sql }}'), |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testInsertWithSelectInComment(): void |
| 44 | + { |
| 45 | + // Critical test: -- SELECT comment should not cause INSERT to be detected as SELECT |
| 46 | + $data = ['id' => '2', 'title' => 'test2']; |
| 47 | + $this->sqlQuery->exec('todo_insert_with_select_comment', $data); |
| 48 | + |
| 49 | + // Verify the INSERT worked |
| 50 | + $result = $this->sqlQuery->getRow('todo_item', ['id' => '2']); |
| 51 | + $this->assertSame($data, $result); |
| 52 | + } |
| 53 | + |
| 54 | + public function testSelectWithLeadingDashComment(): void |
| 55 | + { |
| 56 | + $result = $this->sqlQuery->getRow('todo_select_with_leading_comment', ['id' => '1']); |
| 57 | + $this->assertSame($this->insertData, $result); |
| 58 | + } |
| 59 | + |
| 60 | + public function testUpdateWithInsertComment(): void |
| 61 | + { |
| 62 | + $updatedTitle = 'updated'; |
| 63 | + $this->sqlQuery->exec('todo_update_with_insert_comment', ['id' => '1', 'title' => $updatedTitle]); |
| 64 | + |
| 65 | + // Verify the UPDATE worked |
| 66 | + $result = $this->sqlQuery->getRow('todo_item', ['id' => '1']); |
| 67 | + $this->assertSame(['id' => '1', 'title' => $updatedTitle], $result); |
| 68 | + } |
| 69 | + |
| 70 | + public function testDashesInStringLiteral(): void |
| 71 | + { |
| 72 | + // Test that -- inside string literals is not stripped |
| 73 | + // This query should work correctly despite having -- in the WHERE clause |
| 74 | + $result = $this->sqlQuery->getRow('todo_with_dashes_in_string', ['id' => '1']); |
| 75 | + // Since 'test--value' won't match 'test', we expect null |
| 76 | + $this->assertNull($result); |
| 77 | + } |
| 78 | +} |
0 commit comments