Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function testViewPostAndAddComment()
{
// assumes a "Post" is in the database with an id of 3

$this->kernelBrowser()
$this->browser()
->visit('/posts/3')
->assertSuccessful()
->assertSeeIn('title', 'My First Post')
Expand All @@ -34,7 +34,7 @@ public function testViewPostAndAddComment()
{
$post = PostFactory::new()->create(['title' => 'My First Post']);

$this->kernelBrowser()
$this->browser()
->visit("/posts/{$post->getId()}")
->assertSuccessful()
->assertSeeIn('title', 'My First Post')
Expand Down Expand Up @@ -91,30 +91,22 @@ There are several environment variables available to configure:

This library provides 3 different "browsers":

1. [KernelBrowser](#kernelbrowser): makes requests using your Symfony Kernel, like [the Symfony BrowserKit component](https://symfony.com/doc/current/components/browser_kit.html)
*(this is the fastest browser)*.
1. [KernelBrowser](#kernelbrowser): makes requests using your Symfony Kernel *(this is the fastest browser)*.
2. [HttpBrowser](#httpbrowser): makes requests to a webserver using `symfony/http-client`.
3. [PantherBrowser](#pantherbrowser): makes requests to a webserver with a real browser using `symfony/panther` which
allows testing javascript *(this is the slowest browser)*.

You can use these Browsers in your tests using traits:
You can use these Browsers in your tests by having your test class use the `HasBrowser` trait:

```php
namespace App\Tests;

use PHPUnit\Framework\TestCase;
use Zenstruck\Browser\Test\HasBrowser;
use Zenstruck\Browser\Test\HasHttpBrowser;
use Zenstruck\Browser\Test\HasPantherBrowser;

class MyTest extends TestCase
{
// provides a browser() method that returns the KernelBrowser
use HasBrowser;
// provides httpBrowser()
use HasHttpBrowser;
// provides pantherBrowser()
use HasPantherBrowser;

/**
* Requires this test extend either Symfony\Bundle\FrameworkBundle\Test\KernelTestCase
Expand Down Expand Up @@ -475,17 +467,17 @@ use Zenstruck\Browser\Test\HasBrowser;
class MyTest extends KernelTestCase
{
use HasBrowser {
kernelBrowser as baseKernelBrowser;
browser as baseKernelBrowser;
}

public function testDemo(): void
{
$this->kernelBrowser()
$this->browser()
->assertOn('/') // browser always starts on the homepage (as defined below)
;
}

protected function kernelBrowser(): KernelBrowser
protected function browser(): KernelBrowser
{
return $this->baseKernelBrowser()
->interceptRedirects() // always intercept redirects
Expand Down Expand Up @@ -616,12 +608,12 @@ If you find yourself creating a lot of [http requests](#http-requests) with the
class MyTest extends KernelTestCase
{
use HasBrowser {
kernelBrowser as baseKernelBrowser;
browser as baseKernelBrowser;
}

public function testDemo(): void
{
$this->kernelBrowser()
$this->browser()
// all http requests in this test class will have the X-Token header
->get('/endpoint')

Expand Down Expand Up @@ -714,7 +706,7 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Zenstruck\Browser\Test\HasBrowser;

/**
* @method AppBrowser kernelBrowser()
* @method AppBrowser browser()
*/
abstract class MyTest extends WebTestCase
{
Expand Down Expand Up @@ -756,7 +748,7 @@ Use in your tests:
```php
public function testDemo(): void
{
$this->kernelBrowser()
$this->browser()
// goes to the /login page, fills email/password fields,
// and presses the Login button
->loginAs('kevin@example.com', 'password')
Expand Down
102 changes: 102 additions & 0 deletions src/Browser/Test/HasBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,115 @@

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\HttpBrowser as HttpBrowserClient;
use Symfony\Component\Panther\Client as PantherClient;
use Symfony\Component\Panther\PantherTestCase;
use Zenstruck\Browser\HttpBrowser;
use Zenstruck\Browser\KernelBrowser;
use Zenstruck\Browser\PantherBrowser;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
trait HasBrowser
{
/** @var HttpBrowserClient[] */
private static array $httpBrowserClients = [];
private static ?PantherClient $primaryPantherClient = null;

/**
* @internal
* @after
*/
final public static function _resetBrowserClients(): void
{
self::$httpBrowserClients = [];
self::$primaryPantherClient = null;
}

protected function pantherBrowser(array $options = [], array $kernelOptions = [], array $managerOptions = []): PantherBrowser
{
$class = $_SERVER['PANTHER_BROWSER_CLASS'] ?? PantherBrowser::class;

if (!\is_a($class, PantherBrowser::class, true)) {
throw new \RuntimeException(\sprintf('"PANTHER_BROWSER_CLASS" env variable must reference a class that extends %s.', PantherBrowser::class));
}

if (self::$primaryPantherClient) {
$browser = new $class(static::createAdditionalPantherClient());
} else {
self::$primaryPantherClient = static::createPantherClient(
\array_merge(['browser' => $_SERVER['PANTHER_BROWSER'] ?? static::CHROME], $options),
$kernelOptions,
$managerOptions
);

$browser = new $class(self::$primaryPantherClient);
}

BrowserExtension::registerBrowser($browser);

return $browser
->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
->setScreenshotDir($_SERVER['BROWSER_SCREENSHOT_DIR'] ?? './var/browser/screenshots')
->setConsoleLogDir($_SERVER['BROWSER_CONSOLE_LOG_DIR'] ?? './var/browser/console-logs')
;
}

protected function httpBrowser(array $kernelOptions = [], array $pantherOptions = []): HttpBrowser
{
$class = $_SERVER['HTTP_BROWSER_CLASS'] ?? HttpBrowser::class;

if (!\is_a($class, HttpBrowser::class, true)) {
throw new \RuntimeException(\sprintf('"HTTP_BROWSER_CLASS" env variable must reference a class that extends %s.', HttpBrowser::class));
}

$baseUri = $_SERVER['HTTP_BROWSER_URI'] ?? null;

if (!$baseUri && !$this instanceof PantherTestCase) {
throw new \RuntimeException(\sprintf('If not using "HTTP_BROWSER_URI", your TestCase must extend "%s".', PantherTestCase::class));
}

if (!$baseUri) {
self::startWebServer($pantherOptions);

$baseUri = self::$baseUri;
}

// copied from PantherTestCaseTrait::createHttpBrowserClient()
$client = new HttpBrowserClient();
$urlComponents = \parse_url($baseUri);
$host = $urlComponents['host'];

if (isset($urlComponents['port'])) {
$host .= ":{$urlComponents['port']}";
}

$client->setServerParameter('HTTP_HOST', $host);

if ('https' === ($urlComponents['scheme'] ?? 'http')) {
$client->setServerParameter('HTTPS', 'true');
}

$browser = new $class(self::$httpBrowserClients[] = $client);

if ($this instanceof KernelTestCase) {
if (!static::$booted) {
static::bootKernel($kernelOptions);
}

if (static::$container->has('profiler')) {
$browser->setProfiler(static::$container->get('profiler'));
}
}

BrowserExtension::registerBrowser($browser);

return $browser
->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
;
}

protected function browser(array $options = []): KernelBrowser
{
if (!$this instanceof KernelTestCase) {
Expand Down
77 changes: 0 additions & 77 deletions src/Browser/Test/HasHttpBrowser.php

This file was deleted.

54 changes: 0 additions & 54 deletions src/Browser/Test/HasPantherBrowser.php

This file was deleted.

5 changes: 5 additions & 0 deletions tests/BrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\VarDumper\VarDumper;
use Zenstruck\Browser;
use Zenstruck\Browser\Test\HasBrowser;
use Zenstruck\Browser\Tests\Fixture\TestComponent1;
use Zenstruck\Browser\Tests\Fixture\TestComponent2;
use Zenstruck\Callback\Exception\UnresolveableArgument;
Expand All @@ -14,6 +15,10 @@
*/
trait BrowserTests
{
use HasBrowser {
browser as kernelBrowser;
}

/**
* @test
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/ConfigureBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function browser_has_been_configured(): void
$this->page1Browser()->assertOn('/page1');
}

public function page1Browser(): KernelBrowser
protected function page1Browser(): KernelBrowser
{
return $this->browser()->visit('/page1');
}
Expand Down
3 changes: 1 addition & 2 deletions tests/HttpBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

use Symfony\Component\Panther\PantherTestCase;
use Zenstruck\Browser\HttpBrowser;
use Zenstruck\Browser\Test\HasHttpBrowser;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class HttpBrowserTest extends PantherTestCase
{
use BrowserKitBrowserTests, HasHttpBrowser;
use BrowserKitBrowserTests;

/**
* @test
Expand Down
Loading