Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,9 @@ php artisan tinker

The application includes GitHub Actions workflows in `.github/workflows/`:
- `deploy-preview.yml` - PR previews, `deploy-nightly.yml` - nightly build of `main`, and `upload-altervista-reusable.yml` - deployments, called by other repositories
- `run-pest-tests.yml` - Pest tests (unit and feature), with coverage
- `run-e2e-tests.yml` - Playwright end-to-end tests (`tests/e2e`), on SQLite and MySQL
- `run-pest-tests.yml` - Pest tests (unit and feature), on MySQL (with coverage) and SQLite, each with and without a
table prefix: the database support is tested here
- `run-e2e-tests.yml` - Playwright end-to-end tests (`tests/e2e`), on MySQL; the setup wizard on SQLite and MySQL
- `check-translations.yml` - Translation validation

- **PR previews** (`deploy-preview.yml`): the `PREVIEW_MODE` repository variable sets which PRs get a preview
Expand Down
46 changes: 33 additions & 13 deletions .github/workflows/run-e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,39 @@ env:

jobs:
run-e2e-tests:
name: E2E (${{ matrix.database }})
name: ${{ matrix.suite == 'setup-wizard' && format('E2E setup wizard ({0})', matrix.database) || 'E2E all' }}
runs-on: ubuntu-latest
timeout-minutes: 45
timeout-minutes: ${{ matrix.timeout }}

# The application supports these databases: each one runs the whole suite, the setup wizard included (which
# configures the database of its run, see `tests/e2e/setup-wizard.spec.ts`). PostgreSQL is not supported yet.
# The browser tests run once, on MySQL (the database of production and of the PR previews): the database support
# is tested by the Pest tests (`run-pest-tests.yml`), where the query layer is. The setup wizard is the exception:
# it configures the database of its run (see `tests/e2e/setup-wizard.spec.ts`), so it runs on each supported
# database. To add one (e.g. `mariadb` or `pgsql`), add a `setup-wizard` entry with its image, port and user.
strategy:
fail-fast: false
matrix:
database: [sqlite, mysql]
include:
- database: sqlite
mysql_image: ""
- database: mysql
# Every project but the setup wizard: keep it in sync with the projects of `playwright.config.ts`
- suite: all
database: mysql
projects: --project=auth-setup --project=chromium --project=mobile --project=global-state
timeout: 45
# Same version as docker-compose.yml
mysql_image: mysql:8.0
port: 3306
username: root
- suite: setup-wizard
database: sqlite
projects: --project=setup-wizard
timeout: 15
mysql_image: ""
- suite: setup-wizard
database: mysql
projects: --project=setup-wizard
timeout: 15
mysql_image: mysql:8.0
port: 3306
username: root

# A service with an empty image is not started: only the runs on MySQL have it
services:
Expand Down Expand Up @@ -126,6 +141,8 @@ jobs:
run: npx playwright install-deps chromium

- name: Run E2E tests
env:
PROJECTS: ${{ matrix.projects }}
run: |
# Note when the app server stops answering, with its processes and the memory at that moment (printed by
# the "Print the state of the test servers" step)
Expand All @@ -143,7 +160,9 @@ jobs:
done
) > "${RUNNER_TEMP}/server-watch.log" 2>&1 &

npx playwright test
# Word splitting is wanted: one argument per project
# shellcheck disable=SC2086
npx playwright test ${PROJECTS}

# Printed at the end of the log (the earlier lines of a long log can be hard to reach): why the test servers may
# have stopped, e.g. a crash or the kernel killing them for lack of memory
Expand Down Expand Up @@ -171,7 +190,7 @@ jobs:
if: failure()
uses: actions/upload-artifact@v7
with:
name: playwright-report-${{ matrix.database }}
name: playwright-report-${{ matrix.suite }}-${{ matrix.database }}
path: |
playwright-report/
test-results/
Expand All @@ -181,12 +200,13 @@ jobs:
- name: Explain how to debug the failed tests
if: failure()
env:
DATABASE: ${{ matrix.database }}
REPORT: playwright-report-${{ matrix.suite }}-${{ matrix.database }}
RUN: ${{ matrix.suite }}, ${{ matrix.database }}
run: |
{
echo "### Debugging the failed E2E tests (${DATABASE})"
echo "### Debugging the failed E2E tests (${RUN})"
echo
echo "Download the \`playwright-report-${DATABASE}\` artifact of this run, unzip it, then:"
echo "Download the \`${REPORT}\` artifact of this run, unzip it, then:"
echo
echo "- \`npx playwright show-report playwright-report\`: the report, with the screenshot, the video and the trace of each failed test;"
echo "- \`npx playwright show-trace test-results/<test>/trace.zip\` (or drop the file on https://trace.playwright.dev) to replay a test step by step, with the page, the network and the console;"
Expand Down
163 changes: 65 additions & 98 deletions .github/workflows/run-pest-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,80 @@ env:
,mysqli,mysqlnd,openssl,pcre,PDO,pdo_mysql,pdo_sqlite
,Phar,random,Reflection,session,SimpleXML,SPL,sqlite3,standard
,tokenizer,xml,xmlreader,xmlwriter,xsl,zip,zlib
# Number of days after which the cached Sail image is rebuilt from scratch,
# to pick up updates of its operating system packages, PHP and Node.js.
sail_image_max_age_days: 7

jobs:
run-pest-tests:
name: Pest (prefixed=${{ matrix.db-prefix != '' && 'yes' || 'no' }})
name: Pest (${{ matrix.database }}, prefixed=${{ matrix.db-prefix != '' && 'yes' || 'no' }})
runs-on: ubuntu-latest

# The PR previews share one database, prefixing their tables with `p<N>_` (`DB_PREFIX`): the suite also runs with
# the longest prefix, to catch raw SQL missing the prefix and index names longer than the 64 characters of MySQL.
timeout-minutes: 30

# The database support is tested here, where the query layer (raw SQL, JSON operators, migrations, prefixes) is
# exercised: the E2E tests only run on MySQL. To add a database (e.g. `mariadb` or `pgsql`), add it to `database`,
# an `include` entry with its image, port and user, and its PHP extension to `php_extensions`.
#
# Each database also runs with the longest table prefix: the PR previews share one database, prefixing their tables
# with `p<N>_` (`DB_PREFIX`), so this catches raw SQL missing the prefix and, on MySQL, index names longer than its
# 64 characters.
strategy:
fail-fast: false
matrix:
database: [mysql, sqlite]
db-prefix: ["", "p9999_"]
include:
- database: mysql
# Same version as docker-compose.yml
image: mysql:8.0
port: 3306
username: root
- database: sqlite
image: ""
# The coverage is measured once
- database: mysql
db-prefix: ""
coverage: true

# A service with an empty image is not started: only the runs on a database server have it
services:
database:
image: ${{ matrix.image }}
env:
MYSQL_ROOT_PASSWORD: password
# The parallel processes connect to it to create their own `testing_test_<N>` databases
MYSQL_DATABASE: testing
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping --silent --host=127.0.0.1 -uroot -ppassword"
--health-interval=5s --health-timeout=5s --health-retries=30

# The real environment variables win over `.env` and the `<env>` entries of phpunit.xml
env:
DB_CONNECTION: ${{ matrix.database }}
DB_HOST: 127.0.0.1
# Not empty on SQLite either: the setup wizard tests fall back to them for the database server fields
DB_PORT: ${{ matrix.port || 3306 }}
DB_USERNAME: ${{ matrix.username || 'root' }}
DB_PASSWORD: password
# A file on SQLite, like the installations (each parallel process creates its own `<file>_test_<N>`): an in-memory
# database is lost when `db:wipe` disconnects from it (see `SharedDatabaseSafetyTest`)
DB_DATABASE: ${{ matrix.database == 'sqlite' && format('{0}/database/testing.sqlite', github.workspace) || 'testing' }}
DB_PREFIX: ${{ matrix.db-prefix }}

steps:
- name: Checkout code
uses: actions/checkout@v7
with:
persist-credentials: false

# Tests run inside the Sail container (which has its own coverage
# drivers): the runner's PHP only installs the dependencies.
- name: Setup PHP and Composer
uses: shivammathur/setup-php@v2
with:
# The version of the platform of composer.json, and of the Sail image
php-version: "8.2"
extensions: ${{ env.php_extensions }}
coverage: none
coverage: ${{ matrix.coverage && 'pcov' || 'none' }}
# Only the code measured by phpunit.xml (`<source>`), not `vendor/`
ini-values: ${{ matrix.coverage && 'pcov.directory=app' || '' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
fail-fast: true
Expand All @@ -69,108 +116,28 @@ jobs:
run: composer install --no-ansi --no-interaction --no-progress --no-scripts --optimize-autoloader

- name: Prepare Laravel environment
env:
DB_PREFIX: ${{ matrix.db-prefix }}
run: |
set -e

cp .env.example .env

if [[ -n "$DB_PREFIX" ]]; then
sed -i "s/^DB_PREFIX=.*/DB_PREFIX=\"${DB_PREFIX}\"/" .env
fi

# Print environment variables for debugging
echo "Environment variables:"
sed 's/^/ | /' < .env

php artisan key:generate --force

# The assets are built on the runner, where the npm cache is restored by
# `actions/setup-node`, and reach the container through the project
# directory mount. They must be built after the Composer dependencies
# are installed, since the Filament themes import CSS from `vendor`.
if [[ "$DB_CONNECTION" == "sqlite" ]]; then
touch "$DB_DATABASE"
fi

# The assets must be built after the Composer dependencies are installed, since the Filament themes import CSS
# from `vendor`
- name: Install NodeJS dependencies
run: npm ci

- name: Build front-end assets
run: npm run build

# The cache scope changes every `sail_image_max_age_days` days, so the
# image is periodically rebuilt from scratch. Unused cache entries of the
# previous scopes are deleted by GitHub after 7 days.
- name: Prepare Sail image build
id: sail-image
run: |
# Same group ID Sail would pass to the image build, so the image
# built below matches the one `sail up --build` would produce.
echo "WWWGROUP=$(id -g)" >> "$GITHUB_ENV"

period_seconds=$(( sail_image_max_age_days * 86400 ))
period_start=$(( $(date -u +%s) / period_seconds * period_seconds ))
echo "cache-scope=sail-$(date -u -d "@${period_start}" +%Y%m%d)" >> "$GITHUB_OUTPUT"

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4

# Build the Sail image with Bake, which reads its build definition from
# docker-compose.yml, storing its layers in the GitHub Actions cache:
# subsequent runs reuse them instead of building the image from scratch.
- name: Build Sail image
uses: docker/bake-action@v7
with:
source: .
files: docker-compose.yml
load: true
set: |
*.cache-from=type=gha,scope=${{ steps.sail-image.outputs.cache-scope }}
*.cache-to=type=gha,scope=${{ steps.sail-image.outputs.cache-scope }},mode=max,ignore-error=true

# On startup, the application container changes the ID of the `sail`
# user to the runner's user ID, so that it can write to the project
# directory mount: commands run through Sail before that fail with
# "permission denied" errors.
- name: Start application
run: |
./vendor/bin/sail up -d

for attempt in $(seq 1 30); do
if [[ "$(./vendor/bin/sail exec -T laravel.test id -u sail 2> /dev/null)" == "$(id -u)" ]]; then
echo "The application container is ready (attempt ${attempt})."
exit 0
fi

sleep 1
done

echo "::error::The application container was not ready within 30 seconds."
./vendor/bin/sail logs laravel.test
exit 1

# The MySQL container is reported as started (and its socket-based
# healthcheck can already pass) while its init scripts still run on a
# temporary server without networking: wait until it accepts TCP
# connections, like the tests do, to avoid "Connection refused" errors.
- name: Wait for MySQL
run: |
for attempt in $(seq 1 60); do
if ./vendor/bin/sail exec -T mysql sh -c 'mysqladmin ping --silent --host=127.0.0.1 -uroot -p"$MYSQL_ROOT_PASSWORD"' > /dev/null 2>&1; then
echo "MySQL is accepting TCP connections (attempt ${attempt})."
exit 0
fi

sleep 2
done

echo "::error::MySQL did not accept TCP connections within 120 seconds."
./vendor/bin/sail logs mysql
exit 1

- name: Run tests${{ matrix.db-prefix == '' && ' (with coverage)' || '' }}
run: |
./vendor/bin/sail test --parallel --drop-databases ${{ matrix.db-prefix == '' && '--coverage' || '' }}

- name: Stop application
continue-on-error: true
run: |
./vendor/bin/sail down -v --remove-orphans
- name: Run tests${{ matrix.coverage && ' (with coverage)' || '' }}
run: php artisan test --parallel ${{ matrix.database != 'sqlite' && '--drop-databases' || '' }} ${{ matrix.coverage && '--coverage' || '' }}
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
unless asked to include them, and the AI assistants ask the user before assigning it again
([#114](https://github.com/OpenCHelper/OpenCongregationHelper/pull/114)).
- **End-to-end tests**: browser tests of every feature of the app and of the main workflows (e.g. a zone, then a
territory in it, assigned and returned), run on every pull request on SQLite and MySQL; a failed test keeps a video
and a trace to replay it ([#116](https://github.com/OpenCHelper/OpenCongregationHelper/pull/116)).
territory in it, assigned and returned), run on every pull request on MySQL (the setup wizard on SQLite and MySQL),
while the unit and feature tests run on MySQL and SQLite; a failed test keeps a video and a trace to replay it
([#116](https://github.com/OpenCHelper/OpenCongregationHelper/pull/116),
[#117](https://github.com/OpenCHelper/OpenCongregationHelper/pull/117)).
- The busy timeout and the journal mode of SQLite can be set with `DB_BUSY_TIMEOUT` and `DB_JOURNAL_MODE`
([#116](https://github.com/OpenCHelper/OpenCongregationHelper/pull/116)).

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ development ones: `app`, installed and seeded with `E2eSeeder` (a user for each
They use SQLite by default: set `E2E_DB_CONNECTION` (`mysql`, `mariadb` or `pgsql`) and `E2E_DB_HOST`, `E2E_DB_PORT`,
`E2E_DB_USERNAME`, `E2E_DB_PASSWORD` to run them on another database (the databases `e2e_app` and `e2e_setup` are
created). Outside of CI, servers already running on the ports are reused: stop them to start from fresh data. The
`E2E tests` workflow (`run-e2e-tests.yml`) runs them on SQLite and MySQL.
`E2E tests` workflow (`run-e2e-tests.yml`) runs them on MySQL, and the setup wizard on SQLite and MySQL: the support of
the databases is tested by the `Pest tests` workflow (`run-pest-tests.yml`), on MySQL and SQLite, each with and
without a table prefix.

### Run workflows locally

Expand Down
2 changes: 2 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export default defineConfig({
video: process.env.CI ? 'on-first-retry' : 'off',
screenshot: 'only-on-failure',
},
// The `E2E all` run of `.github/workflows/run-e2e-tests.yml` lists every project but `setup-wizard` (which runs on
// each database): add a new project there too
projects: [
{
name: 'auth-setup',
Expand Down
13 changes: 11 additions & 2 deletions tests/Feature/RootUserTransferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,17 @@
});

test('other errors reading the root user setting are raised', function () {
config(['settings.repositories.database.table' => 'users']);
app()->forgetScopedInstances();

expect(fn () => User::rootUserId())->toThrow(QueryException::class);
// The settings table exists, but reading it fails (e.g. a lost connection), the same way on every database
$failed = false;
DB::connection()->beforeExecuting(function (string $query) use (&$failed): void {
if (! $failed && str_contains($query, 'settings')) {
$failed = true;

throw new QueryException(DB::connection()->getName(), $query, [], new PDOException('Connection lost'));
}
});

expect(fn () => User::rootUserId())->toThrow(QueryException::class, 'Connection lost');
});
Loading