| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,163 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace Telnyx\Util; |
|
| 4 |
+ |
|
| 5 |
+/** |
|
| 6 |
+ * @internal |
|
| 7 |
+ * @covers \Telnyx\Util\Util |
|
| 8 |
+ */ |
|
| 9 |
+final class UtilTest extends \Telnyx\TestCase |
|
| 10 |
+{
|
|
| 11 |
+ public function testIsList() |
|
| 12 |
+ {
|
|
| 13 |
+ $list = [5, 'nstaoush', []]; |
|
| 14 |
+ static::assertTrue(Util::isList($list)); |
|
| 15 |
+ |
|
| 16 |
+ $notlist = [5, 'nstaoush', [], 'bar' => 'baz']; |
|
| 17 |
+ static::assertFalse(Util::isList($notlist)); |
|
| 18 |
+ } |
|
| 19 |
+ |
|
| 20 |
+ public function testThatPHPHasValueSemanticsForArrays() |
|
| 21 |
+ {
|
|
| 22 |
+ $original = ['php-arrays' => 'value-semantics']; |
|
| 23 |
+ $derived = $original; |
|
| 24 |
+ $derived['php-arrays'] = 'reference-semantics'; |
|
| 25 |
+ |
|
| 26 |
+ static::assertSame('value-semantics', $original['php-arrays']);
|
|
| 27 |
+ } |
|
| 28 |
+ |
|
| 29 |
+ public function testConvertTelnyxObjectToArrayIncludesId() |
|
| 30 |
+ {
|
|
| 31 |
+ $customer = Util::convertToTelnyxObject( |
|
| 32 |
+ [ |
|
| 33 |
+ 'id' => 'cus_123', |
|
| 34 |
+ 'object' => 'customer', |
|
| 35 |
+ ], |
|
| 36 |
+ null |
|
| 37 |
+ ); |
|
| 38 |
+ static::assertArrayHasKey('id', $customer->toArray());
|
|
| 39 |
+ } |
|
| 40 |
+ |
|
| 41 |
+ public function testUtf8() |
|
| 42 |
+ {
|
|
| 43 |
+ // UTF-8 string |
|
| 44 |
+ $x = "\xc3\xa9"; |
|
| 45 |
+ static::assertSame(Util::utf8($x), $x); |
|
| 46 |
+ |
|
| 47 |
+ // Latin-1 string |
|
| 48 |
+ $x = "\xe9"; |
|
| 49 |
+ static::assertSame(Util::utf8($x), "\xc3\xa9"); |
|
| 50 |
+ |
|
| 51 |
+ // Not a string |
|
| 52 |
+ $x = true; |
|
| 53 |
+ static::assertSame(Util::utf8($x), $x); |
|
| 54 |
+ } |
|
| 55 |
+ |
|
| 56 |
+ |
|
| 57 |
+ public function testObjectsToIds() |
|
| 58 |
+ {
|
|
| 59 |
+ $params = [ |
|
| 60 |
+ 'foo' => 'bar', |
|
| 61 |
+ 'phone_number' => Util::convertToTelnyxObject( |
|
| 62 |
+ [ |
|
| 63 |
+ 'id' => 'cus_123', |
|
| 64 |
+ 'record_type' => 'phone_number', |
|
| 65 |
+ ], |
|
| 66 |
+ null |
|
| 67 |
+ ), |
|
| 68 |
+ 'null_value' => null, |
|
| 69 |
+ ]; |
|
| 70 |
+ |
|
| 71 |
+ static::assertSame( |
|
| 72 |
+ [ |
|
| 73 |
+ 'foo' => 'bar', |
|
| 74 |
+ 'phone_number' => 'cus_123', |
|
| 75 |
+ ], |
|
| 76 |
+ Util::objectsToIds($params) |
|
| 77 |
+ ); |
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 80 |
+ public function testEncodeParameters() |
|
| 81 |
+ {
|
|
| 82 |
+ $params = [ |
|
| 83 |
+ 'a' => 3, |
|
| 84 |
+ 'b' => '+foo?', |
|
| 85 |
+ 'c' => 'bar&baz', |
|
| 86 |
+ 'd' => ['a' => 'a', 'b' => 'b'], |
|
| 87 |
+ 'e' => [0, 1], |
|
| 88 |
+ 'f' => '', |
|
| 89 |
+ |
|
| 90 |
+ // note the empty hash won't even show up in the request |
|
| 91 |
+ 'g' => [], |
|
| 92 |
+ ]; |
|
| 93 |
+ |
|
| 94 |
+ static::assertSame( |
|
| 95 |
+ 'a=3&b=%2Bfoo%3F&c=bar%26baz&d[a]=a&d[b]=b&e[0]=0&e[1]=1&f=', |
|
| 96 |
+ Util::encodeParameters($params) |
|
| 97 |
+ ); |
|
| 98 |
+ } |
|
| 99 |
+ |
|
| 100 |
+ public function testUrlEncode() |
|
| 101 |
+ {
|
|
| 102 |
+ static::assertSame('foo', Util::urlEncode('foo'));
|
|
| 103 |
+ static::assertSame('foo%2B', Util::urlEncode('foo+'));
|
|
| 104 |
+ static::assertSame('foo%26', Util::urlEncode('foo&'));
|
|
| 105 |
+ static::assertSame('foo[bar]', Util::urlEncode('foo[bar]'));
|
|
| 106 |
+ } |
|
| 107 |
+ |
|
| 108 |
+ public function testFlattenParams() |
|
| 109 |
+ {
|
|
| 110 |
+ $params = [ |
|
| 111 |
+ 'a' => 3, |
|
| 112 |
+ 'b' => '+foo?', |
|
| 113 |
+ 'c' => 'bar&baz', |
|
| 114 |
+ 'd' => ['a' => 'a', 'b' => 'b'], |
|
| 115 |
+ 'e' => [0, 1], |
|
| 116 |
+ 'f' => [ |
|
| 117 |
+ ['foo' => '1', 'ghi' => '2'], |
|
| 118 |
+ ['foo' => '3', 'bar' => '4'], |
|
| 119 |
+ ], |
|
| 120 |
+ ]; |
|
| 121 |
+ |
|
| 122 |
+ static::assertSame( |
|
| 123 |
+ [ |
|
| 124 |
+ ['a', 3], |
|
| 125 |
+ ['b', '+foo?'], |
|
| 126 |
+ ['c', 'bar&baz'], |
|
| 127 |
+ ['d[a]', 'a'], |
|
| 128 |
+ ['d[b]', 'b'], |
|
| 129 |
+ ['e[0]', 0], |
|
| 130 |
+ ['e[1]', 1], |
|
| 131 |
+ ['f[0][foo]', '1'], |
|
| 132 |
+ ['f[0][ghi]', '2'], |
|
| 133 |
+ ['f[1][foo]', '3'], |
|
| 134 |
+ ['f[1][bar]', '4'], |
|
| 135 |
+ ], |
|
| 136 |
+ Util::flattenParams($params) |
|
| 137 |
+ ); |
|
| 138 |
+ } |
|
| 139 |
+ |
|
| 140 |
+ public function testNormalizeId() |
|
| 141 |
+ {
|
|
| 142 |
+ // Test array version |
|
| 143 |
+ $result1 = Util::normalizeId(['id'=>'123', 'a'=>'b', 'c'=>'d']); |
|
| 144 |
+ |
|
| 145 |
+ static::assertSame('123', $result1[0]);
|
|
| 146 |
+ static::assertSame(['a'=>'b', 'c'=>'d'], $result1[1]); |
|
| 147 |
+ |
|
| 148 |
+ // Test flat version |
|
| 149 |
+ $result2 = Util::normalizeId('123');
|
|
| 150 |
+ |
|
| 151 |
+ static::assertSame('123', $result2[0]);
|
|
| 152 |
+ static::assertSame([], $result2[1]); |
|
| 153 |
+ } |
|
| 154 |
+ |
|
| 155 |
+ public function testSecureCompare() |
|
| 156 |
+ {
|
|
| 157 |
+ $expected = crypt('12345', '$2a$07$usesomesillystringforsalt$');
|
|
| 158 |
+ $correct = crypt('12345', '$2a$07$usesomesillystringforsalt$');
|
|
| 159 |
+ |
|
| 160 |
+ $result1 = Util::secureCompare($expected, $correct); |
|
| 161 |
+ static::assertSame(true, $result1); |
|
| 162 |
+ } |
|
| 163 |
+} |