Browse code

Added README.md appinfo/info.xml appinfo/signature.json lib/Controller/AuthorApiController.php and the providers directory

DoubleBastionAdmin authored on 20/08/2022 16:33:00
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,240 @@
1
+<?php
2
+
3
+namespace Telnyx;
4
+
5
+use Telnyx\HttpClient\CurlClient;
6
+
7
+class DummyApiRequestor404 extends ApiResource
8
+{
9
+    const OBJECT_NAME = 'foo';
10
+
11
+    use \Telnyx\ApiOperations\All;
12
+}
13
+
14
+/**
15
+ * @internal
16
+ * @covers \Telnyx\ApiRequestor
17
+ */
18
+final class ApiRequestorTest extends \Telnyx\TestCase
19
+{
20
+    public function testEncodeObjects()
21
+    {
22
+        $reflector = new \ReflectionClass(\Telnyx\ApiRequestor::class);
23
+        $method = $reflector->getMethod('_encodeObjects');
24
+        $method->setAccessible(true);
25
+
26
+        $a = ['customer' => new PhoneNumber('abcd')];
27
+        $enc = $method->invoke(null, $a);
28
+        static::assertSame($enc, ['customer' => 'abcd']);
29
+
30
+        // Preserves UTF-8
31
+        $v = ['customer' => '☃'];
32
+        $enc = $method->invoke(null, $v);
33
+        static::assertSame($enc, $v);
34
+
35
+        // Encodes latin-1 -> UTF-8
36
+        $v = ['customer' => "\xe9"];
37
+        $enc = $method->invoke(null, $v);
38
+        static::assertSame($enc, ['customer' => "\xc3\xa9"]);
39
+
40
+        // Encodes booleans
41
+        $v = true;
42
+        $enc = $method->invoke(null, $v);
43
+        static::assertSame('true', $enc);
44
+
45
+        $v = false;
46
+        $enc = $method->invoke(null, $v);
47
+        static::assertSame('false', $enc);
48
+    }
49
+
50
+    public function testHttpClientInjection()
51
+    {
52
+        $reflector = new \ReflectionClass(\Telnyx\ApiRequestor::class);
53
+        $method = $reflector->getMethod('httpClient');
54
+        $method->setAccessible(true);
55
+
56
+        $curl = new CurlClient();
57
+        $curl->setTimeout(10);
58
+        ApiRequestor::setHttpClient($curl);
59
+
60
+        $injectedCurl = $method->invoke(new ApiRequestor());
61
+        static::assertSame($injectedCurl, $curl);
62
+    }
63
+
64
+    public function testDefaultHeaders()
65
+    {
66
+        $reflector = new \ReflectionClass(\Telnyx\ApiRequestor::class);
67
+        $method = $reflector->getMethod('_defaultHeaders');
68
+        $method->setAccessible(true);
69
+
70
+        // no way to stub static methods with PHPUnit 4.x :(
71
+        Telnyx::setAppInfo('MyTestApp', '1.2.34', 'https://mytestapp.example', 'partner_1234');
72
+        $apiKey = 'sk_test_notarealkey';
73
+        $clientInfo = ['httplib' => 'testlib 0.1.2'];
74
+
75
+        $headers = $method->invoke(null, $apiKey, $clientInfo);
76
+
77
+        $ua = \json_decode($headers['X-Telnyx-Client-User-Agent']);
78
+        static::assertSame($ua->application->name, 'MyTestApp');
79
+        static::assertSame($ua->application->version, '1.2.34');
80
+        static::assertSame($ua->application->url, 'https://mytestapp.example');
81
+        static::assertSame($ua->application->partner_id, 'partner_1234');
82
+
83
+        static::assertSame($ua->httplib, 'testlib 0.1.2');
84
+
85
+        static::assertSame(
86
+            $headers['User-Agent'],
87
+            'Telnyx/v2 PhpBindings/' . Telnyx::VERSION . ' MyTestApp/1.2.34 (https://mytestapp.example)'
88
+        );
89
+
90
+        static::assertSame($headers['Authorization'], 'Bearer ' . $apiKey);
91
+    }
92
+
93
+    public function testRaisesAuthenticationErrorWhenNoApiKey()
94
+    {
95
+        $this->expectException(\Telnyx\Exception\AuthenticationException::class);
96
+        $this->expectExceptionMessageRegExp('#No API key provided#');
97
+
98
+        Telnyx::setApiKey(null);
99
+        Call::create();
100
+    }
101
+
102
+    public function testHeaderTelnyxVersionGlobal()
103
+    {
104
+        Telnyx::setApiVersion('2222-22-22');
105
+        $this->stubRequest(
106
+            'POST',
107
+            '/v2/calls',
108
+            [],
109
+            [
110
+                'Telnyx-Version: 2222-22-22',
111
+            ],
112
+            false,
113
+            [
114
+                'id' => 'ch_123',
115
+                'object' => 'charge',
116
+            ]
117
+        );
118
+        Call::create();
119
+    }
120
+
121
+    public function testHeaderTelnyxVersionRequestOptions()
122
+    {
123
+        $this->stubRequest(
124
+            'POST',
125
+            '/v2/calls',
126
+            [],
127
+            [
128
+                'Telnyx-Version: 2222-22-22',
129
+            ],
130
+            false,
131
+            [
132
+                'id' => 'ch_123',
133
+                'object' => 'charge',
134
+            ]
135
+        );
136
+        Call::create([], ['telnyx_version' => '2222-22-22']);
137
+    }
138
+
139
+    public function testHeaderTelnyxAccountGlobal()
140
+    {
141
+        Telnyx::setAccountId('acct_123');
142
+        $this->stubRequest(
143
+            'POST',
144
+            '/v2/calls',
145
+            [],
146
+            [
147
+                'Telnyx-Account: acct_123',
148
+            ],
149
+            false,
150
+            [
151
+                'id' => 'ch_123',
152
+                'object' => 'charge',
153
+            ]
154
+        );
155
+        Call::create();
156
+    }
157
+
158
+    public function testHeaderTelnyxAccountRequestOptions()
159
+    {
160
+        $this->stubRequest(
161
+            'POST',
162
+            '/v2/calls',
163
+            [],
164
+            [
165
+                'Telnyx-Account: acct_123',
166
+            ],
167
+            false,
168
+            [
169
+                'id' => 'ch_123',
170
+                'object' => 'charge',
171
+            ]
172
+        );
173
+        Call::create([], ['telnyx_account' => 'acct_123']);
174
+    }
175
+
176
+    /*
177
+    // Mock isn't returning a valid result here
178
+    public function testRaisesInvalidRequestErrorOn404()
179
+    {
180
+        try {
181
+            DummyApiRequestor404::all();
182
+            static::fail('Did not raise error');
183
+        } catch (Exception\InvalidRequestException $e) {
184
+            static::assertSame(404, $e->getHttpStatus());
185
+            static::assertInternalType('array', $e->getJsonBody());
186
+            static::assertSame('No such charge: foo', $e->getMessage());
187
+            static::assertSame('id', $e->getStripeParam());
188
+        } catch (\Exception $e) {
189
+            static::fail('Unexpected exception: ' . \get_class($e));
190
+        }
191
+    }
192
+    */
193
+
194
+    // Tests if we pick either the detail or title for the message
195
+    public function testTitleDetailMessage()
196
+    {
197
+        // Test 1
198
+        $rbody1 = '{"errors":[{"code":"1234","title":"title"}]}';
199
+        $rbody2 = '{"errors":[{"code":"1234","detail":"detail"}]}';
200
+
201
+        $this->expectException('Telnyx\Exception\AuthenticationException');
202
+        $this->expectExceptionMessage('title');
203
+
204
+        $class = new ApiRequestor();
205
+        $class->handleErrorResponse($rbody1, 401, [], json_decode($rbody1, true));
206
+
207
+        // Test 2
208
+        $this->expectException('Telnyx\Exception\AuthenticationException');
209
+        $this->expectExceptionMessage('detail');
210
+
211
+        $class = new ApiRequestor();
212
+        $class->handleErrorResponse($rbody2, 401, [], json_decode($rbody2, true));
213
+    }
214
+
215
+    public function testErrorOn401()
216
+    {
217
+        $rbody = '{"errors":[{"code":"1234","title":"error"}]}';
218
+
219
+        $this->expectException('Telnyx\Exception\AuthenticationException');
220
+
221
+        $class = new ApiRequestor();
222
+        $class->handleErrorResponse($rbody, 401, [], json_decode($rbody, true));
223
+    }
224
+
225
+    public function testErrorOn404()
226
+    {
227
+        $rbody = '{"errors":[{"code":"1234","title":"error"}]}';
228
+
229
+        $this->expectException('Telnyx\Exception\InvalidRequestException');
230
+
231
+        $class = new ApiRequestor();
232
+        $class->handleErrorResponse($rbody, 404, [], json_decode($rbody, true));
233
+    }
234
+
235
+    public function testResetTelemetry()
236
+    {
237
+        $class = new ApiRequestor();
238
+        static::assertNull($class->resetTelemetry());
239
+    }
240
+}