Skip to content

Fix test datasource configuration and add GitHub Actions CI - #215

Open
breckenedge wants to merge 3 commits into
Dallas-Makerspace:masterfrom
breckenedge:test-bootstrap-fix
Open

Fix test datasource configuration and add GitHub Actions CI#215
breckenedge wants to merge 3 commits into
Dallas-Makerspace:masterfrom
breckenedge:test-bootstrap-fix

Conversation

@breckenedge

@breckenedge breckenedge commented May 14, 2026

Copy link
Copy Markdown

Summary

Five things in this PR:

  1. Fix tests/bootstrap.php so the test runner can actually load fixtures. The previous bootstrap aliased test → default, but CakePHP's FixtureManager::_aliasConnections() unconditionally calls alias('test', 'default') (the inverse direction) at line 124, overriding the bootstrap alias. After fixtures initialise, both names resolve to a test config that was never set up, so any test that touches a Table fails with:

    Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource configuration "test" was not found.
    

    Replaced the alias with a real test Datasource registration that clones the default connection config and swaps the database name. Fixtures truncate tables, so they get a dedicated dms-calendar-test schema rather than touching dev data.

  2. Make the PHPUnit suite actually discoverable. Two small things were preventing the suite from running:

    • phpunit.xml.dist pointed at ./tests/TestCase, which is empty — every test file in this repo lives under src/. Repointed the testsuite at ./src with a suffix="Test.php" glob, which picks up all 19 test classes.
    • Five test classes referenced a typo'd fixture, app.honorarias (the actual fixture class is HonorariaFixture, singular). That caused fixture init to throw and aborted those classes' runs. Renamed them to app.honoraria in CommitteesControllerTest, ContactsControllerTest, EventsControllerTest, HonorariaControllerTest, and EventsTableTest.
  3. Add a GitHub Actions workflow (.github/workflows/test.yml) that spins up MariaDB 10.11 as a service container, installs PHP 7.4 + Composer deps, symlinks config/app.php to the default config (matching what the docker startup does), creates the test database, and runs the full PHPUnit suite on every push to master and on every PR.

  4. Refuse to run tests against a non-local DB host. Per review feedback: if a developer's DATABASE_URL is misconfigured to point at the prod RDS instance, the bootstrap would derive dms-calendar-test from it and FixtureManager would happily TRUNCATE every table it manages against the prod host. The bootstrap now exits 1 with a clear error if the resolved default host isn't on a small allowlist (localhost, 127.0.0.1, ::1, db — the docker-compose service name). CI uses DB_HOST=127.0.0.1, which is covered. If a future use case legitimately needs another host, the allowlist is one line in tests/bootstrap.php.

  5. Document the whole thing in docs/README.md (new ## Tests section): where tests live, the docker compose exec command to run them, how the test datasource is derived from default, the production guardrail, and a pointer to the CI workflow.

Setup note for local devs

After this PR, running tests locally requires the test database to exist on the same MariaDB server (CI handles this automatically):

CREATE DATABASE `dms-calendar-test`;
GRANT ALL PRIVILEGES ON `dms-calendar-test`.* TO 'calendar'@'%';
FLUSH PRIVILEGES;

Fixtures auto-create their own tables from each fixture's $fields definition, so no schema import is required.

What CI runs

The full suite — 100 test methods across all 19 test classes. Today most of them are still master's markTestIncomplete skeletons, but each class's setUp() exercises fixture init against the new test datasource, so any future regression in bootstrap/fixture wiring would fail CI immediately. Branches that add real tests on top of this inherit CI without any further setup.

@breckenedge
breckenedge force-pushed the test-bootstrap-fix branch from dfaa4a9 to dec3778 Compare May 14, 2026 02:24
Register a dedicated 'test' datasource in tests/bootstrap.php that mirrors
the default connection but targets a separate '<db>-test' database. The
FixtureManager re-aliases 'test' to 'default' on every run, which broke
the previous alias-based setup; setting a real config survives that.

Point the phpunit testsuite at ./src (where the test files actually
live) so all 19 test classes are discovered, and fix five preexisting
references to a typo'd fixture name (app.honorarias -> app.honoraria)
that were failing fixture loading for those classes.

Add a GitHub Actions workflow that boots MariaDB 10.11 as a service,
installs PHP 7.4 + Composer deps, creates the test database, and runs
the full PHPUnit suite on every push to master and on pull requests.
@breckenedge
breckenedge force-pushed the test-bootstrap-fix branch from dec3778 to a86787d Compare May 14, 2026 02:31
@breckenedge

Copy link
Copy Markdown
Author

Note that the updated specs passed on my own fork here: https://github.com/breckenedge/dallas-makerspace-calendar/actions/runs/25838167795/job/75917606374?pr=1

@breckenedge
breckenedge marked this pull request as ready for review May 14, 2026 03:01

@mandarl mandarl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@breckenedge - thanks for doing this!

  • Looks good overall. Do you want to add a sections for Tests in ./docs/README.md?
  • Q: is there a risk that if a developer is pointing to Production in DATABASE_URL (I would never do this 😂but ...) then the test fixtures might delete data in prod. Ideally we would have multiple guardrails to prevent this but I am thinking of worst case scenarios.

@breckenedge

Copy link
Copy Markdown
Author

@breckenedge - thanks for doing this!

  • Looks good overall. Do you want to add a sections for Tests in ./docs/README.md?
  • Q: is there a risk that if a developer is pointing to Production in DATABASE_URL (I would never do this 😂but ...) then the test fixtures might delete data in prod. Ideally we would have multiple guardrails to prevent this but I am thinking of worst case scenarios.

Good call, I'll add a guardrail so that this can only run locally, and update docs/README.md describing the test suite.

Aaron Breckenridge and others added 2 commits May 18, 2026 14:55
Fixture setup TRUNCATEs every table it manages. If a developer's
DATABASE_URL is pointing at prod when they run phpunit, the bootstrap
would derive 'dms-calendar-test' from the prod host and FixtureManager
would wipe whatever 'dms-calendar-test' schema happens to exist there.

Allowlist: localhost, 127.0.0.1, ::1, db (the docker-compose service).
CI uses DB_HOST=127.0.0.1, which is covered.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Adds a Tests section to docs/README.md covering where tests live, how
the suite is run under Docker, how the test datasource is derived from
the default connection, the hostname allowlist that prevents pointing
fixtures at the prod RDS instance, and a pointer to the CI workflow.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@breckenedge

Copy link
Copy Markdown
Author

@mandarl this good to go as is?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants