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,221 @@
1
+<?php
2
+
3
+namespace Telnyx;
4
+
5
+/**
6
+ * @internal
7
+ * @covers \Telnyx\Collection
8
+ */
9
+final class CollectionTest extends \Telnyx\TestCase
10
+{
11
+    /**
12
+     * @before
13
+     */
14
+    public function setUpFixture()
15
+    {
16
+        $this->fixture = Collection::constructFrom([
17
+            'url' => '/things',
18
+            'data' => [
19
+                ['id' => 1],
20
+                ['id' => 2],
21
+                ['id' => 3]
22
+            ],
23
+            'meta' => [
24
+                'page_size' => 3,
25
+                'page_number' => 2,
26
+                'total_results' => 6,
27
+                'total_pages' => 4
28
+            ]
29
+        ]);
30
+    }
31
+
32
+    public function testCanList()
33
+    {
34
+        $this->stubRequest(
35
+            'GET',
36
+            '/things',
37
+            [],
38
+            null,
39
+            false,
40
+            [
41
+                'data' => [
42
+                    ['id' => 1],
43
+                    ['id' => 2],
44
+                    ['id' => 3]
45
+                ],
46
+                'meta' => [
47
+                    'page_size' => 3,
48
+                    'page_number' => 2,
49
+                    'total_results' => 6,
50
+                    'total_pages' => 4
51
+                ]
52
+            ]
53
+        );
54
+
55
+        $resources = $this->fixture->all();
56
+        $this->assertTrue(is_array($resources['data']));
57
+    }
58
+
59
+    public function testCanRetrieve()
60
+    {
61
+        $this->stubRequest(
62
+            'GET',
63
+            '/things/1',
64
+            [],
65
+            null,
66
+            false,
67
+            [
68
+                'id' => 1,
69
+            ]
70
+        );
71
+
72
+        $this->fixture->retrieve(1);
73
+    }
74
+
75
+    public function testCanCreate()
76
+    {
77
+        $this->stubRequest(
78
+            'POST',
79
+            '/things',
80
+            [
81
+                'foo' => 'bar',
82
+            ],
83
+            null,
84
+            false,
85
+            [
86
+                'id' => 2,
87
+            ]
88
+        );
89
+
90
+        $this->fixture->create([
91
+            'foo' => 'bar',
92
+        ]);
93
+    }
94
+
95
+    public function testCanIterate()
96
+    {
97
+        $seen = [];
98
+        foreach ($this->fixture['data'] as $item) {
99
+            array_push($seen, $item['id']);
100
+        }
101
+
102
+        $this->assertSame([1, 2, 3], $seen);
103
+    }
104
+
105
+    public function testSupportsIteratorToArray()
106
+    {
107
+        $seen = [];
108
+        foreach (iterator_to_array($this->fixture) as $item) {
109
+            array_push($seen, $item['id']);
110
+        }
111
+
112
+        $this->assertSame([1, 2, 3], $seen);
113
+    }
114
+
115
+    public function testHeaders()
116
+    {
117
+        $this->stubRequest(
118
+            'POST',
119
+            '/things',
120
+            [
121
+                'foo' => 'bar',
122
+            ],
123
+            [
124
+                'Telnyx-Account: acct_foo',
125
+                'Idempotency-Key: qwertyuiop',
126
+            ],
127
+            false,
128
+            [
129
+                'id' => 2,
130
+            ]
131
+        );
132
+
133
+        $this->fixture->create([
134
+            'foo' => 'bar',
135
+        ], [
136
+            'telnyx_account' => 'acct_foo',
137
+            'idempotency_key' => 'qwertyuiop',
138
+        ]);
139
+    }
140
+
141
+    public function testEmptyCollection()
142
+    {
143
+        $emptyCollection = Collection::emptyCollection();
144
+        $this->assertEquals([], $emptyCollection->data);
145
+    }
146
+
147
+    public function testIsEmpty()
148
+    {
149
+        $empty = Collection::constructFrom(['data' => []]);
150
+        $this->assertTrue($empty->isEmpty());
151
+
152
+        $notEmpty = Collection::constructFrom(['data' => [['id' => 1]]]);
153
+        $this->assertFalse($notEmpty->isEmpty());
154
+    }
155
+
156
+    public function testNextPage()
157
+    {
158
+        $this->stubRequest(
159
+            'GET',
160
+            '/things',
161
+            [
162
+                'page[number]' => 3
163
+            ],
164
+            null,
165
+            false,
166
+            [
167
+                'data' => [
168
+                    ['id' => 1],
169
+                    ['id' => 2],
170
+                    ['id' => 3]
171
+                ],
172
+                'meta' => [
173
+                    'page_size' => 3,
174
+                    'page_number' => 2,
175
+                    'total_results' => 6,
176
+                    'total_pages' => 4
177
+                ]
178
+            ]
179
+        );
180
+
181
+        $nextPage = $this->fixture->nextPage();
182
+        $ids = [];
183
+        foreach ($nextPage->data as $element) {
184
+            array_push($ids, $element['id']);
185
+        }
186
+        $this->assertEquals([1, 2, 3], $ids);
187
+    }
188
+
189
+    public function testPreviousPage()
190
+    {
191
+        $this->stubRequest(
192
+            'GET',
193
+            '/things',
194
+            [
195
+                'page[number]' => 1
196
+            ],
197
+            null,
198
+            false,
199
+            [
200
+                'data' => [
201
+                    ['id' => 1],
202
+                    ['id' => 2],
203
+                    ['id' => 3]
204
+                ],
205
+                'meta' => [
206
+                    'page_size' => 3,
207
+                    'page_number' => 2,
208
+                    'total_results' => 6,
209
+                    'total_pages' => 4
210
+                ]
211
+            ]
212
+        );
213
+
214
+        $previousPage = $this->fixture->previousPage();
215
+        $ids = [];
216
+        foreach ($previousPage->data as $element) {
217
+            array_push($ids, $element['id']);
218
+        }
219
+        $this->assertEquals([1, 2, 3], $ids);
220
+    }
221
+}