From be55901aa20c2b604b2f10a2248e26cb8927e540 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Sat, 27 Feb 2021 16:57:24 +0100 Subject: [PATCH] Revert most of "[feature] Split HasBrowser trait (#23)" This reverts commit df4430dbc66c283664adf93bf55d6115cbb6452f. Only the renaming of kernelBrowser() to browser() is kept. --- README.md | 30 +++----- src/Browser/Test/HasBrowser.php | 102 +++++++++++++++++++++++++ src/Browser/Test/HasHttpBrowser.php | 77 ------------------- src/Browser/Test/HasPantherBrowser.php | 54 ------------- tests/BrowserTests.php | 5 ++ tests/ConfigureBrowserTest.php | 2 +- tests/HttpBrowserTest.php | 3 +- tests/KernelBrowserTests.php | 8 +- tests/NonPantherHttpBrowserTest.php | 3 +- tests/PantherBrowserTest.php | 3 +- 10 files changed, 128 insertions(+), 159 deletions(-) delete mode 100644 src/Browser/Test/HasHttpBrowser.php delete mode 100644 src/Browser/Test/HasPantherBrowser.php diff --git a/README.md b/README.md index 688cc4b..e2fa1a9 100644 --- a/README.md +++ b/README.md @@ -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') @@ -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') @@ -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 @@ -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 @@ -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') @@ -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 { @@ -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') diff --git a/src/Browser/Test/HasBrowser.php b/src/Browser/Test/HasBrowser.php index e8e9093..0e8ef39 100644 --- a/src/Browser/Test/HasBrowser.php +++ b/src/Browser/Test/HasBrowser.php @@ -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 */ 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) { diff --git a/src/Browser/Test/HasHttpBrowser.php b/src/Browser/Test/HasHttpBrowser.php deleted file mode 100644 index 6714e98..0000000 --- a/src/Browser/Test/HasHttpBrowser.php +++ /dev/null @@ -1,77 +0,0 @@ -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') - ; - } -} diff --git a/src/Browser/Test/HasPantherBrowser.php b/src/Browser/Test/HasPantherBrowser.php deleted file mode 100644 index cab2dd6..0000000 --- a/src/Browser/Test/HasPantherBrowser.php +++ /dev/null @@ -1,54 +0,0 @@ - $_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') - ; - } -} diff --git a/tests/BrowserTests.php b/tests/BrowserTests.php index 4db3f5e..e39d171 100644 --- a/tests/BrowserTests.php +++ b/tests/BrowserTests.php @@ -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; @@ -14,6 +15,10 @@ */ trait BrowserTests { + use HasBrowser { + browser as kernelBrowser; + } + /** * @test */ diff --git a/tests/ConfigureBrowserTest.php b/tests/ConfigureBrowserTest.php index 7de6886..f9f6cfe 100644 --- a/tests/ConfigureBrowserTest.php +++ b/tests/ConfigureBrowserTest.php @@ -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'); } diff --git a/tests/HttpBrowserTest.php b/tests/HttpBrowserTest.php index efcab44..d7f2290 100644 --- a/tests/HttpBrowserTest.php +++ b/tests/HttpBrowserTest.php @@ -4,14 +4,13 @@ use Symfony\Component\Panther\PantherTestCase; use Zenstruck\Browser\HttpBrowser; -use Zenstruck\Browser\Test\HasHttpBrowser; /** * @author Kevin Bond */ final class HttpBrowserTest extends PantherTestCase { - use BrowserKitBrowserTests, HasHttpBrowser; + use BrowserKitBrowserTests; /** * @test diff --git a/tests/KernelBrowserTests.php b/tests/KernelBrowserTests.php index b3e5b7e..3a6f9bf 100644 --- a/tests/KernelBrowserTests.php +++ b/tests/KernelBrowserTests.php @@ -5,14 +5,13 @@ use Symfony\Bundle\FrameworkBundle\KernelBrowser as SymfonyKernelBrowser; use Symfony\Component\Security\Core\User\User; use Zenstruck\Browser\KernelBrowser; -use Zenstruck\Browser\Test\HasBrowser; /** * @author Kevin Bond */ trait KernelBrowserTests { - use BrowserKitBrowserTests, HasBrowser; + use BrowserKitBrowserTests; /** * @test @@ -93,4 +92,9 @@ public function can_enable_the_profiler(): void $this->assertTrue($profile->hasCollector('request')); } + + protected function browser(): KernelBrowser + { + return $this->kernelBrowser(); + } } diff --git a/tests/NonPantherHttpBrowserTest.php b/tests/NonPantherHttpBrowserTest.php index a7eb936..81df6ab 100644 --- a/tests/NonPantherHttpBrowserTest.php +++ b/tests/NonPantherHttpBrowserTest.php @@ -4,14 +4,13 @@ use PHPUnit\Framework\TestCase; use Zenstruck\Browser\Test\HasBrowser; -use Zenstruck\Browser\Test\HasHttpBrowser; /** * @author Kevin Bond */ final class NonPantherHttpBrowserTest extends TestCase { - use HasBrowser, HasHttpBrowser; + use HasBrowser; protected function setUp(): void { diff --git a/tests/PantherBrowserTest.php b/tests/PantherBrowserTest.php index 5c3a36e..d30fc3a 100644 --- a/tests/PantherBrowserTest.php +++ b/tests/PantherBrowserTest.php @@ -5,7 +5,6 @@ use PHPUnit\Framework\AssertionFailedError; use Symfony\Component\Panther\PantherTestCase; use Zenstruck\Browser\PantherBrowser; -use Zenstruck\Browser\Test\HasPantherBrowser; /** * @author Kevin Bond @@ -14,7 +13,7 @@ */ final class PantherBrowserTest extends PantherTestCase { - use BrowserTests, HasPantherBrowser; + use BrowserTests; /** * @test