Skip to content

Commit df4430d

Browse files
authored
[feature] Split HasBrowser trait (#23)
1 parent ff32f91 commit df4430d

10 files changed

+155
-124
lines changed

README.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,38 @@ 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 *(this is the fastest browser)*.
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)*.
9596
2. [HttpBrowser](#httpbrowser): makes requests to a webserver using `symfony/http-client`.
9697
3. [PantherBrowser](#pantherbrowser): makes requests to a webserver with a real browser using `symfony/panther` which
9798
allows testing javascript *(this is the slowest browser)*.
9899

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

101102
```php
102103
namespace App\Tests;
103104

104105
use PHPUnit\Framework\TestCase;
105106
use Zenstruck\Browser\Test\HasBrowser;
107+
use Zenstruck\Browser\Test\HasHttpBrowser;
108+
use Zenstruck\Browser\Test\HasPantherBrowser;
106109

107110
class MyTest extends TestCase
108111
{
112+
// provides a browser() method that returns the KernelBrowser
109113
use HasBrowser;
114+
// provides httpBrowser()
115+
use HasHttpBrowser;
116+
// provides pantherBrowser()
117+
use HasPantherBrowser;
110118

111119
/**
112120
* Requires this test extend either Symfony\Bundle\FrameworkBundle\Test\KernelTestCase
113121
* or Symfony\Bundle\FrameworkBundle\Test\WebTestCase.
114122
*/
115123
public function test_using_kernel_browser(): void
116124
{
117-
$this->kernelBrowser()
125+
$this->browser()
118126
->visit('/my/page')
119127
->assertSuccessful()
120128
;

src/Browser/Test/HasBrowser.php

+1-103
Original file line numberDiff line numberDiff line change
@@ -4,116 +4,14 @@
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;
117
use Zenstruck\Browser\KernelBrowser;
12-
use Zenstruck\Browser\PantherBrowser;
138

149
/**
1510
* @author Kevin Bond <[email protected]>
1611
*/
1712
trait HasBrowser
1813
{
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-
116-
protected function kernelBrowser(array $options = []): KernelBrowser
14+
public function browser(array $options = []): KernelBrowser
11715
{
11816
if (!$this instanceof KernelTestCase) {
11917
throw new \RuntimeException(\sprintf('The "%s" method can only be used on TestCases that extend "%s".', __METHOD__, KernelTestCase::class));

src/Browser/Test/HasHttpBrowser.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Zenstruck\Browser\Test;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6+
use Symfony\Component\BrowserKit\HttpBrowser as HttpBrowserClient;
7+
use Symfony\Component\Panther\PantherTestCase;
8+
use Zenstruck\Browser\HttpBrowser;
9+
10+
trait HasHttpBrowser
11+
{
12+
/** @var HttpBrowserClient[] */
13+
private static array $httpBrowserClients = [];
14+
15+
/**
16+
* @internal
17+
* @after
18+
*/
19+
final public static function _resetHttpBrowserClients(): void
20+
{
21+
self::$httpBrowserClients = [];
22+
}
23+
24+
protected function httpBrowser(array $kernelOptions = [], array $pantherOptions = []): HttpBrowser
25+
{
26+
$class = $_SERVER['HTTP_BROWSER_CLASS'] ?? HttpBrowser::class;
27+
28+
if (!\is_a($class, HttpBrowser::class, true)) {
29+
throw new \RuntimeException(\sprintf('"HTTP_BROWSER_CLASS" env variable must reference a class that extends %s.', HttpBrowser::class));
30+
}
31+
32+
$baseUri = $_SERVER['HTTP_BROWSER_URI'] ?? null;
33+
34+
if (!$baseUri && !$this instanceof PantherTestCase) {
35+
throw new \RuntimeException(\sprintf('If not using "HTTP_BROWSER_URI", your TestCase must extend "%s".', PantherTestCase::class));
36+
}
37+
38+
if (!$baseUri) {
39+
self::startWebServer($pantherOptions);
40+
41+
$baseUri = self::$baseUri;
42+
}
43+
44+
// copied from PantherTestCaseTrait::createHttpBrowserClient()
45+
$client = new HttpBrowserClient();
46+
$urlComponents = \parse_url($baseUri);
47+
$host = $urlComponents['host'];
48+
49+
if (isset($urlComponents['port'])) {
50+
$host .= ":{$urlComponents['port']}";
51+
}
52+
53+
$client->setServerParameter('HTTP_HOST', $host);
54+
55+
if ('https' === ($urlComponents['scheme'] ?? 'http')) {
56+
$client->setServerParameter('HTTPS', 'true');
57+
}
58+
59+
$browser = new $class(self::$httpBrowserClients[] = $client);
60+
61+
if ($this instanceof KernelTestCase) {
62+
if (!static::$booted) {
63+
static::bootKernel($kernelOptions);
64+
}
65+
66+
if (static::$container->has('profiler')) {
67+
$browser->setProfiler(static::$container->get('profiler'));
68+
}
69+
}
70+
71+
BrowserExtension::registerBrowser($browser);
72+
73+
return $browser
74+
->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
75+
;
76+
}
77+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Zenstruck\Browser\Test;
4+
5+
use Symfony\Component\Panther\Client as PantherClient;
6+
use Symfony\Component\Panther\PantherTestCase;
7+
use Zenstruck\Browser\PantherBrowser;
8+
9+
trait HasPantherBrowser
10+
{
11+
private static ?PantherClient $primaryPantherClient = null;
12+
13+
/**
14+
* @internal
15+
* @after
16+
*/
17+
final public static function _resetPantherBrowserClient(): void
18+
{
19+
self::$primaryPantherClient = null;
20+
}
21+
22+
protected function pantherBrowser(array $options = [], array $kernelOptions = [], array $managerOptions = []): PantherBrowser
23+
{
24+
$class = $_SERVER['PANTHER_BROWSER_CLASS'] ?? PantherBrowser::class;
25+
26+
if (!$this instanceof PantherTestCase) {
27+
throw new \RuntimeException(\sprintf('The "%s" method can only be used on TestCases that extend "%s".', __METHOD__, PantherTestCase::class));
28+
}
29+
30+
if (!\is_a($class, PantherBrowser::class, true)) {
31+
throw new \RuntimeException(\sprintf('"PANTHER_BROWSER_CLASS" env variable must reference a class that extends %s.', PantherBrowser::class));
32+
}
33+
34+
if (self::$primaryPantherClient) {
35+
$browser = new $class(static::createAdditionalPantherClient());
36+
} else {
37+
self::$primaryPantherClient = static::createPantherClient(
38+
\array_merge(['browser' => $_SERVER['PANTHER_BROWSER'] ?? static::CHROME], $options),
39+
$kernelOptions,
40+
$managerOptions
41+
);
42+
43+
$browser = new $class(self::$primaryPantherClient);
44+
}
45+
46+
BrowserExtension::registerBrowser($browser);
47+
48+
return $browser
49+
->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
50+
->setScreenshotDir($_SERVER['BROWSER_SCREENSHOT_DIR'] ?? './var/browser/screenshots')
51+
->setConsoleLogDir($_SERVER['BROWSER_CONSOLE_LOG_DIR'] ?? './var/browser/console-logs')
52+
;
53+
}
54+
}

tests/BrowserTests.php

-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Symfony\Component\Filesystem\Filesystem;
66
use Symfony\Component\VarDumper\VarDumper;
77
use Zenstruck\Browser;
8-
use Zenstruck\Browser\Test\HasBrowser;
98
use Zenstruck\Browser\Tests\Fixture\TestComponent1;
109
use Zenstruck\Browser\Tests\Fixture\TestComponent2;
1110
use Zenstruck\Callback\Exception\UnresolveableArgument;
@@ -15,8 +14,6 @@
1514
*/
1615
trait BrowserTests
1716
{
18-
use HasBrowser;
19-
2017
/**
2118
* @test
2219
*/

tests/ConfigureBrowserTest.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@
1111
*/
1212
final class ConfigureBrowserTest extends WebTestCase
1313
{
14-
use HasBrowser {
15-
kernelBrowser as baseKernelBrowser;
16-
}
14+
use HasBrowser;
1715

1816
/**
1917
* @test
2018
*/
2119
public function browser_has_been_configured(): void
2220
{
23-
$this->kernelBrowser()->assertOn('/page1');
21+
$this->page1Browser()->assertOn('/page1');
2422
}
2523

26-
protected function kernelBrowser(): KernelBrowser
24+
public function page1Browser(): KernelBrowser
2725
{
28-
return $this->baseKernelBrowser()->visit('/page1');
26+
return $this->browser()->visit('/page1');
2927
}
3028
}

tests/HttpBrowserTest.php

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

55
use Symfony\Component\Panther\PantherTestCase;
66
use Zenstruck\Browser\HttpBrowser;
7+
use Zenstruck\Browser\Test\HasHttpBrowser;
78

89
/**
910
* @author Kevin Bond <[email protected]>
1011
*/
1112
final class HttpBrowserTest extends PantherTestCase
1213
{
13-
use BrowserKitBrowserTests;
14+
use BrowserKitBrowserTests, HasHttpBrowser;
1415

1516
/**
1617
* @test

tests/KernelBrowserTests.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
use Symfony\Bundle\FrameworkBundle\KernelBrowser as SymfonyKernelBrowser;
66
use Symfony\Component\Security\Core\User\User;
77
use Zenstruck\Browser\KernelBrowser;
8+
use Zenstruck\Browser\Test\HasBrowser;
89

910
/**
1011
* @author Kevin Bond <[email protected]>
1112
*/
1213
trait KernelBrowserTests
1314
{
14-
use BrowserKitBrowserTests;
15+
use BrowserKitBrowserTests, HasBrowser;
1516

1617
/**
1718
* @test
@@ -92,9 +93,4 @@ public function can_enable_the_profiler(): void
9293

9394
$this->assertTrue($profile->hasCollector('request'));
9495
}
95-
96-
protected function browser(): KernelBrowser
97-
{
98-
return $this->kernelBrowser();
99-
}
10096
}

tests/NonPantherHttpBrowserTest.php

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

55
use PHPUnit\Framework\TestCase;
66
use Zenstruck\Browser\Test\HasBrowser;
7+
use Zenstruck\Browser\Test\HasHttpBrowser;
78

89
/**
910
* @author Kevin Bond <[email protected]>
1011
*/
1112
final class NonPantherHttpBrowserTest extends TestCase
1213
{
13-
use HasBrowser;
14+
use HasBrowser, HasHttpBrowser;
1415

1516
protected function setUp(): void
1617
{

0 commit comments

Comments
 (0)