Skip to content

Commit

Permalink
Introduce ipl\Tests\Orm\Lib\DataProviders
Browse files Browse the repository at this point in the history
plus integration into github actions
  • Loading branch information
nilmerg committed Feb 22, 2024
1 parent 26b245d commit 8ca38bb
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ jobs:
- php: '7.2'
phpunit-version: 8.5

services:
mysql:
image: mariadb
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: icinga_unittest
MYSQL_USER: icinga_unittest
MYSQL_PASSWORD: icinga_unittest
options: >-
--health-cmd "mariadb -s -uroot -proot -e'SHOW DATABASES;' 2> /dev/null | grep icinga_unittest > test"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 3306/tcp

pgsql:
image: postgres
env:
POSTGRES_USER: icinga_unittest
POSTGRES_PASSWORD: icinga_unittest
POSTGRES_DB: icinga_unittest
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432/tcp

steps:
- name: Checkout code base
uses: actions/checkout@v3
Expand All @@ -77,4 +107,15 @@ jobs:
run: composer install -n --no-progress

- name: PHPUnit
env:
MYSQL_TESTDB: icinga_unittest
MYSQL_TESTDB_HOST: 127.0.0.1
MYSQL_TESTDB_PORT: ${{ job.services.mysql.ports['3306'] }}
MYSQL_TESTDB_USER: icinga_unittest
MYSQL_TESTDB_PASSWORD: icinga_unittest
PGSQL_TESTDB: icinga_unittest
PGSQL_TESTDB_HOST: 127.0.0.1
PGSQL_TESTDB_PORT: ${{ job.services.pgsql.ports['5432'] }}
PGSQL_TESTDB_USER: icinga_unittest
PGSQL_TESTDB_PASSWORD: icinga_unittest
run: phpunit --verbose
92 changes: 92 additions & 0 deletions tests/Lib/DataProviders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace ipl\Tests\Orm\Lib;

use ipl\Sql\Connection;
use RuntimeException;

trait DataProviders
{
/**
* Create the schema for the test database
*
* @param Connection $db
* @param string $driver
*
* @return void
*/
abstract protected function createSchema(Connection $db, string $driver): void;

/**
* Drop the schema of the test database
*
* @param Connection $db
* @param string $driver
*
* @return void
*/
abstract protected function dropSchema(Connection $db, string $driver): void;

/**
* Provide the MySQL database connection
*
* @return array{mysql: Connection[], pgsql: Connection[]}
*/
public function databases(): array
{
return [
'mysql' => [$this->createConnection('mysql')],
'pgsql' => [$this->createConnection('pgsql')]
];
}

/**
* Get the value of an environment variable
*
* @param string $name
*
* @return string
*
* @throws RuntimeException if the environment variable is not set
*/
private function getEnvVariable(string $name): string
{
$value = getenv($name);
if ($value === false) {
throw new RuntimeException("Environment variable $name is not set");
}

return $value;
}

/**
* Create a database connection
*
* @param string $driver
*
* @return Connection
*/
private function createConnection(string $driver): Connection
{
return new Connection([
'db' => $driver,
'host' => $this->getEnvVariable(strtoupper($driver) . '_TESTDB_HOST'),
'port' => $this->getEnvVariable(strtoupper($driver) . '_TESTDB_PORT'),
'username' => $this->getEnvVariable(strtoupper($driver) . '_TESTDB_USER'),
'password' => $this->getEnvVariable(strtoupper($driver) . '_TESTDB_PASSWORD'),
'dbname' => $this->getEnvVariable(strtoupper($driver) . '_TESTDB'),
]);
}

public function setUp(): void
{
$this->createSchema($this->createConnection('mysql'), 'mysql');
$this->createSchema($this->createConnection('pgsql'), 'pgsql');
}

public function tearDown(): void
{
$this->dropSchema($this->createConnection('mysql'), 'mysql');
$this->dropSchema($this->createConnection('pgsql'), 'pgsql');
}
}

0 comments on commit 8ca38bb

Please sign in to comment.