|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace staabm\PHPStanDba\Tests; |
| 4 | + |
| 5 | +use PHPStan\Rules\Rule; |
| 6 | +use PHPStan\Testing\RuleTestCase; |
| 7 | +use staabm\PHPStanDba\QueryReflection\MysqliQueryReflector; |
| 8 | +use staabm\PHPStanDba\QueryReflection\PdoMysqlQueryReflector; |
| 9 | +use staabm\PHPStanDba\QueryReflection\PdoPgSqlQueryReflector; |
| 10 | +use staabm\PHPStanDba\Rules\SyntaxErrorInPreparedStatementMethodRule; |
| 11 | + |
| 12 | +/** |
| 13 | + * @extends RuleTestCase<SyntaxErrorInPreparedStatementMethodRule> |
| 14 | + */ |
| 15 | +class SyntaxErrorInPreparedStatementMethodSubclassedRuleTest extends RuleTestCase |
| 16 | +{ |
| 17 | + protected function getRule(): Rule |
| 18 | + { |
| 19 | + return self::getContainer()->getByType(SyntaxErrorInPreparedStatementMethodRule::class); |
| 20 | + } |
| 21 | + |
| 22 | + public static function getAdditionalConfigFiles(): array |
| 23 | + { |
| 24 | + return [ |
| 25 | + __DIR__.'/config/subclassed-method-rule.neon', |
| 26 | + ]; |
| 27 | + } |
| 28 | + |
| 29 | + public function testSyntaxErrorInQueryRule(): void |
| 30 | + { |
| 31 | + if (\PHP_VERSION_ID < 70400) { |
| 32 | + self::markTestSkipped('Test requires PHP 7.4.'); |
| 33 | + } |
| 34 | + |
| 35 | + require_once __DIR__.'/data/syntax-error-in-method-subclassed.php'; |
| 36 | + |
| 37 | + $this->analyse([__DIR__.'/data/syntax-error-in-method-subclassed.php'], $this->getExpectedErrors()); |
| 38 | + } |
| 39 | + |
| 40 | + /** @return list<array{string, int}> */ |
| 41 | + public function getExpectedErrors(): array |
| 42 | + { |
| 43 | + $dbaReflector = getenv('DBA_REFLECTOR'); |
| 44 | + |
| 45 | + switch ($dbaReflector) { |
| 46 | + case MysqliQueryReflector::NAME: |
| 47 | + return [ |
| 48 | + [ |
| 49 | + "Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'with syntax error GROUPY by x LIMIT 0' at line 1 (1064).", |
| 50 | + 12, |
| 51 | + ], |
| 52 | + [ |
| 53 | + "Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).", |
| 54 | + 18, |
| 55 | + ], |
| 56 | + [ |
| 57 | + "Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'FROM ada LIMIT 0' at line 3 (1064).", |
| 58 | + 20, |
| 59 | + ], |
| 60 | + ]; |
| 61 | + case PdoPgSqlQueryReflector::NAME: |
| 62 | + return [ |
| 63 | + [ |
| 64 | + <<<TEXT |
| 65 | + Query error: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "with" |
| 66 | + LINE 1: SELECT with syntax error GROUPY by x LIMIT 0 |
| 67 | + ^ (42601). |
| 68 | + TEXT, |
| 69 | + 12, |
| 70 | + ], |
| 71 | + [ |
| 72 | + <<<TEXT |
| 73 | + Query error: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "freigabe1u1" |
| 74 | + LINE 1: SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada LIMIT... |
| 75 | + ^ (42601). |
| 76 | + TEXT, |
| 77 | + 18, |
| 78 | + ], |
| 79 | + [ |
| 80 | + <<<TEXT |
| 81 | + Query error: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "FROM" |
| 82 | + LINE 3: FROM ada LIMIT 0 |
| 83 | + ^ (42601). |
| 84 | + TEXT, |
| 85 | + 20, |
| 86 | + ], |
| 87 | + ]; |
| 88 | + case PdoMysqlQueryReflector::NAME: |
| 89 | + return [ |
| 90 | + [ |
| 91 | + "Query error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'with syntax error GROUPY by x LIMIT 0' at line 1 (42000).", |
| 92 | + 12, |
| 93 | + ], |
| 94 | + [ |
| 95 | + "Query error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (42000).", |
| 96 | + 18, |
| 97 | + ], |
| 98 | + [ |
| 99 | + "Query error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'FROM ada LIMIT 0' at line 3 (42000).", |
| 100 | + 20, |
| 101 | + ], |
| 102 | + ]; |
| 103 | + default: throw new \RuntimeException(sprintf('Unsupported DBA_REFLECTOR %s', $dbaReflector)); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments