Install the package:
composer require --dev tarantool/phpunit-extras
Extend Tarantool\PhpUnit\TestCase (or use the AnnotationExtension in phpunit.xml):
use Tarantool\PhpUnit\TestCase;
class MyTarantoolTest extends TestCase {
protected function getClient(): Client {
return new Client('tcp://127.0.0.1:3301');
}
}
First Use Case: Test a Lua operation with pre-execution setup:
/**
* @lua box.schema.space.create('test_space', {if_not_exists = true})
*/
public function testSpaceCreation() {
$this->assertTrue($this->client->spaceExists('test_space'));
}
Pre-Test Setup:
Use @lua/@sql annotations to initialize test data:
/**
* @sql INSERT INTO users (id, name) VALUES (1, 'Alice')
* @lua box.space.users:select{1}
*/
public function testUserFetch() {
$user = $this->client->select('users', 1);
$this->assertEquals('Alice', $user['name']);
}
Request Validation: Assert Tarantool request behavior (e.g., caching):
public function testSpaceCache() {
$this->expectSelectRequestToBeCalledOnce();
$this->client->getSpace('test_space');
$this->client->getSpace('test_space'); // Should not trigger another request
}
Mocking Scenarios: Simulate server responses without a real Tarantool instance:
public function testErrorHandling() {
$mockClient = $this->getTestDoubleClientBuilder()
->shouldHandle(
RequestTypes::EVALUATE,
TestDoubleFactory::createErrorResponse('Error')
)
->build();
$this->expectException(TarantoolException::class);
$mockClient->eval('box.error()');
}
Version-Specific Tests: Skip tests for unsupported Tarantool versions:
/**
* @requires Tarantool ^2.3.0
*/
public function testNewFeature() {
// Only runs if Tarantool >= 2.3.0
}
tarantool/php: Use the official client alongside this package for seamless testing.AnnotationExtension in phpunit.xml to use dynamic Tarantool endpoints:
<extension class="Tarantool\PhpUnit\Annotation\AnnotationExtension">
<arguments><string>tcp://%env(TARANTOOL_HOST)%:%env(TARANTOOL_PORT)%</string></arguments>
</extension>
RequestExpectations and PreparedStatementExpectations for comprehensive request tracking.Annotation Parsing:
@lua/@sql annotations fail silently if the Tarantool server is unreachable.try-catch blocks or mock the client entirely for offline testing:
$mockClient = $this->createDummyClient();
Version Constraints:
@requires Tarantool may misinterpret version strings (e.g., 2.3 vs. 2.3.0).^2.3.0 and verify with composer validate.Mocking Quirks:
TestDoubleClient may not handle custom request types (e.g., CALL2).TestDoubleClientBuilder or use getMockBuilder() for unsupported types.<phpunit ...>
<listeners>
<listener class="Tarantool\PhpUnit\Annotation\AnnotationListener" />
</listeners>
</phpunit>
verifyExpectations() in @after methods to catch unmet request expectations.Custom Annotations:
Extend rybakit/phpunit-extras to add Tarantool-specific requirements (e.g., @requires spaceExists):
class SpaceExistsRequirement extends AbstractRequirement {
public function check(): bool { /* ... */ }
}
Dynamic Client Configuration:
Override getClient() to inject dependencies (e.g., custom packers):
protected function getClient(): Client {
return new Client('tcp://localhost:3301', [
'packer' => new CustomPacker(),
]);
}
Test Data Fixtures:
Combine @lua/@sql with setUp() for hybrid initialization:
public function setUp(): void {
$this->client->eval('box.space.users:truncate()');
}
TestDoubleClient to avoid real Tarantool I/O in CI pipelines.@lua/@sql annotations to minimize connection overhead:
/**
* @lua box.space.users:create()
* @lua box.space.users:format({{name = 'string'}})
*/
How can I help you explore Laravel packages today?