Skip to content

Commit da41b55

Browse files
authored
Merge pull request #7599 from kenjis/fix-TestResponseTest
test: fix TestResponseTest
2 parents 8bcc33e + 202b467 commit da41b55

4 files changed

Lines changed: 47 additions & 39 deletions

File tree

system/Format/FormatterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface FormatterInterface
1919
/**
2020
* Takes the given data and formats it.
2121
*
22-
* @param array|string $data
22+
* @param array|object|string $data
2323
*
2424
* @return false|string
2525
*/

system/HTTP/ResponseTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function setContentType(string $mime, string $charset = 'UTF-8')
239239
/**
240240
* Converts the $body into JSON and sets the Content Type header.
241241
*
242-
* @param array|string $body
242+
* @param array|object|string $body
243243
*
244244
* @return $this
245245
*/
@@ -304,8 +304,8 @@ public function getXML()
304304
* Handles conversion of the data into the appropriate format,
305305
* and sets the correct Content-Type header for our response.
306306
*
307-
* @param array|string $body
308-
* @param string $format Valid: json, xml
307+
* @param array|object|string $body
308+
* @param string $format Valid: json, xml
309309
*
310310
* @return mixed
311311
*

tests/system/Test/FeatureTestTraitTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -372,20 +372,6 @@ public function testCallWithJsonRequest()
372372
$response->assertJSONExact(['foo' => 'bar']);
373373
}
374374

375-
public function testCallWithJsonRequestObject()
376-
{
377-
$this->withRoutes([
378-
[
379-
'post',
380-
'home',
381-
'\Tests\Support\Controllers\Popcorn::echoJson',
382-
],
383-
]);
384-
$response = $this->withBodyFormat('json')->call('post', 'home', ['foo' => 'bar']);
385-
$response->assertOK();
386-
$response->assertJSONExact((object) ['foo' => 'bar']);
387-
}
388-
389375
public function testSetupRequestBodyWithParams()
390376
{
391377
$request = $this->setupRequest('post', 'home');

tests/system/Test/TestResponseTest.php

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,15 @@ public function testAssertCookieExpired()
299299

300300
public function testGetJSON()
301301
{
302-
$this->getTestResponse(['foo' => 'bar']);
303-
$formatter = Services::format()->getFormatter('application/json');
302+
$data = ['foo' => 'bar'];
303+
$this->getTestResponse('');
304+
$this->response->setJSON($data, true);
304305

305-
$this->assertSame($formatter->format(['foo' => 'bar']), $this->testResponse->getJSON());
306+
$formatter = Services::format()->getFormatter('application/json');
307+
$this->assertSame($formatter->format($data), $this->testResponse->getJSON());
306308
}
307309

308-
public function testEmptyJSON()
310+
public function testGetJSONEmptyJSON()
309311
{
310312
$this->getTestResponse('<h1>Hello World</h1>');
311313
$this->response->setJSON('', true);
@@ -314,7 +316,7 @@ public function testEmptyJSON()
314316
$this->assertSame('""', $this->testResponse->getJSON());
315317
}
316318

317-
public function testFalseJSON()
319+
public function testGetJSONFalseJSON()
318320
{
319321
$this->getTestResponse('<h1>Hello World</h1>');
320322
$this->response->setJSON(false, true);
@@ -323,7 +325,7 @@ public function testFalseJSON()
323325
$this->assertSame('false', $this->testResponse->getJSON());
324326
}
325327

326-
public function testTrueJSON()
328+
public function testGetJSONTrueJSON()
327329
{
328330
$this->getTestResponse('<h1>Hello World</h1>');
329331
$this->response->setJSON(true, true);
@@ -332,7 +334,7 @@ public function testTrueJSON()
332334
$this->assertSame('true', $this->testResponse->getJSON());
333335
}
334336

335-
public function testInvalidJSON()
337+
public function testGetJSONInvalidJSON()
336338
{
337339
$tmp = ' test " case ';
338340
$this->getTestResponse('<h1>Hello World</h1>');
@@ -344,30 +346,36 @@ public function testInvalidJSON()
344346

345347
public function testGetXML()
346348
{
347-
$this->getTestResponse(['foo' => 'bar']);
348-
$formatter = Services::format()->getFormatter('application/xml');
349+
$data = ['foo' => 'bar'];
350+
$this->getTestResponse('');
351+
$this->response->setXML($data);
349352

350-
$this->assertSame($formatter->format(['foo' => 'bar']), $this->testResponse->getXML());
353+
$formatter = Services::format()->getFormatter('application/xml');
354+
$this->assertSame($formatter->format($data), $this->testResponse->getXML());
351355
}
352356

353-
public function testJsonFragment()
357+
public function testAssertJSONFragment()
354358
{
355-
$this->getTestResponse([
359+
$data = [
356360
'config' => [
357361
'key-a',
358362
'key-b',
359363
],
360-
]);
364+
];
365+
$this->getTestResponse('');
366+
$this->response->setJSON($data, true);
361367

362368
$this->testResponse->assertJSONFragment(['config' => ['key-a']]);
363369
$this->testResponse->assertJSONFragment(['config' => ['key-a']], true);
364370
}
365371

366372
public function testAssertJSONFragmentFollowingAssertArraySubset()
367373
{
368-
$this->getTestResponse([
374+
$data = [
369375
'config' => '124',
370-
]);
376+
];
377+
$this->getTestResponse('');
378+
$this->response->setJSON($data, true);
371379

372380
$this->testResponse->assertJSONFragment(['config' => 124]); // must fail on strict
373381
$this->testResponse->assertJSONFragment(['config' => '124'], true);
@@ -383,36 +391,50 @@ public function testAssertJSONFragmentFailsGracefullyWhenNotGivenJson()
383391
$this->testResponse->assertJSONFragment(['foo' => 'bar']);
384392
}
385393

386-
public function testJsonExact()
394+
public function testAssertJsonExactArray()
387395
{
388396
$data = [
389397
'config' => [
390398
'key-a',
391399
'key-b',
392400
],
393401
];
402+
$this->getTestResponse('');
403+
$this->response->setJSON($data, true);
404+
405+
$this->testResponse->assertJSONExact($data);
406+
}
394407

395-
$this->getTestResponse($data);
408+
public function testAssertJsonExactObject()
409+
{
410+
$data = (object) [
411+
'config' => [
412+
'key-a',
413+
'key-b',
414+
],
415+
];
416+
$this->getTestResponse('');
417+
$this->response->setJSON($data, true);
396418

397419
$this->testResponse->assertJSONExact($data);
398420
}
399421

400-
public function testJsonExactString()
422+
public function testAssertJsonExactString()
401423
{
402424
$data = [
403425
'config' => [
404426
'key-a',
405427
'key-b',
406428
],
407429
];
430+
$this->getTestResponse('');
431+
$this->response->setJSON($data, true);
408432

409-
$this->getTestResponse($data);
410433
$formatter = Services::format()->getFormatter('application/json');
411-
412434
$this->testResponse->assertJSONExact($formatter->format($data));
413435
}
414436

415-
protected function getTestResponse($body = null, array $responseOptions = [], array $headers = [])
437+
protected function getTestResponse(?string $body = null, array $responseOptions = [], array $headers = [])
416438
{
417439
$this->response = new Response(new App());
418440
$this->response->setBody($body);

0 commit comments

Comments
 (0)