8000 Merge pull request #36 from dunglas/url-no-host · chalasr/symfony@2652f2a · GitHub
[go: up one dir, main page]

Skip to content

Commit 2652f2a

Browse files
authored
Merge pull request symfony#36 from dunglas/url-no-host
Allow to use URLs without a host
2 parents 5081817 + 2a69a9b commit 2652f2a

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class E2eTest extends PanthereTestCase
6464
public function testMyApp()
6565
{
6666
$client = static::createPanthereClient(); // Your app is automatically started using the built-in web server
67-
$crawler = $client->request('GET', static::$baseUri.'/mypage'); // static::$baseUri contains the base URL
67+
$crawler = $client->request('GET', '/mypage');
6868

6969
$this->assertContains('My Title', $crawler->filter('title')->text()); // You can use any PHPUnit assertion
7070
}

src/Client.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,25 @@ final class Client extends BaseClient implements WebDriver
4242
*/
4343
private $webDriver;
4444
private $browserManager;
45+
private $baseUri;
4546

4647
/**
4748
* @param string[]|null $arguments
4849
*/
49-
public static function createChromeClient(?string $chromeDriverBinary = null, ?array $arguments = null): self
50+
public static function createChromeClient(?string $chromeDriverBinary = null, ?array $arguments = null, array $options = [], ?string $baseUri = null): self
5051
{
51-
return new self(new ChromeManager($chromeDriverBinary, $arguments));
52+
return new self(new ChromeManager($chromeDriverBinary, $arguments, $options), $baseUri);
5253
}
5354

54-
public static function createSeleniumClient(?string $host = null, ?WebDriverCapabilities $capabilities = null): self
55+
public static function createSeleniumClient(?string $host = null, ?WebDriverCapabilities $capabilities = null, ?string $baseUri = null): self
5556
{
56-
return new self(new SeleniumManager($host, $capabilities));
57+
return new self(new SeleniumManager($host, $capabilities), $baseUri);
5758
}
5859

59-
public function __construct(BrowserManagerInterface $browserManager)
60+
public function __construct(BrowserManagerInterface $browserManager, ?string $baseUri = null)
6061
{
6162
$this->browserManager = $browserManager;
63+
$this->baseUri = $baseUri;
6264
}
6365

6466
public function __destruct()
@@ -87,7 +89,7 @@ public function isFollowingRedirects(): bool
8789

8890
public function setMaxRedirects($maxRedirects): void
8991
{
90-
if ($maxRedirects !== -1) {
92+
if (-1 !== $maxRedirects) {
9193
throw new \InvalidArgumentException('There are no max redirects when using WebDriver.');
9294
}
9395
}
@@ -242,6 +244,11 @@ public function get($uri)
242244
{
243245
$this->start();
244246

247+
// Prepend the base URI to URIs without a host
248+
if (null !== $this->baseUri && (false !== $components = \parse_url($uri)) && !isset($components['host'])) {
249+
$uri = $this->baseUri.$uri;
250+
}
251+
245252
$this->request = $this->internalRequest = new Request($uri, 'GET');
246253
$this->webDriver->get($uri);
247254
$this->response = $this->internalResponse = new Response($this->webDriver->getPageSource());

src/PanthereTestCase.php

Lines changed: 6 additions & 2 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Panthere;
1515

1616
use Goutte\Client as GoutteClient;
17+
use GuzzleHttp\Client as GuzzleClient;
1718
use Panthere\Client as PanthereClient;
1819
use Panthere\ProcessManager\WebServerManager;
1920
use PHPUnit\Framework\TestCase;
@@ -105,7 +106,7 @@ protected static function createPanthereClient(string $hostname = '127.0.0.1', i
105106
{
106107
self::startWebServer(null, $hostname, $port);
107108
if (null === self::$panthereClient) {
108-
self::$panthereClient = Client::createChromeClient();
109+
self::$panthereClient = Client::createChromeClient(null, null, [], self::$baseUri);
109110
}
110111

111112
return self::$panthereClient;
@@ -119,7 +120,10 @@ protected static function createGoutteClient(): GoutteClient
119120

120121
self::startWebServer();
121122
if (null === self::$goutteClient) {
122-
self::$goutteClient = new GoutteClient();
123+
$goutteClient = new GoutteClient();
124+
$goutteClient->setClient(new GuzzleClient(['base_uri' => self::$baseUri]));
125+
126+
self::$goutteClient = $goutteClient;
123127
}
124128

125129
return self::$goutteClient;

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public function clientFactoryProvider(): array
3939

4040
protected function request(callable $clientFactory, string $path): Crawler
4141
{
42-
return $clientFactory()->request('GET', static::$baseUri.$path);
42+
return $clientFactory()->request('GET', $path);
4343
}
4444
}

0 commit comments

Comments
 (0)
0