Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/elevenlabs/llms.txt

Use this file to discover all available pages before exploring further.

The aisdk/elevenlabs test suite is built on Pest and follows a fixture- and conformance-based approach. No live API credentials are required to run the default suite — all HTTP interactions are intercepted by in-memory fakes that return pre-set responses. Live network verification tests exist separately and are never included in the default composer test run.

Running the Test Suite

1

Run the full test suite

composer test
Executes the Pest suite against all tests in the tests/ directory.
2

Run lint, static analysis, and tests together

composer test:all
Runs Pint code-style checks, PHPStan static analysis, and then the full Pest suite in sequence. Use this before opening a pull request.
3

Check code style independently

composer lint
Runs Laravel Pint in --test mode (reports violations without modifying files).
4

Run static analysis independently

composer analyze
Runs PHPStan with a 512 MB memory limit.
5

Auto-fix code style

composer format
Runs Laravel Pint and applies code-style fixes in place (unlike lint, this modifies files).
6

Preview Rector refactors

composer refactor:dry
Runs Rector in dry-run mode — shows what automated refactors would be applied without changing any files.
7

Apply Rector refactors

composer refactor
Runs Rector and applies automated refactors to the codebase.

Test File Structure

The test suite is organised by capability area. Each file covers one slice of the provider surface:
FileWhat it tests
tests/ElevenLabsSpeechTest.phpTTS model tests — request shape, query parameters, audio-format mapping
tests/ElevenLabsTranscriptionTest.phpScribe v2 batch transcription — multipart form fields, provider option forwarding
tests/ElevenLabsLiveTest.phpRealtime session driver — WebSocket handshake, event encoding, client-secret flow
tests/ElevenLabsMediaTest.phpVoice changer, voice isolator, sound effects, and dialogue — endpoint URLs, multipart vs. JSON bodies
tests/ElevenLabsCreativeMediaTest.phpMusic, voice design, dubbing, forced alignment — typed return values, parameter mapping

FakeHttpClient

FakeHttpClient lives in tests/Fakes/FakeHttpClient.php and implements PSR-18’s ClientInterface. Construct it with a fixed status code, response body string, and optional content type; it records every request it receives so tests can assert on exact URIs, headers, and body payloads.
use AiSdk\ElevenLabs\Tests\Fakes\FakeHttpClient;

$client = new FakeHttpClient(200, 'audio-bytes', 'audio/mpeg');
configureElevenLabsWith($client);
ElevenLabs::create(['apiKey' => 'xi-test']);

$result = Generate::speech('Hello there')
    ->model(ElevenLabs::model('eleven_flash_v2_5'))
    ->voice('voice-id')
    ->run();

// Assert on the outbound request
echo (string) $client->lastRequest?->getUri();
// → https://api.elevenlabs.io/v1/text-to-speech/voice-id

// Decode the JSON request body
$body = $client->sentBody();
// → ['text' => 'Hello there', 'model_id' => 'eleven_flash_v2_5']

// Inspect a request header
$apiKey = $client->lastRequest?->getHeaderLine('xi-api-key');
// → xi-test
__construct(int $status, string $body, string $contentType, array $headers)
method
Creates a fake that always returns a response with the given HTTP status, body string, and Content-Type header. Pass additional headers as the fourth argument when needed.
lastRequest
RequestInterface|null
The most recent PSR-7 request received by the fake. Inspect getUri(), getHeaderLine(), or getBody() to assert on the outbound call.
sentBody(): array
method
Decodes the last request’s body as JSON and returns it as an associative array. Returns an empty array when the body is not valid JSON (e.g. for multipart requests; inspect (string) $client->lastRequest?->getBody() directly in those cases).
configureElevenLabsWith(FakeHttpClient $client) is a test-only helper defined at the top of each test file. It constructs a Nyholm\Psr7\Factory\Psr17Factory and passes both the factory and the fake client into Generate::configure() via an AiSdk\Support\Sdk value object. This wires the PSR-17 request/response factory and the PSR-18 HTTP runner together, ensuring the SDK never makes real network calls during the test run.

FakeTransport

FakeTransport and the inner FakeTransportConnection class live in tests/Fakes/FakeTransport.php and simulate a WebSocket connection for realtime-session tests. FakeTransport implements TransportInterface and always returns a FakeTransportConnection from connect(). The connection records every frame sent by the SDK and lets tests enqueue incoming frames for the session to consume.
use AiSdk\ElevenLabs\Tests\Fakes\FakeTransport;

$transport = new FakeTransport();

$session = Live::transcribe()
    ->model(ElevenLabs::model('scribe_v2_realtime'))
    ->language('en')
    ->connect($transport);

// Inspect the endpoint the SDK connected to
echo (string) $transport->endpoint?->uri;

// Inspect frames the SDK sent
$sent = $transport->connection->sent;

// Enqueue a synthetic inbound frame for the session to process
$transport->connection->enqueue($someFrame);

// Check connection lifecycle
$transport->connection->closed;          // bool
$transport->connection->sendingFinished; // bool
endpoint
TransportEndpoint|null
The endpoint the SDK passed to connect(). Inspect $transport->endpoint->uri to assert the correct WebSocket URL and query parameters.
connection
FakeTransportConnection
The active connection instance. Use its properties to assert on frames sent and to feed inbound events.
connection->sent
list<TransportFrame>
All frames sent by the SDK over this connection, in order.
connection->enqueue(TransportFrame ...$frames)
method
Adds frames to the inbound queue. The session’s event loop will receive these on the next receive() call.
connection->closed
bool
true after the SDK calls close() on the connection.
connection->sendingFinished
bool
true after the SDK calls finishSending(), signalling the end of the audio stream.

Resetting Singletons Between Tests

The ElevenLabs facade and the core Generate facade both hold a static default instance. Tests must reset these after each case to prevent state leaking between tests:
afterEach(function () {
    Generate::reset();
    ElevenLabs::reset();
});
This pattern appears at the top of every test file in the suite. ElevenLabs::reset() sets the internal $default provider back to null; Generate::reset() clears the configured Sdk instance. The next call to ElevenLabs::create() or Generate::configure() starts from a clean slate.
Always pair ElevenLabs::reset() with Generate::reset() in afterEach. Resetting only one of them can leave a stale PSR-18 client or a stale provider wired to the other singleton, producing confusing assertion failures in later tests.

Live Network Tests

Credentialed live network tests exist in the repository but are kept separate from the main Pest suite. They require a real ELEVENLABS_API_KEY environment variable and make actual HTTP requests to the ElevenLabs API. These tests are not run by composer test or composer test:all and should only be executed explicitly in environments where a valid API key is available and network calls are acceptable.

Build docs developers (and LLMs) love