Skip to content

Commit 0572189

Browse files
committed
Test client return values
1 parent d9420c4 commit 0572189

2 files changed

Lines changed: 51 additions & 28 deletions

File tree

tests/ClientTest.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,106 +20,120 @@ public function setUp()
2020

2121
public function testPing()
2222
{
23-
$this->expectRequestFlow('get', '/_ping', $this->createResponse('OK'), 'expectPlain');
23+
$body = 'OK';
24+
$this->expectRequestFlow('get', '/_ping', $this->createResponse($body), 'expectPlain');
2425

25-
$this->client->ping();
26+
$this->expectPromiseResolveWith($body, $this->client->ping());
2627
}
2728

2829
public function testContainerInspect()
2930
{
30-
$this->expectRequestFlow('get', '/containers/123/json', $this->createResponse('{}'), 'expectJson');
31+
$json = array();
32+
$this->expectRequestFlow('get', '/containers/123/json', $this->createResponseJson($json), 'expectJson');
3133

32-
$this->client->containerInspect(123);
34+
$this->expectPromiseResolveWith($json, $this->client->containerInspect(123));
3335
}
3436

3537
public function testContainerTop()
3638
{
37-
$this->expectRequestFlow('get', '/containers/123/top', $this->createResponse('{}'), 'expectJson');
39+
$json = array();
40+
$this->expectRequestFlow('get', '/containers/123/top', $this->createResponseJson($json), 'expectJson');
3841

39-
$this->client->containerTop(123);
42+
$this->expectPromiseResolveWith($json, $this->client->containerTop(123));
4043
}
4144

4245
public function testContainerWait()
4346
{
44-
$this->expectRequestFlow('post', '/containers/123/wait', $this->createResponse('{}'), 'expectJson');
47+
$json = array();
48+
$this->expectRequestFlow('post', '/containers/123/wait', $this->createResponseJson($json), 'expectJson');
4549

46-
$this->client->containerWait(123);
50+
$this->expectPromiseResolveWith($json, $this->client->containerWait(123));
4751
}
4852

4953
public function testContainerKill()
5054
{
5155
$this->expectRequestFlow('post', '/containers/123/kill?signal=', $this->createResponse(), 'expectEmpty');
5256

53-
$this->client->containerKill(123);
57+
$this->expectPromiseResolveWith('', $this->client->containerKill(123));
5458
}
5559

5660
public function testContainerKillSignalName()
5761
{
5862
$this->expectRequestFlow('post', '/containers/123/kill?signal=SIGKILL', $this->createResponse(), 'expectEmpty');
5963

60-
$this->client->containerKill(123, 'SIGKILL');
64+
$this->expectPromiseResolveWith('', $this->client->containerKill(123, 'SIGKILL'));
6165
}
6266

6367
public function testContainerKillSignalNumber()
6468
{
6569
$this->expectRequestFlow('post', '/containers/123/kill?signal=9', $this->createResponse(), 'expectEmpty');
6670

67-
$this->client->containerKill(123, 9);
71+
$this->expectPromiseResolveWith('', $this->client->containerKill(123, 9));
6872
}
6973

7074
public function testContainerStop()
7175
{
7276
$this->expectRequestFlow('post', '/containers/123/stop?t=10', $this->createResponse(), 'expectEmpty');
7377

74-
$this->client->containerStop(123, 10);
78+
$this->expectPromiseResolveWith('', $this->client->containerStop(123, 10));
7579
}
7680

7781
public function testContainerRestart()
7882
{
7983
$this->expectRequestFlow('post', '/containers/123/restart?t=10', $this->createResponse(), 'expectEmpty');
8084

81-
$this->client->containerRestart(123, 10);
85+
$this->expectPromiseResolveWith('', $this->client->containerRestart(123, 10));
8286
}
8387

8488
public function testContainerPause()
8589
{
8690
$this->expectRequestFlow('post', '/containers/123/pause', $this->createResponse(), 'expectEmpty');
8791

88-
$this->client->containerPause(123);
92+
$this->expectPromiseResolveWith('', $this->client->containerPause(123));
8993
}
9094

9195
public function testContainerUnpause()
9296
{
9397
$this->expectRequestFlow('post', '/containers/123/unpause', $this->createResponse(), 'expectEmpty');
9498

95-
$this->client->containerUnpause(123);
99+
$this->expectPromiseResolveWith('', $this->client->containerUnpause(123));
96100
}
97101

98102
public function testContainerDelete()
99103
{
100104
$this->expectRequestFlow('delete', '/containers/123?v=0&force=0', $this->createResponse(), 'expectEmpty');
101105

102-
$this->client->containerDelete(123, false, false);
106+
$this->expectPromiseResolveWith('', $this->client->containerDelete(123, false, false));
103107
}
104108

105109
public function testContainerResize()
106110
{
107111
$this->expectRequestFlow('get', '/containers/123/resize?w=800&h=600', $this->createResponse(), 'expectEmpty');
108112

109-
$this->client->containerResize(123, 800, 600);
113+
$this->expectPromiseResolveWith('', $this->client->containerResize(123, 800, 600));
110114
}
111115

112116
private function expectRequestFlow($method, $url, Response $response, $parser)
113117
{
118+
$return = (string)$response->getBody();
119+
if ($parser === 'expectJson') {
120+
$return = json_decode($return, true);
121+
}
122+
114123
$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));
124+
$this->parser->expects($this->once())->method($parser)->with($this->equalTo($response))->will($this->returnValue($return));
116125
}
117126

118127
private function createResponse($body = '')
119128
{
120129
return new Response('HTTP/1.0', 200, 'OK', null, new Body($body));
121130
}
122131

132+
private function createResponseJson($json)
133+
{
134+
return $this->createResponse(json_encode($json));
135+
}
136+
123137
private function returnPromise($for)
124138
{
125139
$deferred = new Deferred();

tests/bootstrap.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ protected function expectCallableOnce()
1313
{
1414
$mock = $this->createCallableMock();
1515

16+
$mock
17+
->expects($this->once())
18+
->method('__invoke');
1619

17-
if (func_num_args() > 0) {
18-
$mock
19-
->expects($this->once())
20-
->method('__invoke')
21-
->with($this->equalTo(func_get_arg(0)));
22-
} else {
23-
$mock
24-
->expects($this->once())
25-
->method('__invoke');
26-
}
20+
return $mock;
21+
}
22+
23+
protected function expectCallableOnceWith($value)
24+
{
25+
$mock = $this->createCallableMock();
26+
27+
$mock
28+
->expects($this->once())
29+
->method('__invoke')
30+
->with($this->equalTo($value));
2731

2832
return $mock;
2933
}
@@ -66,6 +70,11 @@ protected function expectPromiseResolve($promise)
6670
return $promise;
6771
}
6872

73+
protected function expectPromiseResolveWith($expected, $promise)
74+
{
75+
$promise->then($this->expectCallableOnceWith($expected), $this->expectCallableNever());
76+
}
77+
6978
protected function expectPromiseReject($promise)
7079
{
7180
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);

0 commit comments

Comments
 (0)