1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Happyr Doctrine Specification package. |
5
|
|
|
* |
6
|
|
|
* (c) Tobias Nyholm <[email protected]> |
7
|
|
|
* Kacper Gunia <[email protected]> |
8
|
|
|
* Peter Gribanov <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace tests\Happyr\DoctrineSpecification\Operand; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\ORM\Configuration; |
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
19
|
|
|
use Doctrine\ORM\QueryBuilder; |
20
|
|
|
use Happyr\DoctrineSpecification\Exception\InvalidArgumentException; |
21
|
|
|
use Happyr\DoctrineSpecification\Operand\Field; |
22
|
|
|
use Happyr\DoctrineSpecification\Operand\Operand; |
23
|
|
|
use Happyr\DoctrineSpecification\Operand\PlatformFunction; |
24
|
|
|
use Happyr\DoctrineSpecification\Operand\Value; |
25
|
|
|
use PhpSpec\ObjectBehavior; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @mixin PlatformFunction |
29
|
|
|
*/ |
30
|
|
|
class PlatformFunctionSpec extends ObjectBehavior |
31
|
|
|
{ |
32
|
|
|
private $functionName = 'UPPER'; |
33
|
|
|
|
34
|
|
|
private $arguments = 'foo'; |
35
|
|
|
|
36
|
|
|
public function let() |
37
|
|
|
{ |
38
|
|
|
$this->beConstructedWith($this->functionName, $this->arguments); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function it_is_a_platform_function() |
42
|
|
|
{ |
43
|
|
|
$this->shouldBeAnInstanceOf(PlatformFunction::class); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function it_is_a_operand() |
47
|
|
|
{ |
48
|
|
|
$this->shouldBeAnInstanceOf(Operand::class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function it_is_transformable_doctrine_function(QueryBuilder $qb) |
52
|
|
|
{ |
53
|
|
|
$dqlAlias = 'a'; |
54
|
|
|
$expression = 'UPPER(a.foo)'; |
55
|
|
|
|
56
|
|
|
$this->transform($qb, $dqlAlias)->shouldReturn($expression); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function it_is_transformable_many_arguments(QueryBuilder $qb) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$dqlAlias = 'a'; |
62
|
|
|
$expression = 'concat(a.foo, a.bar)'; |
63
|
|
|
|
64
|
|
|
$this->beConstructedWith('concat', new Field('foo'), new Field('bar')); |
65
|
|
|
|
66
|
|
|
$this->transform($qb, $dqlAlias)->shouldReturn($expression); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function it_is_transformable_many_arguments_as_array(QueryBuilder $qb) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$dqlAlias = 'a'; |
72
|
|
|
$expression = 'concat(a.foo, a.bar)'; |
73
|
|
|
|
74
|
|
|
$this->beConstructedWith('concat', [new Field('foo'), new Field('bar')]); |
75
|
|
|
|
76
|
|
|
$this->transform($qb, $dqlAlias)->shouldReturn($expression); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function it_is_transformable_custom_string_function( |
|
|
|
|
80
|
|
|
QueryBuilder $qb, |
81
|
|
|
EntityManagerInterface $em, |
82
|
|
|
Configuration $configuration |
83
|
|
|
) { |
84
|
|
|
$dqlAlias = 'a'; |
85
|
|
|
$functionName = 'foo'; |
86
|
|
|
$expression = 'foo(a.foo)'; |
87
|
|
|
|
88
|
|
|
$qb->getEntityManager()->willReturn($em); |
89
|
|
|
$em->getConfiguration()->willReturn($configuration); |
90
|
|
|
$configuration->getCustomStringFunction($functionName)->willReturn('ToStringClass'); |
91
|
|
|
$configuration->getCustomNumericFunction($functionName)->willReturn(null); |
92
|
|
|
$configuration->getCustomDatetimeFunction($functionName)->willReturn(null); |
93
|
|
|
|
94
|
|
|
$this->beConstructedWith($functionName, 'foo'); |
95
|
|
|
|
96
|
|
|
$this->transform($qb, $dqlAlias)->shouldReturn($expression); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function it_is_transformable_custom_numeric_function( |
|
|
|
|
100
|
|
|
QueryBuilder $qb, |
101
|
|
|
EntityManagerInterface $em, |
102
|
|
|
Configuration $configuration |
103
|
|
|
) { |
104
|
|
|
$dqlAlias = 'a'; |
105
|
|
|
$functionName = 'foo'; |
106
|
|
|
$expression = 'foo(a.foo)'; |
107
|
|
|
|
108
|
|
|
$qb->getEntityManager()->willReturn($em); |
109
|
|
|
$em->getConfiguration()->willReturn($configuration); |
110
|
|
|
$configuration->getCustomStringFunction($functionName)->willReturn(null); |
111
|
|
|
$configuration->getCustomNumericFunction($functionName)->willReturn('ToNumericClass'); |
112
|
|
|
$configuration->getCustomDatetimeFunction($functionName)->willReturn(null); |
113
|
|
|
|
114
|
|
|
$this->beConstructedWith($functionName, 'foo'); |
115
|
|
|
|
116
|
|
|
$this->transform($qb, $dqlAlias)->shouldReturn($expression); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
public function it_is_transformable_custom_datetime_function( |
|
|
|
|
120
|
|
|
QueryBuilder $qb, |
121
|
|
|
EntityManagerInterface $em, |
122
|
|
|
Configuration $configuration |
123
|
|
|
) { |
124
|
|
|
$dqlAlias = 'a'; |
125
|
|
|
$functionName = 'foo'; |
126
|
|
|
$expression = 'foo(a.foo)'; |
127
|
|
|
|
128
|
|
|
$qb->getEntityManager()->willReturn($em); |
129
|
|
|
$em->getConfiguration()->willReturn($configuration); |
130
|
|
|
$configuration->getCustomStringFunction($functionName)->willReturn(null); |
131
|
|
|
$configuration->getCustomNumericFunction($functionName)->willReturn(null); |
132
|
|
|
$configuration->getCustomDatetimeFunction($functionName)->willReturn('ToDatetimeClass'); |
133
|
|
|
|
134
|
|
|
$this->beConstructedWith($functionName, 'foo'); |
135
|
|
|
|
136
|
|
|
$this->transform($qb, $dqlAlias)->shouldReturn($expression); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function it_is_transformable_undefined_function( |
140
|
|
|
QueryBuilder $qb, |
141
|
|
|
EntityManagerInterface $em, |
142
|
|
|
Configuration $configuration |
143
|
|
|
) { |
144
|
|
|
$functionName = 'foo'; |
145
|
|
|
|
146
|
|
|
$qb->getEntityManager()->willReturn($em); |
147
|
|
|
$em->getConfiguration()->willReturn($configuration); |
148
|
|
|
$configuration->getCustomStringFunction($functionName)->willReturn(null); |
149
|
|
|
$configuration->getCustomNumericFunction($functionName)->willReturn(null); |
150
|
|
|
$configuration->getCustomDatetimeFunction($functionName)->willReturn(null); |
151
|
|
|
|
152
|
|
|
$this->beConstructedWith($functionName, 'foo'); |
153
|
|
|
$this->shouldThrow(InvalidArgumentException::class)->during('transform', [$qb, 'a']); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function it_is_transformable_convertible( |
157
|
|
|
QueryBuilder $qb, |
158
|
|
|
EntityManagerInterface $em, |
159
|
|
|
Configuration $configuration, |
160
|
|
|
ArrayCollection $parameters, |
161
|
|
|
Value $value |
162
|
|
|
) { |
163
|
|
|
$dqlAlias = 'a'; |
164
|
|
|
$functionName = 'concat'; |
165
|
|
|
$expression = 'concat(a.foo, :comparison_10, :comparison_11)'; |
166
|
|
|
|
167
|
|
|
$parameters->count()->willReturn(10); |
168
|
|
|
|
169
|
|
|
$qb->getEntityManager()->willReturn($em); |
170
|
|
|
$qb->getParameters()->willReturn($parameters); |
171
|
|
|
$qb->setParameter('comparison_10', 'bar', null)->shouldBeCalled(); |
172
|
|
|
|
173
|
|
|
$em->getConfiguration()->willReturn($configuration); |
174
|
|
|
|
175
|
|
|
$configuration->getCustomStringFunction($functionName)->willReturn('ToStringClass'); |
176
|
|
|
$configuration->getCustomNumericFunction($functionName)->willReturn(null); |
177
|
|
|
$configuration->getCustomDatetimeFunction($functionName)->willReturn(null); |
178
|
|
|
|
179
|
|
|
$value->transform($qb, $dqlAlias)->willReturn(':comparison_11'); |
180
|
|
|
|
181
|
|
|
$this->beConstructedWith($functionName, ['foo', 'bar', $value]); |
182
|
|
|
|
183
|
|
|
$this->transform($qb, $dqlAlias)->shouldReturn($expression); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.