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