|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Tests\ORM\Query; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Types\Type; |
| 8 | +use Doctrine\DBAL\Types\Types; |
| 9 | +use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; |
| 10 | +use Doctrine\ORM\EntityManagerInterface; |
| 11 | +use Doctrine\ORM\Query\AST\ExpressionWithReturnType; |
| 12 | +use Doctrine\ORM\Query\AST\Functions\FunctionNode; |
| 13 | +use Doctrine\ORM\Query\AST\Node; |
| 14 | +use Doctrine\ORM\Query\AST\TypedExpression; |
| 15 | +use Doctrine\ORM\Query\Parser; |
| 16 | +use Doctrine\ORM\Query\SqlWalker; |
| 17 | +use Doctrine\ORM\Query\TokenType; |
| 18 | +use Doctrine\Tests\Models\CMS\CmsPhonenumber; |
| 19 | +use Doctrine\Tests\OrmTestCase; |
| 20 | +use PHPUnit\Framework\Attributes\IgnoreDeprecations; |
| 21 | + |
| 22 | +class TypedExpressionDeprecationTest extends OrmTestCase |
| 23 | +{ |
| 24 | + use VerifyDeprecations; |
| 25 | + |
| 26 | + private EntityManagerInterface $entityManager; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->entityManager = $this->getTestEntityManager(); |
| 31 | + } |
| 32 | + |
| 33 | + #[IgnoreDeprecations] |
| 34 | + public function testImplementingLegacyTypedExpressionTriggersDeprecation(): void |
| 35 | + { |
| 36 | + $this->entityManager |
| 37 | + ->getConfiguration() |
| 38 | + ->addCustomNumericFunction('LEGACY_TYPED', LegacyTypedFunctionStub::class); |
| 39 | + |
| 40 | + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/12543'); |
| 41 | + |
| 42 | + $this->entityManager |
| 43 | + ->createQuery('SELECT LEGACY_TYPED(p.phonenumber) FROM ' . CmsPhonenumber::class . ' p') |
| 44 | + ->getSQL(); |
| 45 | + } |
| 46 | + |
| 47 | + public function testImplementingExpressionWithReturnTypeDoesNotTriggerDeprecation(): void |
| 48 | + { |
| 49 | + $this->entityManager |
| 50 | + ->getConfiguration() |
| 51 | + ->addCustomNumericFunction('MODERN_TYPED', ModernTypedFunctionStub::class); |
| 52 | + |
| 53 | + $this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/12543'); |
| 54 | + |
| 55 | + $this->entityManager |
| 56 | + ->createQuery('SELECT MODERN_TYPED(p.phonenumber) FROM ' . CmsPhonenumber::class . ' p') |
| 57 | + ->getSQL(); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +final class LegacyTypedFunctionStub extends FunctionNode implements TypedExpression |
| 62 | +{ |
| 63 | + private Node $arithmeticExpression; |
| 64 | + |
| 65 | + public function getSql(SqlWalker $sqlWalker): string |
| 66 | + { |
| 67 | + return 'ABS(' . $sqlWalker->walkSimpleArithmeticExpression($this->arithmeticExpression) . ')'; |
| 68 | + } |
| 69 | + |
| 70 | + public function parse(Parser $parser): void |
| 71 | + { |
| 72 | + $parser->match(TokenType::T_IDENTIFIER); |
| 73 | + $parser->match(TokenType::T_OPEN_PARENTHESIS); |
| 74 | + $this->arithmeticExpression = $parser->SimpleArithmeticExpression(); |
| 75 | + $parser->match(TokenType::T_CLOSE_PARENTHESIS); |
| 76 | + } |
| 77 | + |
| 78 | + public function getReturnType(): Type |
| 79 | + { |
| 80 | + return Type::getType(Types::INTEGER); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +final class ModernTypedFunctionStub extends FunctionNode implements ExpressionWithReturnType, TypedExpression |
| 85 | +{ |
| 86 | + private Node $arithmeticExpression; |
| 87 | + |
| 88 | + public function getSql(SqlWalker $sqlWalker): string |
| 89 | + { |
| 90 | + return 'ABS(' . $sqlWalker->walkSimpleArithmeticExpression($this->arithmeticExpression) . ')'; |
| 91 | + } |
| 92 | + |
| 93 | + public function parse(Parser $parser): void |
| 94 | + { |
| 95 | + $parser->match(TokenType::T_IDENTIFIER); |
| 96 | + $parser->match(TokenType::T_OPEN_PARENTHESIS); |
| 97 | + $this->arithmeticExpression = $parser->SimpleArithmeticExpression(); |
| 98 | + $parser->match(TokenType::T_CLOSE_PARENTHESIS); |
| 99 | + } |
| 100 | + |
| 101 | + public function getReturnTypeName(): string |
| 102 | + { |
| 103 | + return Types::INTEGER; |
| 104 | + } |
| 105 | + |
| 106 | + public function getReturnType(): Type |
| 107 | + { |
| 108 | + return Type::getType($this->getReturnTypeName()); |
| 109 | + } |
| 110 | +} |
0 commit comments