Skip to content

Commit

Permalink
[feature] add BROWSER_FOLLOW_REDIRECTS/CATCH_EXCEPTIONS env vars (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Apr 6, 2022
1 parent acefe9d commit da4a6e4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 52 deletions.
79 changes: 36 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. | `<project-root>/var/browser/source` |
| `BROWSER_SCREENSHOT_DIR` | Directory to save screenshots to. | `<project-root>/var/browser/screenshots` |
| `BROWSER_CONSOLE_LOG_DIR` | Directory to save javascript console logs to. | `<project-root>/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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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.*
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/bin/.phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
Expand Down
3 changes: 3 additions & 0 deletions src/Browser/KernelBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class KernelBrowser extends Browser
*/
final public function __construct(SymfonyKernelBrowser $client, array $options = [])
{
$client->followRedirects((bool) ($options['follow_redirects'] ?? true));
$client->catchExceptions((bool) ($options['catch_exceptions'] ?? true));

parent::__construct(new BrowserKitDriver($client), $options);
}

Expand Down
8 changes: 0 additions & 8 deletions src/Browser/Session/Driver/BrowserKitDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Browser/Test/HasBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit da4a6e4

Please sign in to comment.