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 TestsFeature;
use IlluminateFoundationTestingWithFaker;
use TestsTestCase;
class ContactUsControllerTest extends TestCase
{
use WithFaker;
public function testContactUsPageRendersWell()
{
$this
->get(route("contact-us.index"))
->assertOk();
}
}
1. Assert a user is logged
namespace TestsFeature;
use IlluminateFoundationTestingWithFaker;
use TestsTestCase;
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 TestsFeature;
use IlluminateFoundationTestingWithFaker;
use TestsTestCase;
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 TestsFeature;
use AppModelsItem;
use IlluminateFoundationTestingWithFaker;
use TestsTestCase;
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 TestsFeature;
use AppModelsUser;
use IlluminateHttpUploadedFile;
use TestsTestCase;
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 TestsFeature;
use AppModelsComment;
use AppModelsPost;
use AppModelsUser;
use TestsTestCase;
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 TestsFeature;
use AppModelsPost;
use AppModelsUser;
use IlluminateFoundationTestingWithFaker;
use TestsTestCase;
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 TestsFeature;
use AppModelsComment;
use AppModelsPost;
use AppModelsUser;
use IlluminateFoundationTestingWithFaker;
use TestsTestCase;
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 TestsFeature;
use AppModelsPost;
use AppModelsUser;
use IlluminateFoundationTestingWithFaker;
use TestsTestCase;
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 TestsFeature;
use AppModelsUser;
use CarbonCarbon;
use TestsTestCase;
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 TestsFeature;
use AppModelsUser;
use IlluminateFoundationTestingWithFaker;
use TestsTestCase;
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 🧪