Skip to content

Commit 4c81444

Browse files
committed
@
Restore InnerBrowser navigation state for Codeception runs (#231) The 3.9.0 audit decoupled amOnRoute/amOnAction, seePageIsAvailable, seePageRedirectsTo and submitSymfonyForm from codeception/lib-innerbrowser so they can also run from plain PHPUnit test cases. They now drive the BrowserKit client directly (request()/submit()) and discard the returned Crawler. Under Codeception that broke downstream DOM assertions: only InnerBrowser::_loadPage() writes the Crawler back into the cached $crawler (and resets $baseUrl/$forms), so see(), click() and the form helpers ended up operating on a null or stale Crawler after those methods. Keep the decoupling but branch per runner inside each method: when the module is an InnerBrowser (Codeception) navigation/submission is delegated to amOnPage()/submitForm() so the cached state stays in sync; otherwise (PHPUnit) it keeps driving the client directly. A matching functional test still needs to land in Codeception/symfony-module-tests per CONTRIBUTING.md. @
1 parent bed4de2 commit 4c81444

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Codeception\Module\Symfony;
66

7+
use Codeception\Lib\InnerBrowser;
78
use PHPUnit\Framework\Constraint\Constraint;
89
use PHPUnit\Framework\Constraint\LogicalAnd;
910
use PHPUnit\Framework\Constraint\LogicalNot;
@@ -22,7 +23,6 @@
2223
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseStatusCodeSame;
2324

2425
use function class_exists;
25-
use function count;
2626
use function sprintf;
2727

2828
trait BrowserAssertionsTrait
@@ -318,8 +318,15 @@ protected function doRebootClientKernel(): void {}
318318
public function seePageIsAvailable(?string $url = null): void
319319
{
320320
if ($url !== null) {
321-
$this->getClient()->request('GET', $url);
322-
$this->assertStringContainsString($url, $this->getClient()->getRequest()->getRequestUri());
321+
$client = $this->getClient();
322+
323+
if ($this instanceof InnerBrowser) {
324+
$this->amOnPage($url);
325+
} else {
326+
$client->request('GET', $url);
327+
}
328+
329+
$this->assertStringContainsString($url, $client->getRequest()->getRequestUri());
323330
}
324331

325332
$this->assertResponseIsSuccessful();
@@ -337,7 +344,12 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
337344
{
338345
$client = $this->getClient();
339346
$client->followRedirects(false);
340-
$client->request('GET', $page);
347+
348+
if ($this instanceof InnerBrowser) {
349+
$this->amOnPage($page);
350+
} else {
351+
$client->request('GET', $page);
352+
}
341353

342354
$this->assertThatForResponse(new ResponseIsRedirected(), 'The response is not a redirection.');
343355

@@ -364,17 +376,24 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
364376
*/
365377
public function submitSymfonyForm(string $name, array $fields): void
366378
{
379+
$client = $this->getClient();
367380
$selector = sprintf('form[name=%s]', $name);
368381

369382
$params = [];
370383
foreach ($fields as $key => $value) {
371384
$params[$name . $key] = $value;
372385
}
373386

374-
$node = $this->getClient()->getCrawler()->filter($selector);
375-
$this->assertGreaterThan(0, count($node), sprintf('Form "%s" not found.', $selector));
387+
if ($this instanceof InnerBrowser) {
388+
$this->submitForm($selector, $params, sprintf('%s_submit', $name));
389+
390+
return;
391+
}
392+
393+
$node = $client->getCrawler()->filter($selector);
394+
$this->assertGreaterThan(0, $node->count(), sprintf('Form "%s" not found.', $selector));
376395
$form = $node->form();
377-
$this->getClient()->submit($form, $params);
396+
$client->submit($form, $params);
378397
}
379398

380399
protected function assertThatForClient(Constraint $constraint, string $message = ''): void

src/Codeception/Module/Symfony/RouterAssertionsTrait.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Codeception\Module\Symfony;
66

7+
use Codeception\Lib\InnerBrowser;
78
use PHPUnit\Framework\Assert;
89
use Symfony\Component\Routing\RouterInterface;
910

@@ -157,7 +158,16 @@ private function assertRouteExists(string $routeName): void
157158
/** @param array<string, mixed> $params */
158159
private function openRoute(string $routeName, array $params = []): void
159160
{
160-
$this->getClient()->request('GET', $this->grabRouterService()->generate($routeName, $params));
161+
$client = $this->getClient();
162+
$url = $this->grabRouterService()->generate($routeName, $params);
163+
164+
if ($this instanceof InnerBrowser) {
165+
$this->amOnPage($url);
166+
167+
return;
168+
}
169+
170+
$client->request('GET', $url);
161171
}
162172

163173
protected function grabRouterService(): RouterInterface

0 commit comments

Comments
 (0)