@@ -156,14 +156,14 @@ public function testBeginTransactionMethodNeverRetriesIfWithinTransaction()
156
156
}
157
157
}
158
158
159
- public function testCantSwapPDOWithOpenTransaction ()
159
+ public function testSwapPDOWithOpenTransactionResetsTransactionLevel ()
160
160
{
161
161
$ pdo = $ this ->createMock ('DatabaseConnectionTestMockPDO ' );
162
162
$ pdo ->expects ($ this ->once ())->method ('beginTransaction ' )->will ($ this ->returnValue (true ));
163
163
$ connection = $ this ->getMockConnection ([], $ pdo );
164
164
$ connection ->beginTransaction ();
165
- $ this ->setExpectedException ('RuntimeException ' , "Can't swap PDO instance while within transaction. " );
166
165
$ connection ->disconnect ();
166
+ $ this ->assertEquals (0 , $ connection ->transactionLevel ());
167
167
}
168
168
169
169
public function testBeganTransactionFiresEventsIfSet ()
@@ -240,24 +240,42 @@ public function testTransactionMethodRollsbackAndThrows()
240
240
}
241
241
242
242
/**
243
- * @expectedException RuntimeException
243
+ * @expectedException \Illuminate\Database\QueryException
244
244
*/
245
- public function testTransactionMethodDisallowPDOChanging ()
245
+ public function testOnLostConnectionPDOIsNotSwappedWithinATransaction ()
246
246
{
247
- $ pdo = $ this ->getMockBuilder ('DatabaseConnectionTestMockPDO ' )->setMethods (['beginTransaction ' , 'commit ' , 'rollBack ' ])->getMock ();
248
- $ pdo ->expects ($ this ->once ())->method ('beginTransaction ' );
249
- $ pdo ->expects ($ this ->once ())->method ('rollBack ' );
250
- $ pdo ->expects ($ this ->never ())->method ('commit ' );
247
+ $ pdo = m::mock (PDO ::class);
248
+ $ pdo ->shouldReceive ('beginTransaction ' )->once ();
249
+ $ statement = m::mock (PDOStatement::class);
250
+ $ pdo ->shouldReceive ('prepare ' )->once ()->andReturn ($ statement );
251
+ $ statement ->shouldReceive ('execute ' )->once ()->andThrow (new PDOException ('server has gone away ' ));
251
252
252
- $ mock = $ this ->getMockConnection ([], $ pdo );
253
+ $ connection = new \Illuminate \Database \Connection ($ pdo );
254
+ $ connection ->beginTransaction ();
255
+ $ connection ->statement ('foo ' );
256
+ }
253
257
254
- $ mock ->setReconnector (function ($ connection ) {
255
- $ connection ->setPDO (null );
256
- });
258
+ public function testOnLostConnectionPDOIsSwappedOutsideTransaction ()
259
+ {
260
+ $ pdo = m::mock (PDO ::class);
261
+
262
+ $ statement = m::mock (PDOStatement::class);
263
+ $ statement ->shouldReceive ('execute ' )->once ()->andThrow (new PDOException ('server has gone away ' ));
264
+ $ statement ->shouldReceive ('execute ' )->once ()->andReturn ('result ' );
265
+
266
+ $ pdo ->shouldReceive ('prepare ' )->twice ()->andReturn ($ statement );
257
267
258
- $ mock ->transaction (function ($ connection ) {
259
- $ connection ->reconnect ();
<
87D7
/code>
268
+ $ connection = new \Illuminate \Database \Connection ($ pdo );
269
+
270
+ $ called = false ;
271
+
272
+ $ connection ->setReconnector (function ($ connection ) use (&$ called ) {
273
+ $ called = true ;
260
274
});
275
+
276
+ $ this ->assertEquals ('result ' , $ connection ->statement ('foo ' ));
277
+
278
+ $ this ->assertTrue ($ called );
261
279
}
262
280
263
281
public function testRunMethodRetriesOnFailure ()
0 commit comments