|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Litebase\Tests\Integration; |
| 6 | + |
| 7 | +use Litebase\ApiClient; |
| 8 | +use Litebase\Configuration; |
| 9 | +use Litebase\LitebasePDO; |
| 10 | +use Litebase\OpenAPI\Model\DatabaseStoreRequest; |
| 11 | +use PDO; |
| 12 | + |
| 13 | +beforeAll(function () { |
| 14 | + LitebaseContainer::start(); |
| 15 | + |
| 16 | + $configuration = new Configuration(); |
| 17 | + |
| 18 | + $configuration |
| 19 | + ->setHost('127.0.0.1') |
| 20 | + ->setPort('8888') |
| 21 | + ->setUsername('root') |
| 22 | + ->setPassword('password'); |
| 23 | + |
| 24 | + $client = new ApiClient($configuration); |
| 25 | + |
| 26 | + try { |
| 27 | + $client->database()->createDatabase(new DatabaseStoreRequest([ |
| 28 | + 'name' => 'test', |
| 29 | + ])); |
| 30 | + } catch (\Exception $e) { |
| 31 | + throw new \RuntimeException('Failed to connect to Litebase server for integration tests: ' . $e->getMessage()); |
| 32 | + } |
| 33 | +}); |
| 34 | + |
| 35 | +afterAll(function () { |
| 36 | + LitebaseContainer::stop(); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('LitebasePDO', function () { |
| 40 | + $pdo = new LitebasePDO([ |
| 41 | + 'host' => 'localhost', |
| 42 | + 'port' => '8888', |
| 43 | + 'username' => 'root', |
| 44 | + 'password' => 'password', |
| 45 | + 'database' => 'test/main', |
| 46 | + ]); |
| 47 | + |
| 48 | + test('can perform a transaction', function () use ($pdo) { |
| 49 | + $result = $pdo->beginTransaction(); |
| 50 | + expect($result)->toBeTrue(); |
| 51 | + |
| 52 | + $affectedRows = $pdo->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT)'); |
| 53 | + |
| 54 | + expect($affectedRows)->toBeGreaterThanOrEqual(0); |
| 55 | + |
| 56 | + $statement = $pdo->prepare('INSERT INTO users (name, email) VALUES (?, ?)'); |
| 57 | + |
| 58 | + $insertResult = $statement->execute(['Alice', 'alice@example.com']); |
| 59 | + |
| 60 | + expect($insertResult)->toBeTrue(); |
| 61 | + |
| 62 | + $result = $pdo->commit(); |
| 63 | + expect($result)->toBeTrue(); |
| 64 | + |
| 65 | + $statement = $pdo->prepare('SELECT * FROM users WHERE email = ?'); |
| 66 | + $statement->execute(['alice@example.com']); |
| 67 | + |
| 68 | + /** @var array<string, mixed> $user */ |
| 69 | + $user = $statement->fetch(PDO::FETCH_ASSOC); |
| 70 | + |
| 71 | + expect($user['name'])->toBe('Alice'); |
| 72 | + expect($user['email'])->toBe('alice@example.com'); |
| 73 | + }); |
| 74 | +}); |
0 commit comments