|
18 | 18 | namespace Killbill\Client; |
19 | 19 |
|
20 | 20 | class Client { |
| 21 | + public static $mockManager = null; |
| 22 | + public static $useMockData = false; |
| 23 | + public static $recordMocks = false; |
21 | 24 |
|
22 | 25 | public static $serverUrl = 'http://127.0.0.1:8080'; |
23 | 26 | public static $apiVersion = '1.0'; |
@@ -51,16 +54,21 @@ public function request($method, $uri, $data = null, $user = null, $reason = nul |
51 | 54 |
|
52 | 55 | private function _sendRequest($method, $uri, $data = null, $user = null, $reason = null, $comment = null, $additional_headers = null) { |
53 | 56 |
|
| 57 | + if (self::$useMockData && isset(self::$mockManager)) { |
| 58 | + return $this->getMockResponse($method . ' ' . $uri . ' ' . $data); |
| 59 | + } |
| 60 | + |
54 | 61 | if (function_exists('mb_internal_encoding')) { |
55 | 62 | mb_internal_encoding(self::DEFAULT_ENCODING); |
56 | 63 | } |
57 | 64 |
|
| 65 | + $effectiveUri = $uri; |
58 | 66 | if (substr($uri, 0, 4) != 'http') { |
59 | | - $uri = self::__apiUrl() . $uri; |
| 67 | + $effectiveUri = self::__apiUrl() . $uri; |
60 | 68 | } |
61 | 69 |
|
62 | 70 | $ch = curl_init(); |
63 | | - curl_setopt($ch, CURLOPT_URL, $uri); |
| 71 | + curl_setopt($ch, CURLOPT_URL, $effectiveUri); |
64 | 72 | // Don't follow the Location header |
65 | 73 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); |
66 | 74 | // Include the header in the output |
@@ -128,12 +136,29 @@ private function _sendRequest($method, $uri, $data = null, $user = null, $reason |
128 | 136 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
129 | 137 | curl_close($ch); |
130 | 138 |
|
| 139 | + |
131 | 140 | $header = substr($response, 0, $header_size); |
132 | 141 | $headers = $this->_getHeaders($header); |
133 | 142 |
|
134 | 143 | $body = substr($response, $header_size); |
135 | 144 |
|
136 | | - return new Response($statusCode, $headers, $body); |
| 145 | + $response = new Response($statusCode, $headers, $body); |
| 146 | + if (self::$recordMocks && isset(self::$mockManager)) { |
| 147 | + $this->saveResponseMock($method . ' ' . $uri . ' ' . $data, $response); |
| 148 | + } |
| 149 | + |
| 150 | + return $response; |
| 151 | + } |
| 152 | + |
| 153 | + private function getMockResponse($mockName) { |
| 154 | + $rawMockFileContents = self::$mockManager->getMock($mockName); |
| 155 | + $mockData = json_decode($rawMockFileContents, true); |
| 156 | + return new Response($mockData['statusCode'], $mockData['headers'], $mockData['body']); |
| 157 | + } |
| 158 | + |
| 159 | + private function saveResponseMock($mockName, $response) { |
| 160 | + $mockContents = json_encode($response); |
| 161 | + self::$mockManager->saveMock($mockName, $mockContents); |
137 | 162 | } |
138 | 163 |
|
139 | 164 | private static function __apiUrl() { |
|
0 commit comments