From b13cdba4d039378cf36ca75b5cfe34c05d79ad2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 22 Aug 2019 00:26:46 +0200 Subject: [PATCH] Mercure and WebSocket testing: allow to create several browser instances --- src/Client.php | 14 +++++++++--- src/PantherTestCaseTrait.php | 41 ++++++++++++++++++++++++++++++------ tests/ClientTest.php | 2 +- tests/MultiClientsTest.php | 33 +++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 tests/MultiClientsTest.php diff --git a/src/Client.php b/src/Client.php index f7f26274..c9abf46d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -71,6 +71,11 @@ public function __construct(BrowserManagerInterface $browserManager, ?string $ba $this->baseUri = $baseUri; } + public function getBrowserManager(): BrowserManagerInterface + { + return $this->browserManager; + } + public function __destruct() { $this->quit(); @@ -242,7 +247,7 @@ public function restart() $this->webDriver->manage()->deleteAllCookies(); } - $this->quit(); + $this->quit(false); $this->start(); } @@ -334,13 +339,16 @@ public function getWindowHandles() return $this->webDriver->getWindowHandles(); } - public function quit() + public function quit(bool $quitBrowserManager = true) { if (null !== $this->webDriver) { $this->webDriver->quit(); $this->webDriver = null; } - $this->browserManager->quit(); + + if ($quitBrowserManager) { + $this->browserManager->quit(); + } } public function takeScreenshot($saveAs = null) diff --git a/src/PantherTestCaseTrait.php b/src/PantherTestCaseTrait.php index 4f61cee0..17be93e0 100644 --- a/src/PantherTestCaseTrait.php +++ b/src/PantherTestCaseTrait.php @@ -52,10 +52,15 @@ trait PantherTestCaseTrait protected static $goutteClient; /** - * @var PantherClient|null + * @var PantherClient|null The primary Panther client instance created */ protected static $pantherClient; + /** + * @var PantherClient[] All Panther clients, the first one is the primary one (aka self::$pantherClient) + */ + protected static $pantherClients = []; + /** * @var array */ @@ -83,8 +88,14 @@ public static function stopWebServer() } if (null !== self::$pantherClient) { - self::$pantherClient->quit(); + foreach (self::$pantherClients as $i => $pantherClient) { + // Stop ChromeDriver only when all sessions are already closed + $pantherClient->quit(false); + } + + self::$pantherClient->getBrowserManager()->quit(); self::$pantherClient = null; + self::$pantherClients = []; } if (null !== self::$goutteClient) { @@ -129,16 +140,20 @@ public static function isWebServerStarted() } /** - * @param array $options see {@see $defaultOptions} - * @param array $kernelOptions + * Creates the primary browser. + * + * @param array $options see {@see $defaultOptions} */ protected static function createPantherClient(array $options = [], array $kernelOptions = []): PantherClient { - self::startWebServer($options); - if (null === self::$pantherClient) { - self::$pantherClient = Client::createChromeClient(null, null, [], self::$baseUri); + if (null !== self::$pantherClient) { + return self::$pantherClient; } + self::startWebServer($options); + + self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, null, [], self::$baseUri); + if (\is_a(self::class, KernelTestCase::class, true)) { static::bootKernel($kernelOptions); } @@ -150,6 +165,18 @@ protected static function createPantherClient(array $options = [], array $kernel return self::$pantherClient; } + /** + * Creates an additional browser. Convenient to test apps leveraging Mercure or WebSocket (e.g. a chat). + */ + protected static function createAdditionalPantherClient(): PantherClient + { + if (null === self::$pantherClient) { + return self::createPantherClient(); + } + + return self::$pantherClients[] = self::$pantherClient = new PantherClient(self::$pantherClient->getBrowserManager(), self::$baseUri); + } + /** * @param array $options see {@see $defaultOptions} * @param array $kernelOptions diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 240b4fc7..7fbec64b 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -265,7 +265,7 @@ public function testCookie(callable $clientFactory, string $type) public function testServerPort(callable $clientFactory): void { $expectedPort = $_SERVER['PANTHER_WEB_SERVER_PORT'] ?? '9080'; - $client = $clientFactory(); + $clientFactory(); $this->assertEquals($expectedPort, \mb_substr(self::$baseUri, -4)); } } diff --git a/tests/MultiClientsTest.php b/tests/MultiClientsTest.php new file mode 100644 index 00000000..e41a112a --- /dev/null +++ b/tests/MultiClientsTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Symfony\Component\Panther\Tests; + +class MultiClientsTest extends TestCase +{ + public function testMultiClient(): void + { + $client = self::createPantherClient(); + $client->request('GET', '/cookie.php'); + + $crawler = $client->request('GET', '/cookie.php'); + $this->assertSame('1', $crawler->filter('#barcelona')->text()); + + $client2 = self::createAdditionalPantherClient(); + $crawler2 = $client2->request('GET', '/cookie.php'); + $this->assertSame('0', $crawler2->filter('#barcelona')->text()); + + // Check that the cookie in the other client hasn't changed + $this->assertSame('1', $crawler->filter('#barcelona')->text()); + } +}