pest-testing
Pest PHP testing framework patterns for Laravel applications.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Pest PHP testing framework patterns for Laravel applications.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
PHP Artisan CLI patterns including make commands, flags, custom commands, and AI-friendly usage.
Advanced Eloquent ORM patterns including relationships, eager loading, factories, and query optimization.
Laravel development best practices covering service providers, dependency injection, facades, and the Laravel Way.
Laravel Tinker best practices for debugging, testing ideas, and data exploration. When and how to use safely.
Laravel-native error handling patterns. Renderable exceptions, Livewire traits strategies, and user-facing notifications.
Integrate Anthropic Claude API with Laravel. HTTP client, message format, error handling. Use when calling Claude models from Laravel applications.
| name | pest-testing |
| description | Pest PHP testing framework patterns for Laravel applications. |
# Feature test (recommended for most tests)
php artisan make:test CreatePostTest --pest
# Unit test
php artisan make:test PostServiceTest --pest --unit
it('can create a post', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)
->post('/posts', [
'title' => 'My Post',
'body' => 'Content',
]);
$response->assertRedirect('/posts');
expect(Post::count())->toBe(1);
});
beforeEach(function () {
$this->user = User::factory()->create();
});
it('requires authentication', function () {
$this->post('/posts', ['title' => 'Test'])
->assertRedirect('/login');
});
it('creates a post when authenticated', function () {
$this->actingAs($this->user)
->post('/posts', ['title' => 'Test', 'body' => 'Content'])
->assertRedirect('/posts');
});
// Basic
expect($value)->toBe(5);
expect($value)->toBeTrue();
expect($value)->toBeFalse();
expect($value)->toBeNull();
// Collections
expect($array)->toHaveCount(3);
expect($array)->toContain('item');
expect($array)->toHaveKey('key');
// Types
expect($value)->toBeString();
expect($value)->toBeArray();
expect($value)->toBeInstanceOf(Post::class);
// Chaining
expect($user)
->name->toBe('John')
->email->toContain('@');
it('shows posts list', function () {
Post::factory()->count(3)->create();
$this->get('/posts')
->assertOk()
->assertViewIs('posts.index')
->assertViewHas('posts');
});
it('validates required fields', function () {
$this->actingAs(User::factory()->create())
->post('/posts', [])
->assertSessionHasErrors(['title', 'body']);
});
it('redirects guests', function () {
$this->post('/posts', ['title' => 'Test'])
->assertRedirect('/login');
});
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('stores post in database', function () {
$user = User::factory()->create();
$this->actingAs($user)->post('/posts', [
'title' => 'My Title',
'body' => 'My Body',
]);
$this->assertDatabaseHas('posts', [
'title' => 'My Title',
'user_id' => $user->id,
]);
});
use Livewire\Livewire;
it('can increment counter', function () {
Livewire::test(Counter::class)
->assertSet('count', 0)
->call('increment')
->assertSet('count', 1);
});
it('validates input', function () {
Livewire::test(CreatePost::class)
->set('title', '')
->call('save')
->assertHasErrors(['title' => 'required']);
});
it('emits event on save', function () {
Livewire::test(CreatePost::class)
->set('title', 'Test')
->call('save')
->assertDispatched('post-created');
});
it('uses factories correctly', function () {
// Basic
$user = User::factory()->create();
// With state
$admin = User::factory()->admin()->create();
// With relationship
$post = Post::factory()
->has(Comment::factory()->count(3))
->create();
expect($post->comments)->toHaveCount(3);
});
# Run all tests
php artisan test
# With coverage
php artisan test --coverage
# Filter by name
php artisan test --filter=CreatePostTest
# Parallel (faster)
php artisan test --parallel
✅ Use it() for readable test names
✅ Use expect() for assertions
✅ Use factories for all test data
✅ Use RefreshDatabase trait
✅ Test behavior, not implementation
✅ One assertion per test when possible
❌ Don't test Laravel framework code ❌ Don't use hardcoded IDs ❌ Don't create data manually