create-test
Generates a PHPUnit/Pest test file. Use when adding test coverage for Actions, Features, or Endpoints (e.g., "Create a test for RegisterStudentAction").
When & Why to Use This Skill
This Claude skill automates the generation of PHPUnit and Pest test files for PHP applications, specifically optimized for Laravel-based architectures. It streamlines the creation of unit and feature tests for actions, controllers, and endpoints, ensuring high code quality and robust test coverage through standardized templates and best practices.
Use Cases
- Rapidly scaffolding unit tests for business logic actions (e.g., RegisterStudentAction) to verify success paths and data integrity.
- Generating feature tests for API endpoints and controllers to validate HTTP responses, middleware authorization, and database side effects.
- Ensuring comprehensive coverage by automatically drafting 'unhappy path' scenarios, including validation failures and unauthorized access attempts.
- Standardizing test suites across modular Laravel applications using Pest's succinct syntax and consistent namespace structures.
| name | create-test |
|---|---|
| description | Generates a PHPUnit/Pest test file. Use when adding test coverage for Actions, Features, or Endpoints (e.g., "Create a test for RegisterStudentAction"). |
| allowed-tools | Write, Read |
Create Test
This skill generates a Pest test file (preferred) or PHPUnit test class.
Instructions
Identify Parameters:
- Target: What are we testing? (e.g.,
RegisterStudentAction,CourseController). - Type:
Unit(for Actions/DTOs/Support) orFeature(for Controllers/Integration). - Module: (e.g.,
Academic). - Path:
app/Modules/{Module}/tests/{Type}/{Target}Test.php(if module-based) ORtests/{Type}/{Target}Test.php.
- Target: What are we testing? (e.g.,
Generate Code:
- Use Pest syntax (it's succinct and readable).
- Namespace:
App\Modules\{Module}\Tests\{Type}. - Setup: Use
uses(TestCase::class)->in(__DIR__);if needed, or rely onPest.phpconfiguration. - Content:
- Happy path (Success scenario).
- Validation failure scenario.
- Authorization failure scenario.
Template (Unit - Action)
<?php
namespace App\Modules\Academic\Tests\Unit;
use App\Modules\Academic\Actions\CreateCourseAction;
use App\Modules\Academic\Models\Course;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
test('it creates a course successfully', function () {
$data = [
'name' => 'Introduction to Computer Science',
'code' => 'CS101',
'credits' => 3,
];
$course = CreateCourseAction::run($data);
expect($course)
->toBeInstanceOf(Course::class)
->name->toBe('Introduction to Computer Science');
$this->assertDatabaseHas('courses', ['code' => 'CS101']);
});
Template (Feature - Controller)
<?php
namespace App\Modules\Academic\Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
test('admin can view course list', function () {
$user = User::factory()->create();
// specific permission setup if needed
$response = $this->actingAs($user)
->get(route('academic.courses.index'));
$response->assertStatus(200)
->assertInertia(fn ($page) => $page
->component('Academic/Course/Index')
->has('items')
);
});
Checklist
- Are you using
RefreshDatabase? - Are you asserting the side effects (DB changes, Events dispatched)?
- Are you testing the 'Unhappy Path' (errors)?