7
7
use Twilio \Exceptions \RestException ;
8
8
use Twilio \Http \CurlClient ;
9
9
use Twilio \Http \Response ;
10
+ use Twilio \Page ;
10
11
use Twilio \Rest \Client ;
11
12
use Twilio \Values ;
12
13
use Twilio \Version ;
@@ -51,6 +52,13 @@ public function setVersion(string $version): void {
51
52
}
52
53
}
53
54
55
+
56
+ class TestPage extends Page {
57
+ public function buildInstance (array $ payload ): array {
58
+ return $ payload ;
59
+ }
60
+ }
61
+
54
62
class VersionTest extends UnitTest {
55
63
protected $ curlClient ;
56
64
/** @var Client $client */
@@ -77,76 +85,100 @@ protected function setUp(): void {
77
85
* @param int|null $pageSize PageSize provided by the user
78
86
* @param int|Values::NONE $expectedLimit Expected limit returned by readLimits
79
87
* @param int|Values::NONE $expectedPageSize Expected page size returned by readLimits
80
- * @param int|Values::NONe $expectedPageLimit Expected page limit returned by readLimits
81
88
* @dataProvider readLimitProvider
82
89
*/
83
- public function testReadLimits (string $ message , ?int $ limit , ?int $ pageSize , $ expectedLimit , $ expectedPageSize, $ expectedPageLimit ): void {
90
+ public function testReadLimits (string $ message , ?int $ limit , ?int $ pageSize , $ expectedLimit , $ expectedPageSize ): void {
84
91
$ actual = $ this ->version ->readLimits ($ limit , $ pageSize );
85
- $ this ->assertEquals ($ expectedLimit , $ actual ['limit ' ], "$ message: Limit does not match " );
86
- $ this ->assertEquals ($ expectedPageSize , $ actual ['pageSize ' ], "$ message: PageSize does not match " );
87
- $ this ->assertEquals ($ expectedPageLimit , $ actual ['pageLimit ' ], "$ message: PageLimit does not match " );
92
+ self ::assertEquals ($ expectedLimit , $ actual ['limit ' ], "$ message: Limit does not match " );
93
+ self ::assertEquals ($ expectedPageSize , $ actual ['pageSize ' ], "$ message: PageSize does not match " );
88
94
}
89
95
90
96
public function readLimitProvider (): array {
91
97
return [
92
98
[
93
99
'Nothing Specified ' ,
94
100
null , null ,
95
- Values::NONE , Values::NONE , Values:: NONE ,
101
+ Values::NONE , Values::NONE ,
96
102
],
97
103
[
98
104
'Limit Specified - Under Max Page Size ' ,
99
105
Version::MAX_PAGE_SIZE - 1 , null ,
100
- Version::MAX_PAGE_SIZE - 1 , Version::MAX_PAGE_SIZE - 1 , 1 ,
106
+ Version::MAX_PAGE_SIZE - 1 , Version::MAX_PAGE_SIZE - 1 ,
101
107
],
102
108
[
103
109
'Limit Specified - At Max Page Size ' ,
104
110
Version::MAX_PAGE_SIZE , null ,
105
- Version::MAX_PAGE_SIZE , Version::MAX_PAGE_SIZE , 1 ,
111
+ Version::MAX_PAGE_SIZE , Version::MAX_PAGE_SIZE ,
106
112
],
107
113
[
108
114
'Limit Specified - Over Max Page Size ' ,
109
115
Version::MAX_PAGE_SIZE + 1 , null ,
110
- Version::MAX_PAGE_SIZE + 1 , Version::MAX_PAGE_SIZE , 2 ,
116
+ Version::MAX_PAGE_SIZE + 1 , Version::MAX_PAGE_SIZE ,
111
117
],
112
118
[
113
119
'Page Size Specified - Under Max Page Size ' ,
114
120
null , Version::MAX_PAGE_SIZE - 1 ,
115
- Values::NONE , Version::MAX_PAGE_SIZE - 1 , Values:: NONE ,
121
+ Values::NONE , Version::MAX_PAGE_SIZE - 1
116
122
],
117
123
[
118
124
'Page Size Specified - At Max Page Size ' ,
119
125
null , Version::MAX_PAGE_SIZE ,
120
- Values::NONE , Version::MAX_PAGE_SIZE , Values:: NONE ,
126
+ Values::NONE , Version::MAX_PAGE_SIZE
121
127
],
122
128
[
123
129
'Page Size Specified - Over Max Page Size ' ,
124
130
null , Version::MAX_PAGE_SIZE + 1 ,
125
- Values::NONE , Version::MAX_PAGE_SIZE , Values::NONE
126
- ],
127
- [
128
- 'Limit less than Page Size ' ,
129
- 50 , 100 ,
130
- 50 , 100 , 1 ,
131
- ],
132
- [
133
- 'Limit equal to Page Size ' ,
134
- 100 , 100 ,
135
- 100 , 100 , 1 ,
136
- ],
137
- [
138
- 'Limit greater than Page Size - evenly divisible ' ,
139
- 100 , 50 ,
140
- 100 , 50 , 2 ,
141
- ],
142
- [
143
- 'Limit greater than Page Size - not evenly divisible ' ,
144
- 100 , 30 ,
145
- 100 , 30 , 4
131
+ Values::NONE , Version::MAX_PAGE_SIZE
146
132
],
147
133
];
148
134
}
149
135
136
+ /**
137
+ * @param string $message Case message to display on assertion error
138
+ * @param int|null $limit Limit provided by the user
139
+ * @param int|null $pageLimit Page limit provided by the user
140
+ * @param int $expectedCount Expected record count returned by stream
141
+ * @dataProvider streamProvider
142
+ */
143
+ public function testStream (string $ message , ?int $ limit , ?int $ pageLimit , int $ expectedCount ): void {
144
+ $ this ->curlClient
145
+ ->method ('request ' )
146
+ ->will (self ::onConsecutiveCalls (
147
+ new Response (
148
+ 200 ,
149
+ '{
150
+ "next_page_uri": "/2010-04-01/Accounts/AC123/Messages.json?Page=1",
151
+ "messages": [{"body": "payload0"}, {"body": "payload1"}]
152
+ } ' ),
153
+ new Response (
154
+ 200 ,
155
+ '{
156
+ "next_page_uri": "/2010-04-01/Accounts/AC123/Messages.json?Page=2",
157
+ "messages": [{"body": "payload2"}, {"body": "payload3"}]
158
+ } ' ),
159
+ new Response (
160
+ 200 ,
161
+ '{
162
+ "next_page_uri": null,
163
+ "messages": [{"body": "payload4"}]
164
+ } ' )
165
+ ));
166
+
167
+ $ response = $ this ->version ->page ('GET ' , '/Accounts/AC123/Messages.json ' );
168
+ $ page = new TestPage ($ this ->version , $ response );
169
+ $ messages = $ this ->version ->stream ($ page , $ limit , $ pageLimit );
170
+
171
+ self ::assertEquals ($ expectedCount , \iterator_count ($ messages ), "$ message: Count does not match " );
172
+ }
173
+
174
+ public function streamProvider (): array {
175
+ return [
176
+ ['No limits ' , null , null , 5 ],
177
+ ['Item limit ' , 3 , null , 3 ],
178
+ ['Page limit ' , null , 1 , 2 ],
179
+ ];
180
+ }
181
+
150
182
/**
151
183
* @param string $message Case message to display on assertion error
152
184
* @param string $prefix Version prefix to test
@@ -157,7 +189,7 @@ public function readLimitProvider(): array {
157
189
public function testRelativeUri (string $ message , string $ prefix , string $ uri , string $ expected ): void {
158
190
$ this ->version ->setVersion ($ prefix );
159
191
$ actual = $ this ->version ->relativeUri ($ uri );
160
- $ this -> assertEquals ($ expected , $ actual , $ message );
192
+ self :: assertEquals ($ expected , $ actual , $ message );
161
193
}
162
194
163
195
public function relativeUriProvider (): array {
@@ -225,12 +257,12 @@ public function relativeUriProvider(): array {
225
257
public function testAbsoluteUrl (string $ message , string $ prefix , string $ uri , string $ expected ): void {
226
258
$ this ->version ->setVersion ($ prefix );
227
259
$ actual = $ this ->version ->absoluteUrl ($ uri );
228
- $ this -> assertEquals ($ expected , $ actual , $ message );
260
+ self :: assertEquals ($ expected , $ actual , $ message );
229
261
}
230
262
231
263
public function testRestExceptionWithDetails (): void {
232
264
$ this ->curlClient
233
- ->expects ($ this -> once ())
265
+ ->expects (self :: once ())
234
266
->method ('request ' )
235
267
->willReturn (new Response (400 , '{
236
268
"code": 20001,
@@ -244,17 +276,17 @@ public function testRestExceptionWithDetails(): void {
244
276
$ this ->version ->fetch ('get ' , 'http://foo.bar ' );
245
277
self ::fail ();
246
278
}catch (RestException $ rex ){
247
- $ this -> assertEquals (20001 , $ rex ->getCode ());
248
- $ this -> assertEquals (400 , $ rex ->getStatusCode ());
249
- $ this -> assertEquals ('[HTTP 400] Unable to fetch record: Bad request ' , $ rex ->getMessage ());
250
- $ this -> assertEquals ('https://www.twilio.com/docs/errors/20001 ' , $ rex ->getMoreInfo ());
251
- $ this -> assertEquals (["foo " => "bar " ], $ rex ->getDetails ());
279
+ self :: assertEquals (20001 , $ rex ->getCode ());
280
+ self :: assertEquals (400 , $ rex ->getStatusCode ());
281
+ self :: assertEquals ('[HTTP 400] Unable to fetch record: Bad request ' , $ rex ->getMessage ());
282
+ self :: assertEquals ('https://www.twilio.com/docs/errors/20001 ' , $ rex ->getMoreInfo ());
283
+ self :: assertEquals (["foo " => "bar " ], $ rex ->getDetails ());
252
284
}
253
285
}
254
286
255
287
public function testRestExceptionWithoutDetails (): void {
256
288
$ this ->curlClient
257
- ->expects ($ this -> once ())
289
+ ->expects (self :: once ())
258
290
->method ('request ' )
259
291
->willReturn (new Response (400 , '{
260
292
"code": 20001,
@@ -266,28 +298,28 @@ public function testRestExceptionWithoutDetails(): void {
266
298
$ this ->version ->fetch ('get ' , 'http://foo.bar ' );
267
299
self ::fail ();
268
300
}catch (RestException $ rex ){
269
- $ this -> assertEquals (20001 , $ rex ->getCode ());
270
- $ this -> assertEquals (400 , $ rex ->getStatusCode ());
271
- $ this -> assertEquals ('[HTTP 400] Unable to fetch record: Bad request ' , $ rex ->getMessage ());
272
- $ this -> assertEquals ('https://www.twilio.com/docs/errors/20001 ' , $ rex ->getMoreInfo ());
273
- $ this -> assertEmpty ($ rex ->getDetails ());
301
+ self :: assertEquals (20001 , $ rex ->getCode ());
302
+ self :: assertEquals (400 , $ rex ->getStatusCode ());
303
+ self :: assertEquals ('[HTTP 400] Unable to fetch record: Bad request ' , $ rex ->getMessage ());
304
+ self :: assertEquals ('https://www.twilio.com/docs/errors/20001 ' , $ rex ->getMoreInfo ());
305
+ self :: assertEmpty ($ rex ->getDetails ());
274
306
}
275
307
}
276
308
277
309
public function testRestException (): void {
278
310
$ this ->curlClient
279
- ->expects ($ this -> once ())
311
+ ->expects (self :: once ())
280
312
->method ('request ' )
281
313
->willReturn (new Response (400 , '' ));
282
314
try {
283
315
$ this ->version ->fetch ('get ' , 'http://foo.bar ' );
284
316
self ::fail ();
285
317
}catch (RestException $ rex ){
286
- $ this -> assertEquals (400 , $ rex ->getCode ());
287
- $ this -> assertEquals (400 , $ rex ->getStatusCode ());
288
- $ this -> assertEquals ('[HTTP 400] Unable to fetch record ' , $ rex ->getMessage ());
289
- $ this -> assertEquals ('' , $ rex ->getMoreInfo ());
290
- $ this -> assertEmpty ($ rex ->getDetails ());
318
+ self :: assertEquals (400 , $ rex ->getCode ());
319
+ self :: assertEquals (400 , $ rex ->getStatusCode ());
320
+ self :: assertEquals ('[HTTP 400] Unable to fetch record ' , $ rex ->getMessage ());
321
+ self :: assertEquals ('' , $ rex ->getMoreInfo ());
322
+ self :: assertEmpty ($ rex ->getDetails ());
291
323
}
292
324
}
293
325
0 commit comments