|
| 1 | +<?php |
| 2 | +namespace GT\Database\Test; |
| 3 | + |
| 4 | +use GT\Database\MissingParameterException; |
| 5 | +use GT\Database\PlaceholderValidator; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class PlaceholderValidatorTest extends TestCase { |
| 9 | + public function testValidateAllowsSqlWithoutPlaceholders():void { |
| 10 | + PlaceholderValidator::validate( |
| 11 | + "select 1", |
| 12 | + [] |
| 13 | + ); |
| 14 | + |
| 15 | + self::assertTrue(true); |
| 16 | + } |
| 17 | + |
| 18 | + public function testValidateAllowsRepeatedNamedPlaceholders():void { |
| 19 | + PlaceholderValidator::validate( |
| 20 | + "select * from test_table where name = :name or :name is null", |
| 21 | + ["name" => "one"] |
| 22 | + ); |
| 23 | + |
| 24 | + self::assertTrue(true); |
| 25 | + } |
| 26 | + |
| 27 | + public function testValidateAllowsSequentialBindingsForNamedPlaceholders():void { |
| 28 | + PlaceholderValidator::validate( |
| 29 | + "select * from test_table where name = :name and created_at > :date", |
| 30 | + ["one", "2026-04-15 00:00:00"] |
| 31 | + ); |
| 32 | + |
| 33 | + self::assertTrue(true); |
| 34 | + } |
| 35 | + |
| 36 | + public function testValidateReportsMissingSequentialNamedBindings():void { |
| 37 | + $this->expectException(MissingParameterException::class); |
| 38 | + $this->expectExceptionMessage( |
| 39 | + "Too few parameters were bound - missing `date`, `status`" |
| 40 | + ); |
| 41 | + |
| 42 | + PlaceholderValidator::validate( |
| 43 | + "select * from test_table where name = :name and created_at > :date and status = :status", |
| 44 | + ["one"] |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + public function testValidateReportsMissingIndexedBindings():void { |
| 49 | + $this->expectException(MissingParameterException::class); |
| 50 | + $this->expectExceptionMessage( |
| 51 | + "Too few parameters were bound - expected 3, received 1" |
| 52 | + ); |
| 53 | + |
| 54 | + PlaceholderValidator::validate( |
| 55 | + "select * from test_table where id = ? and name = ? and created_at > ?", |
| 56 | + [1] |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + public function testValidateAllowsCompleteIndexedBindings():void { |
| 61 | + PlaceholderValidator::validate( |
| 62 | + "select * from test_table where id = ? and name = ? and created_at > ?", |
| 63 | + [1, "one", "2026-04-15 00:00:00"] |
| 64 | + ); |
| 65 | + |
| 66 | + self::assertTrue(true); |
| 67 | + } |
| 68 | + |
| 69 | + public function testValidateReportsMissingAssociativeNamedBindings():void { |
| 70 | + $this->expectException(MissingParameterException::class); |
| 71 | + $this->expectExceptionMessage( |
| 72 | + "Too few parameters were bound - missing `date`, `status`" |
| 73 | + ); |
| 74 | + |
| 75 | + PlaceholderValidator::validate( |
| 76 | + "select * from test_table where name = :name and created_at > :date and status = :status", |
| 77 | + ["name" => "one"] |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public function testValidateIgnoresQuotedStringsCommentsAndCastSyntax():void { |
| 82 | + PlaceholderValidator::validate( |
| 83 | + implode("\n", [ |
| 84 | + "select ':ignored', \":ignoredToo\", `:ignoredThree`, column::text", |
| 85 | + "from test_table", |
| 86 | + "# :ignoredFour", |
| 87 | + "where name = :name -- :ignoredFive", |
| 88 | + "and flag = 1 /* :ignoredSix */", |
| 89 | + ]), |
| 90 | + ["name" => "one"] |
| 91 | + ); |
| 92 | + |
| 93 | + self::assertTrue(true); |
| 94 | + } |
| 95 | + |
| 96 | + public function testValidateIgnoresEscapedQuotedStrings():void { |
| 97 | + PlaceholderValidator::validate( |
| 98 | + "select 'it''s still a string :ignored', \"say \"\"hi :stillIgnored\"\"\" from test_table where name = :name", |
| 99 | + ["name" => "one"] |
| 100 | + ); |
| 101 | + |
| 102 | + self::assertTrue(true); |
| 103 | + } |
| 104 | + |
| 105 | + public function testValidateIgnoresInvalidPlaceholderSyntax():void { |
| 106 | + PlaceholderValidator::validate( |
| 107 | + 'select 1:2 as ratio, :validPlaceholder as ok, ::text as casted, :$nope as nope', |
| 108 | + ["validPlaceholder" => "ok"] |
| 109 | + ); |
| 110 | + |
| 111 | + self::assertTrue(true); |
| 112 | + } |
| 113 | + |
| 114 | + public function testValidateHandlesUnterminatedQuotedString():void { |
| 115 | + PlaceholderValidator::validate( |
| 116 | + "select ':ignored", |
| 117 | + [] |
| 118 | + ); |
| 119 | + |
| 120 | + self::assertTrue(true); |
| 121 | + } |
| 122 | + |
| 123 | + public function testValidateHandlesUnterminatedHashComment():void { |
| 124 | + PlaceholderValidator::validate( |
| 125 | + "select 1 # :ignored", |
| 126 | + [] |
| 127 | + ); |
| 128 | + |
| 129 | + self::assertTrue(true); |
| 130 | + } |
| 131 | + |
| 132 | + public function testValidateHandlesUnterminatedBlockComment():void { |
| 133 | + PlaceholderValidator::validate( |
| 134 | + "select 1 /* :ignored", |
| 135 | + [] |
| 136 | + ); |
| 137 | + |
| 138 | + self::assertTrue(true); |
| 139 | + } |
| 140 | +} |
0 commit comments