Overview
OpenEyes tests fall into two main categories:- Sample Data Tests - Use the sample database (recommended for new tests)
- 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:Writing PHPUnit Tests
Test Structure
Tests extendOEDbTestCase 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 theHasFactory 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: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:phpcs.xml
PHPStan
Run static analysis: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
testor use/** @test */ - Use descriptive names:
test_user_can_create_event()
Best Practices
- Tag all tests with
@group sample-dataand relevant groups - Use transactions with
WithTransactionstrait for database tests - Create factories for all models to support testing
- Test isolation - Each test should be independent
- Use factories instead of fixtures for new tests
- Mock external dependencies to keep tests fast
- Test edge cases and error conditions
- 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