|
8 | 8 | class ClientTest extends TestCase |
9 | 9 | { |
10 | 10 | private $browser; |
| 11 | + private $parser; |
11 | 12 | private $client; |
12 | 13 |
|
13 | 14 | public function setUp() |
14 | 15 | { |
15 | 16 | $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); |
17 | 19 | } |
18 | 20 |
|
19 | 21 | public function testPing() |
20 | 22 | { |
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'); |
22 | 24 |
|
23 | 25 | $this->client->ping(); |
24 | 26 | } |
25 | 27 |
|
26 | 28 | public function testContainerInspect() |
27 | 29 | { |
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'); |
29 | 31 |
|
30 | 32 | $this->client->containerInspect(123); |
31 | 33 | } |
32 | 34 |
|
33 | 35 | public function testContainerTop() |
34 | 36 | { |
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'); |
36 | 38 |
|
37 | 39 | $this->client->containerTop(123); |
38 | 40 | } |
39 | 41 |
|
40 | 42 | public function testContainerWait() |
41 | 43 | { |
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'); |
43 | 45 |
|
44 | 46 | $this->client->containerWait(123); |
45 | 47 | } |
46 | 48 |
|
47 | 49 | public function testContainerKill() |
48 | 50 | { |
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'); |
50 | 52 |
|
51 | 53 | $this->client->containerKill(123); |
52 | 54 | } |
53 | 55 |
|
54 | 56 | public function testContainerKillSignalName() |
55 | 57 | { |
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'); |
57 | 59 |
|
58 | 60 | $this->client->containerKill(123, 'SIGKILL'); |
59 | 61 | } |
60 | 62 |
|
61 | 63 | public function testContainerKillSignalNumber() |
62 | 64 | { |
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'); |
64 | 66 |
|
65 | 67 | $this->client->containerKill(123, 9); |
66 | 68 | } |
67 | 69 |
|
68 | 70 | public function testContainerStop() |
69 | 71 | { |
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'); |
71 | 73 |
|
72 | 74 | $this->client->containerStop(123, 10); |
73 | 75 | } |
74 | 76 |
|
75 | 77 | public function testContainerRestart() |
76 | 78 | { |
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'); |
78 | 80 |
|
79 | 81 | $this->client->containerRestart(123, 10); |
80 | 82 | } |
81 | 83 |
|
82 | 84 | public function testContainerPause() |
83 | 85 | { |
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'); |
85 | 87 |
|
86 | 88 | $this->client->containerPause(123); |
87 | 89 | } |
88 | 90 |
|
89 | 91 | public function testContainerUnpause() |
90 | 92 | { |
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'); |
92 | 94 |
|
93 | 95 | $this->client->containerUnpause(123); |
94 | 96 | } |
95 | 97 |
|
96 | 98 | public function testContainerDelete() |
97 | 99 | { |
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'); |
99 | 101 |
|
100 | 102 | $this->client->containerDelete(123, false, false); |
101 | 103 | } |
102 | 104 |
|
103 | 105 | public function testContainerResize() |
104 | 106 | { |
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'); |
106 | 108 |
|
107 | 109 | $this->client->containerResize(123, 800, 600); |
108 | 110 | } |
109 | 111 |
|
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) |
111 | 124 | { |
112 | 125 | $deferred = new Deferred(); |
113 | | - $deferred->resolve(new Response('HTTP/1.0', 200, 'OK', null, new Body($body))); |
| 126 | + $deferred->resolve($for); |
114 | 127 |
|
115 | 128 | return $this->returnValue($deferred->promise()); |
116 | 129 | } |
|
0 commit comments