From c10c8602010f60a5e6d4056626111158a0c817b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=85=8B?= Date: Tue, 7 Apr 2020 14:05:25 +0800 Subject: [PATCH] Add Response layer --- src/Commands/ResponseMakeCommand.php | 45 ++++++++++++++++++ src/GeneratorsServiceProvider.php | 17 ++++++- src/stubs/response.stub | 13 ++++++ tests/Illuminate/Responses/UserResponse.php | 31 ++++++++++++ tests/ResponseTest.php | 52 +++++++++++++++++++++ 5 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/Commands/ResponseMakeCommand.php create mode 100644 src/stubs/response.stub create mode 100644 tests/Illuminate/Responses/UserResponse.php create mode 100644 tests/ResponseTest.php diff --git a/src/Commands/ResponseMakeCommand.php b/src/Commands/ResponseMakeCommand.php new file mode 100644 index 0000000..6ab4ec3 --- /dev/null +++ b/src/Commands/ResponseMakeCommand.php @@ -0,0 +1,45 @@ + 'command.foundation.make', 'ParserMake' => 'command.parser.make', 'TransportMake' => 'command.transport.make', + 'ResponseMake' => 'command.response.make', ]; /** @@ -42,7 +44,8 @@ class GeneratorsServiceProvider extends ServiceProvider public function register() { $this->registerCommands(array_merge( - $this->commands, $this->devCommands + $this->commands, + $this->devCommands )); } @@ -156,4 +159,16 @@ protected function registerFoundationMakeCommand() return new FoundationMakeCommand($app['files']); }); } + + /** + * Register the command. + * + * @return void + */ + protected function registerResponseMakeCommand() + { + $this->app->singleton('command.response.make', function ($app) { + return new ResponseMakeCommand($app['files']); + }); + } } diff --git a/src/stubs/response.stub b/src/stubs/response.stub new file mode 100644 index 0000000..a132b33 --- /dev/null +++ b/src/stubs/response.stub @@ -0,0 +1,13 @@ +users = $users; + } + + public function toResponse($request) + { + return response()->json($this->transform()); + } + + public function transform() + { + return $this->users->map(function ($user) { + return [ + 'name' => $user->name, + 'email' => $user->email, + ]; + }); + } +} diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php new file mode 100644 index 0000000..adb1ff1 --- /dev/null +++ b/tests/ResponseTest.php @@ -0,0 +1,52 @@ + 1, + 'name' => 'yish', + 'email' => 'yish@tt.com', + 'password' => bcrypt(123456), + ]; + + $userGoodjack = [ + 'id' => 2, + 'name' => 'goodjack', + 'email' => 'goodjack@tt.com', + 'password' => bcrypt(123456), + ]; + + $users = new Collection([ + new FakeUser($userYish), + new FakeUser($userGoodjack), + ]); + + Route::get('/user', function () use ($users) { + return new UserResponse($users); + }); + + $expected = $users->map(function ($user) { + return [ + 'name' => $user->name, + 'email' => $user->email, + ]; + }); + + $this->get('/user') + ->assertJsonFragment($expected[0]) + ->assertJsonFragment($expected[1]); + } +}