Last active
April 7, 2019 14:15
-
-
Save mattias-persson/bf6603462f63a133c4332b63e07f807d to your computer and use it in GitHub Desktop.
assertReturnsResource (Laravel test helper)
This file contains hidden or 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
<?php | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
use Illuminate\Foundation\Testing\TestResponse; | |
use Illuminate\Http\Request; | |
use Illuminate\Foundation\Testing\Assert as PHPUnit; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
if ($this->app->environment('testing')) { | |
$this->addTestResponseMacros(); | |
} | |
} | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// | |
} | |
/** | |
* Add test response macros. | |
* | |
* @return void | |
*/ | |
protected function addTestResponseMacros() | |
{ | |
TestResponse::macro('assertReturnsResource', function($resource) { | |
// The response has the exact structure of the resource. | |
$response = $this->assertJsonStructure([ | |
'data' => array_keys($resource->resolve()) | |
]); | |
// The response is the expected model. | |
PHPUnit::assertEquals($response->getData()->data->id, $resource->id); | |
return $this; | |
}); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace Tests\Feature; | |
use Tests\TestCase; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use App\Models\User; | |
use App\Http\Resources\UserResource; | |
use Illuminate\Http\Request; | |
class GetUserTest extends TestCase | |
{ | |
use RefreshDatabase; | |
/** @test */ | |
public function it_returns_the_user_resource() | |
{ | |
$user = factory(User::class)->create(); | |
$this->getJson('users/'.user->id) | |
->assertStatus(200) | |
->assertReturnsResource(new UserResource($user)); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\Http\Controllers; | |
use App\Models\User; | |
use App\Http\Controllers\Controller; | |
use App\Http\Resources\UserResource; | |
class UserController extends Controller | |
{ | |
public function show(User $user) | |
{ | |
return new UserResource($user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment