From 8ca38bb624c6068e0117b0af18d5ddd40a77bd92 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 22 Feb 2024 16:48:13 +0100 Subject: [PATCH] Introduce `ipl\Tests\Orm\Lib\DataProviders` plus integration into github actions --- .github/workflows/php.yml | 41 +++++++++++++++++ tests/Lib/DataProviders.php | 92 +++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 tests/Lib/DataProviders.php diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index ac1f138..a288bea 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -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 @@ -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 diff --git a/tests/Lib/DataProviders.php b/tests/Lib/DataProviders.php new file mode 100644 index 0000000..40f56e1 --- /dev/null +++ b/tests/Lib/DataProviders.php @@ -0,0 +1,92 @@ + [$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'); + } +}