Skip to content

Commit 79172ed

Browse files
committed
test: update failed tests
1 parent 7c99acf commit 79172ed

2 files changed

Lines changed: 79 additions & 46 deletions

File tree

tests/system/API/ResponseTraitTest.php

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ public function testNoFormatterJSON()
110110
{
111111
$this->formatter = null;
112112
$controller = $this->makeController([], 'http://codeigniter.com', ['Accept' => 'application/json']);
113-
$controller->respondCreated(['id' => 3], 'A Custom Reason');
113+
114+
$this->invoke($controller, 'respondCreated', [['id' => 3], 'A Custom Reason']);
114115

115116
$this->assertSame('A Custom Reason', $this->response->getReason());
116117
$this->assertSame(201, $this->response->getStatusCode());
@@ -127,7 +128,8 @@ public function testNoFormatter()
127128
{
128129
$this->formatter = null;
129130
$controller = $this->makeController([], 'http://codeigniter.com', ['Accept' => 'application/json']);
130-
$controller->respondCreated('A Custom Reason');
131+
132+
$this->invoke($controller, 'respondCreated', ['A Custom Reason']);
131133

132134
$this->assertSame('A Custom Reason', $this->response->getBody());
133135
}
@@ -137,12 +139,14 @@ public function testAssociativeArrayPayload()
137139
$this->formatter = null;
138140
$controller = $this->makeController();
139141
$payload = ['answer' => 42];
140-
$expected = <<<'EOH'
142+
143+
$this->invoke($controller, 'respond', [$payload]);
144+
145+
$expected = <<<'EOH'
141146
{
142147
"answer": 42
143148
}
144149
EOH;
145-
$controller->respond($payload);
146150
$this->assertSame($expected, $this->response->getBody());
147151
}
148152

@@ -155,14 +159,16 @@ public function testArrayPayload()
155159
2,
156160
3,
157161
];
162+
163+
$this->invoke($controller, 'respond', [$payload]);
164+
158165
$expected = <<<'EOH'
159166
[
160167
1,
161168
2,
162169
3
163170
]
164171
EOH;
165-
$controller->respond($payload);
166172
$this->assertSame($expected, $this->response->getBody());
167173
}
168174

@@ -173,20 +179,23 @@ public function testPHPtoArrayPayload()
173179
$payload = new stdClass();
174180
$payload->name = 'Tom';
175181
$payload->id = 1;
176-
$expected = <<<'EOH'
182+
183+
$this->invoke($controller, 'respond', [(array) $payload]);
184+
185+
$expected = <<<'EOH'
177186
{
178187
"name": "Tom",
179188
"id": 1
180189
}
181190
EOH;
182-
$controller->respond((array) $payload);
183191
$this->assertSame($expected, $this->response->getBody());
184192
}
185193

186194
public function testRespondSets404WithNoData()
187195
{
188196
$controller = $this->makeController();
189-
$controller->respond(null, null);
197+
198+
$this->invoke($controller, 'respond', [null, null]);
190199

191200
$this->assertSame(404, $this->response->getStatusCode());
192201
$this->assertNull($this->response->getBody());
@@ -195,7 +204,8 @@ public function testRespondSets404WithNoData()
195204
public function testRespondSetsStatusWithEmptyData()
196205
{
197206
$controller = $this->makeController();
198-
$controller->respond(null, 201);
207+
208+
$this->invoke($controller, 'respond', [null, 201]);
199209

200210
$this->assertSame(201, $this->response->getStatusCode());
201211
$this->assertNull($this->response->getBody());
@@ -204,7 +214,8 @@ public function testRespondSetsStatusWithEmptyData()
204214
public function testRespondSetsCorrectBodyAndStatus()
205215
{
206216
$controller = $this->makeController();
207-
$controller->respond('something', 201);
217+
218+
$this->invoke($controller, 'respond', ['something', 201]);
208219

209220
$this->assertSame(201, $this->response->getStatusCode());
210221
$this->assertSame('something', $this->response->getBody());
@@ -215,7 +226,8 @@ public function testRespondSetsCorrectBodyAndStatus()
215226
public function testRespondWithCustomReason()
216227
{
217228
$controller = $this->makeController();
218-
$controller->respond('something', 201, 'A Custom Reason');
229+
230+
$this->invoke($controller, 'respond', ['something', 201, 'A Custom Reason']);
219231

220232
$this->assertSame(201, $this->response->getStatusCode());
221233
$this->assertSame('A Custom Reason', $this->response->getReason());
@@ -225,7 +237,7 @@ public function testFailSingleMessage()
225237
{
226238
$controller = $this->makeController();
227239

228-
$controller->fail('Failure to Launch', 500, 'WHAT!', 'A Custom Reason');
240+
$this->invoke($controller, 'fail', ['Failure to Launch', 500, 'WHAT!', 'A Custom Reason']);
229241

230242
// Will use the JSON formatter by default
231243
$expected = [
@@ -235,7 +247,6 @@ public function testFailSingleMessage()
235247
'error' => 'Failure to Launch',
236248
],
237249
];
238-
239250
$this->assertSame($this->formatter->format($expected), $this->response->getBody());
240251
$this->assertSame(500, $this->response->getStatusCode());
241252
$this->assertSame('A Custom Reason', $this->response->getReason());
@@ -244,7 +255,8 @@ public function testFailSingleMessage()
244255
public function testCreated()
245256
{
246257
$controller = $this->makeController();
247-
$controller->respondCreated(['id' => 3], 'A Custom Reason');
258+
259+
$this->invoke($controller, 'respondCreated', [['id' => 3], 'A Custom Reason']);
248260

249261
$this->assertSame('A Custom Reason', $this->response->getReason());
250262
$this->assertSame(201, $this->response->getStatusCode());
@@ -254,7 +266,8 @@ public function testCreated()
254266
public function testDeleted()
255267
{
256268
$controller = $this->makeController();
257-
$controller->respondDeleted(['id' => 3], 'A Custom Reason');
269+
270+
$this->invoke($controller, 'respondDeleted', [['id' => 3], 'A Custom Reason']);
258271

259272
$this->assertSame('A Custom Reason', $this->response->getReason());
260273
$this->assertSame(200, $this->response->getStatusCode());
@@ -264,7 +277,8 @@ public function testDeleted()
264277
public function testUpdated()
265278
{
266279
$controller = $this->makeController();
267-
$controller->respondUpdated(['id' => 3], 'A Custom Reason');
280+
281+
$this->invoke($controller, 'respondUpdated', [['id' => 3], 'A Custom Reason']);
268282

269283
$this->assertSame('A Custom Reason', $this->response->getReason());
270284
$this->assertSame(200, $this->response->getStatusCode());
@@ -274,7 +288,8 @@ public function testUpdated()
274288
public function testUnauthorized()
275289
{
276290
$controller = $this->makeController();
277-
$controller->failUnauthorized('Nope', 'FAT CHANCE', 'A Custom Reason');
291+
292+
$this->invoke($controller, 'failUnauthorized', ['Nope', 'FAT CHANCE', 'A Custom Reason']);
278293

279294
$expected = [
280295
'status' => 401,
@@ -283,7 +298,6 @@ public function testUnauthorized()
283298
'error' => 'Nope',
284299
],
285300
];
286-
287301
$this->assertSame('A Custom Reason', $this->response->getReason());
288302
$this->assertSame(401, $this->response->getStatusCode());
289303
$this->assertSame($this->formatter->format($expected), $this->response->getBody());
@@ -292,7 +306,8 @@ public function testUnauthorized()
292306
public function testForbidden()
293307
{
294308
$controller = $this->makeController();
295-
$controller->failForbidden('Nope', 'FAT CHANCE', 'A Custom Reason');
309+
310+
$this->invoke($controller, 'failForbidden', ['Nope', 'FAT CHANCE', 'A Custom Reason']);
296311

297312
$expected = [
298313
'status' => 403,
@@ -301,7 +316,6 @@ public function testForbidden()
301316
'error' => 'Nope',
302317
],
303318
];
304-
305319
$this->assertSame('A Custom Reason', $this->response->getReason());
306320
$this->assertSame(403, $this->response->getStatusCode());
307321
$this->assertSame($this->formatter->format($expected), $this->response->getBody());
@@ -310,7 +324,8 @@ public function testForbidden()
310324
public function testNoContent()
311325
{
312326
$controller = $this->makeController();
313-
$controller->respondNoContent('');
327+
328+
$this->invoke($controller, 'respondNoContent', ['']);
314329

315330
$this->assertSame('No Content', $this->response->getReason());
316331
$this->assertSame(204, $this->response->getStatusCode());
@@ -319,7 +334,8 @@ public function testNoContent()
319334
public function testNotFound()
320335
{
321336
$controller = $this->makeController();
322-
$controller->failNotFound('Nope', 'FAT CHANCE', 'A Custom Reason');
337+
338+
$this->invoke($controller, 'failNotFound', ['Nope', 'FAT CHANCE', 'A Custom Reason']);
323339

324340
$expected = [
325341
'status' => 404,
@@ -328,7 +344,6 @@ public function testNotFound()
328344
'error' => 'Nope',
329345
],
330346
];
331-
332347
$this->assertSame('A Custom Reason', $this->response->getReason());
333348
$this->assertSame(404, $this->response->getStatusCode());
334349
$this->assertSame($this->formatter->format($expected), $this->response->getBody());
@@ -337,7 +352,8 @@ public function testNotFound()
337352
public function testValidationError()
338353
{
339354
$controller = $this->makeController();
340-
$controller->failValidationError('Nope', 'FAT CHANCE', 'A Custom Reason');
355+
356+
$this->invoke($controller, 'failValidationError', ['Nope', 'FAT CHANCE', 'A Custom Reason']);
341357

342358
$expected = [
343359
'status' => 400,
@@ -346,7 +362,6 @@ public function testValidationError()
346362
'error' => 'Nope',
347363
],
348364
];
349-
350365
$this->assertSame('A Custom Reason', $this->response->getReason());
351366
$this->assertSame(400, $this->response->getStatusCode());
352367
$this->assertSame($this->formatter->format($expected), $this->response->getBody());
@@ -355,7 +370,8 @@ public function testValidationError()
355370
public function testValidationErrors()
356371
{
357372
$controller = $this->makeController();
358-
$controller->failValidationErrors(['foo' => 'Nope', 'bar' => 'No way'], 'FAT CHANCE', 'A Custom Reason');
373+
374+
$this->invoke($controller, 'failValidationErrors', [['foo' => 'Nope', 'bar' => 'No way'], 'FAT CHANCE', 'A Custom Reason']);
359375

360376
$expected = [
361377
'status' => 400,
@@ -365,7 +381,6 @@ public function testValidationErrors()
365381
'bar' => 'No way',
366382
],
367383
];
368-
369384
$this->assertSame('A Custom Reason', $this->response->getReason());
370385
$this->assertSame(400, $this->response->getStatusCode());
371386
$this->assertSame($this->formatter->format($expected), $this->response->getBody());
@@ -374,7 +389,8 @@ public function testValidationErrors()
374389
public function testResourceExists()
375390
{
376391
$controller = $this->makeController();
377-
$controller->failResourceExists('Nope', 'FAT CHANCE', 'A Custom Reason');
392+
393+
$this->invoke($controller, 'failResourceExists', ['Nope', 'FAT CHANCE', 'A Custom Reason']);
378394

379395
$expected = [
380396
'status' => 409,
@@ -383,7 +399,6 @@ public function testResourceExists()
383399
'error' => 'Nope',
384400
],
385401
];
386-
387402
$this->assertSame('A Custom Reason', $this->response->getReason());
388403
$this->assertSame(409, $this->response->getStatusCode());
389404
$this->assertSame($this->formatter->format($expected), $this->response->getBody());
@@ -392,7 +407,8 @@ public function testResourceExists()
392407
public function testResourceGone()
393408
{
394409
$controller = $this->makeController();
395-
$controller->failResourceGone('Nope', 'FAT CHANCE', 'A Custom Reason');
410+
411+
$this->invoke($controller, 'failResourceGone', ['Nope', 'FAT CHANCE', 'A Custom Reason']);
396412

397413
$expected = [
398414
'status' => 410,
@@ -401,7 +417,6 @@ public function testResourceGone()
401417
'error' => 'Nope',
402418
],
403419
];
404-
405420
$this->assertSame('A Custom Reason', $this->response->getReason());
406421
$this->assertSame(410, $this->response->getStatusCode());
407422
$this->assertSame($this->formatter->format($expected), $this->response->getBody());
@@ -410,7 +425,8 @@ public function testResourceGone()
410425
public function testTooManyRequests()
411426
{
412427
$controller = $this->makeController();
413-
$controller->failTooManyRequests('Nope', 'FAT CHANCE', 'A Custom Reason');
428+
429+
$this->invoke($controller, 'failTooManyRequests', ['Nope', 'FAT CHANCE', 'A Custom Reason']);
414430

415431
$expected = [
416432
'status' => 429,
@@ -419,7 +435,6 @@ public function testTooManyRequests()
419435
'error' => 'Nope',
420436
],
421437
];
422-
423438
$this->assertSame('A Custom Reason', $this->response->getReason());
424439
$this->assertSame(429, $this->response->getStatusCode());
425440
$this->assertSame($this->formatter->format($expected), $this->response->getBody());
@@ -428,7 +443,8 @@ public function testTooManyRequests()
428443
public function testServerError()
429444
{
430445
$controller = $this->makeController();
431-
$controller->failServerError('Nope.', 'FAT-CHANCE', 'A custom reason.');
446+
447+
$this->invoke($controller, 'failServerError', ['Nope.', 'FAT-CHANCE', 'A custom reason.']);
432448

433449
$this->assertSame('A custom reason.', $this->response->getReason());
434450
$this->assertSame(500, $this->response->getStatusCode());
@@ -491,7 +507,8 @@ public function testXMLFormatter()
491507

492508
$this->assertInstanceOf('CodeIgniter\Format\XMLFormatter', $this->formatter);
493509

494-
$controller->respondCreated(['id' => 3], 'A Custom Reason');
510+
$this->invoke($controller, 'respondCreated', [['id' => 3], 'A Custom Reason']);
511+
495512
$expected = <<<'EOH'
496513
<?xml version="1.0"?>
497514
<response><id>3</id></response>
@@ -540,23 +557,24 @@ public function __construct(&$request, &$response)
540557
}
541558
};
542559

543-
$controller->respondCreated(['id' => 3], 'A Custom Reason');
560+
$this->invoke($controller, 'respondCreated', [['id' => 3], 'A Custom Reason']);
561+
544562
$this->assertStringStartsWith(config('Format')->supportedResponseFormats[0], $response->getHeaderLine('Content-Type'));
545563
}
546564

547565
public function testResponseFormat()
548566
{
549-
$data = ['foo' => 'something'];
550-
567+
$data = ['foo' => 'something'];
551568
$controller = $this->makeController();
552-
$controller->setResponseFormat('json');
553-
$controller->respond($data, 201);
569+
570+
$this->invoke($controller, 'setResponseFormat', ['json']);
571+
$this->invoke($controller, 'respond', [$data, 201]);
554572

555573
$this->assertStringStartsWith('application/json', $this->response->getHeaderLine('Content-Type'));
556574
$this->assertSame($this->formatter->format($data), $this->response->getJSON());
557575

558-
$controller->setResponseFormat('xml');
559-
$controller->respond($data, 201);
576+
$this->invoke($controller, 'setResponseFormat', ['xml']);
577+
$this->invoke($controller, 'respond', [$data, 201]);
560578

561579
$this->assertStringStartsWith('application/xml', $this->response->getHeaderLine('Content-Type'));
562580
}
@@ -566,10 +584,18 @@ public function testXMLResponseFormat()
566584
$data = ['foo' => 'bar'];
567585
$controller = $this->makeController();
568586
$controller->resetFormatter();
569-
$controller->setResponseFormat('xml');
570-
$controller->respond($data, 201);
587+
588+
$this->invoke($controller, 'setResponseFormat', ['xml']);
589+
$this->invoke($controller, 'respond', [$data, 201]);
571590

572591
$xmlFormatter = new XMLFormatter();
573592
$this->assertSame($xmlFormatter->format($data), $this->response->getXML());
574593
}
594+
595+
private function invoke(object $controller, string $method, array $args = [])
596+
{
597+
$method = $this->getPrivateMethodInvoker($controller, $method);
598+
599+
return $method(...$args);
600+
}
575601
}

0 commit comments

Comments
 (0)