| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,168 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace Telnyx\Util; |
|
| 4 |
+ |
|
| 5 |
+/** |
|
| 6 |
+ * @internal |
|
| 7 |
+ * @covers \Telnyx\Util\RequestOptions |
|
| 8 |
+ */ |
|
| 9 |
+final class RequestOptionsTest extends \Telnyx\TestCase |
|
| 10 |
+{
|
|
| 11 |
+ public function testParseString() |
|
| 12 |
+ {
|
|
| 13 |
+ $opts = RequestOptions::parse('foo');
|
|
| 14 |
+ static::assertSame('foo', $opts->apiKey);
|
|
| 15 |
+ static::assertSame([], $opts->headers); |
|
| 16 |
+ static::assertNull($opts->apiBase); |
|
| 17 |
+ } |
|
| 18 |
+ |
|
| 19 |
+ public function testParseStringStrict() |
|
| 20 |
+ {
|
|
| 21 |
+ $this->expectException(\Telnyx\Exception\InvalidArgumentException::class); |
|
| 22 |
+ $this->expectExceptionMessageRegExp('#Do not pass a string for request options.#');
|
|
| 23 |
+ |
|
| 24 |
+ $opts = RequestOptions::parse('foo', true);
|
|
| 25 |
+ } |
|
| 26 |
+ |
|
| 27 |
+ public function testParseNull() |
|
| 28 |
+ {
|
|
| 29 |
+ $opts = RequestOptions::parse(null); |
|
| 30 |
+ static::assertNull($opts->apiKey); |
|
| 31 |
+ static::assertSame([], $opts->headers); |
|
| 32 |
+ static::assertNull($opts->apiBase); |
|
| 33 |
+ } |
|
| 34 |
+ |
|
| 35 |
+ public function testParseArrayEmpty() |
|
| 36 |
+ {
|
|
| 37 |
+ $opts = RequestOptions::parse([]); |
|
| 38 |
+ static::assertNull($opts->apiKey); |
|
| 39 |
+ static::assertSame([], $opts->headers); |
|
| 40 |
+ static::assertNull($opts->apiBase); |
|
| 41 |
+ } |
|
| 42 |
+ |
|
| 43 |
+ public function testParseArrayWithAPIKey() |
|
| 44 |
+ {
|
|
| 45 |
+ $opts = RequestOptions::parse( |
|
| 46 |
+ [ |
|
| 47 |
+ 'api_key' => 'foo', |
|
| 48 |
+ ] |
|
| 49 |
+ ); |
|
| 50 |
+ static::assertSame('foo', $opts->apiKey);
|
|
| 51 |
+ static::assertSame([], $opts->headers); |
|
| 52 |
+ static::assertNull($opts->apiBase); |
|
| 53 |
+ } |
|
| 54 |
+ |
|
| 55 |
+ public function testParseArrayWithIdempotencyKey() |
|
| 56 |
+ {
|
|
| 57 |
+ $opts = RequestOptions::parse( |
|
| 58 |
+ [ |
|
| 59 |
+ 'idempotency_key' => 'foo', |
|
| 60 |
+ ] |
|
| 61 |
+ ); |
|
| 62 |
+ static::assertNull($opts->apiKey); |
|
| 63 |
+ static::assertSame(['Idempotency-Key' => 'foo'], $opts->headers); |
|
| 64 |
+ static::assertNull($opts->apiBase); |
|
| 65 |
+ } |
|
| 66 |
+ |
|
| 67 |
+ public function testParseArrayWithAPIKeyAndIdempotencyKey() |
|
| 68 |
+ {
|
|
| 69 |
+ $opts = RequestOptions::parse( |
|
| 70 |
+ [ |
|
| 71 |
+ 'api_key' => 'foo', |
|
| 72 |
+ 'idempotency_key' => 'foo', |
|
| 73 |
+ ] |
|
| 74 |
+ ); |
|
| 75 |
+ static::assertSame('foo', $opts->apiKey);
|
|
| 76 |
+ static::assertSame(['Idempotency-Key' => 'foo'], $opts->headers); |
|
| 77 |
+ static::assertNull($opts->apiBase); |
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 80 |
+ public function testParseArrayWithAPIKeyAndUnexpectedKeys() |
|
| 81 |
+ {
|
|
| 82 |
+ $opts = RequestOptions::parse( |
|
| 83 |
+ [ |
|
| 84 |
+ 'api_key' => 'foo', |
|
| 85 |
+ 'foo' => 'bar', |
|
| 86 |
+ ] |
|
| 87 |
+ ); |
|
| 88 |
+ static::assertSame('foo', $opts->apiKey);
|
|
| 89 |
+ static::assertSame([], $opts->headers); |
|
| 90 |
+ static::assertNull($opts->apiBase); |
|
| 91 |
+ } |
|
| 92 |
+ |
|
| 93 |
+ public function testParseArrayWithAPIKeyAndUnexpectedKeysStrict() |
|
| 94 |
+ {
|
|
| 95 |
+ $this->expectException(\Telnyx\Exception\InvalidArgumentException::class); |
|
| 96 |
+ $this->expectExceptionMessage('Got unexpected keys in options array: foo');
|
|
| 97 |
+ |
|
| 98 |
+ $opts = RequestOptions::parse( |
|
| 99 |
+ [ |
|
| 100 |
+ 'api_key' => 'foo', |
|
| 101 |
+ 'foo' => 'bar', |
|
| 102 |
+ ], |
|
| 103 |
+ true |
|
| 104 |
+ ); |
|
| 105 |
+ } |
|
| 106 |
+ |
|
| 107 |
+ public function testParseArrayWithAPIBase() |
|
| 108 |
+ {
|
|
| 109 |
+ $opts = RequestOptions::parse( |
|
| 110 |
+ [ |
|
| 111 |
+ 'api_base' => 'https://example.com', |
|
| 112 |
+ ] |
|
| 113 |
+ ); |
|
| 114 |
+ static::assertNull($opts->apiKey); |
|
| 115 |
+ static::assertSame([], $opts->headers); |
|
| 116 |
+ static::assertSame('https://example.com', $opts->apiBase);
|
|
| 117 |
+ } |
|
| 118 |
+ |
|
| 119 |
+ public function testParseWrongType() |
|
| 120 |
+ {
|
|
| 121 |
+ $this->expectException(\Telnyx\Exception\InvalidArgumentException::class); |
|
| 122 |
+ |
|
| 123 |
+ $opts = RequestOptions::parse(5); |
|
| 124 |
+ } |
|
| 125 |
+ |
|
| 126 |
+ public function testMerge() |
|
| 127 |
+ {
|
|
| 128 |
+ $baseOpts = RequestOptions::parse( |
|
| 129 |
+ [ |
|
| 130 |
+ 'api_key' => 'foo', |
|
| 131 |
+ 'idempotency_key' => 'foo', |
|
| 132 |
+ ] |
|
| 133 |
+ ); |
|
| 134 |
+ $opts = $baseOpts->merge( |
|
| 135 |
+ [ |
|
| 136 |
+ 'idempotency_key' => 'bar', |
|
| 137 |
+ ] |
|
| 138 |
+ ); |
|
| 139 |
+ static::assertSame('foo', $opts->apiKey);
|
|
| 140 |
+ static::assertNull($opts->apiBase); |
|
| 141 |
+ } |
|
| 142 |
+ |
|
| 143 |
+ public function testDiscardNonPersistentHeaders() |
|
| 144 |
+ {
|
|
| 145 |
+ $opts = RequestOptions::parse( |
|
| 146 |
+ [ |
|
| 147 |
+ 'telnyx_account' => 'foo', |
|
| 148 |
+ ] |
|
| 149 |
+ ); |
|
| 150 |
+ $opts->discardNonPersistentHeaders(); |
|
| 151 |
+ static::assertSame(['Telnyx-Account' => 'foo'], $opts->headers); |
|
| 152 |
+ } |
|
| 153 |
+ |
|
| 154 |
+ public function testDebugInfo() |
|
| 155 |
+ {
|
|
| 156 |
+ $opts = RequestOptions::parse(['api_key' => 'sk_test_1234567890abcdefghijklmn']); |
|
| 157 |
+ $debugInfo = \print_r($opts, true); |
|
| 158 |
+ static::assertContains('[apiKey] => sk_test_********************klmn', $debugInfo);
|
|
| 159 |
+ |
|
| 160 |
+ $opts = RequestOptions::parse(['api_key' => 'sk_1234567890abcdefghijklmn']); |
|
| 161 |
+ $debugInfo = \print_r($opts, true); |
|
| 162 |
+ static::assertContains('[apiKey] => sk_********************klmn', $debugInfo);
|
|
| 163 |
+ |
|
| 164 |
+ $opts = RequestOptions::parse(['api_key' => '1234567890abcdefghijklmn']); |
|
| 165 |
+ $debugInfo = \print_r($opts, true); |
|
| 166 |
+ static::assertContains('[apiKey] => ********************klmn', $debugInfo);
|
|
| 167 |
+ } |
|
| 168 |
+} |