Skip to content

Commit

Permalink
Merge pull request #104 from hirak/unittest
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
hirak authored Jun 12, 2016
2 parents df10ee6 + 2013023 commit 95deef5
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 44 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PATH_add ~/.php/7.0.6/bin
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 15 additions & 15 deletions src/BaseRequest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/*
* hirak/prestissimo
* @author Hiraku NAKANO
* @license MIT https://github.com/hirak/prestissimo
*/
namespace Hirak\Prestissimo;

use Composer\Util;
Expand Down Expand Up @@ -89,21 +94,14 @@ protected function getProxy($url)
}
}

if ($this->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;
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion src/ConfigFacade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php
/*
* hirak/prestissimo
* @author Hiraku NAKANO
* @license MIT https://github.com/hirak/prestissimo
*/
namespace Hirak\Prestissimo;

use Composer\Composer;
use Composer\Config;

class ConfigFacade
{
Expand Down
5 changes: 5 additions & 0 deletions src/FetchRequest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/*
* hirak/prestissimo
* @author Hiraku NAKANO
* @license MIT https://github.com/hirak/prestissimo
*/
namespace Hirak\Prestissimo;

use Composer\IO;
Expand Down
2 changes: 1 addition & 1 deletion src/ParallelizedComposerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function preloadProviderListings($data)

$requests = array();
$cachedir = $this->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);
Expand Down
33 changes: 26 additions & 7 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Composer\Plugin as CPlugin;
use Composer\EventDispatcher;
use Composer\Package;
use Composer\Script;
use Composer\Installer;
use Composer\DependencyResolver;

Expand Down Expand Up @@ -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),
),
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Share.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<?php
/*
* hirak/prestissimo
* @author Hiraku NAKANO
* @license MIT https://github.com/hirak/prestissimo
*/
namespace Hirak\Prestissimo;

class Share
{
/**
* @codeCoverageIgnore
*/
public static function setup($ch)
{
static $sh;
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/BaseRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace Hirak\Prestissimo;

class BaseRequestTest extends \PHPUnit_Framework_TestCase
{
public function testSetURL()
{
$req = new BaseRequest;
$req->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);
}
}
9 changes: 0 additions & 9 deletions tests/unit/CopyRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_');
Expand Down
45 changes: 35 additions & 10 deletions tests/unit/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand All @@ -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));
}
Expand All @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/ShareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Hirak\Prestissimo;

class ShareTest extends \PHPUnit_Framework_TestCase
{
public function testSetup()
{
$ch = curl_init();
Share::setup($ch);

self::assertInternalType('resource', $ch);
}
}

0 comments on commit 95deef5

Please sign in to comment.