diff --git a/README.md b/README.md index 78caf12..094daa4 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,10 @@ $browser ->assertRedirectedTo('/some/page') // follows all redirects by default ->assertRedirectedTo('/some/page', 1) // just follow 1 redirect + // combination of interceptRedirects(), withProfiling(), click() + // useful for submitting forms and making assertions on the "redirect response" + ->clickAndIntercept('button') + // exception assertions for the "next request" ->expectException(MyException::class, 'the message') ->post('/url/that/throws/exception') // fails if above exception not thrown diff --git a/src/Browser/KernelBrowser.php b/src/Browser/KernelBrowser.php index 9455d18..88de53d 100644 --- a/src/Browser/KernelBrowser.php +++ b/src/Browser/KernelBrowser.php @@ -291,6 +291,23 @@ final public function patch(string $url, $options = []): self return $this->request('PATCH', $url, $options); } + /** + * Macro for ->interceptRedirects()->withProfiling()->click(). + * + * Useful for submitting a form and making assertions on the + * redirect response. + * + * @return static + */ + final public function clickAndIntercept(string $selector): self + { + return $this + ->interceptRedirects() + ->withProfiling() + ->click($selector) + ; + } + /** * @return static */ diff --git a/tests/Fixture/Kernel.php b/tests/Fixture/Kernel.php index 61d5551..a4ace4f 100644 --- a/tests/Fixture/Kernel.php +++ b/tests/Fixture/Kernel.php @@ -54,7 +54,7 @@ public function xml(): Response return new Response(\file_get_contents(__DIR__.'/files/xml.xml'), 200, ['Content-Type' => 'text/xml']); } - public function submitForm(Request $request): JsonResponse + public function submitForm(Request $request): Response { $files = \array_map( static function($value) { @@ -71,6 +71,10 @@ static function($value) { throw new \RuntimeException('fail!'); } + if ('r' === $request->request->get('submit_1')) { + return new RedirectResponse('/redirect1'); + } + return new JsonResponse(\array_merge( $request->request->all(), \array_filter($files) diff --git a/tests/Fixture/files/page1.html b/tests/Fixture/files/page1.html index d34492b..a7d98e0 100644 --- a/tests/Fixture/files/page1.html +++ b/tests/Fixture/files/page1.html @@ -61,6 +61,7 @@