Skip to content

Commit f786411

Browse files
committed
Merge pull request #22 from clue-labs/buzz
Update dependencies in order to make code more SOLID
2 parents 8efb578 + 2fa27cf commit f786411

8 files changed

Lines changed: 45 additions & 59 deletions

File tree

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
"require": {
1717
"php": ">=5.3",
1818
"react/event-loop": "~0.3.0|~0.4.0",
19-
"clue/buzz-react": "~0.3.0",
20-
"react/promise": "~1.0|~2.0",
19+
"clue/buzz-react": "~0.4.0",
20+
"react/promise": "~2.0|~1.1",
2121
"clue/json-stream": "~0.1.0"
2222
},
2323
"require-dev": {
2424
"clue/tar-react": "~0.1.0",
25-
"clue/caret-notation": "~0.2.0"
25+
"clue/caret-notation": "~0.2.0",
26+
"clue/block-react": "~0.3.0"
2627
}
2728
}

src/Factory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111
class Factory
1212
{
1313
private $loop;
14+
private $browser;
1415

15-
public function __construct(LoopInterface $loop)
16+
public function __construct(LoopInterface $loop, Browser $browser = null)
1617
{
18+
if ($browser === null) {
19+
$browser = new Browser($loop);
20+
}
21+
1722
$this->loop = $loop;
23+
$this->browser = $browser;
1824
}
1925

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

26-
$sender = null;
32+
$browser = $this->browser;
2733

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

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

36-
$browser = new Browser($this->loop, $sender);
37-
3843
return new Client($browser, $url);
3944
}
4045
}

tests/ClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private function expectRequest($method, $url, Response $response)
342342

343343
private function createResponse($body = '')
344344
{
345-
return new Response('HTTP/1.0', 200, 'OK', null, new Body($body));
345+
return new Response('HTTP/1.0', 200, 'OK', array(), new Body($body));
346346
}
347347

348348
private function createResponseJson($json)

tests/FactoryTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,34 @@
66
class FactoryTest extends TestCase
77
{
88
private $loop;
9+
private $browser;
910
private $factory;
1011

1112
public function setUp()
1213
{
1314
$this->loop = LoopFactory::create();
14-
$this->factory = new Factory($this->loop);
15+
$this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->getMock();
16+
$this->factory = new Factory($this->loop, $this->browser);
1517
}
1618

17-
public function testCreateClientDefault()
19+
public function testCtorDefaultBrowser()
1820
{
21+
$factory = new Factory($this->loop);
22+
}
23+
24+
public function testCreateClientUsesCustomUnixSender()
25+
{
26+
$this->browser->expects($this->once())->method('withSender')->will($this->returnValue($this->browser));
27+
1928
$client = $this->factory->createClient();
2029

2130
$this->assertInstanceOf('Clue\React\Docker\Client', $client);
2231
}
32+
33+
public function testCreateClientWithHttp()
34+
{
35+
$this->browser->expects($this->never())->method('withSender');
36+
37+
$this->factory->createClient('http://localhost:1234/');
38+
}
2339
}

tests/FunctionalClientTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Clue\React\Docker\Client;
44
use React\EventLoop\Factory as LoopFactory;
55
use Clue\React\Docker\Factory;
6+
use Clue\React\Block;
67

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

2122
try {
22-
$this->waitFor($promise, $this->loop);
23+
Block\await($promise, $this->loop);
2324
} catch (Exception $e) {
2425
$this->markTestSkipped('Unable to connect to docker ' . $e->getMessage());
2526
}
@@ -40,18 +41,18 @@ public function testCreateStartAndRemoveContainer()
4041
);
4142

4243
$promise = $this->client->containerCreate($config);
43-
$container = $this->waitFor($promise, $this->loop);
44+
$container = Block\await($promise, $this->loop);
4445

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

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

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

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

5657
$this->assertEquals('', $ret);
5758
}
@@ -62,13 +63,13 @@ public function testCreateStartAndRemoveContainer()
6263
public function testContainerRemoveInvalid()
6364
{
6465
$promise = $this->client->containerRemove('invalid123');
65-
$this->waitFor($promise, $this->loop);
66+
Block\await($promise, $this->loop);
6667
}
6768

6869
public function testImageSearch()
6970
{
7071
$promise = $this->client->imageSearch('clue');
71-
$ret = $this->waitFor($promise, $this->loop);
72+
$ret = Block\await($promise, $this->loop);
7273

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

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

8788
public function testImageCreateStreamMissingWillEmitJsonError()

tests/Io/ResponseParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testEmpty()
3232

3333
private function createResponse($body = '')
3434
{
35-
return new Response('HTTP/1.0', 200, 'OK', null, new Body($body));
35+
return new Response('HTTP/1.0', 200, 'OK', array(), new Body($body));
3636
}
3737

3838
}

tests/Io/StreamingParserTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use React\Promise\Deferred;
55
use React\Stream\ReadableStream;
66
use React\Promise\CancellablePromiseInterface;
7+
use React\Promise;
78

89
class StreamingParserTest extends TestCase
910
{
@@ -16,7 +17,7 @@ public function setUp()
1617

1718
public function testJsonPassingRejectedPromiseResolvesWithClosedStream()
1819
{
19-
$stream = $this->parser->parseJsonStream($this->createPromiseRejected());
20+
$stream = $this->parser->parseJsonStream(Promise\reject());
2021

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

5960
public function testPlainPassingRejectedPromiseResolvesWithClosedStream()
6061
{
61-
$stream = $this->parser->parsePlainStream($this->createPromiseRejected());
62+
$stream = $this->parser->parsePlainStream(Promise\reject());
6263

6364
$this->assertInstanceOf('React\Stream\ReadableStreamInterface', $stream);
6465
$this->assertFalse($stream->isReadable());

tests/bootstrap.php

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -84,44 +84,6 @@ protected function expectPromiseReject($promise)
8484

8585
return $promise;
8686
}
87-
88-
protected function waitFor(PromiseInterface $promise, LoopInterface $loop)
89-
{
90-
$resolved = null;
91-
$exception = null;
92-
93-
$promise->then(function ($c) use (&$resolved) {
94-
$resolved = $c;
95-
}, function($error) use (&$exception) {
96-
$exception = $error;
97-
});
98-
99-
while ($resolved === null && $exception === null) {
100-
$loop->tick();
101-
}
102-
103-
if ($exception !== null) {
104-
throw $exception;
105-
}
106-
107-
return $resolved;
108-
}
109-
110-
protected function createPromiseResolved($value = null)
111-
{
112-
$deferred = new Deferred();
113-
$deferred->resolve($value);
114-
115-
return $deferred->promise();
116-
}
117-
118-
protected function createPromiseRejected($value = null)
119-
{
120-
$deferred = new Deferred();
121-
$deferred->reject($value);
122-
123-
return $deferred->promise();
124-
}
12587
}
12688

12789
class CallableStub

0 commit comments

Comments
 (0)