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 testCustomFunctionWithinInExpression() |
52
|
|
|
{ |
53
|
|
|
$user = new CmsUser(); |
54
|
|
|
$user->name = 'Lo'; |
55
|
|
|
$user->username = 'Doe'; |
56
|
|
|
$this->_em->persist($user); |
57
|
|
|
$this->_em->flush(); |
58
|
|
|
|
59
|
|
|
$this->_em->getConfiguration()->addCustomStringFunction('FOO', function($funcName) { |
60
|
|
|
return new NoOp($funcName); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
$query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' |
64
|
|
|
. ' WHERE u.name IN (FOO(\'Lo\'), \'Lo\', :name)'); |
65
|
|
|
|
66
|
|
|
$query->setParameter('name', 'Lo'); |
67
|
|
|
|
68
|
|
|
$users = $query->getResult(); |
69
|
|
|
$this->assertEquals(1, count($users)); |
70
|
|
|
$this->assertSame($user, $users[0]); |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
class NoOp extends FunctionNode |
76
|
|
|
{ |
77
|
|
|
/** |
78
|
|
|
* @var PathExpression |
79
|
|
|
*/ |
80
|
|
|
private $field; |
81
|
|
|
|
82
|
|
|
public function parse(Parser $parser) |
83
|
|
|
{ |
84
|
|
|
$parser->match(Lexer::T_IDENTIFIER); |
85
|
|
|
$parser->match(Lexer::T_OPEN_PARENTHESIS); |
86
|
|
|
$this->field = $parser->ArithmeticPrimary(); |
87
|
|
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getSql(SqlWalker $sqlWalker) |
91
|
|
|
{ |
92
|
|
|
return $this->field->dispatch($sqlWalker); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|