Testing
Write feature and unit tests with PHPUnit
Laravel provides a testing layer built on PHPUnit with helpers for HTTP tests, database assertions, and more.
Feature tests make HTTP requests to your application and verify responses, testing the full stack.
Test flow: Arrange, Act, Assert
Arrange
Set up test data
Act
Perform the action
Assert
Verify the result
Test results
test_users_indexpending
test_create_userpending
test_delete_userpending
test_update_userpending
PHP
class UserTest extends TestCase {
public function test_users_index_returns_ok(): void {
$response = $this->get('/users');
$response->assertStatus(200);
$response->assertJson([
'data' => true,
]);
}
public function test_guests_cannot_create_users(): void {
$response = $this->post('/users', ['name' => 'John']);
$response->assertRedirect('/login');
}
}