Skip to content

Commit bb7dbbe

Browse files
authored
Merge pull request #3 from litebase/update-readme
Add LitebasePDO integration test and update docs
2 parents 7b94c86 + 6b61430 commit bb7dbbe

9 files changed

Lines changed: 162 additions & 57 deletions

File tree

.github/workflows/integration-tests.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
workflow_dispatch:
77

88
jobs:
9-
test:
9+
integration-tests:
1010
runs-on: ubuntu-latest
1111

1212
steps:
@@ -21,14 +21,6 @@ jobs:
2121
- name: Install dependencies
2222
run: composer install
2323

24-
- name: Set up Docker Compose
25-
uses: docker/setup-compose-action@v1
26-
27-
- name: Prepare data directory with correct permissions
28-
run: |
29-
mkdir -p ./tests/.litebase
30-
chmod 777 ./tests/.litebase
31-
3224
- name: Run integration tests
3325
run: vendor/bin/pest tests/Integration
3426

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ jobs:
2121
run: composer install --no-interaction --prefer-dist --optimize-autoloader
2222

2323
- name: Tests
24-
run: ./vendor/bin/pest --ci
24+
run: ./vendor/bin/pest tests/Unit --ci

README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,45 @@ A PHP SDK for interacting with [Litebase](https://github.com/litebase/litebase),
99
You can install the package via composer:
1010

1111
```bash
12-
composer require ...
12+
composer require litebase/litebase-php
1313
```
1414

1515
## Usage
1616

17-
``` php
18-
// Usage description here
17+
```php
18+
use Litebase\Configuration;
19+
use Litebase\LitebasePDO;
20+
21+
$pdo = new LitebasePDO([
22+
'host' => 'localhost',
23+
'port' => 8888,
24+
'token' => 'your_api_token',
25+
'database' => 'your_database_name/main',
26+
]);
27+
28+
$statement = $pdo->prepare('SELECT * FROM users WHERE id = ?');
29+
$statement->execute([1]);
30+
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
31+
32+
foreach ($result as $row) {
33+
print_r($row);
34+
}
35+
36+
// Use transactions
37+
$pdo = $pdo->beginTransaction();
38+
39+
try {
40+
$statement = $pdo->prepare('INSERT INTO users (name, email) VALUES (?, ?)');
41+
$statement->execute(['John Doe', 'john@example.com']);
42+
43+
$statement = $pdo->prepare('INSERT INTO logs (user_id, action) VALUES (?, ?)');
44+
$statement->execute([$pdo->lastInsertId(), 'user_created']);
45+
46+
$pdo->commit();
47+
} catch (\Exception $e) {
48+
$pdo->rollBack();
49+
throw $e;
50+
}
1951
```
2052

2153
## Contributing
@@ -24,10 +56,17 @@ Please see [CONTRIBUTING](https://github.com/litebase/litebase-php?tab=contribut
2456

2557
### Testing
2658

59+
You can run the tests with:
2760
``` bash
2861
composer test
2962
```
3063

64+
Integration test requires a running Litebase instance. You can start one using Docker:
65+
66+
```bash
67+
docker run -d -p 8888:8888 --name litebase litebase/litebase:latest
68+
```
69+
3170
## Code of Conduct
3271

3372
Please see [CODE OF CONDUCT](https://github.com/litebase/litebase-php?tab=coc-ov-file) for details.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"generate_open_api": "openapi-generator-cli generate -c openapi_config.yaml",
5151
"phpstan": "vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=512M src tests",
5252
"pint": "vendor/bin/pint --no-interaction",
53-
"test": "vendor/bin/pest",
53+
"test": "vendor/bin/pest tests/Unit",
54+
"test-integration": "vendor/bin/pest tests/Integration",
5455
"test-coverage": "vendor/bin/pest --coverage-html coverage"
5556
}
5657
}

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
>
1010
<testsuites>
1111
<testsuite name="Test Suite">
12+
<directory>tests/Integration</directory>
1213
<directory>tests/Unit</directory>
1314
</testsuite>
1415
</testsuites>

tests/Integration/ApiClientTest.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,21 @@
1818
$client = new ApiClient($configuration);
1919

2020
beforeAll(function () use ($client) {
21-
exec('docker compose -f ./tests/docker-compose.test.yml up -d');
22-
23-
// Give the container a moment to initialize
24-
sleep(2);
21+
LitebaseContainer::start();
2522

2623
try {
2724
$response = $client->clusterStatus()->listClusterStatuses();
2825
} catch (\Exception $e) {
29-
$lines = [];
30-
exec('docker ps -a');
31-
exec('docker compose -f ./tests/docker-compose.test.yml logs --tail=200 --no-color', $lines, $rc);
32-
33-
$logs = implode("\n", $lines);
34-
35-
throw new \RuntimeException('Failed to connect to Litebase server for integration tests: '.$e->getMessage()."\nContainer logs:\n{$logs}");
26+
throw new \RuntimeException('Failed to connect to Litebase server for integration tests: ' . $e->getMessage());
3627
}
3728

3829
if ($response->getStatus() !== 'success') {
39-
$lines = [];
40-
exec('docker ps -a');
41-
exec('docker compose -f ./tests/docker-compose.test.yml logs --tail=200 --no-color', $lines, $rc);
42-
43-
$logs = implode("\n", $lines);
44-
45-
throw new \RuntimeException('Failed to connect to Litebase server for integration tests.'."Container logs:\n{$logs}");
30+
throw new \RuntimeException('Failed to connect to Litebase server for integration tests.');
4631
}
4732
});
4833

4934
afterAll(function () {
50-
exec('docker compose -f ./tests/docker-compose.test.yml down -v');
51-
// Delete the .litebase directory to clean up any persisted data
52-
exec('rm -rf ./tests/.litebase');
35+
LitebaseContainer::stop();
5336
});
5437

5538
describe('ApiClient', function () use ($client) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Litebase\Tests\Integration;
6+
7+
class LitebaseContainer
8+
{
9+
public static function start(): void
10+
{
11+
// Remove any existing container first
12+
shell_exec("docker rm -f litebase-test 2>/dev/null || true");
13+
14+
$startCommand = "docker run -d --rm --name litebase-test -p 8888:8888 \\
15+
-e LITEBASE_CLUSTER_ID=cluster-1 \\
16+
-e LITEBASE_DATA_PATH=/tmp/data \\
17+
-e LITEBASE_ENCRYPTION_KEY=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \\
18+
-e LITEBASE_ENV=testing \\
19+
-e LITEBASE_PORT=8888 \\
20+
-e LITEBASE_ROOT_USERNAME=root \\
21+
-e LITEBASE_ROOT_PASSWORD=password \\
22+
-e LITEBASE_STORAGE_NETWORK_PATH=/tmp/data/_network \\
23+
-e LITEBASE_STORAGE_TMP_PATH=/tmp \\
24+
-e LITEBASE_STORAGE_OBJECT_MODE=local \\
25+
litebase/litebase start";
26+
27+
shell_exec($startCommand);
28+
29+
sleep(2);
30+
}
31+
32+
public static function stop(): void
33+
{
34+
$stopCommand = "docker stop litebase-test";
35+
shell_exec($stopCommand);
36+
}
37+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
});

tests/docker-compose.test.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)