Skip to content

Commit

Permalink
Merge 89bd5f5 into 4aabf32
Browse files Browse the repository at this point in the history
  • Loading branch information
hirak committed Jan 31, 2016
2 parents 4aabf32 + 89bd5f5 commit 7b9c74e
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 3 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>
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);
});
68 changes: 68 additions & 0 deletions tests/unit/CurlRemoteFilesystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
namespace Hirak\Prestissimo;

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

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 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 testGetContents()
{
$targetUrl = 'http://localhost:1337/?wait=0';
$targetFile = 'tests/workspace/target/0.txt';

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

self::assertEquals('0', $response);
}
}
93 changes: 93 additions & 0 deletions tests/workspace/sampleapi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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['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 7b9c74e

Please sign in to comment.