Testing approaches and best practices for OpenEyes development
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.
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.
# Run feature testsoe-unit-tests --group=feature# Run profile testsoe-unit-tests --group=profile# Run specific test filephpunit protected/tests/feature/ProfileTest.php
use OE\factories\ModelFactory;// Create and save a patient$patient = ModelFactory::for(Patient::class)->create();$this->assertNotNull($patient->dob);// Create with specific attributes$patient = ModelFactory::for(Patient::class)->create(['dob' => '2005-03-05']);$this->assertEquals(10, $patient->ageOn('2015-03-05'));// Make without saving$patient = ModelFactory::for(Patient::class)->make();$this->assertNull($patient->id);
Models with the HasFactory trait support shorthand syntax:
// Using the traituse OE\factories\models\traits\HasFactory;class Patient extends BaseActiveRecordVersioned{ use HasFactory;}// Shorthand usage$patient = Patient::factory()->create();
class PatientFactory extends ModelFactory{ public function male() { return $this->state(function () { return [ 'gender' => 'M', 'contact_id' => ModelFactory::factoryFor(Contact::class)->male() ]; }); } public function female() { return $this->state([ 'gender' => 'F', 'contact_id' => ModelFactory::factoryFor(Contact::class)->female() ]); }}// Usage$patient = ModelFactory::for(Patient::class)->male()->create();// States can be chained (later states override earlier ones)$patient = ModelFactory::for(Patient::class)->male()->female()->create();// This patient will be female
# Open Cypress UInpx cypress open# Run tests headlesslynpx cypress run# Run specific test filenpx cypress run --spec "cypress/e2e/admin/clinical-pathway-presets.cy.js"
// Create models using factoriescy.createModels("Patient").as("patient");// Logincy.login();// Select by data-test attributecy.getBySel("button-id").click();
# Run with verbose outputoe-unit-tests --group=sample-data --verbose# Stop on first failureoe-unit-tests --group=sample-data --stop-on-failure# Run specific test methodoe-unit-tests --filter=test_user_can_create_event
// Use cy.debug() to pausecy.getBySel("button").debug().click();// Use cy.pause() for interactive debuggingcy.pause();// Screenshot on failure (automatic)