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,526 @@
1
+<?php
2
+
3
+namespace Telnyx;
4
+
5
+/**
6
+ * @internal
7
+ * @covers \Telnyx\TelnyxObject
8
+ */
9
+final class TelnyxObjectTest extends \Telnyx\TestCase
10
+{
11
+    /** @var \ReflectionMethod */
12
+    private $deepCopyReflector;
13
+
14
+    /** @var \ReflectionMethod */
15
+    private $optsReflector;
16
+
17
+    /**
18
+     * @before
19
+     */
20
+    public function setUpReflectors()
21
+    {
22
+        // Sets up reflectors needed by some tests to access protected or
23
+        // private attributes.
24
+
25
+        // This is used to invoke the `deepCopy` protected function
26
+        $this->deepCopyReflector = new \ReflectionMethod(\Telnyx\TelnyxObject::class, 'deepCopy');
27
+        $this->deepCopyReflector->setAccessible(true);
28
+
29
+        // This is used to access the `_opts` protected variable
30
+        $this->optsReflector = new \ReflectionProperty(\Telnyx\TelnyxObject::class, '_opts');
31
+        $this->optsReflector->setAccessible(true);
32
+    }
33
+
34
+    public function testArrayAccessorsSemantics()
35
+    {
36
+        $s = new TelnyxObject();
37
+        $s['foo'] = 'a';
38
+        static::assertSame($s['foo'], 'a');
39
+        static::assertTrue(isset($s['foo']));
40
+        unset($s['foo']);
41
+        static::assertFalse(isset($s['foo']));
42
+    }
43
+
44
+    public function testNormalAccessorsSemantics()
45
+    {
46
+        $s = new TelnyxObject();
47
+        $s->foo = 'a';
48
+        static::assertSame($s->foo, 'a');
49
+        static::assertTrue(isset($s->foo));
50
+        $s->foo = null;
51
+        static::assertFalse(isset($s->foo));
52
+    }
53
+
54
+    public function testArrayAccessorsMatchNormalAccessors()
55
+    {
56
+        $s = new TelnyxObject();
57
+        $s->foo = 'a';
58
+        static::assertSame($s['foo'], 'a');
59
+
60
+        $s['bar'] = 'b';
61
+        static::assertSame($s->bar, 'b');
62
+    }
63
+
64
+    public function testCount()
65
+    {
66
+        $s = new TelnyxObject();
67
+        static::assertCount(0, $s);
68
+
69
+        $s['key1'] = 'value1';
70
+        static::assertCount(1, $s);
71
+
72
+        $s['key2'] = 'value2';
73
+        static::assertCount(2, $s);
74
+
75
+        unset($s['key1']);
76
+        static::assertCount(1, $s);
77
+    }
78
+
79
+    public function testKeys()
80
+    {
81
+        $s = new TelnyxObject();
82
+        $s->foo = 'bar';
83
+        static::assertSame($s->keys(), ['foo']);
84
+    }
85
+
86
+    public function testValues()
87
+    {
88
+        $s = new TelnyxObject();
89
+        $s->foo = 'bar';
90
+        static::assertSame($s->values(), ['bar']);
91
+    }
92
+
93
+    public function testToArray()
94
+    {
95
+        $array = [
96
+            'foo' => 'a',
97
+            'list' => [1, 2, 3],
98
+            'null' => null,
99
+            'metadata' => [
100
+                'key' => 'value',
101
+                1 => 'one',
102
+            ],
103
+        ];
104
+        $s = TelnyxObject::constructFrom($array);
105
+
106
+        $converted = $s->toArray();
107
+
108
+        static::assertInternalType('array', $converted);
109
+        static::assertSame($array, $converted);
110
+    }
111
+
112
+    public function testToArrayRecursive()
113
+    {
114
+        // deep nested associative array (when contained in an indexed array)
115
+        // or TelnyxObject
116
+        $nestedArray = ['id' => 7, 'foo' => 'bar'];
117
+        $nested = TelnyxObject::constructFrom($nestedArray);
118
+
119
+        $obj = TelnyxObject::constructFrom([
120
+            'id' => 1,
121
+            // simple associative array that contains a TelnyxObject to help us
122
+            // test deep recursion
123
+            'nested' => ['object' => 'list', 'data' => [$nested]],
124
+            'list' => [$nested],
125
+        ]);
126
+
127
+        $expected = [
128
+            'id' => 1,
129
+            'nested' => ['object' => 'list', 'data' => [$nestedArray]],
130
+            'list' => [$nestedArray],
131
+        ];
132
+
133
+        static::assertSame($expected, $obj->toArray());
134
+    }
135
+
136
+    public function testNonexistentProperty()
137
+    {
138
+        $capture = \tmpfile();
139
+        $origErrorLog = \ini_set('error_log', \stream_get_meta_data($capture)['uri']);
140
+
141
+        try {
142
+            $s = new TelnyxObject();
143
+            static::assertNull($s->nonexistent);
144
+
145
+            static::assertRegExp(
146
+                '/Telnyx Notice: Undefined property of Telnyx\\\\TelnyxObject instance: nonexistent/',
147
+                \stream_get_contents($capture)
148
+            );
149
+        } finally {
150
+            \ini_set('error_log', $origErrorLog);
151
+            \fclose($capture);
152
+        }
153
+    }
154
+
155
+    public function testPropertyDoesNotExists()
156
+    {
157
+        $s = new TelnyxObject();
158
+        static::assertNull($s['nonexistent']);
159
+    }
160
+
161
+    public function testJsonEncode()
162
+    {
163
+        $s = new TelnyxObject();
164
+        $s->foo = 'a';
165
+
166
+        static::assertSame('{"foo":"a"}', \json_encode($s));
167
+    }
168
+
169
+    public function testToString()
170
+    {
171
+        $s = new TelnyxObject();
172
+        $s->foo = 'a';
173
+
174
+        $string = (string) $s;
175
+        $expected = "Telnyx\TelnyxObject JSON: {\n    \"foo\": \"a\"\n}";
176
+        static::assertSame($expected, $string);
177
+    }
178
+
179
+    public function testReplaceNewNestedUpdatable()
180
+    {
181
+        $s = new TelnyxObject();
182
+
183
+        $s->metadata = ['bar'];
184
+        static::assertSame($s->metadata, ['bar']);
185
+        $s->metadata = ['baz', 'qux'];
186
+        static::assertSame($s->metadata, ['baz', 'qux']);
187
+    }
188
+
189
+    public function testSetPermanentAttribute()
190
+    {
191
+        $this->expectException(\InvalidArgumentException::class);
192
+
193
+        $s = new TelnyxObject();
194
+        $s->id = 'abc_123';
195
+    }
196
+
197
+    public function testSetEmptyStringValue()
198
+    {
199
+        $this->expectException(\InvalidArgumentException::class);
200
+
201
+        $s = new TelnyxObject();
202
+        $s->foo = '';
203
+    }
204
+
205
+    public function testSerializeParametersOnEmptyObject()
206
+    {
207
+        $obj = TelnyxObject::constructFrom([]);
208
+        static::assertSame([], $obj->serializeParameters());
209
+    }
210
+
211
+    public function testSerializeParametersOnNewObjectWithSubObject()
212
+    {
213
+        $obj = new TelnyxObject();
214
+        $obj->metadata = ['foo' => 'bar'];
215
+        static::assertSame(['metadata' => ['foo' => 'bar']], $obj->serializeParameters());
216
+    }
217
+
218
+    public function testSerializeParametersOnBasicObject()
219
+    {
220
+        $obj = TelnyxObject::constructFrom(['foo' => null]);
221
+        $obj->updateAttributes(['foo' => 'bar']);
222
+        static::assertSame(['foo' => 'bar'], $obj->serializeParameters());
223
+    }
224
+
225
+    public function testSerializeParametersOnMoreComplexObject()
226
+    {
227
+        $obj = TelnyxObject::constructFrom([
228
+            'foo' => TelnyxObject::constructFrom([
229
+                'bar' => null,
230
+                'baz' => null,
231
+            ]),
232
+        ]);
233
+        $obj->foo->bar = 'newbar';
234
+        static::assertSame(['foo' => ['bar' => 'newbar']], $obj->serializeParameters());
235
+    }
236
+
237
+    public function testSerializeParametersOnArray()
238
+    {
239
+        $obj = TelnyxObject::constructFrom([
240
+            'foo' => null,
241
+        ]);
242
+        $obj->foo = ['new-value'];
243
+        static::assertSame(['foo' => ['new-value']], $obj->serializeParameters());
244
+    }
245
+
246
+    public function testSerializeParametersOnArrayThatShortens()
247
+    {
248
+        $obj = TelnyxObject::constructFrom([
249
+            'foo' => ['0-index', '1-index', '2-index'],
250
+        ]);
251
+        $obj->foo = ['new-value'];
252
+        static::assertSame(['foo' => ['new-value']], $obj->serializeParameters());
253
+    }
254
+
255
+    public function testSerializeParametersOnArrayThatLengthens()
256
+    {
257
+        $obj = TelnyxObject::constructFrom([
258
+            'foo' => ['0-index', '1-index', '2-index'],
259
+        ]);
260
+        $obj->foo = \array_fill(0, 4, 'new-value');
261
+        static::assertSame(['foo' => \array_fill(0, 4, 'new-value')], $obj->serializeParameters());
262
+    }
263
+
264
+    public function testSerializeParametersOnArrayOfHashes()
265
+    {
266
+        $obj = TelnyxObject::constructFrom(['foo' => null]);
267
+        $obj->foo = [
268
+            TelnyxObject::constructFrom(['bar' => null]),
269
+        ];
270
+
271
+        $obj->foo[0]->bar = 'baz';
272
+        static::assertSame(['foo' => [['bar' => 'baz']]], $obj->serializeParameters());
273
+    }
274
+
275
+    public function testSerializeParametersDoesNotIncludeUnchangedValues()
276
+    {
277
+        $obj = TelnyxObject::constructFrom([
278
+            'foo' => null,
279
+        ]);
280
+        static::assertSame([], $obj->serializeParameters());
281
+    }
282
+
283
+    public function testSerializeParametersOnUnchangedArray()
284
+    {
285
+        $obj = TelnyxObject::constructFrom([
286
+            'foo' => ['0-index', '1-index', '2-index'],
287
+        ]);
288
+        $obj->foo = ['0-index', '1-index', '2-index'];
289
+        static::assertSame([], $obj->serializeParameters());
290
+    }
291
+
292
+    public function testSerializeParametersWithTelnyxObject()
293
+    {
294
+        $obj = TelnyxObject::constructFrom([]);
295
+        $obj->metadata = TelnyxObject::constructFrom(['foo' => 'bar']);
296
+
297
+        $serialized = $obj->serializeParameters();
298
+        static::assertSame(['foo' => 'bar'], $serialized['metadata']);
299
+    }
300
+
301
+    public function testSerializeParametersOnReplacedTelnyxObject()
302
+    {
303
+        $obj = TelnyxObject::constructFrom([
304
+            'source' => TelnyxObject::constructFrom(['bar' => 'foo']),
305
+        ]);
306
+        $obj->source = TelnyxObject::constructFrom(['baz' => 'foo']);
307
+
308
+        $serialized = $obj->serializeParameters();
309
+        static::assertSame(['baz' => 'foo'], $serialized['source']);
310
+    }
311
+
312
+    public function testSerializeParametersOnReplacedTelnyxObjectWhichIsMetadata()
313
+    {
314
+        $obj = TelnyxObject::constructFrom([
315
+            'metadata' => TelnyxObject::constructFrom(['bar' => 'foo']),
316
+        ]);
317
+        $obj->metadata = TelnyxObject::constructFrom(['baz' => 'foo']);
318
+
319
+        $serialized = $obj->serializeParameters();
320
+        static::assertSame(['bar' => '', 'baz' => 'foo'], $serialized['metadata']);
321
+    }
322
+
323
+    public function testSerializeParametersOnArrayOfTelnyxObjects()
324
+    {
325
+        $obj = TelnyxObject::constructFrom([]);
326
+        $obj->metadata = [
327
+            TelnyxObject::constructFrom(['foo' => 'bar']),
328
+        ];
329
+
330
+        $serialized = $obj->serializeParameters();
331
+        static::assertSame([['foo' => 'bar']], $serialized['metadata']);
332
+    }
333
+
334
+    public function testSerializeParametersOnSetApiResource()
335
+    {
336
+        $address = Address::constructFrom(['id' => 'cus_123']);
337
+        $obj = TelnyxObject::constructFrom([]);
338
+
339
+        // the key here is that the property is set explicitly (and therefore
340
+        // marked as unsaved), which is why it gets included below
341
+        $obj->address = $address;
342
+
343
+        $serialized = $obj->serializeParameters();
344
+        static::assertSame(['address' => $address], $serialized);
345
+    }
346
+
347
+    public function testSerializeParametersOnNotSetApiResource()
348
+    {
349
+        $address = Address::constructFrom(['id' => 'cus_123']);
350
+        $obj = TelnyxObject::constructFrom(['address' => $address]);
351
+
352
+        $serialized = $obj->serializeParameters();
353
+        static::assertSame([], $serialized);
354
+    }
355
+
356
+    public function testSerializeParametersOnApiResourceFlaggedWithSaveWithParent()
357
+    {
358
+        $address = Address::constructFrom(['id' => 'cus_123']);
359
+        $address->saveWithParent = true;
360
+
361
+        $obj = TelnyxObject::constructFrom(['address' => $address]);
362
+
363
+        $serialized = $obj->serializeParameters();
364
+        static::assertSame(['address' => []], $serialized);
365
+    }
366
+
367
+    public function testSerializeParametersRaisesExceotionOnOtherEmbeddedApiResources()
368
+    {
369
+        // This address doesn't have an ID and therefore the library doesn't know
370
+        // what to do with it and throws an InvalidArgumentException because it's
371
+        // probably not what the user expected to happen.
372
+        $address = Address::constructFrom([]);
373
+
374
+        $obj = TelnyxObject::constructFrom([]);
375
+        $obj->address = $address;
376
+
377
+        try {
378
+            $serialized = $obj->serializeParameters();
379
+            static::fail('Did not raise error');
380
+        } catch (\InvalidArgumentException $e) {
381
+            static::assertSame(
382
+                'Cannot save property `address` containing an API resource of type Telnyx\\Address. ' .
383
+                "It doesn't appear to be persisted and is not marked as `saveWithParent`.",
384
+                $e->getMessage()
385
+            );
386
+        } catch (\Exception $e) {
387
+            static::fail('Unexpected exception: ' . \get_class($e));
388
+        }
389
+    }
390
+
391
+    public function testSerializeParametersForce()
392
+    {
393
+        $obj = TelnyxObject::constructFrom([
394
+            'id' => 'id',
395
+            'metadata' => TelnyxObject::constructFrom([
396
+                'bar' => 'foo',
397
+            ]),
398
+        ]);
399
+
400
+        $serialized = $obj->serializeParameters(true);
401
+        static::assertSame(['id' => 'id', 'metadata' => ['bar' => 'foo']], $serialized);
402
+    }
403
+
404
+    public function testDirty()
405
+    {
406
+        $obj = TelnyxObject::constructFrom([
407
+            'id' => 'id',
408
+            'metadata' => TelnyxObject::constructFrom([
409
+                'bar' => 'foo',
410
+            ]),
411
+        ]);
412
+
413
+        // note that `$force` and `dirty()` are for different things, but are
414
+        // functionally equivalent
415
+        $obj->dirty();
416
+
417
+        $serialized = $obj->serializeParameters();
418
+        static::assertSame(['id' => 'id', 'metadata' => ['bar' => 'foo']], $serialized);
419
+    }
420
+
421
+    public function testDeepCopy()
422
+    {
423
+        $opts = [
424
+            'api_base' => Telnyx::$apiBase,
425
+            'api_key' => 'apikey',
426
+        ];
427
+        $values = [
428
+            'id' => 1,
429
+            'name' => 'Telnyx',
430
+            'arr' => [
431
+                TelnyxObject::constructFrom(['id' => 'index0'], $opts),
432
+                'index1',
433
+                2,
434
+            ],
435
+            'map' => [
436
+                '0' => TelnyxObject::constructFrom(['id' => 'index0'], $opts),
437
+                '1' => 'index1',
438
+                '2' => 2,
439
+            ],
440
+        ];
441
+
442
+        $copyValues = $this->deepCopyReflector->invoke(null, $values);
443
+
444
+        // we can't compare the hashes directly because they have embedded
445
+        // objects which are different from each other
446
+        static::assertSame($values['id'], $copyValues['id']);
447
+        static::assertSame($values['name'], $copyValues['name']);
448
+        static::assertSame(\count($values['arr']), \count($copyValues['arr']));
449
+
450
+        // internal values of the copied TelnyxObject should be the same,
451
+        // but the object itself should be new (hence the assertNotSame)
452
+        static::assertSame($values['arr'][0]['id'], $copyValues['arr'][0]['id']);
453
+        static::assertNotSame($values['arr'][0], $copyValues['arr'][0]);
454
+
455
+        // likewise, the Util\RequestOptions instance in _opts should have
456
+        // copied values but be a new instance
457
+        static::assertSame(
458
+            $this->optsReflector->getValue($values['arr'][0])->apiKey,
459
+            $this->optsReflector->getValue($copyValues['arr'][0])->apiKey
460
+        );
461
+        static::assertNotSame(
462
+            $this->optsReflector->getValue($values['arr'][0]),
463
+            $this->optsReflector->getValue($copyValues['arr'][0])
464
+        );
465
+
466
+        // scalars however, can be compared
467
+        static::assertSame($values['arr'][1], $copyValues['arr'][1]);
468
+        static::assertSame($values['arr'][2], $copyValues['arr'][2]);
469
+
470
+        // and a similar story with the hash
471
+        static::assertSame($values['map']['0']['id'], $copyValues['map']['0']['id']);
472
+        static::assertNotSame($values['map']['0'], $copyValues['map']['0']);
473
+        static::assertNotSame(
474
+            $this->optsReflector->getValue($values['arr'][0]),
475
+            $this->optsReflector->getValue($copyValues['arr'][0])
476
+        );
477
+        static::assertSame(
478
+            $this->optsReflector->getValue($values['map']['0'])->apiKey,
479
+            $this->optsReflector->getValue($copyValues['map']['0'])->apiKey
480
+        );
481
+        static::assertNotSame(
482
+            $this->optsReflector->getValue($values['map']['0']),
483
+            $this->optsReflector->getValue($copyValues['map']['0'])
484
+        );
485
+        static::assertSame($values['map']['1'], $copyValues['map']['1']);
486
+        static::assertSame($values['map']['2'], $copyValues['map']['2']);
487
+    }
488
+
489
+    public function testDeepCopyMaintainClass()
490
+    {
491
+        $phone_number = PhoneNumber::constructFrom(['id' => 1], null);
492
+        $copyPhoneNumber = $this->deepCopyReflector->invoke(null, $phone_number);
493
+        static::assertSame(\get_class($phone_number), \get_class($copyPhoneNumber));
494
+    }
495
+
496
+    public function testIsDeleted()
497
+    {
498
+        $obj = TelnyxObject::constructFrom([]);
499
+        static::assertFalse($obj->isDeleted());
500
+
501
+        $obj = TelnyxObject::constructFrom(['deleted' => false]);
502
+        static::assertFalse($obj->isDeleted());
503
+
504
+        $obj = TelnyxObject::constructFrom(['deleted' => true]);
505
+        static::assertTrue($obj->isDeleted());
506
+    }
507
+
508
+    public function testDeserializeEmptyMetadata()
509
+    {
510
+        $obj = TelnyxObject::constructFrom([
511
+            'metadata' => [],
512
+        ]);
513
+
514
+        static::assertInstanceOf(\Telnyx\TelnyxObject::class, $obj->metadata);
515
+    }
516
+
517
+    public function testDeserializeMetadataWithKeyNamedMetadata()
518
+    {
519
+        $obj = TelnyxObject::constructFrom([
520
+            'metadata' => ['metadata' => 'value'],
521
+        ]);
522
+
523
+        static::assertInstanceOf(\Telnyx\TelnyxObject::class, $obj->metadata);
524
+        static::assertSame('value', $obj->metadata->metadata);
525
+    }
526
+}