From e4ff7a35a8fd82e6287afda67ac6b1499f705af0 Mon Sep 17 00:00:00 2001 From: Mathew Davies Date: Fri, 9 Jun 2017 10:00:07 +0100 Subject: [PATCH] Write a test case for a custom function override. --- lib/Doctrine/ORM/Query/Parser.php | 2 +- .../ORM/Functional/CustomFunctionsTest.php | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index c8294be36..3a917f059 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -3377,7 +3377,7 @@ class Parser $funcName = strtolower($token['value']); $customFunctionDeclaration = $this->CustomFunctionDeclaration(); - + // Check for custom functions functions first! switch (true) { case $customFunctionDeclaration !== null: diff --git a/tests/Doctrine/Tests/ORM/Functional/CustomFunctionsTest.php b/tests/Doctrine/Tests/ORM/Functional/CustomFunctionsTest.php index 63c3e4ee9..d7231219b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/CustomFunctionsTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/CustomFunctionsTest.php @@ -47,6 +47,24 @@ class CustomFunctionsTest extends OrmFunctionalTestCase $this->assertEquals(1, count($users)); $this->assertSame($user, $users[0]); } + + public function testCustomFunctionOverride() + { + $user = new CmsUser(); + $user->name = 'Bob'; + $user->username = 'Dylan'; + $this->_em->persist($user); + $this->_em->flush(); + + $this->_em->getConfiguration()->addCustomStringFunction('COUNT', 'Doctrine\Tests\ORM\Functional\CustomCount'); + + $query = $this->_em->createQuery('SELECT COUNT(u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u'); + + $users = $query->getResult(); + + $this->assertEquals(1, count($users)); + $this->assertSame($user, $users[0]); + } } class NoOp extends FunctionNode @@ -70,3 +88,23 @@ class NoOp extends FunctionNode } } +class CustomCount extends FunctionNode +{ + /** + * @var PathExpression + */ + private $field; + + public function parse(Parser $parser) + { + $parser->match(Lexer::T_IDENTIFIER); + $parser->match(Lexer::T_OPEN_PARENTHESIS); + $this->field = $parser->StringExpression(); + $parser->match(Lexer::T_CLOSE_PARENTHESIS); + } + + public function getSql(SqlWalker $sqlWalker) + { + return $this->field->dispatch($sqlWalker); + } +}