Skip to content

Make HttpApi injectable + real MockHttpClient tests #32

Description

@Chemaclass

Context

HttpApi constructs its client inline, so the HTTP layer can't be exercised with a fake transport in tests. The graceful failure path (transport/HTTP error → nullBackend "LnBits" unreachable) is only covered indirectly.

  • src/Invoice/Infrastructure/Http/HttpApi.phpHttpClient::create() is called inside postRequestInvoice(); catches Symfony\Contracts\HttpClient\Exception\ExceptionInterface | JsonException → returns null.
  • src/Invoice/InvoiceDependencyProvider.php — provides new HttpApi().

Goal

Make HttpApi injectable so its real behavior (success + transport-error → null) can be unit-tested with Symfony's MockHttpClient, with no network.

Scope / Tasks

  • Add an optional constructor param HttpClientInterface $client = null, defaulting to HttpClient::create() (backwards compatible — new HttpApi() still works, and InvoiceDependencyProvider needs no change).
  • Use the injected client in postRequestInvoice().
  • Add unit tests using Symfony\Component\HttpClient\MockHttpClient:
    • 200 with a JSON body → returns the decoded array.
    • Transport error (MockHttpClient throwing) → returns null.
    • Non-2xx / malformed body → returns null (exercises the ExceptionInterface | JsonException catch).

Implementation notes

  • symfony/http-client is already a dependency; MockHttpClient ships with it (no new dep).
  • Keep the default-construction path so nothing else needs wiring.

Acceptance criteria

  • HttpApi is constructable with a custom client; default construction unchanged.
  • Tests cover success, transport-error→null, and bad-response→null with no real network.
  • composer test-all green.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestrefactoringRefactoring or cleaning related

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions