|
12 | 12 |
|
13 | 13 | class LazyConnectionTest extends BaseTestCase |
14 | 14 | { |
15 | | - public function testPingWillCloseConnectionWithErrorWhenPendingConnectionFails() |
| 15 | + public function testPingWillNotCloseConnectionWhenPendingConnectionFails() |
16 | 16 | { |
17 | 17 | $deferred = new Deferred(); |
18 | 18 | $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
19 | 19 | $factory->expects($this->once())->method('createConnection')->willReturn($deferred->promise()); |
20 | 20 | $connection = new LazyConnection($factory, ''); |
21 | 21 |
|
22 | | - $connection->on('error', $this->expectCallableOnce()); |
23 | | - $connection->on('close', $this->expectCallableOnce()); |
| 22 | + $connection->on('error', $this->expectCallableNever()); |
| 23 | + $connection->on('close', $this->expectCallableNever()); |
24 | 24 |
|
25 | 25 | $connection->ping(); |
26 | 26 |
|
27 | 27 | $deferred->reject(new \RuntimeException()); |
28 | 28 | } |
29 | 29 |
|
30 | | - public function testPingWillCloseConnectionWithoutErrorWhenUnderlyingConnectionCloses() |
| 30 | + public function testPingWillNotCloseConnectionWhenUnderlyingConnectionCloses() |
31 | 31 | { |
32 | | - $promise = new Promise(function () { }); |
33 | | - $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
34 | | - $factory->expects($this->once())->method('createConnection')->willReturn($promise); |
35 | | - $base = new LazyConnection($factory, ''); |
| 32 | + $base = $this->getMockBuilder('React\MySQL\Io\LazyConnection')->setMethods(array('ping'))->disableOriginalConstructor()->getMock(); |
| 33 | + $base->expects($this->once())->method('ping')->willReturn(\React\Promise\resolve()); |
36 | 34 |
|
37 | 35 | $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
38 | 36 | $factory->expects($this->once())->method('createConnection')->willReturn(\React\Promise\resolve($base)); |
39 | 37 | $connection = new LazyConnection($factory, ''); |
40 | 38 |
|
41 | 39 | $connection->on('error', $this->expectCallableNever()); |
42 | | - $connection->on('close', $this->expectCallableOnce()); |
| 40 | + $connection->on('close', $this->expectCallableNever()); |
43 | 41 |
|
44 | 42 | $connection->ping(); |
45 | 43 | $base->close(); |
46 | 44 | } |
47 | 45 |
|
48 | | - public function testPingWillForwardErrorFromUnderlyingConnection() |
| 46 | + public function testPingWillNotForwardErrorFromUnderlyingConnection() |
49 | 47 | { |
50 | | - $promise = new Promise(function () { }); |
51 | | - $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
52 | | - $factory->expects($this->once())->method('createConnection')->willReturn($promise); |
53 | | - $base = new LazyConnection($factory, ''); |
| 48 | + $base = $this->getMockBuilder('React\MySQL\Io\LazyConnection')->setMethods(array('ping'))->disableOriginalConstructor()->getMock(); |
| 49 | + $base->expects($this->once())->method('ping')->willReturn(\React\Promise\resolve()); |
54 | 50 |
|
55 | 51 | $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
56 | 52 | $factory->expects($this->once())->method('createConnection')->willReturn(\React\Promise\resolve($base)); |
57 | 53 | $connection = new LazyConnection($factory, ''); |
58 | 54 |
|
59 | | - $connection->on('error', $this->expectCallableOnce()); |
| 55 | + $connection->on('error', $this->expectCallableNever()); |
60 | 56 | $connection->on('close', $this->expectCallableNever()); |
61 | 57 |
|
62 | 58 | $connection->ping(); |
@@ -178,6 +174,33 @@ public function testPingWillPingUnderlyingConnectionWhenResolved() |
178 | 174 | $connection->ping(); |
179 | 175 | } |
180 | 176 |
|
| 177 | + public function testPingTwiceWillBothRejectWithSameErrorWhenUnderlyingConnectionRejects() |
| 178 | + { |
| 179 | + $error = new \RuntimeException(); |
| 180 | + $deferred = new Deferred(); |
| 181 | + |
| 182 | + $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
| 183 | + $factory->expects($this->once())->method('createConnection')->willReturn($deferred->promise()); |
| 184 | + $connection = new LazyConnection($factory, ''); |
| 185 | + |
| 186 | + $connection->ping()->then($this->expectCallableNever(), $this->expectCallableOnceWith($error)); |
| 187 | + $connection->ping()->then($this->expectCallableNever(), $this->expectCallableOnceWith($error)); |
| 188 | + |
| 189 | + $deferred->reject($error); |
| 190 | + } |
| 191 | + |
| 192 | + public function testPingWillTryToCreateNewUnderlyingConnectionAfterPreviousPingFailedToCreateUnderlyingConnection() |
| 193 | + { |
| 194 | + $error = new \RuntimeException(); |
| 195 | + |
| 196 | + $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
| 197 | + $factory->expects($this->exactly(2))->method('createConnection')->willReturn(\React\Promise\reject($error)); |
| 198 | + $connection = new LazyConnection($factory, ''); |
| 199 | + |
| 200 | + $connection->ping()->then($this->expectCallableNever(), $this->expectCallableOnceWith($error)); |
| 201 | + $connection->ping()->then($this->expectCallableNever(), $this->expectCallableOnceWith($error)); |
| 202 | + } |
| 203 | + |
181 | 204 | public function testQuitResolvesAndEmitsCloseImmediatelyWhenConnectionIsNotAlreadyPending() |
182 | 205 | { |
183 | 206 | $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
@@ -220,6 +243,45 @@ public function testQuitAfterPingWillQuitUnderlyingConnectionWhenResolved() |
220 | 243 | $connection->quit(); |
221 | 244 | } |
222 | 245 |
|
| 246 | + public function testQuitAfterPingResolvesAndEmitsCloseWhenUnderlyingConnectionQuits() |
| 247 | + { |
| 248 | + $base = $this->getMockBuilder('React\MySQL\ConnectionInterface')->getMock(); |
| 249 | + $base->expects($this->once())->method('ping')->willReturn(\React\Promise\resolve()); |
| 250 | + $base->expects($this->once())->method('quit')->willReturn(\React\Promise\resolve()); |
| 251 | + |
| 252 | + $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
| 253 | + $factory->expects($this->once())->method('createConnection')->willReturn(\React\Promise\resolve($base)); |
| 254 | + $connection = new LazyConnection($factory, ''); |
| 255 | + |
| 256 | + $connection->on('close', $this->expectCallableOnce()); |
| 257 | + |
| 258 | + $connection->ping(); |
| 259 | + $ret = $connection->quit(); |
| 260 | + |
| 261 | + $this->assertTrue($ret instanceof PromiseInterface); |
| 262 | + $ret->then($this->expectCallableOnce(), $this->expectCallableNever()); |
| 263 | + } |
| 264 | + |
| 265 | + public function testQuitAfterPingRejectsAndEmitsCloseWhenUnderlyingConnectionFailsToQuit() |
| 266 | + { |
| 267 | + $error = new \RuntimeException(); |
| 268 | + $base = $this->getMockBuilder('React\MySQL\ConnectionInterface')->getMock(); |
| 269 | + $base->expects($this->once())->method('ping')->willReturn(\React\Promise\resolve()); |
| 270 | + $base->expects($this->once())->method('quit')->willReturn(\React\Promise\reject($error)); |
| 271 | + |
| 272 | + $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
| 273 | + $factory->expects($this->once())->method('createConnection')->willReturn(\React\Promise\resolve($base)); |
| 274 | + $connection = new LazyConnection($factory, ''); |
| 275 | + |
| 276 | + $connection->on('close', $this->expectCallableOnce()); |
| 277 | + |
| 278 | + $connection->ping(); |
| 279 | + $ret = $connection->quit(); |
| 280 | + |
| 281 | + $this->assertTrue($ret instanceof PromiseInterface); |
| 282 | + $ret->then($this->expectCallableNever(), $this->expectCallableOnceWith($error)); |
| 283 | + } |
| 284 | + |
223 | 285 | public function testCloseEmitsCloseImmediatelyWhenConnectionIsNotAlreadyPending() |
224 | 286 | { |
225 | 287 | $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); |
|
0 commit comments