11<?php
22
33use Codeception \Configuration ;
4+ use GuzzleHttp \Client ;
5+ use GuzzleHttp \Psr7 \Request ;
46use Mcustiel \Phiremock \Client \Phiremock ;
7+ use function Mcustiel \Phiremock \Client \postRequest ;
8+ use function Mcustiel \Phiremock \Client \respond ;
59use Mcustiel \Phiremock \Client \Utils \A ;
610use Mcustiel \Phiremock \Client \Utils \Is ;
711use Mcustiel \Phiremock \Client \Utils \Respond ;
8- use function Mcustiel \Phiremock \Client \{postRequest , respond };
9- use GuzzleHttp \Client ;
1012
1113class BasicTestCest
1214{
@@ -15,7 +17,12 @@ class BasicTestCest
1517
1618 public function _before (AcceptanceTester $ I )
1719 {
18- $ this ->guzzle = new Client (['http_errors ' => false ]);
20+ $ this ->guzzle = new Client (
21+ [
22+ 'base_uri ' => 'http://localhost:18080 ' ,
23+ 'http_errors ' => false
24+ ]
25+ );
1926 }
2027
2128 public function severalExceptatationsInOneTest (AcceptanceTester $ I )
@@ -49,7 +56,7 @@ public function severalExceptatationsInOneTest(AcceptanceTester $I)
4956 )
5057 );
5158 foreach (['potato ' , 'tomato ' , 'banana ' , 'coconut ' ] as $ item ) {
52- $ response = file_get_contents ( ' http://localhost:18080/ ' . $ item );
59+ $ response = ( string ) $ this -> guzzle -> get ( " / { $ item}" )-> getBody ( );
5360 $ I ->assertEquals ('I am a ' . $ item , $ response );
5461 }
5562 $ I ->seeRemoteServiceReceived (4 , A::getRequest ());
@@ -61,19 +68,37 @@ public function severalExceptatationsInOneTest(AcceptanceTester $I)
6168 $ I ->seeRemoteServiceReceived (0 , A::getRequest ());
6269 }
6370
71+ public function shouldSetTheScenarioState (AcceptanceTester $ I ): void
72+ {
73+ $ I ->expectARequestToRemoteServiceWithAResponse (
74+ Phiremock::on (
75+ A::getRequest ()
76+ ->andUrl (Is::equalTo ('/potato ' ))
77+ ->andScenarioState ('tomatoScenario ' , 'potatoState ' )
78+ )->then (
79+ Respond::withStatusCode (203 )->andBody ('I am a potato ' )
80+ )
81+ );
82+ $ response = $ this ->guzzle ->get ('/potato ' );
83+ $ I ->assertSame (404 , $ response ->getStatusCode ());
84+ $ I ->setScenarioState ('tomatoScenario ' , 'potatoState ' );
85+ $ response = $ this ->guzzle ->get ('/potato ' );
86+ $ I ->assertSame (203 , $ response ->getStatusCode ());
87+ $ I ->assertSame ('I am a potato ' , (string ) $ response ->getBody ());
88+ }
89+
6490 public function shouldCreateAnExpectationWithBinaryResponseTest (AcceptanceTester $ I )
6591 {
6692 $ responseContents = file_get_contents (Configuration::dataDir () . '/fixtures/silhouette-1444982_640.png ' );
6793 $ I ->expectARequestToRemoteServiceWithAResponse (
6894 Phiremock::on (
69- A::getRequest ()->andUrl (Is::equalTo ('/show-me-the-video ' ))
70- )->then (
71- respond (200 )->andBinaryBody ($ responseContents )
95+ A::getRequest ()->andUrl (Is::equalTo ('/show-me-the-video ' ))
96+ )->then (
97+ respond (200 )->andBinaryBody ($ responseContents )
7298 )
7399 );
74100
75-
76- $ responseBody = file_get_contents ('http://localhost:18080/show-me-the-video ' );
101+ $ responseBody = (string ) $ this ->guzzle ->get ('/show-me-the-video ' )->getBody ();
77102 $ I ->assertEquals ($ responseContents , $ responseBody );
78103 }
79104
@@ -84,14 +109,13 @@ public function testGrabRequestsMadeToRemoteService(AcceptanceTester $I)
84109 Phiremock::on ($ requestBuilder )->then (respond (200 ))
85110 );
86111
87- $ options = array (
88- 'http ' => array (
89- 'header ' => 'Content-Type: application/x-www-form-urlencoded ' ,
90- 'method ' => 'POST ' ,
91- 'content ' => http_build_query (['a ' => 'b ' ])
92- )
112+ $ request = new Request (
113+ 'POST ' ,
114+ '/some/url ' ,
115+ ['Content-Type ' => 'application/x-www-form-urlencoded ' ],
116+ http_build_query (['a ' => 'b ' ])
93117 );
94- file_get_contents ( ' http://localhost:18080/some/url ' , false , stream_context_create ( $ options ) );
118+ $ this -> guzzle -> send ( $ request );
95119
96120 $ requests = $ I ->grabRequestsMadeToRemoteService ($ requestBuilder );
97121 $ I ->assertCount (1 , $ requests );
@@ -101,7 +125,7 @@ public function testGrabRequestsMadeToRemoteService(AcceptanceTester $I)
101125
102126 $ headers = (array ) $ first ->headers ;
103127 $ expectedSubset = [
104- 'Host ' => ['localhost:18080 ' ],
128+ 'Host ' => ['localhost:18080 ' ],
105129 'Content-Type ' => ['application/x-www-form-urlencoded ' ]
106130 ];
107131
@@ -118,12 +142,12 @@ public function testGrabRequestsMadeToRemoteService(AcceptanceTester $I)
118142 public function testAnnotationExpectationIsLoaded (AcceptanceTester $ I )
119143 {
120144 $ requestBuilder = A::getRequest ()->andUrl (Is::equalTo ('/expectation/1 ' ));
121- $ response = file_get_contents ( ' http://localhost:18080/ expectation/1 ' );
145+ $ response = ( string ) $ this -> guzzle -> get ( ' / expectation/1 ')-> getBody ( );
122146
123147 $ requests = $ I ->grabRequestsMadeToRemoteService ($ requestBuilder );
124148 $ I ->assertCount (1 , $ requests );
125149
126- $ I ->assertEquals (" response " , $ response );
150+ $ I ->assertEquals (' response ' , $ response );
127151 }
128152
129153 /**
@@ -134,12 +158,12 @@ public function testAnnotationExpectationIsLoaded(AcceptanceTester $I)
134158 public function testMultipleAnnotationsAreLoaded (AcceptanceTester $ I )
135159 {
136160 $ requestBuilder = A::getRequest ()->andUrl (Is::matching ('/ \\/expectation \\/ \\d+/ ' ));
137- file_get_contents ( ' http://localhost:18080 /expectation/1 ' );
138- file_get_contents ( ' http://localhost:18080 /expectation/2 ' );
161+ $ this -> guzzle -> get ( ' /expectation/1 ' );
162+ $ this -> guzzle -> get ( ' /expectation/2 ' );
139163 $ requests = $ I ->grabRequestsMadeToRemoteService ($ requestBuilder );
140164 $ I ->assertCount (2 , $ requests );
141165 }
142-
166+
143167 /**
144168 * @param AcceptanceTester $I
145169 * @expectation("subdirectory/test_first_get")
@@ -148,7 +172,8 @@ public function testAnnotationInSubdirectoryIsLoaded(AcceptanceTester $I)
148172 {
149173 $ conditionsBuilder = A::getRequest ();
150174 $ requestBuilder = $ conditionsBuilder ->andMethod (Is::equalTo ('GET ' ))->andUrl (Is::equalTo ('/expectation/subdirectory ' ));
151- file_get_contents ('http://localhost:18080/expectation/subdirectory ' );
175+ $ responseBody = (string ) $ this ->guzzle ->get ('/expectation/subdirectory ' )->getBody ();
176+ $ I ->assertSame ('response ' , $ responseBody );
152177 $ requests = $ I ->grabRequestsMadeToRemoteService ($ requestBuilder );
153178 $ I ->assertCount (1 , $ requests );
154179 }
0 commit comments