Skip to content

Instantly share code, notes, and snippets.

@devmsh
Last active February 10, 2020 07:45
Show Gist options
  • Select an option

  • Save devmsh/21fab66869eddb8685461fb94118db42 to your computer and use it in GitHub Desktop.

Select an option

Save devmsh/21fab66869eddb8685461fb94118db42 to your computer and use it in GitHub Desktop.
Write better Laravel tests
<?php
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
$user = factory(User::class)->create();
Passport::actingAs($user);
$response = $this->get('api/categories');
$response->assertSuccessful();
$response->assertJsonCount(5);
$response->assertJsonStructure([
[
'id',
'name',
'type',
],
]);
foreach ($response->json() as $category) {
$this->assertEquals($user->id,$category['user_id']);
}
}
<?php
public function test_users_can_get_their_categories_list()
{
// ...
$response = $this->get('api/categories')
->assertSuccessful()
->dump()
->assertJsonCount(5)
->assertJsonStructure([
[
'id',
'name',
'type',
],
]);
foreach ($response->json() as $category) {
$this->assertEquals($user->id,$category['user_id']);
}
}
<?php
// add this macro to your service provider
TestResponse::macro('assertJsonPaths', function ($path, $expected) {
foreach ($this->json($path) as $real) {
PHPUnit::assertEquals($expected, $real);
}
return $this;
});
// then refactor the test
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
$user = factory(User::class)->create();
Passport::actingAs($user);
$this->get('api/categories')
->assertSuccessful()
->assertJsonCount(5)
->assertJsonPaths('*.user_id', $user->id)
->assertJsonStructure([
[
'id',
'name',
'type',
],
]);
}
<?php
// add this method to your TestCase
public function passportAs($user, $scopes = [], $guard = 'api')
{
Passport::actingAs($user, $scopes, $guard);
return $this;
}
// then refacor the test again
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
$user = factory(User::class)->create();
$this->passportAs($user)
// if you did not use passport scope you can also use this
// ->actingAs($user,'api')
->get('api/categories')
->assertSuccessful()
->assertJsonCount(5)
->assertJsonPaths('*.user_id', $user->id)
->assertJsonStructure([
[
'id',
'name',
'type',
],
]);
}
<?php
// Option 1, write a simple comment
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
// once the user created, we will have a copy of the default categories
$user = factory(User::class)->create();
// ...
}
// Option 2, use the test dependancy feature that PHPUnit provide.
/**
* @depends test_user_have_a_copy_of_the_default_categories
*/
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
$user = factory(User::class)->create();
// ...
}
<?php
/**
* @depends test_user_have_a_copy_of_the_default_categories
*/
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
$this->passportAs($user = factory(User::class)->create())
->get('api/categories')
->assertSuccessful()
->assertJsonCount(5)
->assertJsonPaths('*.user_id', $user->id)
->assertJsonStructure([
[
'id',
'name',
'type',
],
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment