Skip to content

Commit f5f377f

Browse files
authored
Merge pull request symfony#219 from dunglas/multi-clients
Mercure and WebSocket testing: allow to create several browser instances
2 parents b9225b0 + b13cdba commit f5f377f

File tree

4 files changed

+79
-11
lines changed

4 files changed

+79
-11
lines changed

src/Client.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public function __construct(BrowserManagerInterface $browserManager, ?string $ba
7171
$this->baseUri = $baseUri;
7272
}
7373

74+
public function getBrowserManager(): BrowserManagerInterface
75+
{
76+
return $this->browserManager;
77+
}
78+
7479
public function __destruct()
7580
{
7681
$this->quit();
@@ -242,7 +247,7 @@ public function restart()
242247
$this->webDriver->manage()->deleteAllCookies();
243248
}
244249

245-
$this->quit();
250+
$this->quit(false);
246251
$this->start();
247252
}
248253

@@ -334,13 +339,16 @@ public function getWindowHandles()
334339
return $this->webDriver->getWindowHandles();
335340
}
336341

337-
public function quit()
342+
public function quit(bool $quitBrowserManager = true)
338343
{
339344
if (null !== $this->webDriver) {
340345
$this->webDriver->quit();
341346
$this->webDriver = null;
342347
}
343-
$this->browserManager->quit();
348+
349+
if ($quitBrowserManager) {
350+
$this->browserManager->quit();
351+
}
344352
}
345353

346354
public function takeScreenshot($saveAs = null)

src/PantherTestCaseTrait.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,15 @@ trait PantherTestCaseTrait
5252
protected static $goutteClient;
5353

5454
/**
55-
* @var PantherClient|null
55+
* @var PantherClient|null The primary Panther client instance created
5656
*/
5757
protected static $pantherClient;
5858

59+
/**
60+
* @var PantherClient[] All Panther clients, the first one is the primary one (aka self::$pantherClient)
61+
*/
62+
protected static $pantherClients = [];
63+
5964
/**
6065
* @var array
6166
*/
@@ -83,8 +88,14 @@ public static function stopWebServer()
8388
}
8489

8590
if (null !== self::$pantherClient) {
86-
self::$pantherClient->quit();
91+
foreach (self::$pantherClients as $i => $pantherClient) {
92+
// Stop ChromeDriver only when all sessions are already closed
93+
$pantherClient->quit(false);
94+
}
95+
96+
self::$pantherClient->getBrowserManager()->quit();
8797
self::$pantherClient = null;
98+
self::$pantherClients = [];
8899
}
89100

90101
if (null !== self::$goutteClient) {
@@ -129,16 +140,20 @@ public static function isWebServerStarted()
129140
}
130141

131142
/**
132-
* @param array $options see {@see $defaultOptions}
133-
* @param array $kernelOptions
143+
* Creates the primary browser.
144+
*
145+
* @param array $options see {@see $defaultOptions}
134146
*/
135147
protected static function createPantherClient(array $options = [], array $kernelOptions = []): PantherClient
136148
{
137-
self::startWebServer($options);
138-
if (null === self::$pantherClient) {
139-
self::$pantherClient = Client::createChromeClient(null, null, [], self::$baseUri);
149+
if (null !== self::$pantherClient) {
150+
return self::$pantherClient;
140151
}
141152

153+
self::startWebServer($options);
154+
155+
self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, null, [], self::$baseUri);
156+
142157
if (\is_a(self::class, KernelTestCase::class, true)) {
143158
static::bootKernel($kernelOptions);
144159
}
@@ -150,6 +165,18 @@ protected static function createPantherClient(array $options = [], array $kernel
150165
return self::$pantherClient;
151166
}
152167

168+
/**
169+
* Creates an additional browser. Convenient to test apps leveraging Mercure or WebSocket (e.g. a chat).
170+
*/
171+
protected static function createAdditionalPantherClient(): PantherClient
172+
{
173+
if (null === self::$pantherClient) {
174+
return self::createPantherClient();
175+
}
176+
177+
return self::$pantherClients[] = self::$pantherClient = new PantherClient(self::$pantherClient->getBrowserManager(), self::$baseUri);
178+
}
179+
153180
/**
154181
* @param array $options see {@see $defaultOptions}
155182
* @param array $kernelOptions

tests/ClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function testCookie(callable $clientFactory, string $type)
265265
public function testServerPort(callable $clientFactory): void
266266
{
267267
$expectedPort = $_SERVER['PANTHER_WEB_SERVER_PORT'] ?? '9080';
268-
$client = $clientFactory();
268+
$clientFactory();
269269
$this->assertEquals($expectedPort, \mb_substr(self::$baseUri, -4));
270270
}
271271
}

tests/MultiClientsTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Panther project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Panther\Tests;
15+
16+
class MultiClientsTest extends TestCase
17+
{
18+
public function testMultiClient(): void
19+
{
20+
$client = self::createPantherClient();
21+
$client->request('GET', '/cookie.php');
22+
23+
$crawler = $client->request('GET', '/cookie.php');
24+
$this->assertSame('1', $crawler->filter('#barcelona')->text());
25+
26+
$client2 = self::createAdditionalPantherClient();
27+
$crawler2 = $client2->request('GET', '/cookie.php');
28+
$this->assertSame('0', $crawler2->filter('#barcelona')->text());
29+
30+
// Check that the cookie in the other client hasn't changed
31+
$this->assertSame('1', $crawler->filter('#barcelona')->text());
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy