1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests\Twig; |
4
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Twig\DoctrineExtension; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class DoctrineExtensionTest extends TestCase |
9
|
|
|
{ |
10
|
|
View Code Duplication |
public function testReplaceQueryParametersWithPostgresCasting() : void |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
$extension = new DoctrineExtension(); |
13
|
|
|
$query = 'a=? OR (1)::string OR b=?'; |
14
|
|
|
$parameters = [1, 2]; |
15
|
|
|
|
16
|
|
|
$result = $extension->replaceQueryParameters($query, $parameters); |
17
|
|
|
$this->assertEquals('a=1 OR (1)::string OR b=2', $result); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
View Code Duplication |
public function testReplaceQueryParametersWithStartingIndexAtOne() : void |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$extension = new DoctrineExtension(); |
23
|
|
|
$query = 'a=? OR b=?'; |
24
|
|
|
$parameters = [ |
25
|
|
|
1 => 1, |
26
|
|
|
2 => 2, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
$result = $extension->replaceQueryParameters($query, $parameters); |
30
|
|
|
$this->assertEquals('a=1 OR b=2', $result); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function testReplaceQueryParameters() : void |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$extension = new DoctrineExtension(); |
36
|
|
|
$query = 'a=? OR b=?'; |
37
|
|
|
$parameters = [ |
38
|
|
|
1, |
39
|
|
|
2, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
$result = $extension->replaceQueryParameters($query, $parameters); |
43
|
|
|
$this->assertEquals('a=1 OR b=2', $result); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testReplaceQueryParametersWithNamedIndex() : void |
47
|
|
|
{ |
48
|
|
|
$extension = new DoctrineExtension(); |
49
|
|
|
$query = 'a=:a OR b=:b'; |
50
|
|
|
$parameters = [ |
51
|
|
|
'a' => 1, |
52
|
|
|
'b' => 2, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$result = $extension->replaceQueryParameters($query, $parameters); |
56
|
|
|
$this->assertEquals('a=1 OR b=2', $result); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function testReplaceQueryParametersWithEmptyArray() : void |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$extension = new DoctrineExtension(); |
62
|
|
|
$query = 'IN (?)'; |
63
|
|
|
$parameters = [ |
64
|
|
|
[], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$result = $extension->replaceQueryParameters($query, $parameters); |
68
|
|
|
$this->assertEquals('IN (NULL)', $result); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testEscapeBinaryParameter() : void |
72
|
|
|
{ |
73
|
|
|
$binaryString = pack('H*', '9d40b8c1417f42d099af4782ec4b20b6'); |
74
|
|
|
$this->assertEquals('0x9D40B8C1417F42D099AF4782EC4B20B6', DoctrineExtension::escapeFunction($binaryString)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testEscapeStringParameter() : void |
78
|
|
|
{ |
79
|
|
|
$this->assertEquals("'test string'", DoctrineExtension::escapeFunction('test string')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testEscapeArrayParameter() : void |
83
|
|
|
{ |
84
|
|
|
$this->assertEquals("1, NULL, 'test', foo, NULL", DoctrineExtension::escapeFunction([1, null, 'test', new DummyClass('foo'), []])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testEscapeObjectParameter() : void |
88
|
|
|
{ |
89
|
|
|
$object = new DummyClass('bar'); |
90
|
|
|
$this->assertEquals('bar', DoctrineExtension::escapeFunction($object)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testEscapeNullParameter() : void |
94
|
|
|
{ |
95
|
|
|
$this->assertEquals('NULL', DoctrineExtension::escapeFunction(null)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testEscapeBooleanParameter() : void |
99
|
|
|
{ |
100
|
|
|
$this->assertEquals('1', DoctrineExtension::escapeFunction(true)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
class DummyClass |
105
|
|
|
{ |
106
|
|
|
/** @var string */ |
107
|
|
|
protected $str; |
108
|
|
|
|
109
|
|
|
public function __construct(string $str) |
110
|
|
|
{ |
111
|
|
|
$this->str = $str; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function __toString() : string |
115
|
|
|
{ |
116
|
|
|
return $this->str; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.