| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,180 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace Telnyx; |
|
| 4 |
+ |
|
| 5 |
+/** |
|
| 6 |
+ * Base class for Telnyx test cases. |
|
| 7 |
+ */ |
|
| 8 |
+class TestCase extends \PHPUnit_Framework_TestCase |
|
| 9 |
+{
|
|
| 10 |
+ /** @var string original API base URL */ |
|
| 11 |
+ protected $origApiBase; |
|
| 12 |
+ |
|
| 13 |
+ /** @var string original API key */ |
|
| 14 |
+ protected $origApiKey; |
|
| 15 |
+ |
|
| 16 |
+ /** @var string original client ID */ |
|
| 17 |
+ protected $origClientId; |
|
| 18 |
+ |
|
| 19 |
+ /** @var string original API version */ |
|
| 20 |
+ protected $origApiVersion; |
|
| 21 |
+ |
|
| 22 |
+ /** @var string original account ID */ |
|
| 23 |
+ protected $origAccountId; |
|
| 24 |
+ |
|
| 25 |
+ /** @var object HTTP client mocker */ |
|
| 26 |
+ protected $clientMock; |
|
| 27 |
+ |
|
| 28 |
+ protected function setUp() |
|
| 29 |
+ {
|
|
| 30 |
+ // Save original values so that we can restore them after running tests |
|
| 31 |
+ $this->origApiBase = Telnyx::$apiBase; |
|
| 32 |
+ $this->origApiKey = Telnyx::getApiKey(); |
|
| 33 |
+ $this->origClientId = Telnyx::getClientId(); |
|
| 34 |
+ $this->origApiVersion = Telnyx::getApiVersion(); |
|
| 35 |
+ $this->origAccountId = Telnyx::getAccountId(); |
|
| 36 |
+ |
|
| 37 |
+ // Set up host and credentials for Telnyx-mock |
|
| 38 |
+ Telnyx::$apiBase = MOCK_URL; |
|
| 39 |
+ Telnyx::setApiKey("KEYSUPERSECRET");
|
|
| 40 |
+ Telnyx::setClientId("ca_123");
|
|
| 41 |
+ Telnyx::setApiVersion(null); |
|
| 42 |
+ Telnyx::setAccountId(null); |
|
| 43 |
+ |
|
| 44 |
+ // Set up the HTTP client mocker |
|
| 45 |
+ $this->clientMock = $this->createMock('\Telnyx\HttpClient\ClientInterface');
|
|
| 46 |
+ |
|
| 47 |
+ // By default, use the real HTTP client |
|
| 48 |
+ ApiRequestor::setHttpClient(HttpClient\CurlClient::instance()); |
|
| 49 |
+ } |
|
| 50 |
+ |
|
| 51 |
+ protected function tearDown() |
|
| 52 |
+ {
|
|
| 53 |
+ // Restore original values |
|
| 54 |
+ Telnyx::$apiBase = $this->origApiBase; |
|
| 55 |
+ Telnyx::setEnableTelemetry(false); |
|
| 56 |
+ Telnyx::setApiKey($this->origApiKey); |
|
| 57 |
+ Telnyx::setClientId($this->origClientId); |
|
| 58 |
+ Telnyx::setApiVersion($this->origApiVersion); |
|
| 59 |
+ Telnyx::setAccountId($this->origAccountId); |
|
| 60 |
+ } |
|
| 61 |
+ |
|
| 62 |
+ /** |
|
| 63 |
+ * Sets up a request expectation with the provided parameters. The request |
|
| 64 |
+ * will actually go through and be emitted. |
|
| 65 |
+ * |
|
| 66 |
+ * @param string $method HTTP method (e.g. 'post', 'get', etc.) |
|
| 67 |
+ * @param string $path relative path |
|
| 68 |
+ * @param array|null $params array of parameters. If null, parameters will |
|
| 69 |
+ * not be checked. |
|
| 70 |
+ * @param string[]|null $headers array of headers. Does not need to be |
|
| 71 |
+ * exhaustive. If null, headers are not checked. |
|
| 72 |
+ * @param bool $hasFile Whether the request parameters contains a file. |
|
| 73 |
+ * Defaults to false. |
|
| 74 |
+ * @param string|null $base base URL (e.g. 'https://api.telnyx.com') |
|
| 75 |
+ */ |
|
| 76 |
+ protected function expectsRequest( |
|
| 77 |
+ $method, |
|
| 78 |
+ $path, |
|
| 79 |
+ $params = null, |
|
| 80 |
+ $headers = null, |
|
| 81 |
+ $hasFile = false, |
|
| 82 |
+ $base = null |
|
| 83 |
+ ) {
|
|
| 84 |
+ $this->prepareRequestMock($method, $path, $params, $headers, $hasFile, $base) |
|
| 85 |
+ ->will($this->returnCallback( |
|
| 86 |
+ function ($method, $absUrl, $headers, $params, $hasFile) {
|
|
| 87 |
+ $curlClient = HttpClient\CurlClient::instance(); |
|
| 88 |
+ ApiRequestor::setHttpClient($curlClient); |
|
| 89 |
+ return $curlClient->request($method, $absUrl, $headers, $params, $hasFile); |
|
| 90 |
+ } |
|
| 91 |
+ )); |
|
| 92 |
+ } |
|
| 93 |
+ |
|
| 94 |
+ /** |
|
| 95 |
+ * Sets up a request expectation with the provided parameters. The request |
|
| 96 |
+ * will not actually be emitted, instead the provided response parameters |
|
| 97 |
+ * will be returned. |
|
| 98 |
+ * |
|
| 99 |
+ * @param string $method HTTP method (e.g. 'post', 'get', etc.) |
|
| 100 |
+ * @param string $path relative path |
|
| 101 |
+ * @param array|null $params array of parameters. If null, parameters will |
|
| 102 |
+ * not be checked. |
|
| 103 |
+ * @param string[]|null $headers array of headers. Does not need to be |
|
| 104 |
+ * exhaustive. If null, headers are not checked. |
|
| 105 |
+ * @param bool $hasFile Whether the request parameters contains a file. |
|
| 106 |
+ * Defaults to false. |
|
| 107 |
+ * @param array $response |
|
| 108 |
+ * @param integer $rcode |
|
| 109 |
+ * @param string|null $base |
|
| 110 |
+ * |
|
| 111 |
+ * @return array |
|
| 112 |
+ */ |
|
| 113 |
+ protected function stubRequest( |
|
| 114 |
+ $method, |
|
| 115 |
+ $path, |
|
| 116 |
+ $params = null, |
|
| 117 |
+ $headers = null, |
|
| 118 |
+ $hasFile = false, |
|
| 119 |
+ $response = [], |
|
| 120 |
+ $rcode = 200, |
|
| 121 |
+ $base = null |
|
| 122 |
+ ) {
|
|
| 123 |
+ $this->prepareRequestMock($method, $path, $params, $headers, $hasFile, $base) |
|
| 124 |
+ ->willReturn([json_encode($response), $rcode, []]); |
|
| 125 |
+ } |
|
| 126 |
+ |
|
| 127 |
+ /** |
|
| 128 |
+ * Prepares the client mocker for an invocation of the `request` method. |
|
| 129 |
+ * This helper method is used by both `expectsRequest` and `stubRequest` to |
|
| 130 |
+ * prepare the client mocker to expect an invocation of the `request` method |
|
| 131 |
+ * with the provided arguments. |
|
| 132 |
+ * |
|
| 133 |
+ * @param string $method HTTP method (e.g. 'post', 'get', etc.) |
|
| 134 |
+ * @param string $path relative path |
|
| 135 |
+ * @param array|null $params array of parameters. If null, parameters will |
|
| 136 |
+ * not be checked. |
|
| 137 |
+ * @param string[]|null $headers array of headers. Does not need to be |
|
| 138 |
+ * exhaustive. If null, headers are not checked. |
|
| 139 |
+ * @param bool $hasFile Whether the request parameters contains a file. |
|
| 140 |
+ * Defaults to false. |
|
| 141 |
+ * @param string|null $base base URL (e.g. 'https://api.telnyx.com') |
|
| 142 |
+ * |
|
| 143 |
+ * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker |
|
| 144 |
+ */ |
|
| 145 |
+ private function prepareRequestMock( |
|
| 146 |
+ $method, |
|
| 147 |
+ $path, |
|
| 148 |
+ $params = null, |
|
| 149 |
+ $headers = null, |
|
| 150 |
+ $hasFile = false, |
|
| 151 |
+ $base = null |
|
| 152 |
+ ) {
|
|
| 153 |
+ ApiRequestor::setHttpClient($this->clientMock); |
|
| 154 |
+ |
|
| 155 |
+ if ($base === null) {
|
|
| 156 |
+ $base = Telnyx::$apiBase; |
|
| 157 |
+ } |
|
| 158 |
+ $absUrl = $base . $path; |
|
| 159 |
+ |
|
| 160 |
+ return $this->clientMock |
|
| 161 |
+ ->expects($this->once()) |
|
| 162 |
+ ->method('request')
|
|
| 163 |
+ ->with( |
|
| 164 |
+ $this->identicalTo(strtolower($method)), |
|
| 165 |
+ $this->identicalTo($absUrl), |
|
| 166 |
+ // for headers, we only check that all of the headers provided in $headers are |
|
| 167 |
+ // present in the list of headers of the actual request |
|
| 168 |
+ $headers === null ? $this->anything() : $this->callback(function ($array) use ($headers) {
|
|
| 169 |
+ foreach ($headers as $header) {
|
|
| 170 |
+ if (!in_array($header, $array)) {
|
|
| 171 |
+ return false; |
|
| 172 |
+ } |
|
| 173 |
+ } |
|
| 174 |
+ return true; |
|
| 175 |
+ }), |
|
| 176 |
+ $params === null ? $this->anything() : $this->identicalTo($params), |
|
| 177 |
+ $this->identicalTo($hasFile) |
|
| 178 |
+ ); |
|
| 179 |
+ } |
|
| 180 |
+} |