Skip to content

Commit

Permalink
Improve DX when using the symfony binary (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas authored Jun 30, 2023
1 parent 7294951 commit 720e119
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ public function get($url): self

// Prepend the base URI to URIs without a host
if (null !== $this->baseUri && (false !== $components = parse_url($url)) && !isset($components['host'])) {
if (str_starts_with($url, '/') && str_ends_with($this->baseUri, '/')) {
$url = substr($url, 1);
} elseif (!str_starts_with($url, '/') && !str_ends_with($this->baseUri, '/')) {
$url = '/'.$url;
}
$url = $this->baseUri.$url;
}

Expand Down
22 changes: 16 additions & 6 deletions src/PantherTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ trait PantherTestCaseTrait
'router' => '',
'external_base_uri' => null,
'readinessPath' => '',
'browser' => PantherTestCase::CHROME,
'env' => [],
];

Expand Down Expand Up @@ -100,7 +99,7 @@ public static function startWebServer(array $options = []): void
return;
}

if ($externalBaseUri = $options['external_base_uri'] ?? $_SERVER['PANTHER_EXTERNAL_BASE_URI'] ?? self::$defaultOptions['external_base_uri']) {
if ($externalBaseUri = $options['external_base_uri'] ?? self::$defaultOptions['external_base_uri'] ?? $_SERVER['PANTHER_EXTERNAL_BASE_URI'] ?? $_SERVER['SYMFONY_PROJECT_DEFAULT_ROUTE_URL'] ?? null) {
self::$baseUri = $externalBaseUri;

return;
Expand Down Expand Up @@ -161,7 +160,7 @@ public function takeScreenshotIfTestFailed(): void
*/
protected static function createPantherClient(array $options = [], array $kernelOptions = [], array $managerOptions = []): PantherClient
{
$browser = ($options['browser'] ?? self::$defaultOptions['browser'] ?? PantherTestCase::CHROME);
$browser = ($options['browser'] ?? self::$defaultOptions['browser'] ?? null);
$callGetClient = method_exists(self::class, 'getClient') && (new \ReflectionMethod(self::class, 'getClient'))->isStatic();
if (null !== self::$pantherClient) {
$browserManager = self::$pantherClient->getBrowserManager();
Expand All @@ -178,10 +177,21 @@ protected static function createPantherClient(array $options = [], array $kernel

self::startWebServer($options);

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

if (null === $browser) {
self::$defaultOptions['browser'] = self::$pantherClient->getBrowserManager() instanceof ChromeManager ? PantherTestCase::CHROME : PantherTestCase::FIREFOX;
}
}

if (is_a(self::class, KernelTestCase::class, true)) {
Expand Down

0 comments on commit 720e119

Please sign in to comment.