1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\Morphism\Parse; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Graze\Morphism\Test\Parse\TestCase; |
7
|
|
|
use LogicException; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
class CollationInfoTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testConstructor() |
13
|
|
|
{ |
14
|
|
|
$collation = new CollationInfo(); |
15
|
|
|
$this->assertThat($collation, $this->isInstanceOf(__NAMESPACE__ . '\CollationInfo')); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @dataProvider providerConstructorWithArgs |
20
|
|
|
* @param string|null $charset |
21
|
|
|
* @param string|null $collation |
22
|
|
|
* @param string $expectedCharset |
23
|
|
|
* @param string $expectedCollation |
24
|
|
|
*/ |
25
|
|
|
public function testConstructorWithArgs($charset, $collation, $expectedCharset, $expectedCollation) |
26
|
|
|
{ |
27
|
|
|
$collation = new CollationInfo($charset, $collation); |
28
|
|
|
$this->assertSame($expectedCharset, $collation->getCharset()); |
29
|
|
|
$this->assertSame($expectedCollation, $collation->getCollation()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function providerConstructorWithArgs() |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
['utf8', null, 'utf8', 'utf8_general_ci'], |
39
|
|
|
['UTF8', null, 'utf8', 'utf8_general_ci'], |
40
|
|
|
['utf8', 'utf8_general_ci', 'utf8', 'utf8_general_ci'], |
41
|
|
|
[null, 'utf8_general_ci', 'utf8', 'utf8_general_ci'], |
42
|
|
|
[null, 'UTF8_GENERAL_CI', 'utf8', 'utf8_general_ci'], |
43
|
|
|
|
44
|
|
|
['utf8', 'utf8_unicode_ci', 'utf8', 'utf8_unicode_ci'], |
45
|
|
|
[null, 'utf8_unicode_ci', 'utf8', 'utf8_unicode_ci'], |
46
|
|
|
|
47
|
|
|
['latin1', null, 'latin1', 'latin1_swedish_ci'], |
48
|
|
|
['latin1', 'latin1_swedish_ci', 'latin1', 'latin1_swedish_ci'], |
49
|
|
|
[null, 'latin1_swedish_ci', 'latin1', 'latin1_swedish_ci'], |
50
|
|
|
[null, 'latin1_general_ci', 'latin1', 'latin1_general_ci'], |
51
|
|
|
|
52
|
|
|
['binary', null, 'binary', 'binary'], |
53
|
|
|
['binary', 'binary', 'binary', 'binary'], |
54
|
|
|
[null, 'binary', 'binary', 'binary'], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $charset |
60
|
|
|
* @param string $collation |
61
|
|
|
* @dataProvider providerConstructorWithBadArgs |
62
|
|
|
* @expectedException RuntimeException |
63
|
|
|
*/ |
64
|
|
|
public function testConstructorWithBadArgs($charset, $collation) |
65
|
|
|
{ |
66
|
|
|
new CollationInfo($charset, $collation); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function providerConstructorWithBadArgs() |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
// Invalid character set |
76
|
|
|
['foo', 'utf8_general_ci'], |
77
|
|
|
// Invalid collation |
78
|
|
|
['utf8', 'foo'], |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testIsSpecified() |
83
|
|
|
{ |
84
|
|
|
$this->assertFalse((new CollationInfo)->isSpecified()); |
85
|
|
|
$this->assertTrue((new CollationInfo('utf8'))->isSpecified()); |
86
|
|
|
$this->assertTrue((new CollationInfo(null, 'utf8_general_ci'))->isSpecified()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testSetBinaryCollation() |
90
|
|
|
{ |
91
|
|
|
$collation = new CollationInfo(); |
92
|
|
|
$collation->setBinaryCollation(); |
93
|
|
|
$collation->setCharset('utf8'); |
94
|
|
|
$this->assertSame('utf8_bin', $collation->getCollation()); |
95
|
|
|
|
96
|
|
|
$collation = new CollationInfo(); |
97
|
|
|
$collation->setCharset('utf8'); |
98
|
|
|
$collation->setBinaryCollation(); |
99
|
|
|
$this->assertSame('utf8_bin', $collation->getCollation()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testIsBinaryCharset() |
103
|
|
|
{ |
104
|
|
|
$this->assertTrue((new CollationInfo('binary'))->isBinaryCharset()); |
105
|
|
|
$this->assertFalse((new CollationInfo('utf8'))->isBinaryCharset()); |
106
|
|
|
$this->assertFalse((new CollationInfo('utf8', 'utf8_bin'))->isBinaryCharset()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** @expectedException LogicException */ |
110
|
|
|
public function testInvalidIsBinaryCharset() |
111
|
|
|
{ |
112
|
|
|
(new CollationInfo())->isBinaryCharset(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testIsDefaultCollation() |
116
|
|
|
{ |
117
|
|
|
$this->assertTrue((new CollationInfo('utf8'))->isDefaultCollation()); |
118
|
|
|
$this->assertTrue((new CollationInfo('utf8', 'utf8_general_ci'))->isDefaultCollation()); |
119
|
|
|
$this->assertTrue((new CollationInfo('latin1'))->isDefaultCollation()); |
120
|
|
|
$this->assertTrue((new CollationInfo('binary'))->isDefaultCollation()); |
121
|
|
|
|
122
|
|
|
$this->assertFalse((new CollationInfo('utf8', 'utf8_unicode_ci'))->isDefaultCollation()); |
123
|
|
|
$this->assertFalse((new CollationInfo('latin1', 'latin1_general_ci'))->isDefaultCollation()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** @expectedException LogicException */ |
127
|
|
|
public function testInvalidSiDefaulfCollation() |
128
|
|
|
{ |
129
|
|
|
(new CollationInfo())->isDefaultCollation(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testSetCharset() |
133
|
|
|
{ |
134
|
|
|
$collation = new CollationInfo(); |
135
|
|
|
$collation->setCharset('utf8'); |
136
|
|
|
$this->assertSame('utf8', $collation->getCharset()); |
137
|
|
|
|
138
|
|
|
$collation = new CollationInfo('utf8', 'utf8_bin'); |
139
|
|
|
$collation->setCharset('utf8'); |
140
|
|
|
$this->assertSame('utf8', $collation->getCharset()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** @expectedException Exception */ |
144
|
|
|
public function testSetCharsetFail() |
145
|
|
|
{ |
146
|
|
|
(new CollationInfo('latin1'))->setCharset('utf8'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** @expectedException LogicException */ |
150
|
|
|
public function testEmptyCharsetFail() |
151
|
|
|
{ |
152
|
|
|
(new CollationInfo())->getCharset(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testSetCollation() |
156
|
|
|
{ |
157
|
|
|
$collation = new CollationInfo(); |
158
|
|
|
$collation->setCollation('utf8_unicode_ci'); |
159
|
|
|
$this->assertSame('utf8', $collation->getCharset()); |
160
|
|
|
$this->assertSame('utf8_unicode_ci', $collation->getCollation()); |
161
|
|
|
|
162
|
|
|
$collation = new CollationInfo('utf8', 'utf8_unicode_ci'); |
163
|
|
|
$collation->setCollation('utf8_general_ci'); |
164
|
|
|
$this->assertSame('utf8', $collation->getCharset()); |
165
|
|
|
$this->assertSame('utf8_general_ci', $collation->getCollation()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** @expectedException Exception */ |
169
|
|
|
public function testSetCollationFail() |
170
|
|
|
{ |
171
|
|
|
(new CollationInfo('latin1'))->setCollation('utf8_general_ci'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testGetCollation() |
175
|
|
|
{ |
176
|
|
|
$collation = new CollationInfo(null, 'utf8_unicode_ci'); |
177
|
|
|
$this->assertSame('utf8_unicode_ci', $collation->getCollation()); |
178
|
|
|
|
179
|
|
|
$collation->setBinaryCollation(); |
180
|
|
|
$this->assertSame('utf8_bin', $collation->getCollation()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** @expectedException LogicException */ |
184
|
|
|
public function testEmptyCollationFail() |
185
|
|
|
{ |
186
|
|
|
(new CollationInfo())->getCollation(); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|