Skip to content

Commit

Permalink
Add ability to customize HttpClient and Panther Client
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC authored and dunglas committed Apr 25, 2024
1 parent e5512d5 commit 9f6010b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/PantherTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function stopWebServer(): void
}

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

self::startWebServer($options);

$browserArguments = $options['browser_arguments'] ?? null;
if (null !== $browserArguments && !\is_array($browserArguments)) {
throw new \TypeError(sprintf('Expected key "browser_arguments" to be an array or null, "%s" given.', get_debug_type($browserArguments)));
}

if (PantherTestCase::FIREFOX === $browser) {
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, null, $managerOptions, self::$baseUri);
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, $browserArguments, $managerOptions, self::$baseUri);
} else {
try {
self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, null, $managerOptions, self::$baseUri);
self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, $browserArguments, $managerOptions, self::$baseUri);
} catch (\RuntimeException $e) {
if (PantherTestCase::CHROME === $browser) {
throw $e;
}
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, null, $managerOptions, self::$baseUri);
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, $browserArguments, $managerOptions, self::$baseUri);
}

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

if (null === self::$httpBrowserClient) {
// The ScopingHttpClient cant't be used cause the HttpBrowser only supports absolute URLs,
$httpClientOptions = $options['http_client_options'] ?? [];
if (!\is_array($httpClientOptions)) {
throw new \TypeError(sprintf('Expected key "http_client_options" to be an array, "%s" given.', get_debug_type($httpClientOptions)));
}

// The ScopingHttpClient can't be used cause the HttpBrowser only supports absolute URLs,
// https://github.com/symfony/symfony/pull/35177
self::$httpBrowserClient = new HttpBrowserClient(HttpClient::create());
self::$httpBrowserClient = new HttpBrowserClient(HttpClient::create($httpClientOptions));
}

if (is_a(self::class, KernelTestCase::class, true)) {
Expand Down
58 changes: 58 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use Symfony\Component\Panther\Client;
use Symfony\Component\Panther\Cookie\CookieJar;
use Symfony\Component\Panther\DomCrawler\Crawler;
use Symfony\Component\Panther\PantherTestCase;
use Symfony\Component\Panther\ProcessManager\ChromeManager;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Kévin Dunglas <[email protected]>
Expand Down Expand Up @@ -450,4 +452,60 @@ public function testPing(): void
self::stopWebServer();
$this->assertFalse($client->ping());
}

public function testCreatePantherClientWithBrowserArguments(): void
{
$client = self::createPantherClient([
'browser' => PantherTestCase::CHROME,
'browser_arguments' => ['--window-size=1400,900'],
]);
$this->assertInstanceOf(AbstractBrowser::class, $client);
$this->assertInstanceOf(WebDriver::class, $client);
$this->assertInstanceOf(JavaScriptExecutor::class, $client);
$this->assertInstanceOf(KernelInterface::class, self::$kernel);

self::stopWebServer();
}

public function testCreatePantherClientWithInvalidBrowserArguments(): void
{
$this->expectException(\TypeError::class);

self::createPantherClient([
'browser_arguments' => 'bad browser arguments data type',
]);
}

public function testCreateHttpBrowserClientWithHttpClientOptions(): void
{
$client = self::createHttpBrowserClient([
'http_client_options' => [
'auth_basic' => ['foo', 'bar'],
'on_progress' => $closure = static function () {},
'cafile' => '/foo/bar',
],
]);

($httpClientRef = new \ReflectionProperty($client, 'client'))->setAccessible(true);
/** @var HttpClientInterface $httpClient */
$httpClient = $httpClientRef->getValue($client);

($httpClientOptionsRef = new \ReflectionProperty($httpClient, 'defaultOptions'))->setAccessible(true);
$httpClientOptions = $httpClientOptionsRef->getValue($httpClient);

$this->assertSame('foo:bar', $httpClientOptions['auth_basic']);
$this->assertSame($closure, $httpClientOptions['on_progress']);
$this->assertSame('/foo/bar', $httpClientOptions['cafile']);

self::stopWebServer();
}

public function testCreateHttpBrowserClientWithInvalidHttpClientOptions(): void
{
$this->expectException(\TypeError::class);

self::createHttpBrowserClient([
'http_client_options' => 'bad http client option data type',
]);
}
}

0 comments on commit 9f6010b

Please sign in to comment.