Skip to content

Commit d9420c4

Browse files
committed
Test interaction between Client and ResponseParser
1 parent 74a029f commit d9420c4

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

tests/ClientTest.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,109 +8,122 @@
88
class ClientTest extends TestCase
99
{
1010
private $browser;
11+
private $parser;
1112
private $client;
1213

1314
public function setUp()
1415
{
1516
$this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->getMock();
16-
$this->client = new Client($this->browser);
17+
$this->parser = $this->getMock('Clue\React\Docker\Io\ResponseParser');
18+
$this->client = new Client($this->browser, $this->parser);
1719
}
1820

1921
public function testPing()
2022
{
21-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('/_ping'))->will($this->returnResponse('OK'));
23+
$this->expectRequestFlow('get', '/_ping', $this->createResponse('OK'), 'expectPlain');
2224

2325
$this->client->ping();
2426
}
2527

2628
public function testContainerInspect()
2729
{
28-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('/containers/123/json'))->will($this->returnResponse('{}'));
30+
$this->expectRequestFlow('get', '/containers/123/json', $this->createResponse('{}'), 'expectJson');
2931

3032
$this->client->containerInspect(123);
3133
}
3234

3335
public function testContainerTop()
3436
{
35-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('/containers/123/top'))->will($this->returnResponse('{}'));
37+
$this->expectRequestFlow('get', '/containers/123/top', $this->createResponse('{}'), 'expectJson');
3638

3739
$this->client->containerTop(123);
3840
}
3941

4042
public function testContainerWait()
4143
{
42-
$this->browser->expects($this->once())->method('post')->with($this->equalTo('/containers/123/wait'))->will($this->returnResponse('{}'));
44+
$this->expectRequestFlow('post', '/containers/123/wait', $this->createResponse('{}'), 'expectJson');
4345

4446
$this->client->containerWait(123);
4547
}
4648

4749
public function testContainerKill()
4850
{
49-
$this->browser->expects($this->once())->method('post')->with($this->equalTo('/containers/123/kill?signal='))->will($this->returnResponse());
51+
$this->expectRequestFlow('post', '/containers/123/kill?signal=', $this->createResponse(), 'expectEmpty');
5052

5153
$this->client->containerKill(123);
5254
}
5355

5456
public function testContainerKillSignalName()
5557
{
56-
$this->browser->expects($this->once())->method('post')->with($this->equalTo('/containers/123/kill?signal=SIGKILL'))->will($this->returnResponse());
58+
$this->expectRequestFlow('post', '/containers/123/kill?signal=SIGKILL', $this->createResponse(), 'expectEmpty');
5759

5860
$this->client->containerKill(123, 'SIGKILL');
5961
}
6062

6163
public function testContainerKillSignalNumber()
6264
{
63-
$this->browser->expects($this->once())->method('post')->with($this->equalTo('/containers/123/kill?signal=9'))->will($this->returnResponse());
65+
$this->expectRequestFlow('post', '/containers/123/kill?signal=9', $this->createResponse(), 'expectEmpty');
6466

6567
$this->client->containerKill(123, 9);
6668
}
6769

6870
public function testContainerStop()
6971
{
70-
$this->browser->expects($this->once())->method('post')->with($this->equalTo('/containers/123/stop?t=10'))->will($this->returnResponse());
72+
$this->expectRequestFlow('post', '/containers/123/stop?t=10', $this->createResponse(), 'expectEmpty');
7173

7274
$this->client->containerStop(123, 10);
7375
}
7476

7577
public function testContainerRestart()
7678
{
77-
$this->browser->expects($this->once())->method('post')->with($this->equalTo('/containers/123/restart?t=10'))->will($this->returnResponse());
79+
$this->expectRequestFlow('post', '/containers/123/restart?t=10', $this->createResponse(), 'expectEmpty');
7880

7981
$this->client->containerRestart(123, 10);
8082
}
8183

8284
public function testContainerPause()
8385
{
84-
$this->browser->expects($this->once())->method('post')->with($this->equalTo('/containers/123/pause'))->will($this->returnResponse());
86+
$this->expectRequestFlow('post', '/containers/123/pause', $this->createResponse(), 'expectEmpty');
8587

8688
$this->client->containerPause(123);
8789
}
8890

8991
public function testContainerUnpause()
9092
{
91-
$this->browser->expects($this->once())->method('post')->with($this->equalTo('/containers/123/unpause'))->will($this->returnResponse());
93+
$this->expectRequestFlow('post', '/containers/123/unpause', $this->createResponse(), 'expectEmpty');
9294

9395
$this->client->containerUnpause(123);
9496
}
9597

9698
public function testContainerDelete()
9799
{
98-
$this->browser->expects($this->once())->method('delete')->with($this->equalTo('/containers/123?v=0&force=0'))->will($this->returnResponse());
100+
$this->expectRequestFlow('delete', '/containers/123?v=0&force=0', $this->createResponse(), 'expectEmpty');
99101

100102
$this->client->containerDelete(123, false, false);
101103
}
102104

103105
public function testContainerResize()
104106
{
105-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('/containers/123/resize?w=800&h=600'))->will($this->returnResponse());
107+
$this->expectRequestFlow('get', '/containers/123/resize?w=800&h=600', $this->createResponse(), 'expectEmpty');
106108

107109
$this->client->containerResize(123, 800, 600);
108110
}
109111

110-
private function returnResponse($body = '')
112+
private function expectRequestFlow($method, $url, Response $response, $parser)
113+
{
114+
$this->browser->expects($this->once())->method($method)->with($this->equalTo($url))->will($this->returnPromise($response));
115+
$this->parser->expects($this->once())->method($parser)->with($this->equalTo($response));
116+
}
117+
118+
private function createResponse($body = '')
119+
{
120+
return new Response('HTTP/1.0', 200, 'OK', null, new Body($body));
121+
}
122+
123+
private function returnPromise($for)
111124
{
112125
$deferred = new Deferred();
113-
$deferred->resolve(new Response('HTTP/1.0', 200, 'OK', null, new Body($body)));
126+
$deferred->resolve($for);
114127

115128
return $this->returnValue($deferred->promise());
116129
}

0 commit comments

Comments
 (0)