diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6f8c874..d25143c 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,6 +1,6 @@ on: [push] jobs: - test: + test-unit: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -8,4 +8,29 @@ jobs: 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: + matrix: + database: + - service: mysql + database_url: pdo-mysql://root@localhost:32016/DataAccessKit + - service: mariadb + database_url: pdo-mysql://root@localhost:35098/DataAccessKit + - service: postgres + database_url: pdo-pgsql://postgres:postgres@localhost:55720/DataAccessKit + - service: sqlite + database_url: "pdo-sqlite:///:memory:" + steps: + - uses: actions/checkout@v4 + - uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + - uses: php-actions/composer@v6 + - uses: adambirds/docker-compose-action@v1.3.0 + with: + compose-file: docker-compose.yaml + services: ${{ matrix.database.service }} + - run: DATABASE_URL=${{ matrix.database.database_url }} ./vendor/bin/phpunit --group database diff --git a/data-access-kit/test/Fixture/User.php b/data-access-kit/test/Fixture/User.php new file mode 100644 index 0000000..a913b6e --- /dev/null +++ b/data-access-kit/test/Fixture/User.php @@ -0,0 +1,15 @@ +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); + } + +} diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..707e672 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,32 @@ +services: + mysql: + image: mysql:latest + environment: + MYSQL_ALLOW_EMPTY_PASSWORD: "1" + MYSQL_DATABASE: "DataAccessKit" + ports: + - 32016:3306 + tmpfs: + - /var/lib/mysql + mariadb: + image: mariadb:latest + environment: + MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: "1" + MARIADB_DATABASE: "DataAccessKit" + ports: + - 35098:3306 + tmpfs: + - /var/lib/mysql + 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 + sqlite: + image: gcr.io/google_containers/pause