Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #8 from goodjack/master
Browse files Browse the repository at this point in the history
Add Response layer
  • Loading branch information
Mombuyish authored Apr 9, 2020
2 parents cc97880 + c10c860 commit 3b6577b
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/Commands/ResponseMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Yish\Generators\Commands;

use Illuminate\Console\GeneratorCommand;

class ResponseMakeCommand extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'make:response';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new response class';

protected $type = 'Response';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__ . '/../stubs/response.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Responses';
}
}
17 changes: 16 additions & 1 deletion src/GeneratorsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Yish\Generators\Commands\FoundationMakeCommand;
use Yish\Generators\Commands\ParserMakeCommand;
use Yish\Generators\Commands\TransportMakeCommand;
use Yish\Generators\Commands\ResponseMakeCommand;

class GeneratorsServiceProvider extends ServiceProvider
{
Expand All @@ -32,6 +33,7 @@ class GeneratorsServiceProvider extends ServiceProvider
'FoundationMake' => 'command.foundation.make',
'ParserMake' => 'command.parser.make',
'TransportMake' => 'command.transport.make',
'ResponseMake' => 'command.response.make',
];

/**
Expand All @@ -42,7 +44,8 @@ class GeneratorsServiceProvider extends ServiceProvider
public function register()
{
$this->registerCommands(array_merge(
$this->commands, $this->devCommands
$this->commands,
$this->devCommands
));
}

Expand Down Expand Up @@ -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']);
});
}
}
13 changes: 13 additions & 0 deletions src/stubs/response.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace DummyNamespace;

use Illuminate\Contracts\Support\Responsable;

class DummyClass implements Responsable
{
public function toResponse($request)
{
//
}
}
31 changes: 31 additions & 0 deletions tests/Illuminate/Responses/UserResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Yish\Generators\Tests\Illuminate\Responses;

use Illuminate\Contracts\Support\Responsable;
use Illuminate\Support\Collection;

class UserResponse implements Responsable
{
protected $users;

public function __construct(Collection $users)
{
$this->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,
];
});
}
}
52 changes: 52 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Yish\Generators\Tests;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Route;
use Yish\Generators\Tests\Illuminate\Responses\UserResponse;
use Yish\Generators\Tests\Illuminate\User as FakeUser;

class ResponseTest extends TestCase
{
/**
* @test
* @group package-foundation
*/
public function it_should_return_specific_data()
{
$userYish = [
'id' => 1,
'name' => 'yish',
'email' => '[email protected]',
'password' => bcrypt(123456),
];

$userGoodjack = [
'id' => 2,
'name' => 'goodjack',
'email' => '[email protected]',
'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]);
}
}

0 comments on commit 3b6577b

Please sign in to comment.