Skip to content

Testing

Daniel Carbone edited this page Apr 2, 2026 · 2 revisions

Testing

php-fhir has two layers of tests:

  1. Generator tests — unit tests for the generator itself (the src/ code).
  2. Generated code tests — PHPUnit tests produced by the generator alongside the FHIR type classes.

Generator Tests

Located in tests/ and run with the PHPUnit configs under phpunit/.

# Run the generator's own unit tests (Config, etc.)
vendor/bin/phpunit -c phpunit/Builder.xml

Generated Code Tests

During generation the builder writes a full suite of PHPUnit tests into the configured testsPath. These tests cover:

  • Core constants and encoding/client configuration classes.
  • Per-version constants, type maps, and individual type serialization round-trips.

How the Bootstrap Works

The test bootstrap (phpunit/bootstrap.php) performs three steps:

  1. Requires the Composer autoloader.
  2. Generates code for the requested test target (unless PHPFHIR_TEST_SKIP_GENERATE=true).
  3. Requires the generated Autoloader.php and TestAutoloader.php.

The test target is selected via the PHPFHIR_TEST_TARGET environment variable.

PHPUnit Configuration Files

File Target Description
phpunit/Core.xml Core Only generates & tests shared core classes (no FHIR versions).
phpunit/Versions.xml Versions Generates all configured versions, tests version-level entities.
phpunit/DSTU1.xml DSTU1 Generates & tests DSTU1 only.
phpunit/DSTU2.xml DSTU2 Generates & tests DSTU2 only.
phpunit/STU3.xml STU3 Generates & tests STU3 only.
phpunit/R4.xml R4 Generates & tests R4 only.
phpunit/R4B.xml R4B Generates & tests R4B only.
phpunit/R5.xml R5 Generates & tests R5 only.
phpunit/Builder.xml Tests for the generator code itself.

Running a Specific Version's Tests

# Generate + test R4:
PHPFHIR_TEST_TARGET=R4 vendor/bin/phpunit -c phpunit/R4.xml

# Skip regeneration if output already exists:
PHPFHIR_TEST_SKIP_GENERATE=true PHPFHIR_TEST_TARGET=R4 vendor/bin/phpunit -c phpunit/R4.xml

Running All Tests

# Core (shared) tests
PHPFHIR_TEST_TARGET=Core vendor/bin/phpunit -c phpunit/Core.xml

# All versions
PHPFHIR_TEST_TARGET=Versions vendor/bin/phpunit -c phpunit/Versions.xml

Integration / Server Tests

Resource types include serialization tests that can optionally run against a live FHIR server. The test server companion project is: dcarbone/php-fhir-test

Set the following environment variable to enable server tests:

export PHPFHIR_TEST_SERVER_ADDR=http://localhost:8080/fhir

When this variable is unset, server-dependent assertions are skipped.


Test Resource Downloads

Some tests download real FHIR example bundles. The download directory can be controlled via:

export PHPFHIR_TEST_RESOURCE_DOWNLOAD_DIR=/path/to/cache

Next: Contributing →

Clone this wiki locally