TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/xai/llms.txt
Use this file to discover all available pages before exploring further.
aisdk/xai package is designed to be fully testable without making real API calls. Because the provider resolves its HTTP transport through a standard PSR-18 ClientInterface, you can swap in any compliant fake client at test time. Inject it through the Sdk value object passed to Generate::configure(), point XAI::create() at a dummy API key, and your tests run entirely in-process — fast, deterministic, and free of network dependencies.
The FakeHttpClient
The package ships a FakeHttpClient helper inside tests/Fakes/ that records the last outgoing request and returns a hand-crafted response. Its constructor accepts an HTTP status code, a raw response body string, and an optional Content-Type header (defaulting to application/json).
| Member | Description |
|---|---|
__construct(int $status, string $body, string $contentType = 'application/json') | Builds the fake with a fixed status and body to return for every request. |
public ?RequestInterface $lastRequest | Populated with the most recent RequestInterface after each sendRequest() call. Inspect it to assert headers, URI path, and HTTP method. |
sentBody(): array<string, mixed> | JSON-decodes $lastRequest->getBody() and returns it as an associative array, making it easy to assert the exact payload your code sent. |
Setting Up a Test
The typical pattern is:- Instantiate
FakeHttpClientwith the JSON payload you want the provider to “receive”. - Wrap a
Psr17Factoryand the fake client together in anSdkobject and pass it toGenerate::configure(). - Call
XAI::create()with a dummy API key. - Exercise your application code normally and assert on the returned result.
configureXAIWith() used throughout the package’s own test suite extracts the Sdk setup into a single reusable call, which can be useful if you write many tests against the same provider:
Asserting Request Shape
After calling->run(), inspect what was actually sent over the wire using $client->sentBody() for the JSON body and $client->lastRequest for transport-level details like URI path and headers.
Testing Image Generation
Image generation follows the same injection pattern. Provide a fake response that mimics the xAI images endpoint, then assert on both the decoded output and the outgoing request body.providerOptions('xai', ['raw' => [...]]) merges extra fields directly into the request body, so you can assert on them through $client->sentBody() just like any other key.
XAI::reset() and Generate::reset()
Both XAI and Generate hold static singleton state. If one test leaves a provider or Sdk instance registered, the next test will inherit it — leading to false positives or confusing failures.
Call both reset methods in an afterEach hook to wipe all state between tests:
XAI::reset()— sets the static$defaultXAIProviderinstance back tonull. The next call toXAI::model(),XAI::image(), orXAI::default()will lazy-create a fresh provider.Generate::reset()— clears theSdkregistered withGenerate::configure(). Without this, the fakehttpClientfrom one test would be reused by the next.
Running the Test Suite
Thecomposer.json for aisdk/xai ships two convenience scripts:
test:all pipeline mirrors CI — it runs Laravel Pint in --test mode (no writes), PHPStan at its configured strictness level, and then Pest.
FakeHttpClient lives in the tests/ directory under the AiSdk\XAI\Tests\Fakes namespace and is not included in the production Composer autoload. It is therefore not available in your application’s vendor tree. If you want to use the same helper in your own project’s test suite, copy the class directly — it has no dependencies beyond nyholm/psr7 (a common PSR-7 implementation) and the PSR interfaces already required by aisdk/xai.