1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\DBAL\Functional\Schema; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Schema\Table; |
8
|
|
|
use Doctrine\Tests\DbalFunctionalTestCase; |
9
|
|
|
use function sprintf; |
10
|
|
|
|
11
|
|
|
class DefaultValueTest extends DbalFunctionalTestCase |
12
|
|
|
{ |
13
|
|
|
/** @var bool */ |
14
|
|
|
private static $initialized = false; |
15
|
|
|
|
16
|
|
|
protected function setUp() : void |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
if (self::$initialized) { |
21
|
|
|
return; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
self::$initialized = true; |
25
|
|
|
|
26
|
|
|
$table = new Table('default_value'); |
27
|
|
|
$table->addColumn('id', 'integer'); |
28
|
|
|
|
29
|
|
|
foreach (self::columnProvider() as [$name, $default]) { |
30
|
|
|
$table->addColumn($name, 'string', [ |
31
|
|
|
'default' => $default, |
32
|
|
|
'notnull' => false, |
33
|
|
|
]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->connection->getSchemaManager() |
37
|
|
|
->dropAndCreateTable($table); |
38
|
|
|
|
39
|
|
|
$this->connection->insert('default_value', ['id' => 1]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @dataProvider columnProvider |
44
|
|
|
*/ |
45
|
|
|
public function testEscapedDefaultValueCanBeIntrospected(string $name, $expectedDefault) : void |
46
|
|
|
{ |
47
|
|
|
self::assertSame( |
48
|
|
|
$expectedDefault, |
49
|
|
|
$this->connection |
50
|
|
|
->getSchemaManager() |
51
|
|
|
->listTableDetails('default_value') |
52
|
|
|
->getColumn($name) |
53
|
|
|
->getDefault() |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @dataProvider columnProvider |
59
|
|
|
*/ |
60
|
|
|
public function testEscapedDefaultValueCanBeInserted(string $name, $expectedDefault) : void |
61
|
|
|
{ |
62
|
|
|
$value = $this->connection->fetchColumn( |
63
|
|
|
sprintf('SELECT %s FROM default_value', $name) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
self::assertSame($expectedDefault, $value); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns potential escaped literals from all platforms combined. |
71
|
|
|
* |
72
|
|
|
* @see https://dev.mysql.com/doc/refman/5.7/en/string-literals.html |
73
|
|
|
* @see http://www.sqlite.org/lang_expr.html |
74
|
|
|
* @see https://www.postgresql.org/docs/9.6/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE |
75
|
|
|
* |
76
|
|
|
* @return mixed[][] |
77
|
|
|
*/ |
78
|
|
|
public static function columnProvider() : iterable |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
'Single quote' => [ |
82
|
|
|
'single_quote', |
83
|
|
|
"foo'bar", |
84
|
|
|
], |
85
|
|
|
'Single quote, doubled' => [ |
86
|
|
|
'single_quote_doubled', |
87
|
|
|
"foo''bar", |
88
|
|
|
], |
89
|
|
|
'Double quote' => [ |
90
|
|
|
'double_quote', |
91
|
|
|
'foo"bar', |
92
|
|
|
], |
93
|
|
|
'Double quote, doubled' => [ |
94
|
|
|
'double_quote_doubled', |
95
|
|
|
'foo""bar', |
96
|
|
|
], |
97
|
|
|
'Backspace' => [ |
98
|
|
|
'backspace', |
99
|
|
|
"foo\x08bar", |
100
|
|
|
], |
101
|
|
|
'New line' => [ |
102
|
|
|
'new_line', |
103
|
|
|
"foo\nbar", |
104
|
|
|
], |
105
|
|
|
'Carriage return' => [ |
106
|
|
|
'carriage_return', |
107
|
|
|
"foo\rbar", |
108
|
|
|
], |
109
|
|
|
'Tab' => [ |
110
|
|
|
'tab', |
111
|
|
|
"foo\tbar", |
112
|
|
|
], |
113
|
|
|
'Substitute' => [ |
114
|
|
|
'substitute', |
115
|
|
|
"foo\x1abar", |
116
|
|
|
], |
117
|
|
|
'Backslash' => [ |
118
|
|
|
'backslash', |
119
|
|
|
'foo\\bar', |
120
|
|
|
], |
121
|
|
|
'Backslash, doubled' => [ |
122
|
|
|
'backslash_doubled', |
123
|
|
|
'foo\\\\bar', |
124
|
|
|
], |
125
|
|
|
'Percent' => [ |
126
|
|
|
'percent_sign', |
127
|
|
|
'foo%bar', |
128
|
|
|
], |
129
|
|
|
'Underscore' => [ |
130
|
|
|
'underscore', |
131
|
|
|
'foo_bar', |
132
|
|
|
], |
133
|
|
|
'NULL string' => [ |
134
|
|
|
'null_string', |
135
|
|
|
'NULL', |
136
|
|
|
], |
137
|
|
|
'NULL value' => [ |
138
|
|
|
'null_value', |
139
|
|
|
null, |
140
|
|
|
], |
141
|
|
|
'SQL expression' => [ |
142
|
|
|
'sql_expression', |
143
|
|
|
"'; DROP DATABASE doctrine --", |
144
|
|
|
], |
145
|
|
|
'No double conversion' => [ |
146
|
|
|
'no_double_conversion', |
147
|
|
|
"\\'", |
148
|
|
|
], |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|