• Start

Languages

/

PHP

/

Frameworks

/

Laravel

Testing

Test SurrealDB code in Laravel with the fake query executor, query assertions, and the trait that resets Surqlize's global state between tests.

The integration provides a fake executor so you can assert the queries your code sends without a live database, plus a trait that resets Surqlize's global caches between tests.

Surqlize keeps global state in its ConnectionManager and metadata caches. The RefreshSurqlizeState trait resets it so tests do not leak state into one another.

use SurrealDB\Laravel\Testing\RefreshSurqlizeState;

final class UserTest extends TestCase
{
use RefreshSurqlizeState;

protected function tearDown(): void
{
$this->resetSurqlizeState();

parent::tearDown();
}
}

The trait also offers useSurqlizeExecutor() to set a specific executor, fakeSurrealDB() to fake the managed executor, and resetSurrealDBFakes() to clear fakes.

SurrealDB::fake() swaps the managed executor for a FakeQueryExecutor that records queries instead of running them. Assert what was sent with assertSurrealQuerySent(), or on the returned fake with assertQuerySent().

use SurrealDB\Laravel\Facades\SurrealDB;

$fake = SurrealDB::fake();

SurrealDB::run('RETURN true;');

SurrealDB::assertSurrealQuerySent('RETURN true;');
$fake->assertQuerySent('RETURN true;');

The fake also provides assertNothingSent() for asserting that no queries were sent.

Fakes are scoped by connection name, so you can fake one connection and leave the others untouched.

SurrealDB::fake('analytics');

SurrealDB::run('RETURN true;', connection: 'analytics');

SurrealDB::assertSurrealQuerySent('RETURN true;', connection: 'analytics');

For unit tests that do not boot the application, pass a FakeQueryExecutor directly to a Surqlize query through withExecutor(), or register it globally with useSurqlizeExecutor() from the trait.

use SurrealDB\Laravel\Testing\FakeQueryExecutor;

$fake = new FakeQueryExecutor();

User::query()->withExecutor($fake)->collect();

$fake->assertQuerySent('SELECT * FROM user');

Was this page helpful?