Back to list
fusengine

laravel-testing

by fusengine

Redefining development through cognitive automation and collaborative agent systems.

0🍴 0📅 Jan 25, 2026

SKILL.md


name: laravel-testing description: Write tests with Pest/PHPUnit, feature tests, unit tests, mocking, and factories. Use when testing controllers, services, models, or implementing TDD. user-invocable: false

Laravel Testing

Documentation

Testing

Code Quality

Pest Feature Test

<?php

declare(strict_types=1);

use App\Models\Post;
use App\Models\User;

describe('PostController', function () {
    beforeEach(function () {
        $this->user = User::factory()->create();
    });

    it('lists all posts', function () {
        Post::factory()->count(3)->create();

        $this->getJson('/api/v1/posts')
            ->assertOk()
            ->assertJsonCount(3, 'data');
    });

    it('creates a post when authenticated', function () {
        $data = ['title' => 'Test', 'content' => 'Content', 'status' => 'draft'];

        $this->actingAs($this->user)
            ->postJson('/api/v1/posts', $data)
            ->assertCreated()
            ->assertJsonPath('data.title', 'Test');

        $this->assertDatabaseHas('posts', ['title' => 'Test']);
    });

    it('returns 401 for unauthenticated users', function () {
        $this->postJson('/api/v1/posts', [])
            ->assertUnauthorized();
    });

    it('validates required fields', function () {
        $this->actingAs($this->user)
            ->postJson('/api/v1/posts', [])
            ->assertUnprocessable()
            ->assertJsonValidationErrors(['title', 'content']);
    });
});

Unit Test with Mocking

<?php

declare(strict_types=1);

use App\Services\PostService;
use App\Repositories\Contracts\PostRepositoryInterface;

describe('PostService', function () {
    it('creates a post with valid data', function () {
        $repository = Mockery::mock(PostRepositoryInterface::class);
        $repository->shouldReceive('create')
            ->once()
            ->andReturn(new Post(['title' => 'Test']));

        $service = new PostService($repository);
        $post = $service->create(['title' => 'Test']);

        expect($post->title)->toBe('Test');
    });
});

Factory

<?php

declare(strict_types=1);

namespace Database\Factories;

final class PostFactory extends Factory
{
    public function definition(): array
    {
        return [
            'title' => fake()->sentence(),
            'content' => fake()->paragraphs(3, true),
            'status' => PostStatus::Draft,
            'user_id' => User::factory(),
        ];
    }

    public function published(): static
    {
        return $this->state([
            'status' => PostStatus::Published,
            'published_at' => now(),
        ]);
    }
}

Assertions

// Response
$response->assertOk();           // 200
$response->assertCreated();      // 201
$response->assertUnauthorized(); // 401
$response->assertUnprocessable(); // 422

// JSON
$response->assertJson(['key' => 'value']);
$response->assertJsonPath('data.id', 1);
$response->assertJsonCount(3, 'data');

// Database
$this->assertDatabaseHas('posts', ['title' => 'Test']);
$this->assertDatabaseMissing('posts', ['title' => 'Deleted']);
$this->assertSoftDeleted('posts', ['id' => 1]);

Mocking

// Mock HTTP
Http::fake([
    'api.example.com/*' => Http::response(['data' => 'test'], 200),
]);

// Mock service
$this->mock(PaymentService::class, function ($mock) {
    $mock->shouldReceive('charge')->once()->andReturn(true);
});

Score

Total Score

60/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon