Skip to content

Commit

Permalink
[feature] add KernelBrowser::clickAndIntercept() (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Apr 7, 2022
1 parent da4a6e4 commit 8989fca
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/Browser/KernelBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
6 changes: 5 additions & 1 deletion tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/Fixture/files/page1.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ <h1>h1 title</h1>
<button type="submit" name="submit_2" value="c">Submit C</button>
<button name="submit_2" value="d">Submit D</button>
<button name="submit_1" value="e">Submit Exception</button>
<button name="submit_1" value="r">Submit Redirect</button>
</form>
</body>
</html>
16 changes: 16 additions & 0 deletions tests/KernelBrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,22 @@ public function can_expect_exception_for_link_click(): void
;
}

/**
* @test
*/
public function click_and_intercept(): void
{
$this->browser()
->visit('/page1')
->clickAndIntercept('Submit Redirect')
->assertOn('/submit-form')
->use(function(RequestDataCollector $collector) {
$this->assertSame('/submit-form', $collector->getPathInfo());
})
->assertRedirectedTo('/page1')
;
}

/**
* @test
*/
Expand Down

0 comments on commit 8989fca

Please sign in to comment.