Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
"require": {
"php": ">=5.3",
"react/event-loop": "~0.3.0|~0.4.0",
"clue/buzz-react": "~0.3.0",
"react/promise": "~1.0|~2.0",
"clue/buzz-react": "~0.4.0",
"react/promise": "~2.0|~1.1",
"clue/json-stream": "~0.1.0"
},
"require-dev": {
"clue/tar-react": "~0.1.0",
"clue/caret-notation": "~0.2.0"
"clue/caret-notation": "~0.2.0",
"clue/block-react": "~0.3.0"
}
}
13 changes: 9 additions & 4 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
class Factory
{
private $loop;
private $browser;

public function __construct(LoopInterface $loop)
public function __construct(LoopInterface $loop, Browser $browser = null)
{
if ($browser === null) {
$browser = new Browser($loop);
}

$this->loop = $loop;
$this->browser = $browser;
}

public function createClient($url = null)
Expand All @@ -23,18 +29,17 @@ public function createClient($url = null)
$url = 'unix:///var/run/docker.sock';
}

$sender = null;
$browser = $this->browser;

if (substr($url, 0, 7) === 'unix://') {
// send everything through a local unix domain socket
$sender = Sender::createFromLoopUnix($this->loop, $url);
$browser = $browser->withSender($sender);

// pretend all HTTP URLs to be on localhost
$url = 'http://localhost';
}

$browser = new Browser($this->loop, $sender);

return new Client($browser, $url);
}
}
2 changes: 1 addition & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private function expectRequest($method, $url, Response $response)

private function createResponse($body = '')
{
return new Response('HTTP/1.0', 200, 'OK', null, new Body($body));
return new Response('HTTP/1.0', 200, 'OK', array(), new Body($body));
}

private function createResponseJson($json)
Expand Down
20 changes: 18 additions & 2 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,34 @@
class FactoryTest extends TestCase
{
private $loop;
private $browser;
private $factory;

public function setUp()
{
$this->loop = LoopFactory::create();
$this->factory = new Factory($this->loop);
$this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->getMock();
$this->factory = new Factory($this->loop, $this->browser);
}

public function testCreateClientDefault()
public function testCtorDefaultBrowser()
{
$factory = new Factory($this->loop);
}

public function testCreateClientUsesCustomUnixSender()
{
$this->browser->expects($this->once())->method('withSender')->will($this->returnValue($this->browser));

$client = $this->factory->createClient();

$this->assertInstanceOf('Clue\React\Docker\Client', $client);
}

public function testCreateClientWithHttp()
{
$this->browser->expects($this->never())->method('withSender');

$this->factory->createClient('http://localhost:1234/');
}
}
17 changes: 9 additions & 8 deletions tests/FunctionalClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Clue\React\Docker\Client;
use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
use Clue\React\Block;

class FunctionalClientTest extends TestCase
{
Expand All @@ -19,7 +20,7 @@ public function setUp()
$promise = $this->client->ping();

try {
$this->waitFor($promise, $this->loop);
Block\await($promise, $this->loop);
} catch (Exception $e) {
$this->markTestSkipped('Unable to connect to docker ' . $e->getMessage());
}
Expand All @@ -40,18 +41,18 @@ public function testCreateStartAndRemoveContainer()
);

$promise = $this->client->containerCreate($config);
$container = $this->waitFor($promise, $this->loop);
$container = Block\await($promise, $this->loop);

$this->assertNotNull($container['Id']);
$this->assertNull($container['Warnings']);

$promise = $this->client->containerStart($container['Id']);
$ret = $this->waitFor($promise, $this->loop);
$ret = Block\await($promise, $this->loop);

$this->assertEquals('', $ret);

$promise = $this->client->containerRemove($container['Id'], false, true);
$ret = $this->waitFor($promise, $this->loop);
$ret = Block\await($promise, $this->loop);

$this->assertEquals('', $ret);
}
Expand All @@ -62,13 +63,13 @@ public function testCreateStartAndRemoveContainer()
public function testContainerRemoveInvalid()
{
$promise = $this->client->containerRemove('invalid123');
$this->waitFor($promise, $this->loop);
Block\await($promise, $this->loop);
}

public function testImageSearch()
{
$promise = $this->client->imageSearch('clue');
$ret = $this->waitFor($promise, $this->loop);
$ret = Block\await($promise, $this->loop);

$this->assertGreaterThan(9, count($ret));
}
Expand All @@ -77,11 +78,11 @@ public function testImageTag()
{
// create new tag "bb:now" on "busybox:latest"
$promise = $this->client->imageTag('busybox', 'bb', 'now');
$ret = $this->waitFor($promise, $this->loop);
$ret = Block\await($promise, $this->loop);

// delete tag "bb:now" again
$promise = $this->client->imageRemove('bb:now');
$ret = $this->waitFor($promise, $this->loop);
$ret = Block\await($promise, $this->loop);
}

public function testImageCreateStreamMissingWillEmitJsonError()
Expand Down
2 changes: 1 addition & 1 deletion tests/Io/ResponseParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testEmpty()

private function createResponse($body = '')
{
return new Response('HTTP/1.0', 200, 'OK', null, new Body($body));
return new Response('HTTP/1.0', 200, 'OK', array(), new Body($body));
}

}
5 changes: 3 additions & 2 deletions tests/Io/StreamingParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use React\Promise\Deferred;
use React\Stream\ReadableStream;
use React\Promise\CancellablePromiseInterface;
use React\Promise;

class StreamingParserTest extends TestCase
{
Expand All @@ -16,7 +17,7 @@ public function setUp()

public function testJsonPassingRejectedPromiseResolvesWithClosedStream()
{
$stream = $this->parser->parseJsonStream($this->createPromiseRejected());
$stream = $this->parser->parseJsonStream(Promise\reject());

$this->assertInstanceOf('React\Stream\ReadableStreamInterface', $stream);
$this->assertFalse($stream->isReadable());
Expand Down Expand Up @@ -58,7 +59,7 @@ public function testJsonResolvingPromiseWillEmitCloseEvent()

public function testPlainPassingRejectedPromiseResolvesWithClosedStream()
{
$stream = $this->parser->parsePlainStream($this->createPromiseRejected());
$stream = $this->parser->parsePlainStream(Promise\reject());

$this->assertInstanceOf('React\Stream\ReadableStreamInterface', $stream);
$this->assertFalse($stream->isReadable());
Expand Down
38 changes: 0 additions & 38 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,44 +84,6 @@ protected function expectPromiseReject($promise)

return $promise;
}

protected function waitFor(PromiseInterface $promise, LoopInterface $loop)
{
$resolved = null;
$exception = null;

$promise->then(function ($c) use (&$resolved) {
$resolved = $c;
}, function($error) use (&$exception) {
$exception = $error;
});

while ($resolved === null && $exception === null) {
$loop->tick();
}

if ($exception !== null) {
throw $exception;
}

return $resolved;
}

protected function createPromiseResolved($value = null)
{
$deferred = new Deferred();
$deferred->resolve($value);

return $deferred->promise();
}

protected function createPromiseRejected($value = null)
{
$deferred = new Deferred();
$deferred->reject($value);

return $deferred->promise();
}
}

class CallableStub
Expand Down