-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathDsnTest.php
232 lines (171 loc) · 6.5 KB
/
DsnTest.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace Enqueue\Dsn\Tests;
use Enqueue\Dsn\Dsn;
use Enqueue\Dsn\InvalidQueryParameterTypeException;
use PHPUnit\Framework\TestCase;
class DsnTest extends TestCase
{
public function testCouldBeConstructedWithDsnAsFirstArgument()
{
new Dsn('foo://localhost:1234');
}
public function testThrowsIfSchemePartIsMissing()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The DSN is invalid. It does not have scheme separator ":".');
new Dsn('foobar');
}
public function testThrowsIfSchemeContainsIllegalSymbols()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The DSN is invalid. Scheme contains illegal symbols.');
new Dsn('foo_&%&^bar://localhost');
}
/**
* @dataProvider provideSchemes
*/
public function testShouldParseSchemeCorrectly(string $dsn, string $expectedScheme, string $expectedSchemeProtocol, array $expectedSchemeExtensions)
{
$dsn = new Dsn($dsn);
$this->assertSame($expectedScheme, $dsn->getScheme());
$this->assertSame($expectedSchemeProtocol, $dsn->getSchemeProtocol());
$this->assertSame($expectedSchemeExtensions, $dsn->getSchemeExtensions());
}
public function testShouldParseUser()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');
$this->assertSame('theUser', $dsn->getUser());
}
public function testShouldParsePassword()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');
$this->assertSame('thePass', $dsn->getPassword());
}
public function testShouldParseHost()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');
$this->assertSame('theHost', $dsn->getHost());
}
public function testShouldParsePort()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');
$this->assertSame(1267, $dsn->getPort());
}
public function testShouldParsePath()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');
$this->assertSame('/thePath', $dsn->getPath());
}
public function testShouldUrlDecodedPath()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/%2f');
$this->assertSame('//', $dsn->getPath());
}
public function testShouldParseQuery()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath?foo=fooVal&bar=bar%2fVal');
$this->assertSame('foo=fooVal&bar=bar%2fVal', $dsn->getQueryString());
$this->assertSame(['foo' => 'fooVal', 'bar' => 'bar/Val'], $dsn->getQuery());
}
public function testShouldParseQueryShouldPreservePlusSymbol()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath?foo=fooVal&bar=bar+Val');
$this->assertSame('foo=fooVal&bar=bar+Val', $dsn->getQueryString());
$this->assertSame(['foo' => 'fooVal', 'bar' => 'bar+Val'], $dsn->getQuery());
}
/**
* @dataProvider provideIntQueryParameters
*/
public function testShouldParseQueryParameterAsInt(string $parameter, int $expected)
{
$dsn = new Dsn('foo:?aName='.$parameter);
$this->assertSame($expected, $dsn->getInt('aName'));
}
public function testShouldReturnDefaultIntIfNotSet()
{
$dsn = new Dsn('foo:');
$this->assertNull($dsn->getInt('aName'));
$this->assertSame(123, $dsn->getInt('aName', 123));
}
public function testThrowIfQueryParameterNotInt()
{
$dsn = new Dsn('foo:?aName=notInt');
$this->expectException(InvalidQueryParameterTypeException::class);
$this->expectExceptionMessage('The query parameter "aName" has invalid type. It must be "integer"');
$dsn->getInt('aName');
}
/**
* @dataProvider provideFloatQueryParameters
*/
public function testShouldParseQueryParameterAsFloat(string $parameter, float $expected)
{
$dsn = new Dsn('foo:?aName='.$parameter);
$this->assertSame($expected, $dsn->getFloat('aName'));
}
public function testShouldReturnDefaultFloatIfNotSet()
{
$dsn = new Dsn('foo:');
$this->assertNull($dsn->getFloat('aName'));
$this->assertSame(123., $dsn->getFloat('aName', 123.));
}
public function testThrowIfQueryParameterNotFloat()
{
$dsn = new Dsn('foo:?aName=notFloat');
$this->expectException(InvalidQueryParameterTypeException::class);
$this->expectExceptionMessage('The query parameter "aName" has invalid type. It must be "float"');
$dsn->getFloat('aName');
}
/**
* @dataProvider provideBooleanQueryParameters
*/
public function testShouldParseQueryParameterAsBoolean(string $parameter, bool $expected)
{
$dsn = new Dsn('foo:?aName='.$parameter);
$this->assertSame($expected, $dsn->getBool('aName'));
}
public function testShouldReturnDefaultBoolIfNotSet()
{
$dsn = new Dsn('foo:');
$this->assertNull($dsn->getBool('aName'));
$this->assertTrue($dsn->getBool('aName', true));
}
public function testThrowIfQueryParameterNotBool()
{
$dsn = new Dsn('foo:?aName=notBool');
$this->expectException(InvalidQueryParameterTypeException::class);
$this->expectExceptionMessage('The query parameter "aName" has invalid type. It must be "bool"');
$dsn->getBool('aName');
}
public static function provideSchemes()
{
yield [':', '', '', []];
yield ['FOO:', 'foo', 'foo', []];
yield ['foo:', 'foo', 'foo', []];
yield ['foo+bar:', 'foo+bar', 'foo', ['bar']];
yield ['foo+bar+baz:', 'foo+bar+baz', 'foo', ['bar', 'baz']];
yield ['foo:?bar=barVal', 'foo', 'foo', []];
yield ['amqp+ext://guest:guest@localhost:5672/%2f', 'amqp+ext', 'amqp', ['ext']];
yield ['amqp+ext+rabbitmq:', 'amqp+ext+rabbitmq', 'amqp', ['ext', 'rabbitmq']];
}
public static function provideIntQueryParameters()
{
yield ['123', 123];
yield ['+123', 123];
yield ['-123', -123];
}
public static function provideFloatQueryParameters()
{
yield ['123', 123.];
yield ['+123', 123.];
yield ['-123', -123.];
yield ['0', 0.];
}
public static function provideBooleanQueryParameters()
{
yield ['', false];
yield ['1', true];
yield ['0', false];
yield ['true', true];
yield ['false', false];
}
}