Skip to content

Commit 9f6010b

Browse files
HypeMCdunglas
authored andcommitted
Add ability to customize HttpClient and Panther Client
1 parent e5512d5 commit 9f6010b

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

src/PantherTestCaseTrait.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function stopWebServer(): void
7474
}
7575

7676
if (null !== self::$pantherClient) {
77-
foreach (self::$pantherClients as $i => $pantherClient) {
77+
foreach (self::$pantherClients as $pantherClient) {
7878
// Stop ChromeDriver only when all sessions are already closed
7979
$pantherClient->quit(false);
8080
}
@@ -178,16 +178,21 @@ protected static function createPantherClient(array $options = [], array $kernel
178178

179179
self::startWebServer($options);
180180

181+
$browserArguments = $options['browser_arguments'] ?? null;
182+
if (null !== $browserArguments && !\is_array($browserArguments)) {
183+
throw new \TypeError(sprintf('Expected key "browser_arguments" to be an array or null, "%s" given.', get_debug_type($browserArguments)));
184+
}
185+
181186
if (PantherTestCase::FIREFOX === $browser) {
182-
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, null, $managerOptions, self::$baseUri);
187+
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, $browserArguments, $managerOptions, self::$baseUri);
183188
} else {
184189
try {
185-
self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, null, $managerOptions, self::$baseUri);
190+
self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, $browserArguments, $managerOptions, self::$baseUri);
186191
} catch (\RuntimeException $e) {
187192
if (PantherTestCase::CHROME === $browser) {
188193
throw $e;
189194
}
190-
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, null, $managerOptions, self::$baseUri);
195+
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, $browserArguments, $managerOptions, self::$baseUri);
191196
}
192197

193198
if (null === $browser) {
@@ -229,9 +234,14 @@ protected static function createHttpBrowserClient(array $options = [], array $ke
229234
self::startWebServer($options);
230235

231236
if (null === self::$httpBrowserClient) {
232-
// The ScopingHttpClient cant't be used cause the HttpBrowser only supports absolute URLs,
237+
$httpClientOptions = $options['http_client_options'] ?? [];
238+
if (!\is_array($httpClientOptions)) {
239+
throw new \TypeError(sprintf('Expected key "http_client_options" to be an array, "%s" given.', get_debug_type($httpClientOptions)));
240+
}
241+
242+
// The ScopingHttpClient can't be used cause the HttpBrowser only supports absolute URLs,
233243
// https://github.com/symfony/symfony/pull/35177
234-
self::$httpBrowserClient = new HttpBrowserClient(HttpClient::create());
244+
self::$httpBrowserClient = new HttpBrowserClient(HttpClient::create($httpClientOptions));
235245
}
236246

237247
if (is_a(self::class, KernelTestCase::class, true)) {

tests/ClientTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
use Symfony\Component\Panther\Client;
2727
use Symfony\Component\Panther\Cookie\CookieJar;
2828
use Symfony\Component\Panther\DomCrawler\Crawler;
29+
use Symfony\Component\Panther\PantherTestCase;
2930
use Symfony\Component\Panther\ProcessManager\ChromeManager;
31+
use Symfony\Contracts\HttpClient\HttpClientInterface;
3032

3133
/**
3234
* @author Kévin Dunglas <[email protected]>
@@ -450,4 +452,60 @@ public function testPing(): void
450452
self::stopWebServer();
451453
$this->assertFalse($client->ping());
452454
}
455+
456+
public function testCreatePantherClientWithBrowserArguments(): void
457+
{
458+
$client = self::createPantherClient([
459+
'browser' => PantherTestCase::CHROME,
460+
'browser_arguments' => ['--window-size=1400,900'],
461+
]);
462+
$this->assertInstanceOf(AbstractBrowser::class, $client);
463+
$this->assertInstanceOf(WebDriver::class, $client);
464+
$this->assertInstanceOf(JavaScriptExecutor::class, $client);
465+
$this->assertInstanceOf(KernelInterface::class, self::$kernel);
466+
467+
self::stopWebServer();
468+
}
469+
470+
public function testCreatePantherClientWithInvalidBrowserArguments(): void
471+
{
472+
$this->expectException(\TypeError::class);
473+
474+
self::createPantherClient([
475+
'browser_arguments' => 'bad browser arguments data type',
476+
]);
477+
}
478+
479+
public function testCreateHttpBrowserClientWithHttpClientOptions(): void
480+
{
481+
$client = self::createHttpBrowserClient([
482+
'http_client_options' => [
483+
'auth_basic' => ['foo', 'bar'],
484+
'on_progress' => $closure = static function () {},
485+
'cafile' => '/foo/bar',
486+
],
487+
]);
488+
489+
($httpClientRef = new \ReflectionProperty($client, 'client'))->setAccessible(true);
490+
/** @var HttpClientInterface $httpClient */
491+
$httpClient = $httpClientRef->getValue($client);
492+
493+
($httpClientOptionsRef = new \ReflectionProperty($httpClient, 'defaultOptions'))->setAccessible(true);
494+
$httpClientOptions = $httpClientOptionsRef->getValue($httpClient);
495+
496+
$this->assertSame('foo:bar', $httpClientOptions['auth_basic']);
497+
$this->assertSame($closure, $httpClientOptions['on_progress']);
498+
$this->assertSame('/foo/bar', $httpClientOptions['cafile']);
499+
500+
self::stopWebServer();
501+
}
502+
503+
public function testCreateHttpBrowserClientWithInvalidHttpClientOptions(): void
504+
{
505+
$this->expectException(\TypeError::class);
506+
507+
self::createHttpBrowserClient([
508+
'http_client_options' => 'bad http client option data type',
509+
]);
510+
}
453511
}

0 commit comments

Comments
 (0)