1
1
<?php
2
2
3
- namespace React \Tests \Mysql \ Io ;
3
+ namespace React \Tests \Mysql ;
4
4
5
5
use React \Mysql \Io \Connection ;
6
6
use React \Mysql \MysqlClient ;
10
10
use React \Promise \PromiseInterface ;
11
11
use React \Stream \ReadableStreamInterface ;
12
12
use React \Stream \ThroughStream ;
13
- use React \Tests \Mysql \BaseTestCase ;
14
13
15
14
class MysqlClientTest extends BaseTestCase
16
15
{
@@ -197,12 +196,12 @@ public function testPingFollowedByIdleTimerWillQuitUnderlyingConnection()
197
196
$ timeout ();
198
197
}
199
198
200
- public function testPingFollowedByIdleTimerWillCloseUnderlyingConnectionWhenQuitFails ()
199
+ public function testPingFollowedByIdleTimerWillNotHaveToCloseUnderlyingConnectionWhenQuitFailsBecauseUnderlyingConnectionEmitsCloseAutomatically ()
201
200
{
202
201
$ base = $ this ->getMockBuilder ('React\Mysql\Io\Connection ' )->setMethods (['ping ' , 'quit ' , 'close ' ])->disableOriginalConstructor ()->getMock ();
203
202
$ base ->expects ($ this ->once ())->method ('ping ' )->willReturn (\React \Promise \resolve (null ));
204
203
$ base ->expects ($ this ->once ())->method ('quit ' )->willReturn (\React \Promise \reject (new \RuntimeException ()));
205
- $ base ->expects ($ this ->once ())->method ('close ' );
204
+ $ base ->expects ($ this ->never ())->method ('close ' );
206
205
207
206
$ factory = $ this ->getMockBuilder ('React\Mysql\Io\Factory ' )->disableOriginalConstructor ()->getMock ();
208
207
$ factory ->expects ($ this ->once ())->method ('createConnection ' )->willReturn (\React \Promise \resolve ($ base ));
@@ -227,6 +226,15 @@ public function testPingFollowedByIdleTimerWillCloseUnderlyingConnectionWhenQuit
227
226
228
227
$ this ->assertNotNull ($ timeout );
229
228
$ timeout ();
229
+
230
+ assert ($ base instanceof Connection);
231
+ $ base ->emit ('close ' );
232
+
233
+ $ ref = new \ReflectionProperty ($ connection , 'connecting ' );
234
+ $ ref ->setAccessible (true );
235
+ $ connecting = $ ref ->getValue ($ connection );
236
+
237
+ $ this ->assertNull ($ connecting );
230
238
}
231
239
232
240
public function testPingAfterIdleTimerWillCloseUnderlyingConnectionBeforeCreatingSecondConnection ()
@@ -757,6 +765,32 @@ public function testQuitAfterPingReturnsPendingPromiseWhenConnectionIsPending()
757
765
$ ret ->then ($ this ->expectCallableNever (), $ this ->expectCallableNever ());
758
766
}
759
767
768
+ public function testQuitAfterPingRejectsAndThenEmitsCloseWhenFactoryFailsToCreateUnderlyingConnection ()
769
+ {
770
+ $ deferred = new Deferred ();
771
+ $ factory = $ this ->getMockBuilder ('React\MySQL\Io\Factory ' )->disableOriginalConstructor ()->getMock ();
772
+ $ factory ->expects ($ this ->once ())->method ('createConnection ' )->willReturn ($ deferred ->promise ());
773
+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
774
+
775
+ $ connection = new MysqlClient ('' , null , $ loop );
776
+
777
+ $ ref = new \ReflectionProperty ($ connection , 'factory ' );
778
+ $ ref ->setAccessible (true );
779
+ $ ref ->setValue ($ connection , $ factory );
780
+
781
+ $ connection ->ping ()->then (null , $ this ->expectCallableOnce ());
782
+
783
+ $ this ->expectOutputString ('reject.close. ' );
784
+ $ connection ->on ('close ' , function () {
785
+ echo 'close. ' ;
786
+ });
787
+ $ connection ->quit ()->then (null , function () {
788
+ echo 'reject. ' ;
789
+ });
790
+
791
+ $ deferred ->reject (new \RuntimeException ());
792
+ }
793
+
760
794
public function testQuitAfterPingWillQuitUnderlyingConnectionWhenResolved ()
761
795
{
762
796
$ base = $ this ->getMockBuilder ('React\Mysql\Io\Connection ' )->disableOriginalConstructor ()->getMock ();
@@ -777,11 +811,12 @@ public function testQuitAfterPingWillQuitUnderlyingConnectionWhenResolved()
777
811
$ connection ->quit ();
778
812
}
779
813
780
- public function testQuitAfterPingResolvesAndEmitsCloseWhenUnderlyingConnectionQuits ()
814
+ public function testQuitAfterPingResolvesAndThenEmitsCloseWhenUnderlyingConnectionQuits ()
781
815
{
782
816
$ base = $ this ->getMockBuilder ('React\Mysql\Io\Connection ' )->disableOriginalConstructor ()->getMock ();
817
+ $ deferred = new Deferred ();
783
818
$ base ->expects ($ this ->once ())->method ('ping ' )->willReturn (\React \Promise \resolve (null ));
784
- $ base ->expects ($ this ->once ())->method ('quit ' )->willReturn (\ React \ Promise \resolve ( null ));
819
+ $ base ->expects ($ this ->once ())->method ('quit ' )->willReturn ($ deferred -> promise ( ));
785
820
786
821
$ factory = $ this ->getMockBuilder ('React\Mysql\Io\Factory ' )->disableOriginalConstructor ()->getMock ();
787
822
$ factory ->expects ($ this ->once ())->method ('createConnection ' )->willReturn (\React \Promise \resolve ($ base ));
@@ -793,21 +828,25 @@ public function testQuitAfterPingResolvesAndEmitsCloseWhenUnderlyingConnectionQu
793
828
$ ref ->setAccessible (true );
794
829
$ ref ->setValue ($ connection , $ factory );
795
830
796
- $ connection ->on ('close ' , $ this ->expectCallableOnce ());
797
-
798
831
$ connection ->ping ();
799
- $ ret = $ connection ->quit ();
800
832
801
- $ this ->assertTrue ($ ret instanceof PromiseInterface);
802
- $ ret ->then ($ this ->expectCallableOnce (), $ this ->expectCallableNever ());
833
+ $ this ->expectOutputString ('quit.close. ' );
834
+ $ connection ->on ('close ' , function () {
835
+ echo 'close. ' ;
836
+ });
837
+ $ connection ->quit ()->then (function () {
838
+ echo 'quit. ' ;
839
+ });
840
+
841
+ $ deferred ->resolve (null );
803
842
}
804
843
805
- public function testQuitAfterPingRejectsAndEmitsCloseWhenUnderlyingConnectionFailsToQuit ()
844
+ public function testQuitAfterPingRejectsAndThenEmitsCloseWhenUnderlyingConnectionFailsToQuit ()
806
845
{
807
- $ error = new \ RuntimeException ();
846
+ $ deferred = new Deferred ();
808
847
$ base = $ this ->getMockBuilder ('React\Mysql\Io\Connection ' )->disableOriginalConstructor ()->getMock ();
809
848
$ base ->expects ($ this ->once ())->method ('ping ' )->willReturn (\React \Promise \resolve (null ));
810
- $ base ->expects ($ this ->once ())->method ('quit ' )->willReturn (\ React \ Promise \reject ( $ error ));
849
+ $ base ->expects ($ this ->once ())->method ('quit ' )->willReturn ($ deferred -> promise ( ));
811
850
812
851
$ factory = $ this ->getMockBuilder ('React\Mysql\Io\Factory ' )->disableOriginalConstructor ()->getMock ();
813
852
$ factory ->expects ($ this ->once ())->method ('createConnection ' )->willReturn (\React \Promise \resolve ($ base ));
@@ -819,13 +858,17 @@ public function testQuitAfterPingRejectsAndEmitsCloseWhenUnderlyingConnectionFai
819
858
$ ref ->setAccessible (true );
820
859
$ ref ->setValue ($ connection , $ factory );
821
860
822
- $ connection ->on ('close ' , $ this ->expectCallableOnce ());
823
-
824
861
$ connection ->ping ();
825
- $ ret = $ connection ->quit ();
826
862
827
- $ this ->assertTrue ($ ret instanceof PromiseInterface);
828
- $ ret ->then ($ this ->expectCallableNever (), $ this ->expectCallableOnceWith ($ error ));
863
+ $ this ->expectOutputString ('reject.close. ' );
864
+ $ connection ->on ('close ' , function () {
865
+ echo 'close. ' ;
866
+ });
867
+ $ connection ->quit ()->then (null , function () {
868
+ echo 'reject. ' ;
869
+ });
870
+
871
+ $ deferred ->reject (new \RuntimeException ());
829
872
}
830
873
831
874
public function testCloseEmitsCloseImmediatelyWhenConnectionIsNotAlreadyPending ()
0 commit comments