Skip to content

Commit

Permalink
[feature] fail if trying to manipulate an "exception" page (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Apr 7, 2022
1 parent 8989fca commit 2afbcca
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"behat/mink": "^1.8",
"symfony/browser-kit": "^5.4|^6.0",
"symfony/css-selector": "^5.4|^6.0",
"symfony/dom-crawler": "^5.4|^6.0",
"symfony/framework-bundle": "^5.4|^6.0",
"symfony/polyfill-php80": "^1.20",
"zenstruck/assert": "^1.0",
Expand Down
4 changes: 3 additions & 1 deletion src/Browser/KernelBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ final public function clickAndIntercept(string $selector): self
*/
final public function assertStatus(int $expected): self
{
$this->session()->assert()->statusCodeEquals($expected);
Assert::that($this->session()->getStatusCode())
->is($expected, 'Current response status code is {actual}, but {expected} expected.')
;

return $this;
}
Expand Down
25 changes: 25 additions & 0 deletions src/Browser/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Behat\Mink\WebAssert;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\DomCrawler\Crawler;
use Zenstruck\Assert as ZenstruckAssert;
use Zenstruck\Browser\Session\Assert;
use Zenstruck\Browser\Session\Driver;

Expand Down Expand Up @@ -40,11 +41,15 @@ public function client(): AbstractBrowser

public function assert(): Assert
{
$this->ensureNoException();

return new Assert(new WebAssert($this));
}

public function page(): DocumentElement
{
$this->ensureNoException();

return $this->getPage();
}

Expand Down Expand Up @@ -135,4 +140,24 @@ public function exit(): void
$this->getDriver()->quit();
exit(1);
}

private function ensureNoException(): void
{
if (!$this->isStarted()) {
ZenstruckAssert::fail('A request has not yet been made.');
}

$crawler = $this->client()->getCrawler();

if (!\count($exceptionClassNode = $crawler->filter('.trace-details .trace-class')->first())) {
return;
}

$messageNode = $crawler->filter('.exception-message-wrapper .exception-message')->first();

ZenstruckAssert::fail('The last request threw an exception: %s - %s', [
\preg_replace('/\s+/', '', $exceptionClassNode->text()),
\count($messageNode) ? $messageNode->text() : 'unknown message',
]);
}
}
29 changes: 29 additions & 0 deletions tests/BrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Zenstruck\Browser\Tests;

use PHPUnit\Framework\AssertionFailedError;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\VarDumper\VarDumper;
use Zenstruck\Assert;
use Zenstruck\Browser;
use Zenstruck\Browser\Test\HasBrowser;
use Zenstruck\Browser\Tests\Fixture\TestComponent1;
Expand Down Expand Up @@ -601,6 +603,33 @@ public function can_access_the_html_crawler(): void
$this->assertCount(2, $crawler);
}

/**
* @test
*/
public function fails_if_trying_to_manipulate_exception_page(): void
{
Assert::that(function() {
$this->browser()
->visit('/exception')
->click('foo')
;
})->throws(AssertionFailedError::class, 'The last request threw an exception: Zenstruck\Browser\Tests\Fixture\CustomException - exception thrown');

Assert::that(function() {
$this->browser()
->visit('/exception')
->fillField('foo', 'bar')
;
})->throws(AssertionFailedError::class, 'The last request threw an exception: Zenstruck\Browser\Tests\Fixture\CustomException - exception thrown');

Assert::that(function() {
$this->browser()
->visit('/exception')
->assertSee('foo')
;
})->throws(AssertionFailedError::class, 'The last request threw an exception: Zenstruck\Browser\Tests\Fixture\CustomException - exception thrown');
}

protected static function catchFileContents(string $expectedFile, callable $callback): string
{
(new Filesystem())->remove($expectedFile);
Expand Down
10 changes: 10 additions & 0 deletions tests/Fixture/CustomException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Zenstruck\Browser\Tests\Fixture;

/**
* @author Kevin Bond <[email protected]>
*/
final class CustomException extends \RuntimeException
{
}
2 changes: 1 addition & 1 deletion tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function json(Request $request): JsonResponse

public function exception(): void
{
throw new \Exception('exception thrown');
throw new CustomException('exception thrown');
}

public function redirect1(): RedirectResponse
Expand Down

0 comments on commit 2afbcca

Please sign in to comment.