Skip to content

Commit 6175462

Browse files
authored
feat: add BROWSER_ALWAYS_START_WEBSERVER env var (#156)
1 parent 49881ed commit 6175462

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

README.md

+27-17
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ $browser
407407
Make assertions about json responses using [JMESPath expressions](https://jmespath.org/)
408408
See the [JMESPath Tutorials](https://jmespath.org/tutorial.html) to learn more.
409409

410-
> **Note**
410+
> [!NOTE]
411411
> `mtdowling/jmespath.php` is required: `composer require --dev mtdowling/jmespath.php`.
412412
413413
```php
@@ -453,13 +453,21 @@ $json = $browser
453453
;
454454
```
455455

456-
> **Note**
456+
> [!NOTE]
457457
> See the [full `zenstruck/assert` expectation API documentation](https://github.com/zenstruck/assert#expectation-api)
458458
> to see all the methods available on `Zenstruck\Browser\Json`.
459459
460460
### PantherBrowser
461461

462-
*The `PantherBrowser` is experimental in 1.0 and may be subject to BC Breaks.*
462+
> [!NOTE]
463+
> The `PantherBrowser` is experimental in 1.0 and may be subject to BC Breaks.
464+
465+
> [!TIP]
466+
> By default, Panther will not start a web server if it detects one already running
467+
> with the Symfony CLI. This is likely running in your `dev` environment and will cause
468+
> unexpected test failures. Set the env variable `BROWSER_ALWAYS_START_WEBSERVER=1`
469+
> to always start a webserver configured for your current test env when running
470+
> Panther tests.
463471
464472
This browser has the following extra methods:
465473

@@ -543,17 +551,18 @@ class MyTest extends PantherTestCase
543551

544552
There are several environment variables available to configure:
545553

546-
| Variable | Description | Default |
547-
|----------------------------|--------------------------------------------------------------------------------------------|------------------------------------|
548-
| `BROWSER_SOURCE_DIR` | Directory to save source files to. | `./var/browser/source` |
549-
| `BROWSER_SCREENSHOT_DIR` | Directory to save screenshots to (only applies to `PantherBrowser`). | `./var/browser/screenshots` |
550-
| `BROWSER_CONSOLE_LOG_DIR` | Directory to save javascript console logs to (only applies to `PantherBrowser`). | `./var/browser/console-logs` |
551-
| `BROWSER_FOLLOW_REDIRECTS` | Whether to follow redirects by default (only applies to `KernelBrowser`). | `1` _(true)_ |
552-
| `BROWSER_CATCH_EXCEPTIONS` | Whether to catch exceptions by default (only applies to `KernelBrowser`). | `1` _(true)_ |
553-
| `BROWSER_SOURCE_DEBUG` | Whether to add request metadata to written source files (only applies to `KernelBrowser`). | `0` _(false)_ |
554-
| `KERNEL_BROWSER_CLASS` | `KernelBrowser` class to use. | `Zenstruck\Browser\KernelBrowser` |
555-
| `PANTHER_BROWSER_CLASS` | `PantherBrowser` class to use. | `Zenstruck\Browser\PantherBrowser` |
556-
| `PANTHER_NO_HEADLESS` | Disable headless-mode and allow usage of `PantherBrowser::pause()`. | `0` _(false)_ |
554+
| Variable | Description | Default |
555+
|----------------------------------|------------------------------------------------------------------------------------------------------------------------|------------------------------------|
556+
| `BROWSER_SOURCE_DIR` | Directory to save source files to. | `./var/browser/source` |
557+
| `BROWSER_SCREENSHOT_DIR` | Directory to save screenshots to (only applies to `PantherBrowser`). | `./var/browser/screenshots` |
558+
| `BROWSER_CONSOLE_LOG_DIR` | Directory to save javascript console logs to (only applies to `PantherBrowser`). | `./var/browser/console-logs` |
559+
| `BROWSER_FOLLOW_REDIRECTS` | Whether to follow redirects by default (only applies to `KernelBrowser`). | `1` _(true)_ |
560+
| `BROWSER_CATCH_EXCEPTIONS` | Whether to catch exceptions by default (only applies to `KernelBrowser`). | `1` _(true)_ |
561+
| `BROWSER_SOURCE_DEBUG` | Whether to add request metadata to written source files (only applies to `KernelBrowser`). | `0` _(false)_ |
562+
| `KERNEL_BROWSER_CLASS` | `KernelBrowser` class to use. | `Zenstruck\Browser\KernelBrowser` |
563+
| `PANTHER_BROWSER_CLASS` | `PantherBrowser` class to use. | `Zenstruck\Browser\PantherBrowser` |
564+
| `PANTHER_NO_HEADLESS` | Disable headless-mode and allow usage of `PantherBrowser::pause()`. | `0` _(false)_ |
565+
| `BROWSER_ALWAYS_START_WEBSERVER` | Always start a webserver configured for your current test env before running tests (only applies to `PantherBrowser`). | `0` _(false)_ |
557566

558567
## Extending
559568

@@ -818,9 +827,10 @@ Then, depending on the implementation you extended from, set the appropriate env
818827

819828
For the example above, you would set `KERNEL_BROWSER_CLASS=App\Tests\AppBrowser`.
820829

821-
**TIP**: Create a base functional test case so all your tests can use your
822-
custom browser and use the `@method` annotation to ensure your tests can
823-
autocomplete your custom methods:
830+
> [!TIP]
831+
> Create a base functional test case so all your tests can use your
832+
> custom browser and use the `@method` annotation to ensure your tests can
833+
> autocomplete your custom methods:
824834
825835
```php
826836
namespace App\Tests;

src/Browser/Test/HasBrowser.php

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ protected function pantherBrowser(array $options = [], array $kernelOptions = []
6464
'console_log_dir' => $_SERVER['BROWSER_CONSOLE_LOG_DIR'] ?? './var/browser/console-logs',
6565
];
6666

67+
if ($_SERVER['BROWSER_ALWAYS_START_WEBSERVER'] ?? null) {
68+
$_SERVER['PANTHER_APP_ENV'] = $_SERVER['APP_ENV'] ?? 'test'; // use current environment
69+
$_SERVER['SYMFONY_PROJECT_DEFAULT_ROUTE_URL'] = ''; // ignore existing server running with Symfony CLI
70+
}
71+
6772
if (self::$primaryPantherClient) {
6873
$browser = new $class(static::createAdditionalPantherClient(), $browserOptions);
6974
} else {

0 commit comments

Comments
 (0)