This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from goodjack/master
Add Response layer
- Loading branch information
Showing
5 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |