@@ -22,8 +22,8 @@ public function __construct($jsonFile, $hostname, $port)
2222 public function mockHttpCall (array $ matchedRequest , array $ mockedResponse , array $ events = [])
2323 {
2424 $ entry = [
25- " request " => $ matchedRequest ,
26- " response " => $ mockedResponse
25+ ' request ' => $ matchedRequest ,
26+ ' response ' => $ mockedResponse,
2727 ];
2828
2929 if (0 < count ($ events )) {
@@ -33,35 +33,47 @@ public function mockHttpCall(array $matchedRequest, array $mockedResponse, array
3333 $ this ->payload [] = $ entry ;
3434 }
3535
36- public function writeForMoco ()
36+ public function writeForMoco ($ maxAttempt = 10 , $ tempoInMs = 200 )
3737 {
3838 file_put_contents ($ this ->jsonFile , json_encode ($ this ->payload ));
3939 // We need to wait for moco detecting the fixtures file changed
4040 // If not, we can perform a request on old configFile
4141 sleep (1 );
42- $ this ->waitForMoco ();
42+ $ this ->waitForMoco ($ maxAttempt , $ tempoInMs );
4343 }
4444
45- public function reset ()
45+ public function reset ($ maxAttempt = 10 , $ tempoInMs = 200 )
4646 {
4747 $ this ->payload = [];
4848 // To avoid false positive by having the next scenario using moco response of the previous scenario
49- $ this ->writeForMoco ();
49+ $ this ->writeForMoco ($ maxAttempt , $ tempoInMs );
5050 }
5151
52- private function waitForMoco ()
52+ private function waitForMoco ($ maxAttempt , $ tempoInMs )
5353 {
5454 $ attempts = 0 ;
55- $ max = 10 ;
5655 $ ip = gethostbyname ($ this ->hostname );
57- while (false === @stream_socket_client ('tcp:// ' .$ ip .': ' .$ this ->port , $ errno , $ errstr , 5 ) && ($ attempts < $ max )) {
58- usleep (200000 ); // 200ms
59- $ attempts ++;
56+ $ up = false ;
57+ while ($ attempts < $ maxAttempt && false === $ up ) {
58+ $ socket = @stream_socket_client ('tcp:// ' .$ ip .': ' .$ this ->port , $ errno , $ errstr , 5 );
59+
60+ if (false === $ socket ) {
61+ usleep ($ tempoInMs * 1000 );
62+ ++$ attempts ;
63+ } else {
64+ $ up = true ;
65+ @fclose ($ socket );
66+ }
6067 }
6168
62- if ($ max <= $ attempts ) {
69+ if ($ maxAttempt <= $ attempts ) {
6370 throw new \Exception (
64- sprintf ('You should run moco by "bin/moco start -p %s -c %s" ' , $ this ->port , $ this ->jsonFile )
71+ sprintf ('Cannot connect to moco on %s : %s. Ensure to run moco with "bin/moco start -p %s -c %s" ' ,
72+ $ ip ,
73+ $ errstr ,
74+ $ this ->port ,
75+ $ this ->jsonFile
76+ )
6577 );
6678 }
6779 }
0 commit comments