| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,117 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace Telnyx; |
|
| 4 |
+ |
|
| 5 |
+/** |
|
| 6 |
+ * @internal |
|
| 7 |
+ * @coversNothing |
|
| 8 |
+ */ |
|
| 9 |
+final class TelnyxTelemetryTest extends \Telnyx\TestCase |
|
| 10 |
+{
|
|
| 11 |
+ const TEST_API_KEY = 'KEY123'; |
|
| 12 |
+ const TEST_RESOURCE_ID = 'acct_123'; |
|
| 13 |
+ const TEST_EXTERNALACCOUNT_ID = 'ba_123'; |
|
| 14 |
+ const TEST_PERSON_ID = 'person_123'; |
|
| 15 |
+ |
|
| 16 |
+ const FAKE_VALID_RESPONSE = '{
|
|
| 17 |
+ "data": [], |
|
| 18 |
+ "record_type": "list", |
|
| 19 |
+ "meta": [], |
|
| 20 |
+ "url": "/v2/phone_numbers" |
|
| 21 |
+ }'; |
|
| 22 |
+ |
|
| 23 |
+ protected function setUp() |
|
| 24 |
+ {
|
|
| 25 |
+ // clear static telemetry data |
|
| 26 |
+ ApiRequestor::resetTelemetry(); |
|
| 27 |
+ } |
|
| 28 |
+ |
|
| 29 |
+ public function testNoTelemetrySentIfNotEnabled() |
|
| 30 |
+ {
|
|
| 31 |
+ Telnyx::setApiKey(self::TEST_API_KEY); |
|
| 32 |
+ $requestheaders = null; |
|
| 33 |
+ |
|
| 34 |
+ $stub = $this |
|
| 35 |
+ ->getMockBuilder('HttpClient\\ClientInterface')
|
|
| 36 |
+ ->setMethods(['request']) |
|
| 37 |
+ ->getMock() |
|
| 38 |
+ ; |
|
| 39 |
+ |
|
| 40 |
+ $stub->expects(static::any()) |
|
| 41 |
+ ->method('request')
|
|
| 42 |
+ ->with( |
|
| 43 |
+ static::anything(), |
|
| 44 |
+ static::anything(), |
|
| 45 |
+ static::callback(function ($headers) use (&$requestheaders) {
|
|
| 46 |
+ foreach ($headers as $index => $header) {
|
|
| 47 |
+ // capture the requested headers and format back to into an assoc array |
|
| 48 |
+ $components = \explode(': ', $header, 2);
|
|
| 49 |
+ $requestheaders[$components[0]] = $components[1]; |
|
| 50 |
+ } |
|
| 51 |
+ |
|
| 52 |
+ return true; |
|
| 53 |
+ }), |
|
| 54 |
+ static::anything(), |
|
| 55 |
+ static::anything() |
|
| 56 |
+ )->willReturn([self::FAKE_VALID_RESPONSE, 200, ['request-id' => '123']]); |
|
| 57 |
+ |
|
| 58 |
+ ApiRequestor::setHttpClient($stub); |
|
| 59 |
+ |
|
| 60 |
+ // make one request to capture its result |
|
| 61 |
+ PhoneNumber::all(); |
|
| 62 |
+ static::assertArrayNotHasKey('X-Telnyx-Client-Telemetry', $requestheaders);
|
|
| 63 |
+ |
|
| 64 |
+ // make another request and verify telemetry isn't sent |
|
| 65 |
+ PhoneNumber::all(); |
|
| 66 |
+ static::assertArrayNotHasKey('X-Telnyx-Client-Telemetry', $requestheaders);
|
|
| 67 |
+ |
|
| 68 |
+ ApiRequestor::setHttpClient(null); |
|
| 69 |
+ } |
|
| 70 |
+ |
|
| 71 |
+ public function testTelemetrySetIfEnabled() |
|
| 72 |
+ {
|
|
| 73 |
+ Telnyx::setApiKey(self::TEST_API_KEY); |
|
| 74 |
+ Telnyx::setEnableTelemetry(true); |
|
| 75 |
+ |
|
| 76 |
+ $requestheaders = null; |
|
| 77 |
+ |
|
| 78 |
+ $stub = $this |
|
| 79 |
+ ->getMockBuilder('HttpClient\\ClientInterface')
|
|
| 80 |
+ ->setMethods(['request']) |
|
| 81 |
+ ->getMock() |
|
| 82 |
+ ; |
|
| 83 |
+ |
|
| 84 |
+ $stub->expects(static::any()) |
|
| 85 |
+ ->method('request')
|
|
| 86 |
+ ->with( |
|
| 87 |
+ static::anything(), |
|
| 88 |
+ static::anything(), |
|
| 89 |
+ static::callback(function ($headers) use (&$requestheaders) {
|
|
| 90 |
+ // capture the requested headers and format back to into an assoc array |
|
| 91 |
+ foreach ($headers as $index => $header) {
|
|
| 92 |
+ $components = \explode(': ', $header, 2);
|
|
| 93 |
+ $requestheaders[$components[0]] = $components[1]; |
|
| 94 |
+ } |
|
| 95 |
+ |
|
| 96 |
+ return true; |
|
| 97 |
+ }), |
|
| 98 |
+ static::anything(), |
|
| 99 |
+ static::anything() |
|
| 100 |
+ )->willReturn([self::FAKE_VALID_RESPONSE, 200, ['request-id' => ['req_123']]]); |
|
| 101 |
+ |
|
| 102 |
+ ApiRequestor::setHttpClient($stub); |
|
| 103 |
+ |
|
| 104 |
+ // make one request to capture its result |
|
| 105 |
+ PhoneNumber::all(); |
|
| 106 |
+ static::assertArrayNotHasKey('X-Telnyx-Client-Telemetry', $requestheaders);
|
|
| 107 |
+ |
|
| 108 |
+ // make another request to send the previous |
|
| 109 |
+ PhoneNumber::all(); |
|
| 110 |
+ static::assertArrayHasKey('X-Telnyx-Client-Telemetry', $requestheaders);
|
|
| 111 |
+ |
|
| 112 |
+ $data = \json_decode($requestheaders['X-Telnyx-Client-Telemetry'], true); |
|
| 113 |
+ static::assertNotNull($data['last_request_metrics']['request_duration_ms']); |
|
| 114 |
+ |
|
| 115 |
+ ApiRequestor::setHttpClient(null); |
|
| 116 |
+ } |
|
| 117 |
+} |