From da4a6e4dca642051206e1cf892d7c6d5fe8fcad2 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Wed, 6 Apr 2022 18:45:28 -0400 Subject: [PATCH] [feature] add `BROWSER_FOLLOW_REDIRECTS/CATCH_EXCEPTIONS` env vars (#79) --- README.md | 79 +++++++++---------- phpunit.xml.dist | 2 +- src/Browser/KernelBrowser.php | 3 + .../Session/Driver/BrowserKitDriver.php | 8 -- src/Browser/Test/HasBrowser.php | 2 + 5 files changed, 42 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 70be1d6..78caf12 100644 --- a/README.md +++ b/README.md @@ -74,14 +74,16 @@ This extension provides the following features: There are several environment variables available to configure: -| Variable | Description | Default | -|---------------------------|---------------------------------------------------------------------|-------------------------------------------| -| `BROWSER_SOURCE_DIR` | Directory to save source files to. | `/var/browser/source` | -| `BROWSER_SCREENSHOT_DIR` | Directory to save screenshots to. | `/var/browser/screenshots` | -| `BROWSER_CONSOLE_LOG_DIR` | Directory to save javascript console logs to. | `/var/browser/console-logs` | -| `KERNEL_BROWSER_CLASS` | `KernelBrowser` class to use. | `Zenstruck\Browser\KernelBrowser` | -| `PANTHER_BROWSER_CLASS` | `PantherBrowser` class to use. | `Zenstruck\Browser\PantherBrowser` | -| `PANTHER_NO_HEADLESS` | Disable headless-mode and allow usage of `PantherBrowser::pause()`. | `0` | +| Variable | Description | Default | +|----------------------------|----------------------------------------------------------------------------------|------------------------------------| +| `BROWSER_SOURCE_DIR` | Directory to save source files to. | `./var/browser/source` | +| `BROWSER_SCREENSHOT_DIR` | Directory to save screenshots to (only applies to `PantherBrowser`). | `./var/browser/screenshots` | +| `BROWSER_CONSOLE_LOG_DIR` | Directory to save javascript console logs to (only applies to `PantherBrowser`). | `./var/browser/console-logs` | +| `BROWSER_FOLLOW_REDIRECTS` | Whether to follow redirects by default (only applies to `KernelBrowser`). | `1` _(true)_ | +| `BROWSER_CATCH_EXCEPTIONS` | Whether to catch exceptions by default (only applies to `KernelBrowser`). | `1` _(true)_ | +| `KERNEL_BROWSER_CLASS` | `KernelBrowser` class to use. | `Zenstruck\Browser\KernelBrowser` | +| `PANTHER_BROWSER_CLASS` | `PantherBrowser` class to use. | `Zenstruck\Browser\PantherBrowser` | +| `PANTHER_NO_HEADLESS` | Disable headless-mode and allow usage of `PantherBrowser::pause()`. | `0` _(false)_ | ## Usage @@ -245,10 +247,35 @@ $browser ->assertXml() ->assertHtml() + // authenticate a user for subsequent actions + ->actingAs($user) // \Symfony\Component\Security\Core\User\UserInterface + + // If using zenstruck/foundry, you can pass a factory/proxy + ->actingAs(UserFactory::new()) + + // by default, exceptions are caught and converted to a response + // use the BROWSER_CATCH_EXCEPTIONS environment variable to change default + // this disables that behaviour allowing you to use TestCase::expectException() + ->throwExceptions() + + // enable catching exceptions + ->catchExceptions() + + // by default, the kernel is rebooted between requests + // this disables this behaviour + ->disableReboot() + + // re-enable rebooting between requests if previously disabled + ->enableReboot() + + // enable the profiler for the next request (if not globally enabled) + ->withProfiling() + // by default, redirects are followed, this disables that behaviour + // use the BROWSER_FOLLOW_REDIRECTS environment variable to change default ->interceptRedirects() - // re-enable following redirects by default + // enable following redirects // if currently on a redirect response, follows ->followRedirects() @@ -367,40 +394,6 @@ $json = $browser ; ``` -### KernelBrowser - -This browser has the following extra methods: - -```php -/** @var \Zenstruck\Browser\KernelBrowser $browser **/ -/** @var \Symfony\Component\Security\Core\User\UserInterface $user **/ - -$browser - // authenticate a user for subsequent actions - ->actingAs($user) - - // If using zenstruck/foundry, you can pass a factory/proxy - ->actingAs(UserFactory::new()) - - // by default, exceptions are caught and converted to a response - // this disables that behaviour allowing you to use TestCase::expectException() - ->throwExceptions() - - // re-enable catching exceptions - ->catchExceptions() - - // by default, the kernel is rebooted between requests - // this disables this behaviour - ->disableReboot() - - // re-enable rebooting between requests if previously disabled - ->enableReboot() - - // enable the profiler for the next request (if not globally enabled) - ->withProfiling() -; -``` - ### PantherBrowser *The `PantherBrowser` is experimental in 1.0 and may be subject to BC Breaks.* diff --git a/phpunit.xml.dist b/phpunit.xml.dist index dbb40d5..8ae7624 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,7 +2,7 @@ followRedirects((bool) ($options['follow_redirects'] ?? true)); + $client->catchExceptions((bool) ($options['catch_exceptions'] ?? true)); + parent::__construct(new BrowserKitDriver($client), $options); } diff --git a/src/Browser/Session/Driver/BrowserKitDriver.php b/src/Browser/Session/Driver/BrowserKitDriver.php index cdc9391..2f76376 100644 --- a/src/Browser/Session/Driver/BrowserKitDriver.php +++ b/src/Browser/Session/Driver/BrowserKitDriver.php @@ -13,7 +13,6 @@ use Behat\Mink\Exception\DriverException; use Behat\Mink\Exception\UnsupportedDriverActionException; use Symfony\Bundle\FrameworkBundle\KernelBrowser; -use Symfony\Component\BrowserKit\AbstractBrowser; use Symfony\Component\BrowserKit\Cookie; use Symfony\Component\BrowserKit\Exception\BadMethodCallException; use Symfony\Component\BrowserKit\Response; @@ -51,13 +50,6 @@ final class BrowserKitDriver extends Driver private $expectedException; private ?string $expectedExceptionMessage = null; - public function __construct(AbstractBrowser $client) - { - $client->followRedirects(true); - - parent::__construct($client); - } - public function expectException($expectedException, ?string $expectedMessage = null): void { $this->expectedException = $expectedException; diff --git a/src/Browser/Test/HasBrowser.php b/src/Browser/Test/HasBrowser.php index 3591152..66fb224 100644 --- a/src/Browser/Test/HasBrowser.php +++ b/src/Browser/Test/HasBrowser.php @@ -85,6 +85,8 @@ protected function browser(array $options = [], array $server = []): KernelBrows $browserOptions = [ 'source_dir' => $_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source', + 'follow_redirects' => (bool) ($_SERVER['BROWSER_FOLLOW_REDIRECTS'] ?? true), + 'catch_exceptions' => (bool) ($_SERVER['BROWSER_CATCH_EXCEPTIONS'] ?? true), ]; if ($this instanceof WebTestCase) {