-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Feature: Introduced a new DownloadUrlResolver, which can be used to find the specific download url for a version * Feature: Implement the new url resolver in the chromedriver downloader * Feature: Implement the new url resolver in the chromedriver downloader * Feature: Update the DownloaderTest * Feature: Small changes to the url resolver, and added a unit test * Feature: Change multiline call into one liner * Feature: Moved the binary and platform resolving logic to the DownloadUrlResolver * Feature: Remove the expected "with" argument * Feature: Updated test, to remove the binary param, and test all OS version on the old and new download endpoint
- Loading branch information
1 parent
2628c95
commit 36bacb9
Showing
8 changed files
with
305 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DBrekelmans\BrowserDriverInstaller\Driver\ChromeDriver; | ||
|
||
use DBrekelmans\BrowserDriverInstaller\Driver\DownloadUrlResolver as DownloadUrlResolverInterface; | ||
use DBrekelmans\BrowserDriverInstaller\Driver\Driver; | ||
use DBrekelmans\BrowserDriverInstaller\Exception\NotImplemented; | ||
use DBrekelmans\BrowserDriverInstaller\OperatingSystem\OperatingSystem; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use UnexpectedValueException; | ||
|
||
use function is_string; | ||
use function Safe\sprintf; | ||
|
||
final class DownloadUrlResolver implements DownloadUrlResolverInterface | ||
{ | ||
private const BINARY_LINUX = 'chromedriver_linux64'; | ||
private const BINARY_MAC = 'chromedriver_mac64'; | ||
private const BINARY_WINDOWS = 'chromedriver_win32'; | ||
private const LEGACY_DOWNLOAD_ENDPOINT = 'https://chromedriver.storage.googleapis.com'; | ||
private const LATEST_PATCH_WITH_DOWNLOAD_ENDPOINT = 'https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build-with-downloads.json'; | ||
|
||
private HttpClientInterface $httpClient; | ||
|
||
public function __construct(HttpClientInterface $httpClient) | ||
{ | ||
$this->httpClient = $httpClient; | ||
} | ||
|
||
public function byDriver(Driver $driver): string | ||
{ | ||
if (! VersionResolver::isJsonVersion($driver->version())) { | ||
return sprintf( | ||
'%s/%s/%s.zip', | ||
self::LEGACY_DOWNLOAD_ENDPOINT, | ||
$driver->version()->toBuildString(), | ||
$this->getBinaryName($driver), | ||
); | ||
} | ||
|
||
$response = $this->httpClient->request('GET', self::LATEST_PATCH_WITH_DOWNLOAD_ENDPOINT); | ||
|
||
$versions = $response->toArray(); | ||
if (! isset($versions['builds'][$driver->version()->toString()]['downloads']['chromedriver'])) { | ||
throw new UnexpectedValueException(sprintf('Could not find the chromedriver downloads for version %s', $driver->version()->toString())); | ||
} | ||
|
||
$platformName = $this->getPlatformName($driver); | ||
$downloads = $versions['builds'][$driver->version()->toString()]['downloads']['chromedriver']; | ||
foreach ($downloads as $download) { | ||
if ($download['platform'] === $platformName && isset($download['url']) && is_string($download['url'])) { | ||
return $download['url']; | ||
} | ||
} | ||
|
||
$operatingSystem = $driver->operatingSystem(); | ||
|
||
throw new UnexpectedValueException(sprintf( | ||
'Could not resolve chromedriver download url for version %s with binary %s', | ||
$driver->version()->toString(), | ||
$operatingSystem->getValue() | ||
)); | ||
} | ||
|
||
private function getBinaryName(Driver $driver): string | ||
{ | ||
$operatingSystem = $driver->operatingSystem(); | ||
if ($operatingSystem->equals(OperatingSystem::WINDOWS())) { | ||
return self::BINARY_WINDOWS; | ||
} | ||
|
||
if ($operatingSystem->equals(OperatingSystem::MACOS())) { | ||
return self::BINARY_MAC; | ||
} | ||
|
||
if ($operatingSystem->equals(OperatingSystem::LINUX())) { | ||
return self::BINARY_LINUX; | ||
} | ||
|
||
throw NotImplemented::feature( | ||
sprintf('Downloading %s for %s', $driver->name()->getValue(), $operatingSystem->getValue()) | ||
); | ||
} | ||
|
||
private function getPlatformName(Driver $driver): string | ||
{ | ||
$operatingSystem = $driver->operatingSystem(); | ||
if ($operatingSystem->equals(OperatingSystem::WINDOWS())) { | ||
return 'win32'; | ||
} | ||
|
||
if ($operatingSystem->equals(OperatingSystem::MACOS())) { | ||
return 'mac-x64'; | ||
} | ||
|
||
if ($operatingSystem->equals(OperatingSystem::LINUX())) { | ||
return 'linux64'; | ||
} | ||
|
||
throw NotImplemented::feature( | ||
sprintf('Downloading %s for %s', $driver->name()->getValue(), $operatingSystem->getValue()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DBrekelmans\BrowserDriverInstaller\Driver; | ||
|
||
interface DownloadUrlResolver | ||
{ | ||
public function byDriver(Driver $driver): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DBrekelmans\BrowserDriverInstaller\Tests\Driver\ChromeDriver; | ||
|
||
use DBrekelmans\BrowserDriverInstaller\Driver\ChromeDriver\DownloadUrlResolver; | ||
use DBrekelmans\BrowserDriverInstaller\Driver\Driver; | ||
use DBrekelmans\BrowserDriverInstaller\Driver\DriverName; | ||
use DBrekelmans\BrowserDriverInstaller\OperatingSystem\OperatingSystem; | ||
use DBrekelmans\BrowserDriverInstaller\Version; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
|
||
use function Safe\json_encode; | ||
|
||
final class DownloadUrlResolverTest extends TestCase | ||
{ | ||
private DownloadUrlResolver $urlResolver; | ||
|
||
/** | ||
* @return iterable<string, array{0: Driver, 1: string}> | ||
*/ | ||
public static function byDriverDataProvider(): iterable | ||
{ | ||
yield 'legacy version linux' => [ | ||
new Driver(DriverName::CHROME(), Version::fromString('88.0.4299.0'), OperatingSystem::LINUX()), | ||
'https://chromedriver.storage.googleapis.com/88.0.4299.0/chromedriver_linux64.zip', | ||
]; | ||
|
||
yield 'legacy version macos' => [ | ||
new Driver(DriverName::CHROME(), Version::fromString('88.0.4299.0'), OperatingSystem::MACOS()), | ||
'https://chromedriver.storage.googleapis.com/88.0.4299.0/chromedriver_mac64.zip', | ||
]; | ||
|
||
yield 'legacy version windows' => [ | ||
new Driver(DriverName::CHROME(), Version::fromString('88.0.4299.0'), OperatingSystem::WINDOWS()), | ||
'https://chromedriver.storage.googleapis.com/88.0.4299.0/chromedriver_win32.zip', | ||
]; | ||
|
||
yield 'new version linux' => [ | ||
new Driver(DriverName::CHROME(), Version::fromString('115.0.5790.170'), OperatingSystem::LINUX()), | ||
'https://dynamic-url-2/', | ||
]; | ||
|
||
yield 'new version macos' => [ | ||
new Driver(DriverName::CHROME(), Version::fromString('115.0.5790.170'), OperatingSystem::MACOS()), | ||
'https://dynamic-url-3/', | ||
]; | ||
|
||
yield 'new version windows' => [ | ||
new Driver(DriverName::CHROME(), Version::fromString('115.0.5790.170'), OperatingSystem::WINDOWS()), | ||
'https://dynamic-url-1/', | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider byDriverDataProvider | ||
*/ | ||
public function testByDriver(Driver $driver, string $expectedUrl): void | ||
{ | ||
self::assertSame($expectedUrl, $this->urlResolver->byDriver($driver)); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$httpClientMock = new MockHttpClient( | ||
static function (string $method, string $url): MockResponse { | ||
if ($method === 'GET') { | ||
if ($url === 'https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build-with-downloads.json') { | ||
return new MockResponse( | ||
json_encode([ | ||
'builds' => [ | ||
'115.0.5790' => [ | ||
'downloads' => [ | ||
'chromedriver' => [ | ||
['platform' => 'win32', 'url' => 'https://dynamic-url-1/'], | ||
['platform' => 'linux64', 'url' => 'https://dynamic-url-2/'], | ||
['platform' => 'mac-x64', 'url' => 'https://dynamic-url-3/'], | ||
], | ||
], | ||
], | ||
], | ||
]) | ||
); | ||
} | ||
} | ||
|
||
return new MockResponse( | ||
'<?xml version=\'1.0\' encoding=\'UTF-8\'?><Error><Code>NoSuchKey</Code><Message>The specified key does not exist.</Message><Details>No such object: chromedriver/LATEST_RELEASE_xxx</Details></Error>', | ||
['http_code' => 404] | ||
); | ||
} | ||
); | ||
|
||
$this->urlResolver = new DownloadUrlResolver($httpClientMock); | ||
} | ||
} |
Oops, something went wrong.