@@ -866,4 +866,81 @@ private function invokeModelMethod(MockObject $adapter, string $method, array $p
866
866
867
867
return $ method ->invokeArgs ($ adapter , $ parameters );
868
868
}
869
+
870
+ /**
871
+ * @dataProvider retryExceptionDataProvider
872
+ * @param \Exception $exception
873
+ * @return void
874
+ */
875
+ public function testBeginTransactionWithReconnect (\Exception $ exception ): void
876
+ {
877
+ $ adapter = $ this ->getMysqlPdoAdapterMock (['_connect ' , '_beginTransaction ' , '_rollBack ' ]);
878
+ $ adapter ->expects (self ::exactly (4 ))
879
+ ->method ('_connect ' );
880
+ $ adapter ->expects (self ::once ())
881
+ ->method ('_rollBack ' );
882
+
883
+ $ matcher = self ::exactly (2 );
884
+ $ adapter ->expects ($ matcher )
885
+ ->method ('_beginTransaction ' )
886
+ ->willReturnCallback (
887
+ function () use ($ matcher , $ exception ) {
888
+ if ($ matcher ->getInvocationCount () === 1 ) {
889
+ throw $ exception ;
890
+ }
891
+ }
892
+ );
893
+ $ adapter ->beginTransaction ();
894
+ $ adapter ->rollBack ();
895
+ }
896
+
897
+ /**
898
+ * @return array[]
899
+ */
900
+ public function retryExceptionDataProvider (): array
901
+ {
902
+ $ serverHasGoneAwayException = new \PDOException ();
903
+ $ serverHasGoneAwayException ->errorInfo = [1 => 2006 ];
904
+ $ lostConnectionException = new \PDOException ();
905
+ $ lostConnectionException ->errorInfo = [1 => 2013 ];
906
+
907
+ return [
908
+ [$ serverHasGoneAwayException ],
909
+ [$ lostConnectionException ],
910
+ [new \Zend_Db_Statement_Exception ('' , 0 , $ serverHasGoneAwayException )],
911
+ [new \Zend_Db_Statement_Exception ('' , 0 , $ lostConnectionException )],
912
+ ];
913
+ }
914
+
915
+ /**
916
+ * @dataProvider exceptionDataProvider
917
+ * @param \Exception $exception
918
+ * @return void
919
+ */
920
+ public function testBeginTransactionWithoutReconnect (\Exception $ exception ): void
921
+ {
922
+ $ this ->expectException (\Exception::class);
923
+ $ adapter = $ this ->getMysqlPdoAdapterMock (['_connect ' , '_beginTransaction ' , '_rollBack ' ]);
924
+ $ adapter ->expects (self ::once ())
925
+ ->method ('_connect ' );
926
+ $ adapter ->expects (self ::once ())
927
+ ->method ('_beginTransaction ' )
928
+ ->willThrowException ($ exception );
929
+ $ adapter ->beginTransaction ();
930
+ }
931
+
932
+ /**
933
+ * @return array[]
934
+ */
935
+ public function exceptionDataProvider (): array
936
+ {
937
+ $ pdoException = new \PDOException ();
938
+ $ pdoException ->errorInfo = [1 => 1213 ];
939
+
940
+ return [
941
+ [$ pdoException ],
942
+ [new \Zend_Db_Statement_Exception ('' , 0 , $ pdoException )],
943
+ [new \Exception ()],
944
+ ];
945
+ }
869
946
}
0 commit comments