Skip to content

Commit d33be6b

Browse files
p4veIPavel Karfik
andauthored
Merge separate PDO and Mysqli rule tests (#410)
Co-authored-by: Pavel Karfik <[email protected]>
1 parent 5533f24 commit d33be6b

12 files changed

+308
-402
lines changed

src/QueryReflection/MysqliQueryReflector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ final class MysqliQueryReflector implements QueryReflector, RecordingReflector
2323

2424
public const MYSQL_HOST_NOT_FOUND = 2002;
2525

26+
public const NAME = 'mysqli';
27+
2628
private const MYSQL_ERROR_CODES = [
2729
self::MYSQL_SYNTAX_ERROR_CODE,
2830
self::MYSQL_UNKNOWN_COLUMN_IN_FIELDLIST,

src/QueryReflection/PdoMysqlQueryReflector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
class PdoMysqlQueryReflector extends BasePdoQueryReflector
2121
{
22+
public const NAME = 'pdo-mysql';
23+
2224
public function __construct(PDO $pdo)
2325
{
2426
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

src/QueryReflection/PdoPgSqlQueryReflector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
final class PdoPgSqlQueryReflector extends BasePdoQueryReflector
1919
{
20+
public const NAME = 'pdo-pgsql';
21+
2022
public function __construct(PDO $pdo)
2123
{
2224
$typeMapper = new PgsqlTypeMapper();

tests/rules/SyntaxErrorInPreparedStatementMethodSubclassedRuleMysqliReflectorTest.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

tests/rules/SyntaxErrorInPreparedStatementMethodSubclassedRulePdoReflectorTest.php

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}

tests/rules/SyntaxErrorInQueryFunctionRuleMysqliReflectorTest.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)