|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query; |
|
6
|
|
|
use Doctrine\ORM\Query\AST\Functions\FunctionNode; |
|
7
|
|
|
use Doctrine\ORM\Query\AST\PathExpression; |
|
8
|
|
|
use Doctrine\ORM\Query\Lexer; |
|
9
|
|
|
use Doctrine\ORM\Query\Parser; |
|
10
|
|
|
use Doctrine\ORM\Query\SqlWalker; |
|
11
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
|
12
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
|
13
|
|
|
|
|
14
|
|
|
require_once __DIR__ . '/../../TestInit.php'; |
|
15
|
|
|
|
|
16
|
|
|
class CustomFunctionsTest extends OrmFunctionalTestCase |
|
17
|
|
|
{ |
|
18
|
|
|
protected function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->useModelSet('cms'); |
|
21
|
|
|
|
|
22
|
|
|
parent::setUp(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testCustomFunctionDefinedWithCallback() |
|
26
|
|
|
{ |
|
27
|
|
|
$user = new CmsUser(); |
|
28
|
|
|
$user->name = 'Bob'; |
|
29
|
|
|
$user->username = 'Dylan'; |
|
30
|
|
|
$this->_em->persist($user); |
|
31
|
|
|
$this->_em->flush(); |
|
32
|
|
|
|
|
33
|
|
|
// Instead of defining the function with the class name, we use a callback |
|
34
|
|
|
$this->_em->getConfiguration()->addCustomStringFunction('FOO', function($funcName) { |
|
35
|
|
|
return new NoOp($funcName); |
|
36
|
|
|
}); |
|
37
|
|
|
$this->_em->getConfiguration()->addCustomNumericFunction('BAR', function($funcName) { |
|
38
|
|
|
return new NoOp($funcName); |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
$query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' |
|
42
|
|
|
. ' WHERE FOO(u.name) = \'Bob\'' |
|
43
|
|
|
. ' AND BAR(1) = 1'); |
|
44
|
|
|
|
|
45
|
|
|
$users = $query->getResult(); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals(1, count($users)); |
|
48
|
|
|
$this->assertSame($user, $users[0]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testCustomFunctionOverride() |
|
52
|
|
|
{ |
|
53
|
|
|
$user = new CmsUser(); |
|
54
|
|
|
$user->name = 'Bob'; |
|
55
|
|
|
$user->username = 'Dylan'; |
|
56
|
|
|
$this->_em->persist($user); |
|
57
|
|
|
$this->_em->flush(); |
|
58
|
|
|
|
|
59
|
|
|
$this->_em->getConfiguration()->addCustomStringFunction('COUNT', 'Doctrine\Tests\ORM\Functional\CustomCount'); |
|
60
|
|
|
|
|
61
|
|
|
$query = $this->_em->createQuery('SELECT COUNT(DISTINCT u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u'); |
|
62
|
|
|
|
|
63
|
|
|
$usersCount = $query->getSingleScalarResult(); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertEquals(1, $usersCount); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
class NoOp extends FunctionNode |
|
70
|
|
|
{ |
|
71
|
|
|
/** |
|
72
|
|
|
* @var PathExpression |
|
73
|
|
|
*/ |
|
74
|
|
|
private $field; |
|
75
|
|
|
|
|
76
|
|
|
public function parse(Parser $parser) |
|
77
|
|
|
{ |
|
78
|
|
|
$parser->match(Lexer::T_IDENTIFIER); |
|
79
|
|
|
$parser->match(Lexer::T_OPEN_PARENTHESIS); |
|
80
|
|
|
$this->field = $parser->ArithmeticPrimary(); |
|
81
|
|
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getSql(SqlWalker $sqlWalker) |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->field->dispatch($sqlWalker); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
class CustomCount extends FunctionNode |
|
91
|
|
|
{ |
|
92
|
|
|
/** |
|
93
|
|
|
* @var Query\AST\AggregateExpression |
|
94
|
|
|
*/ |
|
95
|
|
|
private $aggregateExpression; |
|
96
|
|
|
|
|
97
|
|
|
public function parse(Parser $parser): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->aggregateExpression = $parser->AggregateExpression(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getSql(SqlWalker $sqlWalker): string |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->aggregateExpression->dispatch($sqlWalker); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|