2020use Codeception \Util \Locator ;
2121use Codeception \Util \ReflectionHelper ;
2222use Codeception \Util \Uri ;
23+ use Symfony \Component \BrowserKit \AbstractBrowser ;
2324use Symfony \Component \BrowserKit \Cookie ;
2425use Symfony \Component \BrowserKit \Exception \BadMethodCallException ;
2526use Symfony \Component \DomCrawler \Crawler ;
@@ -45,7 +46,7 @@ class InnerBrowser extends Module implements Web, PageSourceSaver, ElementLocato
4546
4647 /**
4748 * @api
48- * @var \Symfony\Component\BrowserKit\AbstractBrowser
49+ * @var \Symfony\Component\BrowserKit\AbstractBrowser|null
4950 */
5051 public $ client ;
5152
@@ -62,10 +63,18 @@ class InnerBrowser extends Module implements Web, PageSourceSaver, ElementLocato
6263
6364 private $ baseUrl ;
6465
66+ /**
67+ * @return \Symfony\Component\BrowserKit\AbstractBrowser|null
68+ */
69+ protected function getClient ()
70+ {
71+ return $ this ->client ;
72+ }
73+
6574 public function _failed (TestInterface $ test , $ fail )
6675 {
6776 try {
68- if (!$ this ->client || !$ this ->client ->getInternalResponse ()) {
77+ if (!$ this ->getClient () || !$ this ->getClient () ->getInternalResponse ()) {
6978 return ;
7079 }
7180 } catch (BadMethodCallException $ e ) {
@@ -83,7 +92,7 @@ public function _failed(TestInterface $test, $fail)
8392 ];
8493
8594 try {
86- $ internalResponse = $ this ->client ->getInternalResponse ();
95+ $ internalResponse = $ this ->getClient () ->getInternalResponse ();
8796 } catch (BadMethodCallException $ e ) {
8897 $ internalResponse = false ;
8998 }
@@ -214,23 +223,25 @@ protected function clientRequest($method, $uri, array $parameters = [], array $f
214223 }
215224 }
216225
217- if (method_exists ($ this ->client , 'isFollowingRedirects ' )) {
218- $ isFollowingRedirects = $ this ->client ->isFollowingRedirects ();
219- $ maxRedirects = $ this ->client ->getMaxRedirects ();
226+ $ client = $ this ->getClient ();
227+
228+ if (method_exists ($ client , 'isFollowingRedirects ' )) {
229+ $ isFollowingRedirects = $ client ->isFollowingRedirects ();
230+ $ maxRedirects = $ client ->getMaxRedirects ();
220231 } else {
221232 //Symfony 2.7 support
222- $ isFollowingRedirects = ReflectionHelper::readPrivateProperty ($ this -> client , 'followRedirects ' , 'Symfony\Component\BrowserKit\Client ' );
223- $ maxRedirects = ReflectionHelper::readPrivateProperty ($ this -> client , 'maxRedirects ' , 'Symfony\Component\BrowserKit\Client ' );
233+ $ isFollowingRedirects = ReflectionHelper::readPrivateProperty ($ client , 'followRedirects ' , 'Symfony\Component\BrowserKit\Client ' );
234+ $ maxRedirects = ReflectionHelper::readPrivateProperty ($ client , 'maxRedirects ' , 'Symfony\Component\BrowserKit\Client ' );
224235 }
225236
226237 if (!$ isFollowingRedirects ) {
227- $ result = $ this -> client ->request ($ method , $ uri , $ parameters , $ files , $ server , $ content , $ changeHistory );
238+ $ result = $ client ->request ($ method , $ uri , $ parameters , $ files , $ server , $ content , $ changeHistory );
228239 $ this ->debugResponse ($ uri );
229240 return $ result ;
230241 }
231242
232- $ this -> client ->followRedirects (false );
233- $ result = $ this -> client ->request ($ method , $ uri , $ parameters , $ files , $ server , $ content , $ changeHistory );
243+ $ client ->followRedirects (false );
244+ $ result = $ client ->request ($ method , $ uri , $ parameters , $ files , $ server , $ content , $ changeHistory );
234245 $ this ->debugResponse ($ uri );
235246 return $ this ->redirectIfNecessary ($ result , $ maxRedirects , 0 );
236247 }
@@ -297,8 +308,10 @@ private function getCrawler()
297308
298309 private function getRunningClient ()
299310 {
311+ $ client = $ this ->getClient ();
312+
300313 try {
301- if ($ this -> client ->getInternalRequest () === null ) {
314+ if ($ client ->getInternalRequest () === null ) {
302315 throw new ModuleException (
303316 $ this ,
304317 "Page not loaded. Use ` \$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it "
@@ -311,7 +324,7 @@ private function getRunningClient()
311324 "Page not loaded. Use ` \$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it "
312325 );
313326 }
314- return $ this -> client ;
327+ return $ client ;
315328 }
316329
317330 public function _savePageSource ($ filename )
@@ -327,8 +340,10 @@ public function _savePageSource($filename)
327340 */
328341 public function amHttpAuthenticated ($ username , $ password )
329342 {
330- $ this ->client ->setServerParameter ('PHP_AUTH_USER ' , $ username );
331- $ this ->client ->setServerParameter ('PHP_AUTH_PW ' , $ password );
343+ $ client = $ this ->getClient ();
344+
345+ $ client ->setServerParameter ('PHP_AUTH_USER ' , $ username );
346+ $ client ->setServerParameter ('PHP_AUTH_PW ' , $ password );
332347 }
333348
334349 /**
@@ -1422,7 +1437,7 @@ static function (Crawler $node) use ($attr, $val) {
14221437
14231438 public function grabTextFrom ($ cssOrXPathOrRegex )
14241439 {
1425- if (@preg_match ($ cssOrXPathOrRegex , $ this ->client ->getInternalResponse ()->getContent (), $ matches )) {
1440+ if (@preg_match ($ cssOrXPathOrRegex , $ this ->getClient () ->getInternalResponse ()->getContent (), $ matches )) {
14261441 return $ matches [1 ];
14271442 }
14281443 $ nodes = $ this ->match ($ cssOrXPathOrRegex );
@@ -1498,7 +1513,7 @@ public function grabValueFrom($field)
14981513
14991514 public function setCookie ($ name , $ val , array $ params = [])
15001515 {
1501- $ cookies = $ this ->client ->getCookieJar ();
1516+ $ cookies = $ this ->getClient () ->getCookieJar ();
15021517 $ params = array_merge ($ this ->defaultCookieParameters , $ params );
15031518
15041519 $ expires = isset ($ params ['expiry ' ]) ? $ params ['expiry ' ] : null ; // WebDriver compatibility
@@ -1542,20 +1557,20 @@ public function seeCookie($cookie, array $params = [])
15421557 {
15431558 $ params = array_merge ($ this ->defaultCookieParameters , $ params );
15441559 $ this ->debugCookieJar ();
1545- $ this ->assertNotNull ($ this ->client ->getCookieJar ()->get ($ cookie , $ params ['path ' ], $ params ['domain ' ]));
1560+ $ this ->assertNotNull ($ this ->getClient () ->getCookieJar ()->get ($ cookie , $ params ['path ' ], $ params ['domain ' ]));
15461561 }
15471562
15481563 public function dontSeeCookie ($ cookie , array $ params = [])
15491564 {
15501565 $ params = array_merge ($ this ->defaultCookieParameters , $ params );
15511566 $ this ->debugCookieJar ();
1552- $ this ->assertNull ($ this ->client ->getCookieJar ()->get ($ cookie , $ params ['path ' ], $ params ['domain ' ]));
1567+ $ this ->assertNull ($ this ->getClient () ->getCookieJar ()->get ($ cookie , $ params ['path ' ], $ params ['domain ' ]));
15531568 }
15541569
15551570 public function resetCookie ($ name , array $ params = [])
15561571 {
15571572 $ params = array_merge ($ this ->defaultCookieParameters , $ params );
1558- $ this ->client ->getCookieJar ()->expire ($ name , $ params ['path ' ], $ params ['domain ' ]);
1573+ $ this ->getClient () ->getCookieJar ()->expire ($ name , $ params ['path ' ], $ params ['domain ' ]);
15591574 $ this ->debugCookieJar ();
15601575 }
15611576
@@ -1892,7 +1907,7 @@ protected function getFormPhpValues($requestParams)
18921907 */
18931908 protected function redirectIfNecessary ($ result , $ maxRedirects , $ redirectCount )
18941909 {
1895- $ locationHeader = $ this ->client ->getInternalResponse ()->getHeader ('Location ' );
1910+ $ locationHeader = $ this ->getClient () ->getInternalResponse ()->getHeader ('Location ' );
18961911 $ statusCode = $ this ->getResponseStatusCode ();
18971912 if ($ locationHeader && $ statusCode >= 300 && $ statusCode < 400 ) {
18981913 if ($ redirectCount === $ maxRedirects ) {
@@ -1904,11 +1919,11 @@ protected function redirectIfNecessary($result, $maxRedirects, $redirectCount)
19041919
19051920 $ this ->debugSection ('Redirecting to ' , $ locationHeader );
19061921
1907- $ result = $ this ->client ->followRedirect ();
1922+ $ result = $ this ->getClient () ->followRedirect ();
19081923 $ this ->debugResponse ($ locationHeader );
19091924 return $ this ->redirectIfNecessary ($ result , $ maxRedirects , $ redirectCount + 1 );
19101925 }
1911- $ this ->client ->followRedirects (true );
1926+ $ this ->getClient () ->followRedirects (true );
19121927 return $ result ;
19131928 }
19141929
@@ -1979,7 +1994,7 @@ public function moveBack($numberOfSteps = 1)
19791994
19801995 protected function debugCookieJar ()
19811996 {
1982- $ cookies = $ this ->client ->getCookieJar ()->all ();
1997+ $ cookies = $ this ->getClient () ->getCookieJar ()->all ();
19831998 $ cookieStrings = array_map ('strval ' , $ cookies );
19841999 $ this ->debugSection ('Cookie Jar ' , $ cookieStrings );
19852000 }
@@ -1988,7 +2003,7 @@ protected function setCookiesFromOptions()
19882003 {
19892004 if (isset ($ this ->config ['cookies ' ]) && is_array ($ this ->config ['cookies ' ]) && !empty ($ this ->config ['cookies ' ])) {
19902005 $ domain = parse_url ($ this ->config ['url ' ], PHP_URL_HOST );
1991- $ cookieJar = $ this ->client ->getCookieJar ();
2006+ $ cookieJar = $ this ->getClient () ->getCookieJar ();
19922007 foreach ($ this ->config ['cookies ' ] as &$ cookie ) {
19932008 if (!is_array ($ cookie ) || !array_key_exists ('Name ' , $ cookie ) || !array_key_exists ('Value ' , $ cookie )) {
19942009 throw new \InvalidArgumentException ('Cookies must have at least Name and Value attributes ' );
@@ -2050,7 +2065,7 @@ protected function getNormalizedResponseContent()
20502065 */
20512066 public function setServerParameters (array $ params )
20522067 {
2053- $ this ->client ->setServerParameters ($ params );
2068+ $ this ->getClient () ->setServerParameters ($ params );
20542069 }
20552070
20562071 /**
@@ -2064,6 +2079,6 @@ public function setServerParameters(array $params)
20642079 */
20652080 public function haveServerParameter ($ name , $ value )
20662081 {
2067- $ this ->client ->setServerParameter ($ name , $ value );
2082+ $ this ->getClient () ->setServerParameter ($ name , $ value );
20682083 }
20692084}
0 commit comments