diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..d97a127 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +PATH_add ~/.php/7.0.6/bin diff --git a/gulpfile.js b/gulpfile.js index 3921852..85f78b3 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -12,7 +12,7 @@ var bs = require('browser-sync').create(); gulp.task('default', ['test', 'inspect']); function test(done) { - var p = exec('composer test'); + var p = exec('composer fasttest'); p.stdout.pipe(process.stdout); p.stderr.pipe(process.stderr); p.on('exit', done); diff --git a/src/BaseRequest.php b/src/BaseRequest.php index 9cc2728..7fa3dac 100644 --- a/src/BaseRequest.php +++ b/src/BaseRequest.php @@ -1,4 +1,9 @@ scheme === 'https') { - if (isset($_SERVER['HTTPS_PROXY'])) { - return $_SERVER['HTTPS_PROXY']; - } - if (isset($_SERVER['https_proxy'])) { - return $_SERVER['https_proxy']; - } - } - - if ($this->scheme === 'http') { - if (isset($_SERVER['HTTP_PROXY'])) { - return $_SERVER['HTTP_PROXY']; - } - if (isset($_SERVER['http_proxy'])) { - return $_SERVER['http_proxy']; + foreach (array('https', 'http') as $scheme) { + if ($this->scheme === $scheme) { + $label = $scheme . '_proxy'; + foreach (array(strtoupper($label), $label) as $l) { + if (isset($_SERVER[$l])) { + return $_SERVER[$l]; + } + } } } return null; @@ -175,9 +173,11 @@ public function getCurlOptions() ); $curlOpts += static::$defaultCurlOptions; + // @codeCoverageIgnoreStart if ($ciphers = $this->nssCiphers()) { $curlOpts[CURLOPT_SSL_CIPHER_LIST] = $ciphers; } + // @codeCoverageIgnoreEnd if ($proxy = $this->getProxy($url)) { $curlOpts[CURLOPT_PROXY] = $proxy; } diff --git a/src/ConfigFacade.php b/src/ConfigFacade.php index cd21843..ef0119d 100644 --- a/src/ConfigFacade.php +++ b/src/ConfigFacade.php @@ -1,8 +1,12 @@ config->get('cache-repo-dir'); - $cacheBase = $cachedir . DIRECTORY_SEPARATOR . str_replace('://', '---', $this->baseUrl); + $cacheBase = $cachedir . DIRECTORY_SEPARATOR . strtr($this->baseUrl, ':/', '--'); foreach ($includes as $include => $metadata) { $url = $this->baseUrl . '/' . str_replace('%hash%', $metadata['sha256'], $include); $cacheKey = str_replace(array('%hash%','$'), '', $include); diff --git a/src/Plugin.php b/src/Plugin.php index 2c37d63..faaa121 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -11,6 +11,7 @@ use Composer\Plugin as CPlugin; use Composer\EventDispatcher; use Composer\Package; +use Composer\Script; use Composer\Installer; use Composer\DependencyResolver; @@ -62,15 +63,31 @@ class_exists(__NAMESPACE__ . '\\' . $class); $this->io = $io; $this->config = $composer->getConfig(); - $this->package = $composer->getPackage(); + + foreach ($GLOBALS['argv'] as $arg) { + switch ($arg) { + case 'create-project': + case 'update': + case 'outdated': + case 'require': + $this->prefetchComposerRepositories(); + break 2; + case 'install': + if (file_exists('composer.json') && !file_exists('composer.lock')) { + $this->prefetchComposerRepositories(); + } + break 2; + } + } } public static function getSubscribedEvents() { return array( CPlugin\PluginEvents::PRE_FILE_DOWNLOAD => 'onPreFileDownload', - Installer\InstallerEvents::PRE_DEPENDENCIES_SOLVING => 'onPreDependenciesSolving', +// Script\ScriptEvents::POST_ROOT_PACKAGE_INSTALL => 'prefetchComposerRepositories', +// Installer\InstallerEvents::PRE_DEPENDENCIES_SOLVING => 'prefetchComposerRepositories', Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array( array('onPostDependenciesSolving', PHP_INT_MAX), ), @@ -94,7 +111,7 @@ public function onPreFileDownload(CPlugin\PreFileDownloadEvent $ev) $ev->setRemoteFilesystem($curlrfs); } - public function onPreDependenciesSolving(Installer\InstallerEvent $ev) + public function prefetchComposerRepositories() { if ($this->disabled) { return; @@ -103,11 +120,13 @@ public function onPreDependenciesSolving(Installer\InstallerEvent $ev) return; } $repos = $this->package->getRepositories(); - if (isset($repos['packagist']['type']) && $repos['packagist']['type'] === 'composer') { - $repo = new ParallelizedComposerRepository($repos['packagist'], $this->io, $this->config); - $repo->prefetch(); - $this->cached = true; + foreach ($repos as $label => $repo) { + if (isset($repo['type']) && $repo['type'] === 'composer') { + $r = new ParallelizedComposerRepository($repo, $this->io, $this->config); + $r->prefetch(); + } } + $this->cached = true; } /** diff --git a/src/Share.php b/src/Share.php index e8a7965..d73c75e 100644 --- a/src/Share.php +++ b/src/Share.php @@ -1,8 +1,16 @@ setURL('http://www.example.com/'); + + self::assertEquals('http://www.example.com/', $req->getURL()); + } + + public function testGetMaskedURL() + { + $req = new BaseRequest; + $req->setURL('http://user:pass@example.com/p/a/t/h?token=opensesame'); + + self::assertEquals('http://example.com/p/a/t/h', $req->getMaskedURL()); + } + + public function testGetOriginURL() + { + $req = new BaseRequest; + $req->setURL('http://user:pass@example.com:1337/p/a/t/h?token=opensesame'); + + self::assertEquals('http://example.com:1337', $req->getOriginURL()); + } + + public function testCA() + { + $req = new BaseRequest; + $req->setURL('http://www.example.com/'); + $req->setCA('path/to/capath', 'path/to/ca.pem'); + + $options = $req->getCurlOptions(); + self::assertArrayHasKey(CURLOPT_CAPATH, $options); + self::assertArrayHasKey(CURLOPT_CAINFO, $options); + } +} diff --git a/tests/unit/CopyRequestTest.php b/tests/unit/CopyRequestTest.php index f2025d9..d7aa6ef 100644 --- a/tests/unit/CopyRequestTest.php +++ b/tests/unit/CopyRequestTest.php @@ -79,15 +79,6 @@ public function testDestruct() $this->assertFileNotExists($tmpfile); } - public function testGetMaskedURL() - { - $tmpfile = tempnam(sys_get_temp_dir(), 'composer_unit_test_'); - - $req = new CopyRequest('http://user:pass@example.com/p/a/t/h?token=opensesame', $tmpfile, false, $this->iop->reveal(), $this->configp->reveal()); - // user/pass/query masked - $this->assertEquals('http://example.com/p/a/t/h', $req->getMaskedURL()); - } - public function testGitHubRedirector() { $tmpfile = tempnam(sys_get_temp_dir(), 'composer_unit_test_'); diff --git a/tests/unit/PluginTest.php b/tests/unit/PluginTest.php index bfa8394..2e43f3c 100644 --- a/tests/unit/PluginTest.php +++ b/tests/unit/PluginTest.php @@ -2,10 +2,6 @@ namespace Hirak\Prestissimo; use Composer\Composer; -use Composer\Config as CConfig; -use Composer\Plugin as CPlugin; -use Composer\Util as CUtil; -use Composer\IO; use Composer\DependencyResolver\Operation; use Composer\Package; @@ -14,16 +10,28 @@ class PluginTest extends \PHPUnit_Framework_TestCase // dummy objects private $iop; private $configp; - private $composer; + private $composerp; protected function setUp() { $this->iop = $this->prophesize('Composer\IO\IOInterface'); + $this->configp = $configp = $this->prophesize('Composer\Config'); $configp->get('cache-files-dir') ->willReturn('tests/workspace/'); - $this->composer = new Composer($this->iop->reveal()); - $this->composer->setConfig($this->configp->reveal()); + + $this->composerp = $composerp = $this->prophesize('Composer\Composer'); + + $packagep = $this->prophesize('Composer\Package\CompletePackageInterface'); + $packagep->getRepositories() + ->willReturn(array()); + + $composerp->getPackage() + ->willReturn($packagep->reveal()); + $composerp->getConfig() + ->willReturn($this->configp->reveal()); + $composerp->getPackage() + ->willReturn($packagep->reveal()); } public function testConstruct() @@ -42,14 +50,14 @@ public function testConstructWithEval() $class = 'Hirak\\Prestissimo\\Plugin_composer_tmp1'; $plugin = new $class; - $plugin->activate($this->composer, $this->iop->reveal()); + $plugin->activate($this->composerp->reveal(), $this->iop->reveal()); self::assertTrue($plugin->isDisabled()); } public function testActivate() { $plugin = new Plugin; - $plugin->activate($this->composer, $this->iop->reveal()); + $plugin->activate($this->composerp->reveal(), $this->iop->reveal()); self::assertTrue(class_exists('Hirak\Prestissimo\CopyRequest', false)); } @@ -59,10 +67,27 @@ public function testGetSubscribedEvent() self::assertInternalType('array', Plugin::getSubscribedEvents()); } + public function testOnPreDependenciesSolving() + { + $plugin = new Plugin; + $plugin->activate($this->composerp->reveal(), $this->iop->reveal()); + + $evp = $this->prophesize('Composer\Installer\InstallerEvent'); + // on enabled + $plugin->prefetchComposerRepositories($evp->reveal()); + + // on disabled + $plugin->disable(); + $evp = $this->prophesize('Composer\Installer\InstallerEvent'); + $evp->getOperations() + ->shouldNotBeCalled(); + $plugin->prefetchComposerRepositories($evp->reveal()); + } + public function testOnPostDependenciesSolving() { $plugin = new Plugin; - $plugin->activate($this->composer, $this->iop->reveal()); + $plugin->activate($this->composerp->reveal(), $this->iop->reveal()); $evp = $this->prophesize('Composer\Installer\InstallerEvent'); $evp->getOperations() diff --git a/tests/unit/ShareTest.php b/tests/unit/ShareTest.php new file mode 100644 index 0000000..4895d24 --- /dev/null +++ b/tests/unit/ShareTest.php @@ -0,0 +1,13 @@ +