forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix doctrine#7286: StringPrimary no longer accepts aggregate function…
…s as argument
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
tests/Doctrine/Tests/ORM/Functional/Ticket/GH7286Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\AST\Node; | ||
use Doctrine\ORM\Query\Lexer; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
final class GH7286Test extends OrmFunctionalTestCase | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function setUp() : void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->setUpEntitySchema( | ||
[ | ||
GH7286Entity::class, | ||
] | ||
); | ||
|
||
$entityA = new GH7286Entity('foo', 1); | ||
$entityB = new GH7286Entity('foo', 2); | ||
$entityC = new GH7286Entity('bar', 3); | ||
|
||
$this->_em->persist($entityA); | ||
$this->_em->persist($entityB); | ||
$this->_em->persist($entityC); | ||
$this->_em->flush(); | ||
$this->_em->clear(); | ||
} | ||
|
||
public function testAggregateExpressionInFunction() : void | ||
{ | ||
$query = $this->_em->createQuery( | ||
'SELECT CONCAT(e.type, MIN(e.version)) pair' | ||
. ' FROM ' . GH7286Entity::class . ' e' | ||
. ' GROUP BY e.type' | ||
. ' ORDER BY e.type' | ||
); | ||
|
||
self::assertSame( | ||
[ | ||
['pair' => 'bar3'], | ||
['pair' => 'foo1'], | ||
], | ||
$query->getArrayResult() | ||
); | ||
} | ||
|
||
/** | ||
* @group DDC-1091 | ||
*/ | ||
public function testAggregateFunctionInCustomFunction() | ||
{ | ||
$this->_em->getConfiguration()->addCustomStringFunction('CC', GH7286CustomConcat::class); | ||
|
||
$entityA = new GH7286Entity('foo', 1); | ||
$this->_em->persist($entityA); | ||
$this->_em->flush(); | ||
$this->_em->clear(); | ||
|
||
$query = $this->_em->createQuery( | ||
'SELECT CC(e.type, MIN(e.version)) pair' | ||
. ' FROM ' . GH7286Entity::class . ' e' | ||
. ' WHERE e.type = :type' | ||
); | ||
$query->setParameter('type', 'foo'); | ||
|
||
self::assertSame( | ||
['pair' => 'foo1'], | ||
$query->getSingleResult() | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @Entity | ||
*/ | ||
class GH7286Entity | ||
{ | ||
/** | ||
* @Id | ||
* @Column(type="integer") | ||
* @GeneratedValue | ||
* @var int | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @Column | ||
* @var string | ||
*/ | ||
public $type; | ||
|
||
/** | ||
* @Column(type="integer") | ||
* @var int | ||
*/ | ||
public $version; | ||
|
||
public function __construct(string $type, int $version) | ||
{ | ||
$this->type = $type; | ||
$this->version = $version; | ||
} | ||
} | ||
|
||
class GH7286CustomConcat extends FunctionNode | ||
{ | ||
/** @var Node */ | ||
private $first; | ||
|
||
/** @var Node */ | ||
private $second; | ||
|
||
public function parse(Parser $parser): void | ||
{ | ||
$parser->match(Lexer::T_IDENTIFIER); | ||
$parser->match(Lexer::T_OPEN_PARENTHESIS); | ||
|
||
$this->first = $parser->StringPrimary(); | ||
$parser->match(Lexer::T_COMMA); | ||
$this->second = $parser->StringPrimary(); | ||
|
||
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
public function getSql(SqlWalker $walker): string | ||
{ | ||
return $walker->getConnection()->getDatabasePlatform()->getConcatExpression( | ||
$this->first->dispatch($walker), | ||
$this->second->dispatch($walker) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters