Journal

Start Dynoxide from PHP, Laravel and Node tests

Dynoxide 1.7.0 and 1.8.0 let you start it from Node, PHP or Laravel tests with no process code of your own, and give it a fixed port for local development.

Martin Hicks
The Dynoxide hexagonal logo on a near-black background, with dashed amber lines running from it to three smaller hexagons

I want Dynoxide to be as easy to use as I can make it, in your tests and when you're developing locally. 1.7.0 and 1.8.0 are mostly about that. You can now start it from a Node, PHP or Laravel test suite without writing any process handling, and give it a fixed port when something else needs to know where it is. 1.7.0 went out on the 17th and 1.8.0 on the 20th. I've rolled them into one post because they're one piece of work.

Two new flags

Everything else sits on two flags that 1.7.0 added to the binary.

--ready-json prints a single line of JSON once the server can take requests, with the endpoint, port and process ID in it. Whatever started the server reads that line and knows it can go, so there's nothing to guess. Pair it with --port 0 and the operating system picks a free port, so parallel test runs never fight over 8000.

--exit-with-parent shuts the server down cleanly when its stdin closes. The launcher holds the other end of that pipe, and when the launcher goes away the operating system closes it, however it went: a clean exit, a Ctrl+C or a kill -9. In 0.9.8 the npm wrapper had to forward signals and poll its parent's PID every second to get anywhere near that. It's also the tidy way to stop a server on Windows, which has no SIGTERM.

Those two, plus how to find and verify the binary, are in docs/launcher.md. It's for anyone wrapping Dynoxide in another language, so the wrappers all behave the same way and you don't have to relearn it when you switch.

Packages that do it for you

You shouldn't need to know about either flag to use Dynoxide in your tests. There are now packages for Node, PHP and Laravel that use them, and each one finds the binary, starts a server on a free port and hands back the settings your AWS SDK needs, then stops it when the run ends.

Node

The npm package has a start() now:

import { start } from "dynoxide";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";

const server = await start();
const client = new DynamoDBClient({
  endpoint: server.endpoint,
  region: server.region,
  credentials: server.credentials,
});

// ...

await server.stop();

It runs on a free port and stops when Node exits, whether or not you call stop(). If your runtime has Symbol.asyncDispose, await using server = await start() stops it at the end of the block. When it can't start, the error has a code on it, such as DYNOXIDE_TIMEOUT, so you're not pattern-matching stderr.

PHP

composer require --dev nubo-db/dynoxide gets you a package that downloads the right binary for your platform, checks its hash and caches it. No Docker or Java involved. With PHPUnit, the setup is one extension in phpunit.xml:

<extensions>
    <bootstrap class="Dynoxide\PHPUnit\Extension"/>
</extensions>

That starts a server when the tests start, points the AWS SDK's environment variables at it, and stops it when the run finishes. A new DynamoDbClient(['version' => 'latest']) inside a test talks to Dynoxide without any more configuration. Under ParaTest or pest --parallel, each worker gets its own server, so they never see each other's data.

If the server can't start, the run fails with a warning. The tests that still run point at http://127.0.0.1:1, where nothing's listening, with dummy credentials. A broken setup can't wander off to real DynamoDB with whatever credentials happen to be in your shell.

One to watch: the SDK only honours AWS_ENDPOINT_URL_DYNAMODB properly from aws/aws-sdk-php 3.298.9. Versions back to 3.281.0 read it and then send the requests to AWS anyway. On those, pass the endpoint to the client yourself.

Laravel

Laravel ships DynamoDB drivers for the cache, sessions, rate limiter and failed jobs. nubo-db/dynoxide-laravel is a trait for your base TestCase:

use Dynoxide\Laravel\Testing\UsesDynoxide;

abstract class TestCase extends BaseTestCase
{
    use UsesDynoxide;
}

It reads your app's own config, creates the tables it names with the key attributes you've configured, points those stores at Dynoxide and empties the tables after every test. Custom table names need no extra setup.

The one catch is that DynamoDB can't empty a table in a single call, so Laravel's DynamoDbStore::flush() throws. Switch the cache to DynamoDB and any test calling Cache::flush() will fail. The trait's reset after each test does the same job, so those calls can go.

For local development there's php artisan dynoxide:serve. It runs Dynoxide on port 8800 with your tables already created, out of the way of php artisan serve on 8000.

On the docs site

I've updated dynoxide.dev for all of this. The integrations page covers each package, and there's a Laravel guide for setting it up with the package or without it.

The same page has a community section for packages other people have built on Dynoxide. SEEK's vitest-dynoxide, maintained by Sam Chung, gives each Vitest test file its own in-memory Dynoxide. Hapag-Lloyd's sbt-dynoxide starts one for your Scala tests and stops it afterwards. If you've built something on Dynoxide, there's a link there to get it listed.

A fixed port, and knowing when it's taken

Tests should stay on a free port. Local development is different, because something often wants the endpoint before anything has started: a second app, an .env file or NoSQL Workbench. So 1.8.0 adds a port option to start() in Node and PHP.

For that to be any use, a launcher has to be able to tell when the port's taken. Before 1.8.0 every startup failure exited 1, and the message depended on which check caught it and whether you were on Windows. A taken port now exits 4. Node turns that into DYNOXIDE_PORT_IN_USE and PHP into a PortInUseException, both with the port on them.

The port checks got stricter while I was in there. On macOS, the MCP listener on 127.0.0.1 could bind the same port as DynamoDB on 0.0.0.0 and quietly take the loopback requests meant for DynamoDB. Dynoxide now refuses an --mcp-port that matches --port. It also counts a listener on the other loopback family as a taken port, so --port 8000 won't start if something holds [::1]:8000, because a localhost client might reach that first. It's a cousin of the Windows port hijack in 0.11.3, except this time Dynoxide was the one moving in next door.

What's next

I'm working on Python now: pip install dynoxide, with a pytest plugin, built on the same contract as the Node and PHP packages.

After that, I'd do Java if there's demand for it. Scala builds already have Hapag-Lloyd's plugin. The same goes for .NET or anything else: if you'd use one, open an issue and say so.

Upgrading

Both releases change how a few requests answer, and 1.7.0 upgrades the database file the first time it opens one for writing. The upgrade notes on dynoxide.dev say what to check before you move, and the changelog has the full detail. If you embed Dynoxide in Rust, the crate is now dynoxide-rs 4.1.0.

npm install --save-dev dynoxide@1.8.0 if you're on npm. For PHP it's composer require --dev nubo-db/dynoxide, or nubo-db/dynoxide-laravel for Laravel.

A black and white portrait photo of Martin Hicks.

Written by Martin Hicks.
Software developer, AWS consultant and co-founder of Si Novi.

Back to the journal