Skip to main content
OpenEyes uses a comprehensive testing approach combining PHPUnit for PHP tests and Cypress for end-to-end testing. This guide covers how to write and run tests.

Overview

OpenEyes tests fall into two main categories:
  1. Sample Data Tests - Use the sample database (recommended for new tests)
  2. Fixture-based Tests - Use fixtures (legacy approach)
All new tests should be written to work with the sample database, as this allows developers to run tests locally without interfering with their development environment.

PHP Testing

PHPUnit Configuration

OpenEyes uses PHPUnit 9.6.5 for PHP testing:
composer.json

Running Tests

Run tests tagged with the sample-data group:
Run specific test groups:

Writing PHPUnit Tests

Test Structure

Tests extend OEDbTestCase and use traits for functionality:
protected/tests/feature/ProfileTest.php

Required Annotations

All new tests should include the @group sample-data annotation:

Model Factories

Model factories provide a simple way to generate test data.

Using Factories

Create test instances with default values:

Factory Shorthand

Models with the HasFactory trait support shorthand syntax:

Creating Factories

Define a factory for your model:
protected/factories/models/AddressFactory.php

Factory Dependencies

Define dependent relations as factory calls:

Factory States

Define multiple states for different scenarios:

Event Factories

Event factories create complete events with elements:
Event factories depend on element model factories being implemented.

Test Traits

WithTransactions

Wraps each test in a database transaction:

MocksSession

Provides session mocking utilities:

MakesApplicationRequests

Simplifies making HTTP requests:

Cypress E2E Testing

Configuration

Cypress is configured for end-to-end testing:
cypress.config.js

Running Cypress Tests

Writing Cypress Tests

Cypress tests use a BDD-style syntax:
cypress/e2e/admin/clinical-pathway-presets.cy.js

Custom Cypress Commands

OpenEyes provides custom commands:

Code Quality Tools

PHP CodeSniffer

Check code style compliance:
OpenEyes follows PSR-12 with exceptions:
phpcs.xml

PHPStan

Run static analysis:
Configuration:
phpstan.neon

PHP CS Fixer

Automatically fix code style:

Test Organization

Directory Structure

Test Naming

Follow these conventions:
  • Test files end with Test.php
  • Test methods start with test or use /** @test */
  • Use descriptive names: test_user_can_create_event()

Best Practices

  1. Tag all tests with @group sample-data and relevant groups
  2. Use transactions with WithTransactions trait for database tests
  3. Create factories for all models to support testing
  4. Test isolation - Each test should be independent
  5. Use factories instead of fixtures for new tests
  6. Mock external dependencies to keep tests fast
  7. Test edge cases and error conditions
  8. Keep tests simple - One assertion per test when possible

Continuous Integration

Tests run automatically in CI pipelines. Ensure:
  • All tests pass before committing
  • No skipped tests without good reason
  • Code style checks pass
  • PHPStan analysis passes

Debugging Tests

PHPUnit Debugging

Cypress Debugging

Next Steps

Contributing

Learn how to contribute your code

Module Development

Create custom modules