Skip to content

Commit

Permalink
Eliminate SqlWalker deprecation trigger in Doctrine 3.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Oct 18, 2024
1 parent d709f18 commit 3f2a0c1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"require": {
"php": "^8.1",
"doctrine/orm": "^3.0.0"
"doctrine/orm": "^3.3.0"
},
"require-dev": {
"doctrine/dbal": "^4.0",
Expand Down
20 changes: 18 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutTestsThatDoNotTestAnything="true"
failOnRisky="true"
failOnIncomplete="true"
failOnDeprecation="true"
failOnNotice="true"
failOnWarning="true"
cacheResultFile="cache/phpunit.result.cache"

displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerErrors="true"

cacheDirectory="cache/phpunit"
>
<php>
<ini name="error_reporting" value="-1"/>
<env name="DOCTRINE_DEPRECATIONS" value="trigger"/>
</php>
<source ignoreSuppressionOfDeprecations="true">
<include>
<directory>tests</directory>
</include>
</source>
</phpunit>
22 changes: 21 additions & 1 deletion src/HintDrivenSqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
use Doctrine\ORM\Query\AST\UpdateItem;
use Doctrine\ORM\Query\AST\UpdateStatement;
use Doctrine\ORM\Query\AST\WhereClause;
use Doctrine\ORM\Query\Exec\PreparedExecutorFinalizer;
use Doctrine\ORM\Query\Exec\SingleSelectSqlFinalizer;
use Doctrine\ORM\Query\Exec\SqlFinalizer;
use Doctrine\ORM\Query\OutputWalker;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use LogicException;
Expand All @@ -61,7 +65,7 @@
/**
* @psalm-import-type QueryComponent from Parser
*/
class HintDrivenSqlWalker extends SqlWalker
class HintDrivenSqlWalker extends SqlWalker implements OutputWalker
{

/**
Expand Down Expand Up @@ -91,6 +95,22 @@ public function __construct(
}
}

public function getFinalizer(DeleteStatement|UpdateStatement|SelectStatement $AST): SqlFinalizer
{
switch (true) {
case $AST instanceof SelectStatement:
return new SingleSelectSqlFinalizer($this->walkSelectStatement($AST));

case $AST instanceof UpdateStatement:
return new PreparedExecutorFinalizer($this->createUpdateStatementExecutor($AST));

case $AST instanceof DeleteStatement: // @phpstan-ignore instanceof.alwaysTrue (keep it readable)
return new PreparedExecutorFinalizer($this->createDeleteStatementExecutor($AST));
}

throw new LogicException('Unexpected AST node type');
}

public function walkSelectStatement(SelectStatement $AST): string
{
return $this->callWalkers(SqlNode::SelectStatement, parent::walkSelectStatement($AST));
Expand Down
2 changes: 1 addition & 1 deletion tests/Handlers/CommentWholeSqlHintHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CommentWholeSqlHintHandler extends HintHandler

public function getNodes(): array
{
return [SqlNode::SelectStatement];
return [SqlNode::SelectStatement, SqlNode::UpdateStatement, SqlNode::DeleteStatement];
}

public function processNode(SqlNode $sqlNode, string $sql): string
Expand Down
19 changes: 16 additions & 3 deletions tests/HintDrivenSqlWalkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ class HintDrivenSqlWalkerTest extends TestCase
{

/**
* @param mixed $hintValue
* @dataProvider walksProvider
*/
public function testWalker(
string $dql,
string $handlerClass,
$hintValue,
mixed $hintValue,
string $expectedSql,
): void
{
Expand Down Expand Up @@ -56,12 +55,26 @@ public static function walksProvider(): iterable
'select d0_.id AS id_0 FROM dummy_entity d0_',
];

yield 'Comment whole sql' => [
yield 'Comment whole sql - select' => [
$selectDql,
CommentWholeSqlHintHandler::class,
'custom comment',
'SELECT d0_.id AS id_0 FROM dummy_entity d0_ -- custom comment',
];

yield 'Comment whole sql - update' => [
sprintf('UPDATE %s w SET w.id = 1', DummyEntity::class),
CommentWholeSqlHintHandler::class,
'custom comment',
'UPDATE dummy_entity SET id = 1 -- custom comment',
];

yield 'Comment whole sql - delete' => [
sprintf('DELETE FROM %s w', DummyEntity::class),
CommentWholeSqlHintHandler::class,
'custom comment',
'DELETE FROM dummy_entity -- custom comment',
];
}

private function createEntityManagerMock(): EntityManager
Expand Down

0 comments on commit 3f2a0c1

Please sign in to comment.