Skip to content

Commit a3460a5

Browse files
authored
Revert most of "[feature] Split HasBrowser trait (#23)" (#32)
This reverts commit df4430d. Only the renaming of kernelBrowser() to browser() is kept.
1 parent 4b8d902 commit a3460a5

10 files changed

+128
-159
lines changed

README.md

+11-19
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testViewPostAndAddComment()
1212
{
1313
// assumes a "Post" is in the database with an id of 3
1414

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

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

9292
This library provides 3 different "browsers":
9393

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

100-
You can use these Browsers in your tests using traits:
99+
You can use these Browsers in your tests by having your test class use the `HasBrowser` trait:
101100

102101
```php
103102
namespace App\Tests;
104103

105104
use PHPUnit\Framework\TestCase;
106105
use Zenstruck\Browser\Test\HasBrowser;
107-
use Zenstruck\Browser\Test\HasHttpBrowser;
108-
use Zenstruck\Browser\Test\HasPantherBrowser;
109106

110107
class MyTest extends TestCase
111108
{
112-
// provides a browser() method that returns the KernelBrowser
113109
use HasBrowser;
114-
// provides httpBrowser()
115-
use HasHttpBrowser;
116-
// provides pantherBrowser()
117-
use HasPantherBrowser;
118110

119111
/**
120112
* Requires this test extend either Symfony\Bundle\FrameworkBundle\Test\KernelTestCase
@@ -475,17 +467,17 @@ use Zenstruck\Browser\Test\HasBrowser;
475467
class MyTest extends KernelTestCase
476468
{
477469
use HasBrowser {
478-
kernelBrowser as baseKernelBrowser;
470+
browser as baseKernelBrowser;
479471
}
480472

481473
public function testDemo(): void
482474
{
483-
$this->kernelBrowser()
475+
$this->browser()
484476
->assertOn('/') // browser always starts on the homepage (as defined below)
485477
;
486478
}
487479

488-
protected function kernelBrowser(): KernelBrowser
480+
protected function browser(): KernelBrowser
489481
{
490482
return $this->baseKernelBrowser()
491483
->interceptRedirects() // always intercept redirects
@@ -616,12 +608,12 @@ If you find yourself creating a lot of [http requests](#http-requests) with the
616608
class MyTest extends KernelTestCase
617609
{
618610
use HasBrowser {
619-
kernelBrowser as baseKernelBrowser;
611+
browser as baseKernelBrowser;
620612
}
621613

622614
public function testDemo(): void
623615
{
624-
$this->kernelBrowser()
616+
$this->browser()
625617
// all http requests in this test class will have the X-Token header
626618
->get('/endpoint')
627619

@@ -714,7 +706,7 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
714706
use Zenstruck\Browser\Test\HasBrowser;
715707

716708
/**
717-
* @method AppBrowser kernelBrowser()
709+
* @method AppBrowser browser()
718710
*/
719711
abstract class MyTest extends WebTestCase
720712
{
@@ -756,7 +748,7 @@ Use in your tests:
756748
```php
757749
public function testDemo(): void
758750
{
759-
$this->kernelBrowser()
751+
$this->browser()
760752
// goes to the /login page, fills email/password fields,
761753
// and presses the Login button
762754
->loginAs('[email protected]', 'password')

src/Browser/Test/HasBrowser.php

+102
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,115 @@
44

55
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
66
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7+
use Symfony\Component\BrowserKit\HttpBrowser as HttpBrowserClient;
8+
use Symfony\Component\Panther\Client as PantherClient;
9+
use Symfony\Component\Panther\PantherTestCase;
10+
use Zenstruck\Browser\HttpBrowser;
711
use Zenstruck\Browser\KernelBrowser;
12+
use Zenstruck\Browser\PantherBrowser;
813

914
/**
1015
* @author Kevin Bond <[email protected]>
1116
*/
1217
trait HasBrowser
1318
{
19+
/** @var HttpBrowserClient[] */
20+
private static array $httpBrowserClients = [];
21+
private static ?PantherClient $primaryPantherClient = null;
22+
23+
/**
24+
* @internal
25+
* @after
26+
*/
27+
final public static function _resetBrowserClients(): void
28+
{
29+
self::$httpBrowserClients = [];
30+
self::$primaryPantherClient = null;
31+
}
32+
33+
protected function pantherBrowser(array $options = [], array $kernelOptions = [], array $managerOptions = []): PantherBrowser
34+
{
35+
$class = $_SERVER['PANTHER_BROWSER_CLASS'] ?? PantherBrowser::class;
36+
37+
if (!\is_a($class, PantherBrowser::class, true)) {
38+
throw new \RuntimeException(\sprintf('"PANTHER_BROWSER_CLASS" env variable must reference a class that extends %s.', PantherBrowser::class));
39+
}
40+
41+
if (self::$primaryPantherClient) {
42+
$browser = new $class(static::createAdditionalPantherClient());
43+
} else {
44+
self::$primaryPantherClient = static::createPantherClient(
45+
\array_merge(['browser' => $_SERVER['PANTHER_BROWSER'] ?? static::CHROME], $options),
46+
$kernelOptions,
47+
$managerOptions
48+
);
49+
50+
$browser = new $class(self::$primaryPantherClient);
51+
}
52+
53+
BrowserExtension::registerBrowser($browser);
54+
55+
return $browser
56+
->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
57+
->setScreenshotDir($_SERVER['BROWSER_SCREENSHOT_DIR'] ?? './var/browser/screenshots')
58+
->setConsoleLogDir($_SERVER['BROWSER_CONSOLE_LOG_DIR'] ?? './var/browser/console-logs')
59+
;
60+
}
61+
62+
protected function httpBrowser(array $kernelOptions = [], array $pantherOptions = []): HttpBrowser
63+
{
64+
$class = $_SERVER['HTTP_BROWSER_CLASS'] ?? HttpBrowser::class;
65+
66+
if (!\is_a($class, HttpBrowser::class, true)) {
67+
throw new \RuntimeException(\sprintf('"HTTP_BROWSER_CLASS" env variable must reference a class that extends %s.', HttpBrowser::class));
68+
}
69+
70+
$baseUri = $_SERVER['HTTP_BROWSER_URI'] ?? null;
71+
72+
if (!$baseUri && !$this instanceof PantherTestCase) {
73+
throw new \RuntimeException(\sprintf('If not using "HTTP_BROWSER_URI", your TestCase must extend "%s".', PantherTestCase::class));
74+
}
75+
76+
if (!$baseUri) {
77+
self::startWebServer($pantherOptions);
78+
79+
$baseUri = self::$baseUri;
80+
}
81+
82+
// copied from PantherTestCaseTrait::createHttpBrowserClient()
83+
$client = new HttpBrowserClient();
84+
$urlComponents = \parse_url($baseUri);
85+
$host = $urlComponents['host'];
86+
87+
if (isset($urlComponents['port'])) {
88+
$host .= ":{$urlComponents['port']}";
89+
}
90+
91+
$client->setServerParameter('HTTP_HOST', $host);
92+
93+
if ('https' === ($urlComponents['scheme'] ?? 'http')) {
94+
$client->setServerParameter('HTTPS', 'true');
95+
}
96+
97+
$browser = new $class(self::$httpBrowserClients[] = $client);
98+
99+
if ($this instanceof KernelTestCase) {
100+
if (!static::$booted) {
101+
static::bootKernel($kernelOptions);
102+
}
103+
104+
if (static::$container->has('profiler')) {
105+
$browser->setProfiler(static::$container->get('profiler'));
106+
}
107+
}
108+
109+
BrowserExtension::registerBrowser($browser);
110+
111+
return $browser
112+
->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
113+
;
114+
}
115+
14116
protected function browser(array $options = []): KernelBrowser
15117
{
16118
if (!$this instanceof KernelTestCase) {

src/Browser/Test/HasHttpBrowser.php

-77
This file was deleted.

src/Browser/Test/HasPantherBrowser.php

-54
This file was deleted.

tests/BrowserTests.php

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Symfony\Component\Filesystem\Filesystem;
66
use Symfony\Component\VarDumper\VarDumper;
77
use Zenstruck\Browser;
8+
use Zenstruck\Browser\Test\HasBrowser;
89
use Zenstruck\Browser\Tests\Fixture\TestComponent1;
910
use Zenstruck\Browser\Tests\Fixture\TestComponent2;
1011
use Zenstruck\Callback\Exception\UnresolveableArgument;
@@ -14,6 +15,10 @@
1415
*/
1516
trait BrowserTests
1617
{
18+
use HasBrowser {
19+
browser as kernelBrowser;
20+
}
21+
1722
/**
1823
* @test
1924
*/

tests/ConfigureBrowserTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function browser_has_been_configured(): void
2121
$this->page1Browser()->assertOn('/page1');
2222
}
2323

24-
public function page1Browser(): KernelBrowser
24+
protected function page1Browser(): KernelBrowser
2525
{
2626
return $this->browser()->visit('/page1');
2727
}

tests/HttpBrowserTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
use Symfony\Component\Panther\PantherTestCase;
66
use Zenstruck\Browser\HttpBrowser;
7-
use Zenstruck\Browser\Test\HasHttpBrowser;
87

98
/**
109
* @author Kevin Bond <[email protected]>
1110
*/
1211
final class HttpBrowserTest extends PantherTestCase
1312
{
14-
use BrowserKitBrowserTests, HasHttpBrowser;
13+
use BrowserKitBrowserTests;
1514

1615
/**
1716
* @test

0 commit comments

Comments
 (0)