This content originally appeared on DEV Community ๐ฉโ๐ป๐จโ๐ป and was authored by Anwar
Hi and welcome for another article ๐
This one will be a rapid-fire oriented post so that you can both see how easy it is to test your Laravel app, and you can refer to it whenever you want to remember how to test something.
Everytime you want to create a test, and the test class do not exist yet, just run this command.
php artisan make:test LoginTest
And you run your tests using this command.
php artisan test
Without further do, let's get into it!
- Folder architecture suggestion
- 1. Assert a route returns response without errors
- 1. Assert a user is logged
- 2. Assert an authenticated user can perform actions
- 3. Assert a model has been saved after form submission
- 4. Assert a file has been uploaded
- 5. Creating a fake model with relationships
- 6. Assert that a session flash message is visible
- 7. Assert a validation error message is returned
- 8. Assert an authorization
- 9. Assert a command ended without errors
- 11. Assert a json content is sent
- Conclusion
Folder architecture suggestion
If you have a lot of tests, naming can be challenging. To not think of it, and if your test file is focused on a controller, model, ... you can just name the file the same as the entity you test.
your-app/
โโโ app/
โ โโโ Http/
โ โ โโโ Controllers/
โ โ โโโ LoginController.php
โ โ โโโ PostController.php
โ โโโ Models/
โ โ โโโ Post.php
โ โ โโโ User.php
โ โโโ Rules/
โ โ โโโ BarCodeValid.php
โ โโโ Helpers/
โ โโโ Color.php
โโโ tests/
โโโ Feature/
โ โโโ Http/
โ โ โโโ Controllers/
โ โ โโโ LoginControllerTest.php
โ โ โโโ PostControllerTest.php
โ โโโ Models/
โ โ โโโ PostTest.php
โ โ โโโ UserTest.php
โ โโโ Rules/
โ โโโ BarCodeValidTest.php
โโโ Unit/
โโโ Helpers/
โโโ ColorTest.php
1. Assert a route returns response without errors
namespace Tests\Feature;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ContactUsControllerTest extends TestCase
{
use WithFaker;
public function testContactUsPageRendersWell()
{
$this
->get(route("contact-us.index"))
->assertOk();
}
}
1. Assert a user is logged
namespace Tests\Feature;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class LoginControllerTest extends TestCase
{
use WithFaker;
public function testUserIsLoggedAfterFormIsSubmitted()
{
$password = $this->faker->password();
$user = User::factory()->create();
$this->post(route("login"), [
"email" => $user->email,
"password" => $password,
])
->assertValid()
->assertRedirect(route("dashboard"));
$this->assertAuthenticated();
// or
$this->assertAuthenticatedAs($user);
}
}
2. Assert an authenticated user can perform actions
namespace Tests\Feature;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class PostControllerTest extends TestCase
{
use WithFaker;
public function testAuthenticatedUserCanCreateBlogPost()
{
$user = User::factory()->create();
$this->actingAs($user)
->post(route("post.store"), [
"title" => $this->faker->sentence(),
"content" => $this->faker->text(),
])
->assertValid();
}
}
3. Assert a model has been saved after form submission
namespace Tests\Feature;
use App\Models\Item;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ItemControllerTest extends TestCase
{
use WithFaker;
public function testItemSavedAfterUserSubmitsForm()
{
$user = User::factory()->create();
$name = $this->faker->name();
$this->assertDatabaseMissing(Item::class, [
"name" => $name,
]);
$this->actingAs($user)
->post(route("item.store"), [
"name" => $name,
])
->assertValid();
$this->assertDatabaseHas(Item::class, [
"name" => $name,
]);
}
}
4. Assert a file has been uploaded
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Tests\TestCase;
class SettingControllerTest extends TestCase
{
public function testUserCanUploadItsProfilePicture()
{
$user = User::factory()->create();
$file = UploadedFile::fake()->file("avatar.jpg");
$this->assertDatabaseMissing(User::class, [
"user_id" => $user->id,
"profile_picture" => $file->hashName(),
]);
$this->actingAs($user)
->post(route("setting.update"), [
"profile_picture" => $file,
])
->assertValid();
$this->assertDatabaseHas(User::class, [
"user_id" => $user->id,
"profile_picture" => $file->hashName(),
]);
}
}
5. Creating a fake model with relationships
namespace Tests\Feature;
use App\Models\Comment;
use App\Models\Post;
use App\Models\User;
use Tests\TestCase;
class PostControllerTest extends TestCase
{
public function testAuthorCanEditPost()
{
$user = User::factory()->create();
// Creates a post with 5 comments, each comments has 25 likes
$post = Post::factory()
->for($user, "author")
->has(
Comment::factory()
->has(Like::factory()->count(25))
->count(5)
)
->create();
}
}
6. Assert that a session flash message is visible
namespace Tests\Feature;
use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class PostControllerTest extends TestCase
{
use WithFaker;
public function testAuthorCanEditItsPost()
{
$user = User::factory()->create();
$post = Post::factory()->for($user, "author")->create();
$this->actingAs($user)
->post(route("post.update", $post), [
"title" => $this->faker->name(),
])
->assertValid()
->assertSessionHas("success", "Post updated.");
}
}
7. Assert a validation error message is returned
namespace Tests\Feature;
use App\Models\Comment;
use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class PostControllerTest extends TestCase
{
use WithFaker;
public function testAuthorSeeErrorIfEditingPostWithSameNameAsOneOfItsOtherPost()
{
$user = User::factory()->create();
$post = Post::factory()->for($user, "author")->create();
$name = $this->faker->name();
Post::factory()
->for($user, "author")
->create([
"title" => $name,
]);
$this->actingAs($user)
->post(route("post.update", $post), [
"title" => $name,
])
->assertInvalid([
"title" => "The title has already been taken.",
]);
}
}
8. Assert an authorization
namespace Tests\Feature;
use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class PostControllerTest extends TestCase
{
use WithFaker;
public function testUserCannotSeePostHeOrSheDidNotCreate()
{
$user = User::factory()->create();
$otherUser = User::factory()->create();
$post = Post::factory()->for($otherUser, "author")->create();
$this->actingAs($user)
->get(route("post.show", $post))
->assertForbidden();
}
}
9. Assert a command ended without errors
namespace Tests\Feature;
use App\Models\User;
use Carbon\Carbon;
use Tests\TestCase;
class NotifyFreePlanEndsTest extends TestCase
{
public function testUserCannotSeePostHeOrSheDidNotCreate()
{
$user = User::factory()
->count(15)
->create([
"plan" => "free",
"subscribed_at" => Carbon::now()->subDays(15),
]);
$this->artisan("notify:free-plan-ends")
->assertSuccessful()
->expectsOutputToContain("15 users notified.");
}
}
11. Assert a json content is sent
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class TaskControllerTest extends TestCase
{
use WithFaker;
public function testAuthenticatedUserCanCreateTask()
{
$user = User::factory()->create();
$name = $this->faker->name();
$dueAt = $this->faker->date();
$this->actingAs($user)
->post(route("task.store", [
"name" => $name,
"due_at" => $dueAt,
])
->assertJson([
"name" => $name,
"dueAt" => $dueAt,
]);
}
}
Conclusion
I hope this post gave you the motivation to start testing if you did not get used to it yet! When you feel ready, head on the Laravel Test documentation, this is a gold mine and you will find other awesome assertions.
Please also share your snippets in the comments section if you also have some quick and easy ways to test things in Laravel ๐
Happy testing ๐งช
This content originally appeared on DEV Community ๐ฉโ๐ป๐จโ๐ป and was authored by Anwar
Anwar | Sciencx (2022-12-10T15:39:42+00:00) Demystifying tests in Laravel. Retrieved from https://www.scien.cx/2022/12/10/demystifying-tests-in-laravel/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.