20
20
use Codeception \Util \Locator ;
21
21
use Codeception \Util \ReflectionHelper ;
22
22
use Codeception \Util \Uri ;
23
+ use Symfony \Component \BrowserKit \AbstractBrowser ;
23
24
use Symfony \Component \BrowserKit \Cookie ;
24
25
use Symfony \Component \BrowserKit \Exception \BadMethodCallException ;
25
26
use Symfony \Component \DomCrawler \Crawler ;
@@ -45,7 +46,7 @@ class InnerBrowser extends Module implements Web, PageSourceSaver, ElementLocato
45
46
46
47
/**
47
48
* @api
48
- * @var \Symfony\Component\BrowserKit\AbstractBrowser
49
+ * @var \Symfony\Component\BrowserKit\AbstractBrowser|null
49
50
*/
50
51
public $ client ;
51
52
@@ -62,10 +63,18 @@ class InnerBrowser extends Module implements Web, PageSourceSaver, ElementLocato
62
63
63
64
private $ baseUrl ;
64
65
66
+ /**
67
+ * @return \Symfony\Component\BrowserKit\AbstractBrowser|null
68
+ */
69
+ protected function getClient (): ?AbstractBrowser
70
+ {
71
+ return $ this ->client ;
72
+ }
73
+
65
74
public function _failed (TestInterface $ test , $ fail )
66
75
{
67
76
try {
68
- if (!$ this ->client || !$ this ->client ->getInternalResponse ()) {
77
+ if (!$ this ->getClient () || !$ this ->getClient () ->getInternalResponse ()) {
69
78
return ;
70
79
}
71
80
} catch (BadMethodCallException $ e ) {
@@ -83,7 +92,7 @@ public function _failed(TestInterface $test, $fail)
83
92
];
84
93
85
94
try {
86
- $ internalResponse = $ this ->client ->getInternalResponse ();
95
+ $ internalResponse = $ this ->getClient () ->getInternalResponse ();
87
96
} catch (BadMethodCallException $ e ) {
88
97
$ internalResponse = false ;
89
98
}
@@ -214,23 +223,25 @@ protected function clientRequest($method, $uri, array $parameters = [], array $f
214
223
}
215
224
}
216
225
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 ();
220
231
} else {
221
232
//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 ' );
224
235
}
225
236
226
237
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 );
228
239
$ this ->debugResponse ($ uri );
229
240
return $ result ;
230
241
}
231
242
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 );
234
245
$ this ->debugResponse ($ uri );
235
246
return $ this ->redirectIfNecessary ($ result , $ maxRedirects , 0 );
236
247
}
@@ -297,8 +308,10 @@ private function getCrawler()
297
308
298
309
private function getRunningClient ()
299
310
{
311
+ $ client = $ this ->getClient ();
312
+
300
313
try {
301
- if ($ this -> client ->getInternalRequest () === null ) {
314
+ if ($ client ->getInternalRequest () === null ) {
302
315
throw new ModuleException (
303
316
$ this ,
304
317
"Page not loaded. Use ` \$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it "
@@ -311,7 +324,7 @@ private function getRunningClient()
311
324
"Page not loaded. Use ` \$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it "
312
325
);
313
326
}
314
- return $ this -> client ;
327
+ return $ client ;
315
328
}
316
329
317
330
public function _savePageSource ($ filename )
@@ -327,8 +340,10 @@ public function _savePageSource($filename)
327
340
*/
328
341
public function amHttpAuthenticated ($ username , $ password )
329
342
{
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 );
332
347
}
333
348
334
349
/**
@@ -1422,7 +1437,7 @@ static function (Crawler $node) use ($attr, $val) {
1422
1437
1423
1438
public function grabTextFrom ($ cssOrXPathOrRegex )
1424
1439
{
1425
- if (@preg_match ($ cssOrXPathOrRegex , $ this ->client ->getInternalResponse ()->getContent (), $ matches )) {
1440
+ if (@preg_match ($ cssOrXPathOrRegex , $ this ->getClient () ->getInternalResponse ()->getContent (), $ matches )) {
1426
1441
return $ matches [1 ];
1427
1442
}
1428
1443
$ nodes = $ this ->match ($ cssOrXPathOrRegex );
@@ -1498,7 +1513,7 @@ public function grabValueFrom($field)
1498
1513
1499
1514
public function setCookie ($ name , $ val , array $ params = [])
1500
1515
{
1501
- $ cookies = $ this ->client ->getCookieJar ();
1516
+ $ cookies = $ this ->getClient () ->getCookieJar ();
1502
1517
$ params = array_merge ($ this ->defaultCookieParameters , $ params );
1503
1518
1504
1519
$ expires = isset ($ params ['expiry ' ]) ? $ params ['expiry ' ] : null ; // WebDriver compatibility
@@ -1542,20 +1557,20 @@ public function seeCookie($cookie, array $params = [])
1542
1557
{
1543
1558
$ params = array_merge ($ this ->defaultCookieParameters , $ params );
1544
1559
$ 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 ' ]));
1546
1561
}
1547
1562
1548
1563
public function dontSeeCookie ($ cookie , array $ params = [])
1549
1564
{
1550
1565
$ params = array_merge ($ this ->defaultCookieParameters , $ params );
1551
1566
$ 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 ' ]));
1553
1568
}
1554
1569
1555
1570
public function resetCookie ($ name , array $ params = [])
1556
1571
{
1557
1572
$ 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 ' ]);
1559
1574
$ this ->debugCookieJar ();
1560
1575
}
1561
1576
@@ -1892,7 +1907,7 @@ protected function getFormPhpValues($requestParams)
1892
1907
*/
1893
1908
protected function redirectIfNecessary ($ result , $ maxRedirects , $ redirectCount )
1894
1909
{
1895
- $ locationHeader = $ this ->client ->getInternalResponse ()->getHeader ('Location ' );
1910
+ $ locationHeader = $ this ->getClient () ->getInternalResponse ()->getHeader ('Location ' );
1896
1911
$ statusCode = $ this ->getResponseStatusCode ();
1897
1912
if ($ locationHeader && $ statusCode >= 300 && $ statusCode < 400 ) {
1898
1913
if ($ redirectCount === $ maxRedirects ) {
@@ -1904,11 +1919,11 @@ protected function redirectIfNecessary($result, $maxRedirects, $redirectCount)
1904
1919
1905
1920
$ this ->debugSection ('Redirecting to ' , $ locationHeader );
1906
1921
1907
- $ result = $ this ->client ->followRedirect ();
1922
+ $ result = $ this ->getClient () ->followRedirect ();
1908
1923
$ this ->debugResponse ($ locationHeader );
1909
1924
return $ this ->redirectIfNecessary ($ result , $ maxRedirects , $ redirectCount + 1 );
1910
1925
}
1911
- $ this ->client ->followRedirects (true );
1926
+ $ this ->getClient () ->followRedirects (true );
1912
1927
return $ result ;
1913
1928
}
1914
1929
@@ -1979,7 +1994,7 @@ public function moveBack($numberOfSteps = 1)
1979
1994
1980
1995
protected function debugCookieJar ()
1981
1996
{
1982
- $ cookies = $ this ->client ->getCookieJar ()->all ();
1997
+ $ cookies = $ this ->getClient () ->getCookieJar ()->all ();
1983
1998
$ cookieStrings = array_map ('strval ' , $ cookies );
1984
1999
$ this ->debugSection ('Cookie Jar ' , $ cookieStrings );
1985
2000
}
@@ -1988,7 +2003,7 @@ protected function setCookiesFromOptions()
1988
2003
{
1989
2004
if (isset ($ this ->config ['cookies ' ]) && is_array ($ this ->config ['cookies ' ]) && !empty ($ this ->config ['cookies ' ])) {
1990
2005
$ domain = parse_url ($ this ->config ['url ' ], PHP_URL_HOST );
1991
- $ cookieJar = $ this ->client ->getCookieJar ();
2006
+ $ cookieJar = $ this ->getClient () ->getCookieJar ();
1992
2007
foreach ($ this ->config ['cookies ' ] as &$ cookie ) {
1993
2008
if (!is_array ($ cookie ) || !array_key_exists ('Name ' , $ cookie ) || !array_key_exists ('Value ' , $ cookie )) {
1994
2009
throw new \InvalidArgumentException ('Cookies must have at least Name and Value attributes ' );
@@ -2050,7 +2065,7 @@ protected function getNormalizedResponseContent()
2050
2065
*/
2051
2066
public function setServerParameters (array $ params )
2052
2067
{
2053
- $ this ->client ->setServerParameters ($ params );
2068
+ $ this ->getClient () ->setServerParameters ($ params );
2054
2069
}
2055
2070
2056
2071
/**
@@ -2064,6 +2079,6 @@ public function setServerParameters(array $params)
2064
2079
*/
2065
2080
public function haveServerParameter ($ name , $ value )
2066
2081
{
2067
- $ this ->client ->setServerParameter ($ name , $ value );
2082
+ $ this ->getClient () ->setServerParameter ($ name , $ value );
2068
2083
}
2069
2084
}
0 commit comments