Skip to content

Commit

Permalink
Merge 1456183 into 4aabf32
Browse files Browse the repository at this point in the history
  • Loading branch information
hirak committed Jan 31, 2016
2 parents 4aabf32 + 1456183 commit 0295938
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 6 deletions.
1 change: 0 additions & 1 deletion .coveralls.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
service_name: travis-ci
repo_token: UWp1EuHuGdBmMnyvryRJQW3rTXl7zBtpc

# for php-coveralls
coverage_clover: artifacts/clover.xml
Expand Down
9 changes: 7 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="always"
bootstrap="vendor/autoload.php">
colors="true"
bootstrap="tests/bootstrap.php">
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
Expand All @@ -20,7 +20,12 @@
<php>
<ini name="error_reporting" value="-1"/>
</php>

<testsuite name="unit tests">
<directory>tests/unit/</directory>
</testsuite>

<testsuite name="functional tests">
<directory>tests/functional/</directory>
</testsuite>
</phpunit>
1 change: 1 addition & 0 deletions src/CurlRemoteFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Composer\Config;
use Composer\IO;
use Composer\Downloader;
use Composer\Util;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/ParallelDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function download(array $packages, array $pluginConfig)
$unused[] = curl_init();
}

/// @codeCoverageIgnoreStart
// @codeCoverageIgnoreStart
if (function_exists('curl_share_init')) {
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
Expand All @@ -61,7 +61,7 @@ public function download(array $packages, array $pluginConfig)
curl_multi_setopt($mh, CURLMOPT_PIPELINING, true);
}
}
/// @codeCoverageIgnoreEnd
// @codeCoverageIgnoreEnd

$cachedir = rtrim($this->config->get('cache-files-dir'), '\/');

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Plugin implements
/** @var IO\IOInterface */
private $io;

/** @var Config */
/** @var Composer\Config */
private $config;

public function activate(Composer $composer, IO\IOInterface $io)
Expand Down
18 changes: 18 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';

// start test server
execInBackground('php ' . __DIR__ . '/workspace/sampleapi.php');

function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) === 'Windows') {
pclose(popen('start /B ' . $cmd, 'r'));
} else {
exec($cmd . ' >/dev/null &');
}
}

register_shutdown_function(function(){
$ch = curl_init('http://localhost:1337/?exit=1');
curl_exec($ch);
});
109 changes: 109 additions & 0 deletions tests/unit/CurlRemoteFilesystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
namespace Hirak\Prestissimo;

use Composer\Config;
use Composer\IO;

class CurlRemoteFilesystemTest extends \PHPUnit_Framework_TestCase
{
protected $rfs;

protected function setUp()
{
$io = new IO\NullIO;
$config = new Config;
$this->rfs = new CurlRemoteFilesystem($io, $config);

$this->rfs->setPluginConfig(array(
'maxConnections' => 6,
'minConnections' => 3,
'pipeline' => false,
'verbose' => false,
'insecure' => false,
'capath' => '',
'privatePackages' => array(),
));
}

public static function tearDownAfterClass()
{
$targetFile = 'tests/workspace/target/0.txt';
if (file_exists($targetFile)) {
unlink($targetFile);
rmdir(dirname($targetFile));
}
}

public function testConstruct()
{
self::assertInstanceOf('Hirak\Prestissimo\CurlRemoteFilesystem', $this->rfs);
}

public function testGetOptions()
{
self::assertInternalType('array', $this->rfs->getOptions());
}

public function testGetLastHeaders()
{
self::assertInternalType('array', $this->rfs->getLastHeaders());
}

public function testCopy()
{
$targetUrl = 'http://localhost:1337/?wait=0';
$targetFile = 'tests/workspace/target/0.txt';

$this->rfs->copy('localhost', $targetUrl, $targetFile);

self::assertFileExists($targetFile);
self::assertStringEqualsFile($targetFile, '0');

// clean
unlink($targetFile);
rmdir(dirname($targetFile));
}

public function testCopyFailure()
{
$targetUrl = 'http://localhost:1337/?status=404';
$targetFile = 'tests/workspace/target/0.txt';

$this->rfs->copy('localhost', $targetUrl, $targetFile);

self::assertFileNotExists($targetFile);
self::assertFileNotExists(dirname($targetFile));
}

/**
* @expectedException Composer\Downloader\TransportException
*/
public function testCopyPromptAndRetry1()
{
$targetUrl = 'http://localhost:1337/?status=401';
$targetFile = 'tests/workspace/target/0.txt';

$this->rfs->copy('localhost', $targetUrl, $targetFile);
}

/**
* @expectedException Composer\Downloader\TransportException
*/
public function testCopyPromptAndRetry2()
{
$targetUrl = 'http://localhost:1337/?status=403';
$targetFile = 'tests/workspace/target/0.txt';

$this->rfs->copy('localhost', $targetUrl, $targetFile);
}

public function testGetContents()
{
$targetUrl = 'http://localhost:1337/?wait=0';

$progress = false;
$response = $this->rfs->getContents('localhost', $targetUrl, $progress);

self::assertEquals('0', $response);
}
}
52 changes: 52 additions & 0 deletions tests/unit/ParallelDownloaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace Hirak\Prestissimo;

use Composer\Config;
use Composer\IO;
use Composer\Package;

class ParallelDownloaderTest extends \PHPUnit_Framework_TestCase
{
protected $downloader;

protected $pluginConfig = array(
'maxConnections' => 6,
'minConnections' => 3,
'pipeline' => false,
'verbose' => false,
'insecure' => false,
'capath' => '',
'privatePackages' => array(),
);

protected function setUp()
{
$io = new IO\NullIO;
$config = new Config;
$this->downloader = new ParallelDownloader($io, $config);
}

public function testConstruct()
{
self::assertInstanceOf('Hirak\Prestissimo\ParallelDownloader', $this->downloader);
}

public function testDownloadSimple()
{
$packages = array(
self::createPackage('vendor/package1', 'http://localhost:1337/', '1234'),
self::createPackage('vendor/package2', 'http://localhost:1337/', '2345'),
);

$this->markTestIncomplete();
}

private static function createPackage($name, $url, $ref)
{
$package = new Package\Package($name, '1.0.0', 'v1.0.0');
$package->setDistUrl($url);
$package->setDistReference($ref);

return $package;
}
}
127 changes: 127 additions & 0 deletions tests/workspace/sampleapi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* test http server
*
* php http.php
*
* exit: http://localhost:1337/?exit=1
* delay: http://localhost:1337/?wait=1 (seconds)
*/
set_time_limit(0);
ob_implicit_flush();

$response =<<<_HTTP_
HTTP/1.1 200 OK\r
Content-Type: text/plain\r
Content-Length: 1\r
\r
_HTTP_;

$context['socket']['backlog'] = 128;
$server = stream_socket_server(
'tcp://0.0.0.0:1337',
$errno,
$errmsg,
STREAM_SERVER_LISTEN | STREAM_SERVER_BIND,
stream_context_create($context)
);
stream_set_blocking($server, 0);
stream_set_timeout($server, 0);

$readOrigin = array();
$readOrigin[(int)$server] = $server;
$writeOrigin = array();
$except = null;

$waiting = array();

LOOP: {
$read = $readOrigin;
$write = $writeOrigin;

stream_select($read, $write, $except, 1); //block

if (PHP_VERSION_ID < 50400) {
$readfix = array();
foreach ($read as $stream) {
$readfix[(int)$stream] = $stream;
}
$read = $readfix;
}

if (isset($read[(int)$server])) {
$connection = stream_socket_accept($server, 30, $peername);
stream_set_blocking($connection, 0);
$readOrigin[(int)$connection] = $connection;
unset($read[(int)$server]);
}

$now = microtime(true);
foreach ($waiting as $waitingStream) {
list($stream, $until, $messageBody) = $waitingStream;
if ($now > $until) {
$index = (int)$stream;
fwrite($stream, $response . $messageBody);
fclose($stream);
unset($waiting[$index]);
unset($readOrigin[$index]);
}
}

foreach ($read as $stream) {
$requestLine = stream_get_line($stream, 4096, "\r\n");

list(, $path,) = explode(' ', $requestLine);
parse_str(parse_url($path, PHP_URL_QUERY), $query);
if (isset($query['exit']) && $query['exit'] === '1') exit;

if (isset($query['status'])) {
switch ($query['status']) {
case '401':
$errorResponse = <<<_HTTP_
HTTP/1.1 401 Unauthorized\r
Connection: close\r
\r
_HTTP_;
break;
case '403':
$errorResponse = <<<_HTTP_
HTTP/1.1 403 Forbidden\r
Connection: close\r
\r
_HTTP_;
break;
case '404':
default:
$errorResponse = <<<_HTTP_
HTTP/1.1 404 Not Found\r
Connection: close\r
\r
_HTTP_;
break;
}
fwrite($stream, $errorResponse);
fclose($stream);
unset($readOrigin[(int)$stream]);
continue;
}

if (isset($query['wait']) && $query['wait'] > 0) {
$waiting[(int)$stream] = array(
$stream,
microtime(true) + (int)$query['wait'],
(int)$query['wait'],
);
} else {
fwrite($stream, $response . '0');
fclose($stream);
}
unset($readOrigin[(int)$stream]);
}

goto LOOP;
}

0 comments on commit 0295938

Please sign in to comment.