diff --git a/.gitignore b/.gitignore index d54de87..51197ff 100644 --- a/.gitignore +++ b/.gitignore @@ -8,10 +8,13 @@ # Composer /composer.phar +/composer.lock /vendor # Vagrant .vagrant Vagrantfile -.idea \ No newline at end of file +.idea + +/src/PhantomInstaller/PhantomBinary.php diff --git a/README.md b/README.md index c74a34b..c74f6b7 100644 --- a/README.md +++ b/README.md @@ -113,11 +113,20 @@ Possible values for ## Downloading from a mirror -The environment and server variable `PHANTOMJS_CDNURL` enables you to override -the default download location of the PhantomJS binary file. +You can override the default download location of the PhantomJS binary file by setting it in one of these locations. Listed in order of precedence (highest first): +* The environment variable `PHANTOMJS_CDNURL` +* The server variable `PHANTOMJS_CDNURL` +* At `$['extra']['jakoch/phantomjs-installer']['cdnurl']` in your composer.json. E.g.: + ```json + "extra": { + "jakoch/phantomjs-installer": { + "cdnurl": "https://github.com/Medium/phantomjs/releases/download/v1.9.19/" + } + }, + ``` The default download location is Bitbucket: `https://bitbucket.org/ariya/phantomjs/downloads/`. -You don't need to set it explicitly. It's used, when `CDNURL` is not set. +You don't need to set it explicitly. It's used, when `PHANTOMJS_CDNURL` is not set. You might use one of the following mirror URLs as a value for `PHANTOMJS_CDNURL`: - `https://cnpmjs.org/downloads/` - USA, San Mateo (47.88.189.193) diff --git a/composer.json b/composer.json index b620861..22a3fc2 100644 --- a/composer.json +++ b/composer.json @@ -11,10 +11,18 @@ } ], "require": { + "php": ">5.3", "ext-bz2": "*", "ext-openssl": "*" }, + "require-dev": { + "composer/composer": "^1.2", + "phpunit/phpunit": "^4.8" + }, "autoload": { + "psr-4": { + "PhantomInstaller\\Test\\" : "tests/" + }, "psr-0": { "PhantomInstaller\\": "src/" } diff --git a/src/PhantomInstaller/Installer.php b/src/PhantomInstaller/Installer.php index 303e300..a015c05 100644 --- a/src/PhantomInstaller/Installer.php +++ b/src/PhantomInstaller/Installer.php @@ -11,12 +11,13 @@ namespace PhantomInstaller; -use Composer\Script\Event; use Composer\Composer; - +use Composer\Downloader\TransportException; +use Composer\IO\BaseIO as IO; use Composer\Package\Package; use Composer\Package\RootPackageInterface; use Composer\Package\Version\VersionParser; +use Composer\Script\Event; class Installer { @@ -28,6 +29,54 @@ class Installer const PACKAGE_NAME = 'jakoch/phantomjs-installer'; + /** @var Composer */ + protected $composer; + + /** @var IO */ + protected $io; + + /** + * @return Composer + */ + public function getComposer() + { + return $this->composer; + } + + /** + * @param Composer $composer + * @return static + */ + public function setComposer(Composer $composer) + { + $this->composer = $composer; + return $this; + } + + /** + * @return IO + */ + public function getIO() + { + return $this->io; + } + + /** + * @param IO $io + * @return static + */ + public function setIO(IO $io) + { + $this->io = $io; + return $this; + } + + public function __construct(Composer $composer, IO $io) + { + $this->setComposer($composer); + $this->setIO($io); + } + /** * installPhantomJS is the main function of the install script. * @@ -37,63 +86,62 @@ class Installer * You need to invoke it from the scripts section of your * "composer.json" file as "post-install-cmd" or "post-update-cmd". * - * @param Composer\Event $event + * @param Event $event */ public static function installPhantomJS(Event $event) { - $composer = $event->getComposer(); + $installer = new static($event->getComposer(), $event->getIO()); + return $installer->__invoke(); + } - $version = self::getVersion($composer); + public function __invoke() + { + $version = $this->getVersion(); - $config = $composer->getConfig(); + $config = $this->getComposer()->getConfig(); $binDir = $config->get('bin-dir'); // the installation folder depends on the vendor-dir (default prefix is './vendor') - $targetDir = $config->get('vendor-dir') . self::PHANTOMJS_TARGETDIR; + $targetDir = $config->get('vendor-dir') . static::PHANTOMJS_TARGETDIR; - $io = $event->getIO(); + $io = $this->getIO(); // do not install a lower or equal version - $phantomJsBinary = self::getPhantomJsBinary($binDir); - if($phantomJsBinary) { - $installedVersion = self::getPhantomJsVersionFromBinary($phantomJsBinary, $io); - if(version_compare($version, $installedVersion) !== 1) { + $phantomJsBinary = $this->getPhantomJsBinary($binDir); + if ($phantomJsBinary) { + $installedVersion = $this->getPhantomJsVersionFromBinary($phantomJsBinary); + if (version_compare($version, $installedVersion) !== 1) { $io->write(' - PhantomJS v' . $installedVersion . ' is already installed. Skipping the installation.'); return; } } - /* @var \Composer\Downloader\DownloadManager $downloadManager */ - $downloadManager = $composer->getDownloadManager(); - // Download the Archive - - if(self::download($io, $downloadManager, $targetDir, $version)) - { + if ($this->download($targetDir, $version)) { // Copy only the PhantomJS binary from the installation "target dir" to the "bin" folder - self::copyPhantomJsBinaryToBinFolder($targetDir, $binDir); + $this->copyPhantomJsBinaryToBinFolder($targetDir, $binDir); } } /** * Get PhantomJS application version. Equals running "phantomjs -v" on the CLI. * - * @param string $binary - * @param Composer\IO $io + * @param string $pathToBinary * @return string PhantomJS Version */ - public static function getPhantomJsVersionFromBinary($binary, $io) + public function getPhantomJsVersionFromBinary($pathToBinary) { + $io = $this->getIO(); + try { - $cmd = escapeshellarg($binary) . ' -v'; + $cmd = escapeshellarg($pathToBinary) . ' -v'; exec($cmd, $stdout); $version = $stdout[0]; return $version; - } - catch (\Exception $e) { - $io->notice("Caught exception while checking PhantomJS version:\n" . $e->getMessage() . ''); + } catch (\Exception $e) { + $io->warning("Caught exception while checking PhantomJS version:\n" . $e->getMessage()); $io->notice('Re-downloading PhantomJS'); return false; } @@ -105,9 +153,9 @@ public static function getPhantomJsVersionFromBinary($binary, $io) * @param string $binDir * @return string|bool Returns false, if file not found, else filepath. */ - public static function getPhantomJsBinary($binDir) + public function getPhantomJsBinary($binDir) { - $os = self::getOS(); + $os = $this->getOS(); $binary = $binDir . '/phantomjs'; @@ -127,31 +175,30 @@ public static function getPhantomJsBinary($binDir) * Downloads are automatically retried with a lower version number, * when the resource it not found (404). * - * @param Composer\IO $io - * @param Composer\DownloadManager $downloadManager * @param string $targetDir * @param string $version * @return boolean */ - public static function download($io, $downloadManager, $targetDir, $version) + public function download($targetDir, $version) { - $retries = count(self::getPhantomJsVersions()); + $io = $this->getIO(); + $downloadManager = $this->getComposer()->getDownloadManager(); + $retries = count($this->getPhantomJsVersions()); - while ($retries--) - { - $package = self::createComposerInMemoryPackage($targetDir, $version); + while ($retries--) { + $package = $this->createComposerInMemoryPackage($targetDir, $version); try { $downloadManager->download($package, $targetDir, false); return true; - } catch (\Composer\Downloader\TransportException $e) { + } catch (TransportException $e) { if ($e->getStatusCode() === 404) { - $version = self::getLowerVersion($version); - $io->write('Retrying the download with a lower version number: "'. $version .'".'); + $version = $this->getLowerVersion($version); + $io->warning('Retrying the download with a lower version number: "' . $version . '"'); } } catch (\Exception $e) { $message = $e->getMessage(); - $io->write(PHP_EOL . 'While downloading version '. $version. ' the following error accoured: '. $message .''); + $io->error(PHP_EOL . 'While downloading version ' . $version . ' the following error accoured: ' . $message . ''); return false; } } @@ -162,16 +209,16 @@ public static function download($io, $downloadManager, $targetDir, $version) * * @param string $targetDir * @param string $version - * @return Composer\Package + * @return Package */ - public static function createComposerInMemoryPackage($targetDir, $version) + public function createComposerInMemoryPackage($targetDir, $version) { - $url = self::getURL($version); + $url = $this->getURL($version); $versionParser = new VersionParser(); $normVersion = $versionParser->normalize($version); - $package = new Package(self::PHANTOMJS_NAME, $normVersion, $version); + $package = new Package(static::PHANTOMJS_NAME, $normVersion, $version); $package->setTargetDir($targetDir); $package->setInstallationSource('dist'); $package->setDistType(pathinfo($url, PATHINFO_EXTENSION) === 'zip' ? 'zip' : 'tar'); // set zip, tarball @@ -185,7 +232,7 @@ public static function createComposerInMemoryPackage($targetDir, $version) * * @return array PhantomJs version numbers */ - public static function getPhantomJsVersions() + public function getPhantomJsVersions() { return array('2.1.1', '2.0.0', '1.9.8', '1.9.7'); } @@ -195,9 +242,9 @@ public static function getPhantomJsVersions() * * @return string Latest PhantomJs Version. */ - public static function getLatestPhantomJsVersion() + public function getLatestPhantomJsVersion() { - $versions = self::getPhantomJsVersions(); + $versions = $this->getPhantomJsVersions(); return $versions[0]; } @@ -208,12 +255,11 @@ public static function getLatestPhantomJsVersion() * @param string $old_version Version number * @return string Lower version number. */ - public static function getLowerVersion($old_version) + public function getLowerVersion($old_version) { - foreach(self::getPhantomJsVersions() as $idx => $version) - { + foreach ($this->getPhantomJsVersions() as $idx => $version) { // if $old_version is bigger than $version from versions array, return $version - if(version_compare($old_version, $version) == 1) { + if (version_compare($old_version, $version) == 1) { return $version; } } @@ -226,48 +272,47 @@ public static function getLowerVersion($old_version) * secondly, in the root package. * A version specification of "dev-master#" is disallowed. * - * @param Composer $composer * @return string $version Version */ - public static function getVersion($composer) + public function getVersion() { + $composer = $this->getComposer(); + // try getting the version from the local repository $packages = $composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages(); - foreach($packages as $package) { - if($package->getName() === self::PACKAGE_NAME) { + foreach ($packages as $package) { + if ($package->getName() === static::PACKAGE_NAME) { $version = $package->getPrettyVersion(); break; } } // let's take a look at the aliases - if($package->getVersion() === '9999999-dev') { // this indicates the version alias??? - $aliases = $composer->getLocker()->getAliases(); - foreach($aliases as $idx => $alias) { - if($alias['package'] === self::PACKAGE_NAME) { - return $alias['alias']; - } + $aliases = $composer->getLocker()->getAliases(); + foreach ($aliases as $idx => $alias) { + if ($alias['package'] === static::PACKAGE_NAME) { + return $alias['alias']; } } // fallback to the hardcoded latest version, if "dev-master" was set if ($version === 'dev-master') { - return self::getLatestPhantomJsVersion(); + return $this->getLatestPhantomJsVersion(); } // grab version from commit-reference, e.g. "dev-master# as version" - if(preg_match('/dev-master#(?:.*)(\d.\d.\d)/i', $version, $matches)) { + if (preg_match('/dev-master#(?:.*)(\d.\d.\d)/i', $version, $matches)) { return $matches[1]; } // grab version from a Composer patch version tag with a patch level, like "1.9.8-p02" - if(preg_match('/(\d.\d.\d)(?:(?:-p\d{2})?)/i', $version, $matches)) { + if (preg_match('/(\d.\d.\d)(?:(?:-p\d{2})?)/i', $version, $matches)) { return $matches[1]; } // let's take a look at the root package - if(!empty($version)) { - $version = self::getRequiredVersion($composer->getPackage()); + if (!empty($version)) { + $version = $this->getRequiredVersion($composer->getPackage()); } return $version; @@ -277,36 +322,35 @@ public static function getVersion($composer) * Returns the version for the given package either from the "require" or "require-dev" packages array. * * @param RootPackageInterface $package - * @param string $packageName * @throws \RuntimeException * @return mixed */ - public static function getRequiredVersion(RootPackageInterface $package) + public function getRequiredVersion(RootPackageInterface $package) { foreach (array($package->getRequires(), $package->getDevRequires()) as $requiredPackages) { - if (isset($requiredPackages[self::PACKAGE_NAME])) { - return $requiredPackages[self::PACKAGE_NAME]->getPrettyConstraint(); + if (isset($requiredPackages[static::PACKAGE_NAME])) { + return $requiredPackages[static::PACKAGE_NAME]->getPrettyConstraint(); } } - throw new \RuntimeException('Can not determine required version of ' . self::PACKAGE_NAME); + throw new \RuntimeException('Can not determine required version of ' . static::PACKAGE_NAME); } /** * Copies the PhantomJs binary to the bin folder. * Takes different "folder structure" of the archives and different "binary file names" into account. * - * @param string $targetDir path to /vendor/jakoch/phantomjs - * @param string $binDir path to binary folder + * @param string $targetDir path to /vendor/jakoch/phantomjs + * @param string $binDir path to binary folder * * @return bool True, if file dropped. False, otherwise. */ - public static function copyPhantomJsBinaryToBinFolder($targetDir, $binDir) + public function copyPhantomJsBinaryToBinFolder($targetDir, $binDir) { if (!is_dir($binDir)) { mkdir($binDir); } - $os = self::getOS(); + $os = $this->getOS(); $sourceName = '/bin/phantomjs'; $targetName = $binDir . '/phantomjs'; @@ -321,7 +365,7 @@ public static function copyPhantomJsBinaryToBinFolder($targetDir, $binDir) * For versions up to v1.9.8, the executables resides at the root. * From v2.0.0 on, the executable resides in the bin folder. */ - if(is_file($targetDir . '/phantomjs.exe')) { + if (is_file($targetDir . '/phantomjs.exe')) { $sourceName = str_replace('/bin', '', $sourceName); } @@ -334,7 +378,7 @@ public static function copyPhantomJsBinaryToBinFolder($targetDir, $binDir) chmod($targetName, static::PHANTOMJS_CHMODE); } - self::dropClassWithPathToInstalledBinary($targetName); + $this->dropClassWithPathToInstalledBinary($targetName); } /** @@ -350,14 +394,13 @@ public static function copyPhantomJsBinaryToBinFolder($targetDir, $binDir) * $bin = PhantomInstaller\PhantomBinary::getBin(); * $dir = PhantomInstaller\PhantomBinary::getDir(); * - * @param string $targetDir path to /vendor/jakoch/phantomjs - * @param string $BinaryPath full path to binary + * @param string $binaryPath full path to binary * * @return bool True, if file dropped. False, otherwise. */ - public static function dropClassWithPathToInstalledBinary($binaryPath) + public function dropClassWithPathToInstalledBinary($binaryPath) { - $code = "getOS(); + $cdn_url = $this->getCdnUrl($version); if ($os === 'windows') { $file = 'phantomjs-' . $version . '-windows.zip'; } if ($os === 'linux') { - $bitsize = self::getBitSize(); + $bitsize = $this->getBitSize(); if ($bitsize === '32') { $file = 'phantomjs-' . $version . '-linux-i686.tar.bz2'; @@ -429,9 +472,19 @@ public static function getURL($version) return $cdn_url . $file; } + /** + * Default CDN URL + */ + const PHANTOMJS_CDNURL_DEFAULT = 'https://bitbucket.org/ariya/phantomjs/downloads/'; + /** * Returns the base URL for downloads. - * Uses the ENV var "PHANTOMJS_CDNURL" or returns the default location (bitbucket). + * + * Checks (order by highest precedence on top): + * - ENV var "PHANTOMJS_CDNURL" + * - SERVER var "PHANTOMJS_CDNURL" + * - $['extra']['jakoch/phantomjs-installer']['cdnurl'] in composer.json + * - default location (bitbucket) * * == Official Downloads * @@ -442,48 +495,50 @@ public static function getURL($version) * * NPM USA * https://cnpmjs.org/downloads - * https://cnpmjs.org/mirrors/phantomjs/phantomjs-2.1.1-windows.zip + * https://cnpmjs.org/mirrors/phantomjs/phantomjs-2.1.1-windows.zip * * NPM China * https://npm.taobao.org/mirrors/phantomjs/ * https://npm.taobao.org/mirrors/phantomjs/phantomjs-2.1.1-windows.zip - * + * * Github, USA, SF - * https://github.com/Medium/phantomjs/ + * https://github.com/Medium/phantomjs/ * https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-windows.zip * + * @param string $version + * * @return string URL */ - public static function getCdnUrl($version) + public function getCdnUrl($version) { $url = ''; + $extraData = $this->getComposer()->getPackage()->getExtra(); // override the detection of the default URL // by checking for an env var and returning early if (isset($_ENV['PHANTOMJS_CDNURL'])) { $url = $_ENV['PHANTOMJS_CDNURL']; - } - elseif (isset($_SERVER['PHANTOMJS_CDNURL'])) { + } elseif (isset($_SERVER['PHANTOMJS_CDNURL'])) { $url = $_SERVER['PHANTOMJS_CDNURL']; + } elseif (isset($extraData[static::PACKAGE_NAME]['cdnurl'])) { + $url = $extraData[static::PACKAGE_NAME]['cdnurl']; } - if($url !== '') { - $url = strtolower($url); - - // add version to URL when using "github.com/medium/phantomjs" - if(strpos($url, 'github.com/medium/phantomjs') !== false) { - return 'https://github.com/medium/phantomjs/releases/download/v'.$version.'/'; - } + if ($url == '') { + $url = static::PHANTOMJS_CDNURL_DEFAULT; + } - // add slash at the end of the URL, if missing - if($url[strlen($url)-1] != '/') { - $url .= '/'; - } + // add slash at the end of the URL, if missing + if ($url[strlen($url) - 1] != '/') { + $url .= '/'; + } - return $url; + // add version to URL when using "github.com/medium/phantomjs" + if (substr(strtolower($url), -28) == 'github.com/medium/phantomjs/') { + $url .= 'releases/download/v' . $version . '/'; } - return 'https://bitbucket.org/ariya/phantomjs/downloads/'; + return $url; } /** @@ -491,14 +546,14 @@ public static function getCdnUrl($version) * * @return string OS, e.g. macosx, windows, linux. */ - public static function getOS() + public function getOS() { // override the detection of the operation system // by checking for an env var and returning early if (isset($_ENV['PHANTOMJS_PLATFORM'])) { return strtolower($_ENV['PHANTOMJS_PLATFORM']); } - + if (isset($_SERVER['PHANTOMJS_PLATFORM'])) { return strtolower($_SERVER['PHANTOMJS_PLATFORM']); } @@ -507,7 +562,8 @@ public static function getOS() if (strpos($uname, 'darwin') !== false || strpos($uname, 'openbsd') !== false || - strpos($uname, 'freebsd') !== false) { + strpos($uname, 'freebsd') !== false + ) { return 'macosx'; } elseif (strpos($uname, 'win') !== false) { return 'windows'; @@ -523,14 +579,14 @@ public static function getOS() * * @return string BitSize, e.g. 32, 64. */ - public static function getBitSize() + public function getBitSize() { // override the detection of the bitsize // by checking for an env var and returning early if (isset($_ENV['PHANTOMJS_BITSIZE'])) { return strtolower($_ENV['PHANTOMJS_BITSIZE']); } - + if (isset($_SERVER['PHANTOMJS_BITSIZE'])) { return strtolower($_SERVER['PHANTOMJS_BITSIZE']); } @@ -543,6 +599,6 @@ public static function getBitSize() return '64'; } - return (string) PHP_INT_SIZE; // 16-bit? + return (string)PHP_INT_SIZE; // 16-bit? } } diff --git a/tests/InstallerTest.php b/tests/InstallerTest.php index c972572..3114548 100644 --- a/tests/InstallerTest.php +++ b/tests/InstallerTest.php @@ -1,44 +1,66 @@ object = new Installer; + parent::setUp(); + + $mockComposer = $this->getMockComposer(); + $mockIO = $this->getMockIO(); + $this->object = new Installer($mockComposer, $mockIO); + + // Backup $_ENV and $_SERVER + $this->bakEnvVars = $_ENV; + $this->bakServerVars = $_SERVER; } - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - public function tearDown() + protected function tearDown() { + // Restore $_ENV and $_SERVER + $_ENV = $this->bakEnvVars; + $_SERVER = $this->bakServerVars; + } + + protected function getMockComposer() + { + $mockComposer = $this->getMockBuilder('Composer\Composer')->getMock(); + + return $mockComposer; + } + + protected function getMockIO() + { + $mockIO = $this->getMockBuilder('Composer\IO\BaseIO')->getMockForAbstractClass(); + + return $mockIO; } public function testInstallPhantomJS() { // composer testing: mocks.. for nothing //InstallPhantomJS(Event $event) - $this->assertTrue(true); // contribute ? + $this->markTestSkipped('contribute ?'); } public function testCopyPhantomJsBinaryToBinFolder() { - $this->assertTrue(true); // contribute ? + $this->markTestSkipped('contribute ?'); } public function testDropClassWithPathToInstalledBinary() @@ -46,51 +68,112 @@ public function testDropClassWithPathToInstalledBinary() $binaryPath = __DIR__ . '/a_fake_phantomjs_binary'; // generate file - $this->assertTrue(\PhantomInstaller\Installer::dropClassWithPathToInstalledBinary($binaryPath)); - $this->assertTrue(is_file(dirname(__DIR__).'/src/PhantomInstaller/PhantomBinary.php')); + $this->assertTrue($this->object->dropClassWithPathToInstalledBinary($binaryPath)); + $this->assertTrue(is_file(dirname(__DIR__) . '/src/PhantomInstaller/PhantomBinary.php')); // test the generated file - require_once dirname(__DIR__).'/src/PhantomInstaller/PhantomBinary.php'; - $this->assertSame($binaryPath, \PhantomInstaller\PhantomBinary::BIN); - $this->assertSame(dirname($binaryPath), \PhantomInstaller\PhantomBinary::DIR); + require_once dirname(__DIR__) . '/src/PhantomInstaller/PhantomBinary.php'; + $this->assertSame($binaryPath, PhantomBinary::BIN); + $this->assertSame(dirname($binaryPath), PhantomBinary::DIR); + } + + /** + * @param array $extraConfig mock composer.json 'extra' config with this array + */ + public function setUpForGetCdnUrl(array $extraConfig = array()) + { + $object = $this->object; + $mockComposer = $this->getMockComposer(); + $object->setComposer($mockComposer); + $mockPackage = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock(); + $mockComposer->method('getPackage')->willReturn($mockPackage); + $mockPackage->method('getExtra')->willReturn($extraConfig); + } + + /** + * @backupGlobals + */ + public function testCdnUrlTrailingSlash() + { + $this->setUpForGetCdnUrl(); + $version = '1.0.0'; + $configuredCdnUrl = 'scheme://host/path'; // without slash + $_ENV['PHANTOMJS_CDNURL'] = $configuredCdnUrl; + $cdnurl = $this->object->getCdnUrl($version); + $this->assertRegExp('{(?:^|[^/])/$}', $cdnurl, 'CdnUrl should end with one slash.'); } - public function testGetCdnUrl() + /** + * @backupGlobals + */ + public function testSpecialGithubPatternForCdnUrl() { + $this->setUpForGetCdnUrl(); $version = '1.0.0'; - $cdnurl = \PhantomInstaller\Installer::getCdnUrl($version); - $this->assertSame('https://bitbucket.org/ariya/phantomjs/downloads/', $cdnurl); - $_ENV['PHANTOMJS_CDNURL'] = 'https://cnpmjs.org/downloads'; // without slash - $cdnurl = \PhantomInstaller\Installer::getCdnUrl($version); - $this->assertSame('https://cnpmjs.org/downloads/', $cdnurl); + // Test rewrite for the Medium url as documented + $configuredCdnUrl = 'https://github.com/Medium/phantomjs'; + $_ENV['PHANTOMJS_CDNURL'] = $configuredCdnUrl; + $cdnurl = $this->object->getCdnUrl($version); + $this->assertSame($configuredCdnUrl . '/releases/download/v' . $version . '/', $cdnurl); + + // Test that a longer url is not rewritten + $configuredCdnUrl = 'https://github.com/Medium/phantomjs/releases/download/v1.9.19/'; + $_ENV['PHANTOMJS_CDNURL'] = $configuredCdnUrl; + $cdnurl = $this->object->getCdnUrl($version); + $this->assertSame($configuredCdnUrl, $cdnurl); + } - $_ENV['PHANTOMJS_CDNURL'] = 'https://github.com/medium/phantomjs'; - $cdnurl = \PhantomInstaller\Installer::getCdnUrl($version); - $this->assertSame('https://github.com/medium/phantomjs/releases/download/v'.$version.'/', $cdnurl); + /** + * @backupGlobals + */ + public function testGetCdnUrlConfigPrecedence() + { + $this->setUpForGetCdnUrl(); + $version = '1.0.0'; - unset($_ENV['PHANTOMJS_CDNURL']); - $_SERVER['PHANTOMJS_CDNURL'] = 'scheme://another-download-url'; - $cdnurl = \PhantomInstaller\Installer::getCdnUrl($version); - $this->assertSame('scheme://another-download-url/', $cdnurl); + // Test default URL is returned when there is no config + $cdnurlExpected = Installer::PHANTOMJS_CDNURL_DEFAULT; + $cdnurl = $this->object->getCdnUrl($version); + $this->assertSame($cdnurlExpected, $cdnurl); + + // Test composer.json extra config overrides the default URL + $cdnurlExpected = 'scheme://host/extra-url/'; + $extraData = array(Installer::PACKAGE_NAME => array('cdnurl' => $cdnurlExpected)); + $this->setUpForGetCdnUrl($extraData); + $cdnurl = $this->object->getCdnUrl($version); + $this->assertSame($cdnurlExpected, $cdnurl); + + // Test $_SERVER var overrides default URL and extra config + $cdnurlExpected = 'scheme://host/server-var-url/'; + $_SERVER['PHANTOMJS_CDNURL'] = $cdnurlExpected; + $cdnurl = $this->object->getCdnUrl($version); + $this->assertSame($cdnurlExpected, $cdnurl); + + // Test $_ENV var overrides default URL, extra config and $_SERVER var + $cdnurlExpected = 'scheme://host/env-var-url/'; + $_ENV['PHANTOMJS_CDNURL'] = $cdnurlExpected; + $cdnurl = $this->object->getCdnUrl($version); + $this->assertSame($cdnurlExpected, $cdnurl); } public function testgetURL() { + $this->setUpForGetCdnUrl(); $version = '1.0.0'; - $os = \PhantomInstaller\Installer::getURL($version); - $this->assertTrue(is_string($os)); + $url = $this->object->getURL($version); + $this->assertTrue(is_string($url)); } public function testGetOS() { - $os = \PhantomInstaller\Installer::getOS(); + $os = $this->object->getOS(); $this->assertTrue(is_string($os)); } public function testGetBitSize() { - $bitsize = \PhantomInstaller\Installer::getBitSize(); + $bitsize = $this->object->getBitSize(); $this->assertTrue(is_string($bitsize)); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..8aa3c42 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,7 @@ +