Skip to content

Commit dc1c787

Browse files
authored
Eliminate SqlWalker deprecation trigger in Doctrine 3.3.0 (#51)
1 parent d709f18 commit dc1c787

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
],
77
"require": {
88
"php": "^8.1",
9-
"doctrine/orm": "^3.0.0"
9+
"doctrine/orm": "^3.3.0"
1010
},
1111
"require-dev": {
1212
"doctrine/dbal": "^4.0",

phpunit.xml.dist

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
44
colors="true"
55
bootstrap="vendor/autoload.php"
66
beStrictAboutOutputDuringTests="true"
77
beStrictAboutChangesToGlobalState="true"
88
beStrictAboutCoverageMetadata="true"
9+
beStrictAboutTestsThatDoNotTestAnything="true"
910
failOnRisky="true"
11+
failOnIncomplete="true"
12+
failOnDeprecation="true"
13+
failOnNotice="true"
1014
failOnWarning="true"
11-
cacheResultFile="cache/phpunit.result.cache"
15+
16+
displayDetailsOnTestsThatTriggerDeprecations="true"
17+
displayDetailsOnTestsThatTriggerNotices="true"
18+
displayDetailsOnTestsThatTriggerWarnings="true"
19+
displayDetailsOnTestsThatTriggerErrors="true"
20+
21+
cacheDirectory="cache/phpunit"
1222
>
1323
<php>
1424
<ini name="error_reporting" value="-1"/>
25+
<env name="DOCTRINE_DEPRECATIONS" value="trigger"/>
1526
</php>
27+
<source ignoreSuppressionOfDeprecations="true">
28+
<include>
29+
<directory>tests</directory>
30+
</include>
31+
</source>
1632
</phpunit>

src/HintDrivenSqlWalker.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
use Doctrine\ORM\Query\AST\UpdateItem;
5454
use Doctrine\ORM\Query\AST\UpdateStatement;
5555
use Doctrine\ORM\Query\AST\WhereClause;
56+
use Doctrine\ORM\Query\Exec\PreparedExecutorFinalizer;
57+
use Doctrine\ORM\Query\Exec\SingleSelectSqlFinalizer;
58+
use Doctrine\ORM\Query\Exec\SqlFinalizer;
59+
use Doctrine\ORM\Query\OutputWalker;
5660
use Doctrine\ORM\Query\Parser;
5761
use Doctrine\ORM\Query\SqlWalker;
5862
use LogicException;
@@ -61,7 +65,7 @@
6165
/**
6266
* @psalm-import-type QueryComponent from Parser
6367
*/
64-
class HintDrivenSqlWalker extends SqlWalker
68+
class HintDrivenSqlWalker extends SqlWalker implements OutputWalker
6569
{
6670

6771
/**
@@ -91,6 +95,22 @@ public function __construct(
9195
}
9296
}
9397

98+
public function getFinalizer(DeleteStatement|UpdateStatement|SelectStatement $AST): SqlFinalizer
99+
{
100+
switch (true) {
101+
case $AST instanceof SelectStatement:
102+
return new SingleSelectSqlFinalizer($this->walkSelectStatement($AST));
103+
104+
case $AST instanceof UpdateStatement:
105+
return new PreparedExecutorFinalizer($this->createUpdateStatementExecutor($AST));
106+
107+
case $AST instanceof DeleteStatement: // @phpstan-ignore instanceof.alwaysTrue (keep it readable)
108+
return new PreparedExecutorFinalizer($this->createDeleteStatementExecutor($AST));
109+
}
110+
111+
throw new LogicException('Unexpected AST node type');
112+
}
113+
94114
public function walkSelectStatement(SelectStatement $AST): string
95115
{
96116
return $this->callWalkers(SqlNode::SelectStatement, parent::walkSelectStatement($AST));

tests/Handlers/CommentWholeSqlHintHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CommentWholeSqlHintHandler extends HintHandler
1313

1414
public function getNodes(): array
1515
{
16-
return [SqlNode::SelectStatement];
16+
return [SqlNode::SelectStatement, SqlNode::UpdateStatement, SqlNode::DeleteStatement];
1717
}
1818

1919
public function processNode(SqlNode $sqlNode, string $sql): string

tests/HintDrivenSqlWalkerTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ class HintDrivenSqlWalkerTest extends TestCase
2020
{
2121

2222
/**
23-
* @param mixed $hintValue
2423
* @dataProvider walksProvider
2524
*/
2625
public function testWalker(
2726
string $dql,
2827
string $handlerClass,
29-
$hintValue,
28+
mixed $hintValue,
3029
string $expectedSql,
3130
): void
3231
{
@@ -56,12 +55,26 @@ public static function walksProvider(): iterable
5655
'select d0_.id AS id_0 FROM dummy_entity d0_',
5756
];
5857

59-
yield 'Comment whole sql' => [
58+
yield 'Comment whole sql - select' => [
6059
$selectDql,
6160
CommentWholeSqlHintHandler::class,
6261
'custom comment',
6362
'SELECT d0_.id AS id_0 FROM dummy_entity d0_ -- custom comment',
6463
];
64+
65+
yield 'Comment whole sql - update' => [
66+
sprintf('UPDATE %s w SET w.id = 1', DummyEntity::class),
67+
CommentWholeSqlHintHandler::class,
68+
'custom comment',
69+
'UPDATE dummy_entity SET id = 1 -- custom comment',
70+
];
71+
72+
yield 'Comment whole sql - delete' => [
73+
sprintf('DELETE FROM %s w', DummyEntity::class),
74+
CommentWholeSqlHintHandler::class,
75+
'custom comment',
76+
'DELETE FROM dummy_entity -- custom comment',
77+
];
6578
}
6679

6780
private function createEntityManagerMock(): EntityManager

0 commit comments

Comments
 (0)