11<?php
22
3- namespace React \Tests \Mysql \ Io ;
3+ namespace React \Tests \Mysql ;
44
55use React \Mysql \Io \Connection ;
66use React \Mysql \MysqlClient ;
1010use React \Promise \PromiseInterface ;
1111use React \Stream \ReadableStreamInterface ;
1212use React \Stream \ThroughStream ;
13- use React \Tests \Mysql \BaseTestCase ;
1413
1514class MysqlClientTest extends BaseTestCase
1615{
@@ -197,12 +196,12 @@ public function testPingFollowedByIdleTimerWillQuitUnderlyingConnection()
197196 $ timeout ();
198197 }
199198
200- public function testPingFollowedByIdleTimerWillCloseUnderlyingConnectionWhenQuitFails ()
199+ public function testPingFollowedByIdleTimerWillNotHaveToCloseUnderlyingConnectionWhenQuitFailsBecauseUnderlyingConnectionEmitsCloseAutomatically ()
201200 {
202201 $ base = $ this ->getMockBuilder ('React\Mysql\Io\Connection ' )->setMethods (['ping ' , 'quit ' , 'close ' ])->disableOriginalConstructor ()->getMock ();
203202 $ base ->expects ($ this ->once ())->method ('ping ' )->willReturn (\React \Promise \resolve (null ));
204203 $ 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 ' );
206205
207206 $ factory = $ this ->getMockBuilder ('React\Mysql\Io\Factory ' )->disableOriginalConstructor ()->getMock ();
208207 $ factory ->expects ($ this ->once ())->method ('createConnection ' )->willReturn (\React \Promise \resolve ($ base ));
@@ -227,6 +226,15 @@ public function testPingFollowedByIdleTimerWillCloseUnderlyingConnectionWhenQuit
227226
228227 $ this ->assertNotNull ($ timeout );
229228 $ 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 );
230238 }
231239
232240 public function testPingAfterIdleTimerWillCloseUnderlyingConnectionBeforeCreatingSecondConnection ()
@@ -757,6 +765,32 @@ public function testQuitAfterPingReturnsPendingPromiseWhenConnectionIsPending()
757765 $ ret ->then ($ this ->expectCallableNever (), $ this ->expectCallableNever ());
758766 }
759767
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+
760794 public function testQuitAfterPingWillQuitUnderlyingConnectionWhenResolved ()
761795 {
762796 $ base = $ this ->getMockBuilder ('React\Mysql\Io\Connection ' )->disableOriginalConstructor ()->getMock ();
@@ -777,11 +811,12 @@ public function testQuitAfterPingWillQuitUnderlyingConnectionWhenResolved()
777811 $ connection ->quit ();
778812 }
779813
780- public function testQuitAfterPingResolvesAndEmitsCloseWhenUnderlyingConnectionQuits ()
814+ public function testQuitAfterPingResolvesAndThenEmitsCloseWhenUnderlyingConnectionQuits ()
781815 {
782816 $ base = $ this ->getMockBuilder ('React\Mysql\Io\Connection ' )->disableOriginalConstructor ()->getMock ();
817+ $ deferred = new Deferred ();
783818 $ 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 ( ));
785820
786821 $ factory = $ this ->getMockBuilder ('React\Mysql\Io\Factory ' )->disableOriginalConstructor ()->getMock ();
787822 $ factory ->expects ($ this ->once ())->method ('createConnection ' )->willReturn (\React \Promise \resolve ($ base ));
@@ -793,21 +828,25 @@ public function testQuitAfterPingResolvesAndEmitsCloseWhenUnderlyingConnectionQu
793828 $ ref ->setAccessible (true );
794829 $ ref ->setValue ($ connection , $ factory );
795830
796- $ connection ->on ('close ' , $ this ->expectCallableOnce ());
797-
798831 $ connection ->ping ();
799- $ ret = $ connection ->quit ();
800832
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 );
803842 }
804843
805- public function testQuitAfterPingRejectsAndEmitsCloseWhenUnderlyingConnectionFailsToQuit ()
844+ public function testQuitAfterPingRejectsAndThenEmitsCloseWhenUnderlyingConnectionFailsToQuit ()
806845 {
807- $ error = new \ RuntimeException ();
846+ $ deferred = new Deferred ();
808847 $ base = $ this ->getMockBuilder ('React\Mysql\Io\Connection ' )->disableOriginalConstructor ()->getMock ();
809848 $ 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 ( ));
811850
812851 $ factory = $ this ->getMockBuilder ('React\Mysql\Io\Factory ' )->disableOriginalConstructor ()->getMock ();
813852 $ factory ->expects ($ this ->once ())->method ('createConnection ' )->willReturn (\React \Promise \resolve ($ base ));
@@ -819,13 +858,17 @@ public function testQuitAfterPingRejectsAndEmitsCloseWhenUnderlyingConnectionFai
819858 $ ref ->setAccessible (true );
820859 $ ref ->setValue ($ connection , $ factory );
821860
822- $ connection ->on ('close ' , $ this ->expectCallableOnce ());
823-
824861 $ connection ->ping ();
825- $ ret = $ connection ->quit ();
826862
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 ());
829872 }
830873
831874 public function testCloseEmitsCloseImmediatelyWhenConnectionIsNotAlreadyPending ()
0 commit comments