Skip to content

Commit 086c0a4

Browse files
authored
Merge pull request #77 from se7enxweb/copilot/update-database-tests-setup
Add MongoDB and sqlite3 runtime support to PHPUnit GitHub Actions workflow
2 parents 20846b4 + 16dbf9a commit 086c0a4

3 files changed

Lines changed: 144 additions & 13 deletions

File tree

.github/workflows/phpunit.yml

Lines changed: 131 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ on:
1919
- '.github/workflows/phpunit.yml'
2020
workflow_dispatch:
2121

22+
permissions:
23+
contents: read
24+
25+
env:
26+
CI_COMPOSER_FILE: composer.ci-phpunit.json
27+
2228
jobs:
2329
phpunit:
2430
name: PHPUnit (PHP ${{ matrix.php }})
@@ -37,14 +43,14 @@ jobs:
3743
uses: shivammathur/setup-php@v2
3844
with:
3945
php-version: ${{ matrix.php }}
40-
extensions: dom, libxml, mbstring, pcre, json, iconv, reflection, session, spl, simplexml, gd, mysqli, pdo_mysql, pdo_sqlite, zip
46+
extensions: dom, libxml, mbstring, pcre, json, iconv, reflection, session, spl, simplexml, gd, mongodb, mysqli, pdo_mysql, pdo_sqlite, sqlite3, zip
4147
coverage: xdebug
4248
tools: composer:v2
4349

44-
- name: Install ImageMagick
50+
- name: Install system packages
4551
run: |
4652
sudo apt-get update
47-
sudo apt-get install -y imagemagick
53+
sudo apt-get install -y imagemagick libbson-dev libmongoc-dev
4854
4955
- name: Get Composer cache directory
5056
id: composer-cache
@@ -54,30 +60,27 @@ jobs:
5460
uses: actions/cache@v4
5561
with:
5662
path: ${{ steps.composer-cache.outputs.dir }}
57-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
63+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock', 'composer.json', '.github/workflows/phpunit.yml') }}
5864
restore-keys: ${{ runner.os }}-composer-
5965

6066
- name: Install dependencies
6167
run: |
68+
cp composer.json "${CI_COMPOSER_FILE}"
6269
if [[ "${{ matrix.php }}" == "8.1" || "${{ matrix.php }}" == "8.2" || "${{ matrix.php }}" == "8.3" ]]; then
63-
trap 'rm -f composer.ci-phpunit-legacy.json composer.ci-phpunit-legacy.lock' EXIT
64-
cp composer.json composer.ci-phpunit-legacy.json
6570
if [[ "${{ matrix.php }}" == "8.1" ]]; then
6671
export PHPUNIT_VERSION="^10.5"
6772
else
6873
export PHPUNIT_VERSION="^11.5"
6974
fi
70-
php -r '$file = "composer.ci-phpunit-legacy.json"; $data = json_decode(file_get_contents($file), true, 512, JSON_THROW_ON_ERROR); $data["require-dev"]["phpunit/phpunit"] = getenv("PHPUNIT_VERSION"); file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL);'
71-
COMPOSER=composer.ci-phpunit-legacy.json composer install --prefer-dist --no-progress --no-interaction --ignore-platform-req=php
72-
else
73-
composer install --prefer-dist --no-progress --no-interaction --ignore-platform-req=php
7475
fi
76+
php -r '$file = getenv("CI_COMPOSER_FILE"); $data = json_decode(file_get_contents($file), true, 512, JSON_THROW_ON_ERROR); if (($phpunit = getenv("PHPUNIT_VERSION")) !== false && $phpunit !== "") { $data["require-dev"]["phpunit/phpunit"] = $phpunit; } $data["require-dev"]["mongodb/mongodb"] = "^1.21"; file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL);'
77+
COMPOSER="${CI_COMPOSER_FILE}" composer install --prefer-dist --no-progress --no-interaction --ignore-platform-req=php
7578
7679
- name: Ensure vendor is present
7780
run: |
7881
if [[ ! -x vendor/bin/phpunit ]]; then
7982
echo "vendor/bin/phpunit is missing, reinstalling dependencies"
80-
composer install --prefer-dist --no-progress --no-interaction --ignore-platform-req=php
83+
COMPOSER="${CI_COMPOSER_FILE}" composer install --prefer-dist --no-progress --no-interaction --ignore-platform-req=php
8184
fi
8285
test -x vendor/bin/phpunit
8386
@@ -87,3 +90,120 @@ jobs:
8790

8891
- name: Run PHPUnit
8992
run: vendor/bin/phpunit --colors=always
93+
94+
- name: Clean CI Composer overlay
95+
if: always()
96+
run: rm -f "${CI_COMPOSER_FILE}" "${CI_COMPOSER_FILE%.json}.lock"
97+
98+
phpunit-db:
99+
name: Database tests (${{ matrix.db }}, PHP ${{ matrix.php }})
100+
runs-on: ubuntu-latest
101+
102+
strategy:
103+
fail-fast: false
104+
matrix:
105+
include:
106+
- php: '8.3'
107+
db: mysql
108+
dsn: 'mysql://root@127.0.0.1/testdb'
109+
- php: '8.3'
110+
db: postgresql
111+
dsn: 'postgresql://postgres@127.0.0.1/testdb'
112+
113+
services:
114+
mysql:
115+
image: mysql:8.0
116+
env:
117+
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
118+
MYSQL_DATABASE: testdb
119+
ports:
120+
- 3306:3306
121+
options: >-
122+
--health-cmd="mysqladmin ping -h 127.0.0.1 -uroot"
123+
--health-interval=10s
124+
--health-timeout=5s
125+
--health-retries=10
126+
postgresql:
127+
image: postgres:15
128+
env:
129+
POSTGRES_USER: postgres
130+
POSTGRES_DB: postgres
131+
POSTGRES_HOST_AUTH_METHOD: trust
132+
ports:
133+
- 5432:5432
134+
options: >-
135+
--health-cmd="pg_isready -h 127.0.0.1 -U postgres -d postgres"
136+
--health-interval=10s
137+
--health-timeout=5s
138+
--health-retries=10
139+
140+
env:
141+
TEST_DB_NAME: testdb
142+
143+
steps:
144+
- name: Checkout code
145+
uses: actions/checkout@v4
146+
147+
- name: Setup PHP
148+
uses: shivammathur/setup-php@v2
149+
with:
150+
php-version: ${{ matrix.php }}
151+
extensions: dom, libxml, mbstring, pcre, json, iconv, reflection, session, spl, simplexml, gd, mongodb, mysqli, pgsql, pdo_mysql, pdo_pgsql, pdo_sqlite, sqlite3, zip
152+
coverage: none
153+
tools: composer:v2
154+
155+
- name: Install system packages
156+
run: |
157+
sudo apt-get update
158+
sudo apt-get install -y imagemagick libbson-dev libmongoc-dev
159+
160+
- name: Get Composer cache directory
161+
id: composer-cache-db
162+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
163+
164+
- name: Cache Composer dependencies
165+
uses: actions/cache@v4
166+
with:
167+
path: ${{ steps.composer-cache-db.outputs.dir }}
168+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock', 'composer.json', '.github/workflows/phpunit.yml') }}
169+
restore-keys: ${{ runner.os }}-composer-
170+
171+
- name: Install dependencies
172+
run: |
173+
cp composer.json "${CI_COMPOSER_FILE}"
174+
export PHPUNIT_VERSION="^11.5"
175+
php -r '$file = getenv("CI_COMPOSER_FILE"); $data = json_decode(file_get_contents($file), true, 512, JSON_THROW_ON_ERROR); if (($phpunit = getenv("PHPUNIT_VERSION")) !== false && $phpunit !== "") { $data["require-dev"]["phpunit/phpunit"] = $phpunit; } $data["require-dev"]["mongodb/mongodb"] = "^1.21"; file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL);'
176+
COMPOSER="${CI_COMPOSER_FILE}" composer install --prefer-dist --no-progress --no-interaction --ignore-platform-req=php
177+
178+
- name: Ensure vendor is present
179+
run: |
180+
if [[ ! -x vendor/bin/phpunit ]]; then
181+
echo "vendor/bin/phpunit is missing, reinstalling dependencies"
182+
COMPOSER="${CI_COMPOSER_FILE}" composer install --prefer-dist --no-progress --no-interaction --ignore-platform-req=php
183+
fi
184+
test -x vendor/bin/phpunit
185+
186+
- name: Generate autoloads
187+
run: php bin/php/ezpgenerateautoloads.php -s -e || true
188+
189+
- name: Prepare database
190+
run: |
191+
case "${{ matrix.db }}" in
192+
mysql)
193+
mysql -h 127.0.0.1 -uroot -e "CREATE DATABASE IF NOT EXISTS ${TEST_DB_NAME};"
194+
;;
195+
postgresql)
196+
psql -v ON_ERROR_STOP=1 -h 127.0.0.1 -U postgres -d postgres -c "CREATE DATABASE ${TEST_DB_NAME};"
197+
psql -v ON_ERROR_STOP=1 -h 127.0.0.1 -U postgres -d "${TEST_DB_NAME}" -c "CREATE EXTENSION IF NOT EXISTS pgcrypto;"
198+
;;
199+
esac
200+
201+
- name: Run database tests
202+
run: |
203+
TEST_TIMEZONE=$(shuf -e UTC America/New_York Asia/Kolkata -n 1)
204+
echo "$TEST_TIMEZONE"
205+
php -d date.timezone="$TEST_TIMEZONE" tests/runtests.php --dsn "${{ matrix.dsn }}" --db-per-test tests extension/ezoe
206+
207+
- name: Clean CI Composer overlay
208+
if: always()
209+
run: rm -f "${CI_COMPOSER_FILE}" "${CI_COMPOSER_FILE%.json}.lock"

tests/bootstrap.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@
7272
class_alias( \PHPUnit\Framework\TestCase::class, 'PHPUnit_Framework_TestCase' );
7373
}
7474

75+
if ( !class_exists( 'PHPUnit_Runner_Version', false ) )
76+
{
77+
class PHPUnit_Runner_Version
78+
{
79+
public static function id(): string
80+
{
81+
return \PHPUnit\Runner\Version::id();
82+
}
83+
}
84+
}
85+
7586
if ( !class_exists( 'PHPUnit_Framework_TestSuite', false ) )
7687
{
7788
class PHPUnit_Framework_TestSuite

tests/runtests.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
set_time_limit( 0 );
1313

14-
require_once 'vendor/autoload.php';
14+
require_once __DIR__ . '/bootstrap.php';
1515

16-
require_once 'autoload.php';
16+
require_once __DIR__ . '/../autoload.php';
1717

1818
if ( !class_exists( 'ezpTestRunner', true ) )
1919
{

0 commit comments

Comments
 (0)