Skip to content

Commit 1223436

Browse files
committed
json response service
1 parent 4c07220 commit 1223436

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/Api/Controller.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,32 @@
44
*/
55
namespace Api;
66

7-
use Mvc5\Http\Response;
7+
use Mvc5\Http;
8+
use Mvc5\Plugins\Response;
9+
use Mvc5\Plugins\Service;
810
use Mvc5\Request\Request;
9-
use Mvc5\Response\JsonResponse;
1011

1112
class Controller
1213
{
14+
/**
15+
*
16+
*/
17+
use Response;
18+
use Service;
19+
1320
/**
1421
* @param Request $request
15-
* @return Response
22+
* @return Http\Response
1623
* @throws \Throwable
1724
*/
18-
function __invoke(Request $request) : Response
25+
function __invoke(Request $request) : Http\Response
1926
{
2027
$data = $request->data();
2128

2229
if (!$request->isPost()) {
2330
throw new \Exception('foo bar');
2431
}
2532

26-
return new JsonResponse(['foo' => $data['foo'], 'baz' => $data['baz']]);
33+
return $this->json(['foo' => $data['foo'], 'baz' => $data['baz']]);
2734
}
2835
}

tests/Api/ControllerTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
class ControllerTest
1717
extends TestCase
1818
{
19-
2019
/**
2120
* @runInSeparateProcess
2221
* @throws \Throwable
@@ -33,11 +32,11 @@ function test_error()
3332

3433
$response = (new App($config))->call('web');
3534

36-
$result = json_decode($response->body);
35+
$result = json_decode((string) $response->body);
3736

3837
$this->assertInstanceOf(Response::class, $response);
3938
$this->assertEquals(404, $response->status);
40-
$this->assertEquals('application/json', $response->header('content-type'));
39+
$this->assertEquals('application/json', $response->headers['content-type']);
4140
$this->assertEquals('Not Found', $response->reason);
4241
$this->assertEquals('Not Found', $result->message);
4342
$this->assertEquals('The server can not find the requested resource.', $result->description);
@@ -60,7 +59,7 @@ function test_exception()
6059

6160
$response = (new App($config))->call('exception\response', ['exception' => new \Exception('foobar')]);
6261

63-
$result = json_decode($response->body);
62+
$result = json_decode((string) $response->body);
6463

6564
$this->assertInstanceOf(Response::class, $response);
6665
$this->assertEquals(500, $response->status);
@@ -84,15 +83,15 @@ function test_exception_trace()
8483

8584
$response = (new App($config))->call('exception\response', ['exception' => new \Exception('foobar', 900)]);
8685

87-
$result = json_decode($response->body);
86+
$result = json_decode((string) $response->body);
8887

8988
$this->assertInstanceOf(Response::class, $response);
9089
$this->assertEquals(500, $response->status);
9190
$this->assertEquals('application/json', $response->headers['content-type']);
9291
$this->assertEquals('Internal Server Error', $response->reason);
9392
$this->assertEquals(900, $result->code);
9493
$this->assertEquals('foobar', $result->message);
95-
$this->assertEquals(85, $result->line);
94+
$this->assertEquals(84, $result->line);
9695
$this->assertEquals(__FILE__, $result->file);
9796
$this->assertInternalType('array', $result->trace);
9897
$this->assertNotEmpty($result->trace);
@@ -116,7 +115,7 @@ function test_post_form()
116115

117116
$response = (new App($config))->call('web');
118117

119-
$result = json_decode($response->body);
118+
$result = json_decode((string) $response->body);
120119

121120
$this->assertInstanceOf(Response::class, $response);
122121
$this->assertEquals(200, $response->status);
@@ -144,7 +143,7 @@ function test_post_json()
144143

145144
$response = (new App($config))->call('web');
146145

147-
$result = json_decode($response->body);
146+
$result = json_decode((string) $response->body);
148147

149148
$this->assertInstanceOf(Response::class, $response);
150149
$this->assertEquals(200, $response->status);

0 commit comments

Comments
 (0)