> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# テスト

> laravel-bluesky を使ったコードのテスト方法。Facade モック、HTTP フェイク、テストできない機能の解説。

## 概要

`laravel-bluesky` の主要機能は `Bluesky` Facade 経由で提供されるため、標準的な Laravel モックを使ってテストできます。パッケージ開発のテスト全般については [package-testing](/jp/advanced/package-testing) も参照してください。

## Facade モック

`Bluesky::expects()` を使ってメソッドチェーンごとモックします。

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\Response;

Bluesky::expects('login->getProfile->json')->once()->andReturn([]);
Bluesky::expects('login->getProfile')->once()->andReturn(new Response(Http::response([])->wait()));
```

## HTTP フェイク

`laravel-bluesky` は内部で Laravel の HTTP クライアントを使用しています。`Http::preventStrayRequests()` を `setUp()` に書いておくと、予期しない外部リクエストを即座に検知できます。

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;
use Illuminate\Support\Facades\Http;

protected function setUp(): void
{
    parent::setUp();

    Http::preventStrayRequests();
}

public function test_post(): void
{
    Http::fake();

    Http::fakeSequence()
        ->push();

    Bluesky::expects('resolveHandle->json')->once()->andReturn('did');
}
```

<Tip>
  `Http::preventStrayRequests()` はテスト実行中に本物の外部リクエストが発生した場合に例外をスローします。意図しない外部通信をすぐに発見できるため、テストの信頼性が上がります。
</Tip>

## モックできない機能

### FeedGenerator

FeedGenerator は Bluesky サーバー側から呼び出されるため、エンドツーエンドのモックはありません。ただし FeedGenerator のアルゴリズム部分はモック可能です。

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\FeedGenerator\FeedGenerator;

public function test_feed_generator(): void
{
    FeedGenerator::register(name: 'test', algo: function (?int $limit, ?string $cursor) {
        // API 制限により認証が必要
        $posts = Bluesky::login(identifier: config('bluesky.identifier'), password: config('bluesky.password'))
            ->searchPosts(q: '#bluesky')->collect('posts');
        $feed = $posts->map(function (array $post) {
            return ['post' => data_get($post, 'uri')];
        })->toArray();
        return ['feed' => $feed];
    });

    Bluesky::expects('login->searchPosts->collect')->once()->andReturn(collect([['uri' => 'at://']]));

    $response = $this->get(route('bluesky.feed.skeleton', ['feed' => 'at://did:/app.bsky.feed.generator/test']));

    $response->assertSuccessful();
    $response->assertJson(['feed' => [['post' => 'at://']]]);
}
```

### Core

Bluesky/AtProtocol のコア機能は外部アクセスを伴わないため、モックは必要ありません。

<Info>
  Source: [docs/testing.md](https://github.com/invokable/laravel-bluesky/blob/main/docs/testing.md)
</Info>
