-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9411be2
commit 6d0ec8a
Showing
4 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,43 @@ | ||
on: [push] | ||
jobs: | ||
test: | ||
test-unit: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '8.3' | ||
- uses: php-actions/composer@v6 | ||
- run: ./vendor/bin/phpunit | ||
- run: ./vendor/bin/phpunit --group unit | ||
|
||
test-database: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
database: | ||
- service: mysql | ||
database_url: pdo-mysql://root@127.0.0.1:32016/DataAccessKit | ||
- service: mariadb | ||
database_url: pdo-mysql://root@127.0.0.1:35098/DataAccessKit | ||
- service: postgres | ||
database_url: pdo-pgsql://postgres:postgres@127.0.0.1:55720/DataAccessKit | ||
- service: sqlite | ||
database_url: "pdo-sqlite:///:memory:" | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: adambirds/docker-compose-action@v1.3.0 | ||
with: | ||
compose-file: docker-compose.yaml | ||
services: ${{ matrix.database.service }} | ||
- uses: stringbean/docker-healthcheck-action@v1 | ||
with: | ||
container: data-access-kit-src_${{ matrix.database.service }}_1 | ||
wait-time: 30 | ||
require-status: running | ||
require-healthy: true | ||
- uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '8.3' | ||
- uses: php-actions/composer@v6 | ||
- run: DATABASE_URL=${{ matrix.database.database_url }} ./vendor/bin/phpunit --group database |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DataAccessKit\Fixture; | ||
|
||
use DataAccessKit\Attribute\Column; | ||
use DataAccessKit\Attribute\Table; | ||
|
||
#[Table] | ||
class User | ||
{ | ||
#[Column(name: "user_id", primary: true, generated: true)] | ||
public int $id; | ||
#[Column] | ||
public string $firstName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DataAccessKit; | ||
|
||
use DataAccessKit\Fixture\User; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\DriverManager; | ||
use Doctrine\DBAL\Tools\DsnParser; | ||
use PHPUnit\Framework\Attributes\Group; | ||
use PHPUnit\Framework\TestCase; | ||
use function getenv; | ||
use function iterator_to_array; | ||
use function var_dump; | ||
|
||
#[Group("database")] | ||
class PersistenceTest extends TestCase | ||
{ | ||
|
||
private Connection $connection; | ||
private Persistence $persistence; | ||
|
||
public function setUp(): void | ||
{ | ||
if (getenv("DATABASE_URL") === false) { | ||
$this->markTestSkipped("Environment variable DATABASE_URL not set"); | ||
} | ||
|
||
$dsnParser = new DsnParser(); | ||
$this->connection = DriverManager::getConnection($dsnParser->parse(getenv("DATABASE_URL"))); | ||
$this->persistence = new Persistence($this->connection, new Registry(new DefaultNameConverter()), new DefaultValueConverter()); | ||
} | ||
|
||
private function setUpUsersTable(): void | ||
{ | ||
$this->connection->executeStatement("DROP TABLE IF EXISTS users"); | ||
$this->connection->executeStatement("CREATE TABLE users (user_id INT PRIMARY KEY, first_name VARCHAR(255))"); | ||
$this->connection->executeStatement("INSERT INTO users (user_id, first_name) VALUES (1, 'Alice')"); | ||
$this->connection->executeStatement("INSERT INTO users (user_id, first_name) VALUES (2, 'Bob')"); | ||
} | ||
|
||
public function testSelect(): void | ||
{ | ||
$this->setUpUsersTable(); | ||
$users = iterator_to_array($this->persistence->select(User::class, "SELECT user_id, first_name FROM users")); | ||
$this->assertCount(2, $users); | ||
$this->assertEquals(1, $users[0]->id); | ||
$this->assertEquals("Alice", $users[0]->firstName); | ||
$this->assertEquals(2, $users[1]->id); | ||
$this->assertEquals("Bob", $users[1]->firstName); | ||
} | ||
|
||
public function testSelectScalar(): void | ||
{ | ||
$this->setUpUsersTable(); | ||
$count = $this->persistence->selectScalar("SELECT COUNT(*) FROM users"); | ||
$this->assertEquals(2, $count); | ||
} | ||
|
||
public function testExecute(): void | ||
{ | ||
$this->setUpUsersTable(); | ||
$this->persistence->execute("DELETE FROM users WHERE user_id = 1"); | ||
$count = $this->persistence->selectScalar("SELECT COUNT(*) FROM users"); | ||
$this->assertEquals(1, $count); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
services: | ||
mysql: | ||
image: mysql:latest | ||
environment: | ||
MYSQL_ALLOW_EMPTY_PASSWORD: "1" | ||
MYSQL_DATABASE: "DataAccessKit" | ||
ports: | ||
- 32016:3306 | ||
tmpfs: | ||
- /var/lib/mysql | ||
healthcheck: | ||
test: ["CMD", "mysqladmin", "ping"] | ||
interval: 1s | ||
timeout: 1s | ||
retries: 30 | ||
start_period: 30s | ||
mariadb: | ||
image: mariadb:latest | ||
environment: | ||
MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: "1" | ||
MARIADB_DATABASE: "DataAccessKit" | ||
ports: | ||
- 35098:3306 | ||
tmpfs: | ||
- /var/lib/mysql | ||
healthcheck: | ||
test: ["CMD", "mariadb-admin", "ping"] | ||
interval: 1s | ||
timeout: 1s | ||
retries: 30 | ||
start_period: 30s | ||
postgres: | ||
image: postgres:latest | ||
shm_size: 128MB | ||
environment: | ||
POSTGRES_PASSWORD: "postgres" | ||
POSTGRES_DB: "DataAccessKit" | ||
ports: | ||
- 55720:5432 | ||
tmpfs: | ||
- /var/lib/postgresql | ||
- /var/lib/postgresql/data | ||
healthcheck: | ||
test: ["CMD", "pg_isready", "-U", "postgres"] | ||
interval: 1s | ||
timeout: 1s | ||
retries: 30 | ||
start_period: 30s | ||
sqlite: | ||
image: busybox | ||
command: | ||
- tail | ||
- -f | ||
- /dev/null | ||
healthcheck: | ||
test: ["CMD", "true"] | ||
interval: 1s | ||
timeout: 1s | ||
retries: 30 | ||
start_period: 30s |