Repository navigation
Testing Guide
| Suite | Tool | Location | Count |
|---|---|---|---|
| PHP unit | PHPUnit 10 | tests/Unit/ |
31 |
| PHP integration | PHPUnit 10 + SQLite | tests/Integration/ |
19 |
| JavaScript | bun + happy-dom | tests/js/ |
30 |
Integration tests use an in-memory SQLite database. No MySQL instance is required to run the test suite.
bun run testThis runs vendor/bin/phpunit then bun test tests/js/ sequentially via the test script in package.json.
vendor/bin/phpunit --testdoxBy suite:
vendor/bin/phpunit --testsuite Unit
vendor/bin/phpunit --testsuite IntegrationBy file:
vendor/bin/phpunit tests/Unit/Core/ValidatorTest.php --testdoxbun test tests/js/By file:
bun test tests/js/components/toast.test.jsvendor/bin/phpstan analyse --no-progressLevel 5. Config in phpstan.neon.
tests/bootstrap.php provides:
-
require_once __DIR__ . '/../vendor/autoload.php'— loads the autoloader -
testDbPdo(): PDO— returns a fresh SQLite in-memory PDO with the full application schema (patients, doctors, prescriptions, prescription_items, waiting_queue, audit_log tables)
MySQL-specific constructs not supported by SQLite are handled as follows:
| Construct | Strategy |
|---|---|
SELECT ... FOR UPDATE |
Mock PDO (no real DB) |
CURDATE(), NOW()
|
Skip the method or mock PDO |
AUTO_INCREMENT |
Changed to AUTOINCREMENT in test DDL |
ENUM(...) |
Changed to TEXT in test DDL |
Use mock PDO objects ($this->createMock(\PDO::class)) for anything requiring MySQL-specific behaviour. Use ReflectionMethod::setAccessible(true) to test private methods.
Files:
| File | Class under test | Approach |
|---|---|---|
Core/ValidatorTest.php |
Validator |
Pure PHP, no DB |
Services/QueueServiceTest.php |
QueueService |
Mock PDO (uses FOR UPDATE) |
Controllers/SettingsControllerTest.php |
SettingsController::validateLicenseKey |
Reflection + no DB |
Use testDbPdo() in setUpBeforeClass(). Reset table state in setUp() with DELETE FROM in FK-safe order.
Files:
| File | Class under test | Approach |
|---|---|---|
Services/PrescriptionServiceTest.php |
PrescriptionService |
SQLite happy path + mock rollback |
Repositories/PatientRepositoryTest.php |
PatientRepository |
SQLite |
Repositories/PrescriptionRepositoryTest.php |
PrescriptionRepository |
SQLite |
bunfig.toml preloads tests/js/setup.js before every test file. setup.js registers happy-dom globals (window, document) and sets window.__crxToken to a test value.
import { describe, test, expect, beforeEach } from 'bun:test';
import { myFunction } from '../../assets/js/modules/my-module.js';
describe('myFunction', () => {
test('does the thing', () => {
expect(myFunction('input')).toBe('expected output');
});
});Put the file in tests/js/ (or tests/js/components/ for component tests). bun test tests/js/ discovers it automatically.
happy-dom provides document, window, and Event. Set up DOM nodes in beforeEach, clean up in afterEach:
beforeEach(() => {
document.body.innerHTML = '<div id="my-el"></div>';
});
afterEach(() => {
document.body.innerHTML = '';
});<?php
namespace CureRx\Tests\Unit\Services;
use CureRx\Services\MyService;
use PHPUnit\Framework\TestCase;
class MyServiceTest extends TestCase
{
public function testDoesTheThing(): void
{
$mockStmt = $this->createMock(\PDOStatement::class);
$mockPdo = $this->createMock(\PDO::class);
$mockPdo->method('prepare')->willReturn($mockStmt);
$mockStmt->method('execute')->willReturn(true);
$result = (new MyService($mockPdo))->doTheThing('arg');
$this->assertSame('expected', $result);
}
}<?php
namespace CureRx\Tests\Integration\Repositories;
use CureRx\Repositories\MyRepository;
use PHPUnit\Framework\TestCase;
class MyRepositoryTest extends TestCase
{
private static \PDO $pdo;
public static function setUpBeforeClass(): void
{
self::$pdo = testDbPdo();
}
protected function setUp(): void
{
self::$pdo->exec('DELETE FROM my_table');
}
public function testCreateReturnsId(): void
{
$repo = new MyRepository(self::$pdo);
$id = $repo->create(['name' => 'Test']);
$this->assertGreaterThan(0, $id);
}
}.github/workflows/lint.yml runs on every push:
- Setup PHP 8.1 with
pdo_sqliteextension composer installvendor/bin/phpstan analysevendor/bin/phpunit --colors=never- Setup Bun
bun installbun run test:js
All four steps must be green for a PR to be mergeable.