Feature Testing in Laravel: A Beginner's Guide

WhoAmI => notes.sohag.pro/author
Search for a command to run...

WhoAmI => notes.sohag.pro/author
No comments yet. Be the first to comment.
SSH client and terminal : From Coffee Shop Chaos to Command Line Comfort Picture this: You're sitting in your favorite coffee shop, sipping your third latte of the day, when suddenly your phone buzzes. Your production server is acting up, and you nee...
I had a solid list of reasons my life wasn't moving faster, until a grainy old lecture pointed out the one name missing from it

The finale isn't a victory lap. It's the story of the control I shipped that did nothing, the footgun still sitting in my demo, and the handful of things I'd keep exactly as they are.

How do you hold a large payment for a second pair of eyes without ever letting the unapproved money touch a balance, and how do you stream that decision to the outside world without standing up a broker?

How do you show the total under a parent account when the whole system refuses to store a balance? A recursive query, a trigger that refuses to draw a circle, and a rule about what actually has to sum to zero.

Every time I wanted to change an FX rate I had to edit a file on the server and restart the app. So I moved rates and markup into a live admin API, and then audited it hard enough to find the bug that quietly undid the whole thing.

Think of feature testing like being a food critic at a restaurant. Before giving a review, you test everything: from walking in the door, being seated, ordering food, to paying the bill. Similarly, in Laravel, feature testing ensures your application's entire features work correctly from start to finish.
Let's create our first feature test. Imagine we're building a simple todo list application.
php artisan make:test TodoTest
This creates a new test file in tests/Feature/TodoTest.php. Let's write our first test:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Todo;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
class TodoTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_create_todo()
{
// Arrange
$user = User::factory()->create();
$todoData = [
'title' => 'Buy groceries',
'description' => 'Milk, bread, and eggs'
];
// Act
$response = $this->actingAs($user)
->post('/todos', $todoData);
// Assert
$response->assertStatus(201);
$this->assertDatabaseHas('todos', [
'title' => 'Buy groceries',
'user_id' => $user->id
]);
}
}
Would you like me to explain the code above?
Use Descriptive Test Names
Bad: test_create()
Good: test_user_can_create_todo()
Follow AAA Pattern
Arrange: Set up your test data
Act: Perform the action
Assert: Verify the results
Use Database Transactions
Always use RefreshDatabase trait for clean test environment
Each test should start with a fresh database
Test Real-World Scenarios Think of real-life situations:
What if a user tries to create a todo without a title?
What if a user tries to view another user's private todos?
Here's an example testing invalid input:
public function test_todo_title_is_required()
{
$user = User::factory()->create();
$todoData = [
'description' => 'Milk, bread, and eggs'
];
$response = $this->actingAs($user)
->post('/todos', $todoData);
$response->assertStatus(422)
->assertJsonValidationErrors(['title']);
}
Would you like me to explain this code as well?
get('/url') - Like visiting a webpage
post('/url', $data) - Like submitting a form
put('/url', $data) - Like updating your profile
delete('/url') - Like deleting a post
assertStatus($code) - Checking if the operation was successful
assertSee($text) - Checking if text appears on the page
Imagine testing a shopping cart feature - it's like checking if a physical shopping cart works:
public function test_user_can_add_item_to_cart()
{
$user = User::factory()->create();
$product = Product::factory()->create([
'name' => 'Coffee Mug',
'price' => 9.99
]);
$response = $this->actingAs($user)
->post('/cart/add', [
'product_id' => $product->id,
'quantity' => 2
]);
$response->assertStatus(200);
$this->assertDatabaseHas('cart_items', [
'user_id' => $user->id,
'product_id' => $product->id,
'quantity' => 2
]);
}
Would you like me to explain this code?
# Run all tests
php artisan test
# Run a specific test class
php artisan test --filter TodoTest
# Run a specific test method
php artisan test --filter test_user_can_create_todo
Testing Fundamentals
PHPUnit basics
Test-Driven Development (TDD)
Behavior-Driven Development (BDD)
Advanced Testing
Mocking
Database seeding
Testing APIs
Browser testing with Laravel Dusk
Testing Best Practices
Test coverage
Continuous Integration
Testing patterns
Related Tools
PHPUnit
Pest
Laravel Dusk
Faker
Database factories
Feature testing might seem overwhelming at first, but think of it as writing a checklist for your application. Just like you'd check if you've packed everything before a trip, tests verify if your application works as expected.
Remember: A well-tested application is like a well-inspected car - you can trust it to work reliably when you need it.