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]); + } +}