Files
clinicpro/tests/Policy/PolicyFieldCoverageTest.php
T
hamedandClaude Opus 5 f2600f9922 refactor(policy): build the registries and six engines the architecture asked for
The task 09 architecture specified FieldRegistry, OperatorRegistry, six engine
classes and a stored specificity. What shipped was a single PolicySchema
constant list, six operators, one resolver and a specificity recomputed on
every booking. Each shortcut was defensible on its own; together they left the
starred risk the task itself recorded — a field can be advertised in the form
and supplied by nobody, and the rule silently never matches.

OperatorRegistry now holds all eleven operators. The five that were missing are
real capability, not ceremony: greater_or_equal and less_or_equal make boundary
rules expressible without off-by-one, not_in is the natural way to write an
exclusion, between stops "18 to 65" needing two clauses, and days_since is the
documented operator for "more than N days since" — until now every caller
computed that by hand. between is inclusive at both ends because that is what
the Persian phrasing means and what the user will type.

FieldRegistry is now the single source: it builds the form schema and extracts
the value, so a field that exists in one and not the other is impossible. It
also declares which categories each field belongs to, which is what the closed
list per category used to do separately. Adding it immediately caught its own
first case — last_visit_at was advertised and supplied nowhere, so the guard
now populates it and days_since has something to read.

The six engines are thin on purpose. They give the call site a type — "the
pricing engine" rather than "the resolver with the string pricing" — and a
place for evaluateIsolated, which the sandbox needs to answer "what would this
one rule do". Conflict resolution and effect combination stay in
PolicyResolver: six copies of that would be six places to break.

specificity is a stored column now, computed on save with the documented
weights, and the migration backfills existing rows with the same formula. Left
at zero they would all have tied and the ordering would have changed overnight.

Field names stay as they are rather than moving to the document's dotted names
(patient.age). Stored condition_json rows point at the current names on live
clinic policies; renaming them is a data migration, and the mapping is not
one-to-one — implementation_notes.md says as much.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-01 15:56:50 +03:30

79 lines
2.8 KiB
PHP

<?php
namespace App\Tests\Policy;
use App\Policy\Entity\Policy;
use App\Policy\Service\FieldRegistry;
use App\Policy\Service\OperatorRegistry;
use PHPUnit\Framework\TestCase;
/**
* هر فیلدی که schema تبلیغ می‌کند، باید جایی در کد **واقعاً پر شود**.
*
* خطر مشخص است و بی‌صداست: قانونی که روی فیلدی شرط بگذارد که هیچ فراخوانی آن را
* نمی‌فرستد، هرگز مطابقت نمی‌کند و هیچ خطایی هم نمی‌دهد. اپراتور قانون را می‌سازد،
* فعالش می‌کند، و تا ابد فکر می‌کند دارد کار می‌کند.
*
* این تست عمداً ساختاری است نه رفتاری: پیمایشِ همهٔ مسیرهای واقعی برای هر فیلد،
* دستگاه تستی می‌خواست بزرگ‌تر از خودِ موتور.
*/
class PolicyFieldCoverageTest extends TestCase
{
public function testEveryAdvertisedFieldIsSuppliedSomewhereInTheCode(): void
{
$registry = new FieldRegistry(new OperatorRegistry());
$fields = [];
foreach (Policy::CATEGORIES as $category) {
foreach ($registry->forCategory($category) as $field) {
$fields[$field][] = $category;
}
}
$sources = $this->sourceFiles(dirname(__DIR__, 2) . '/src');
$missing = [];
foreach ($fields as $field => $categories) {
$found = false;
foreach ($sources as $file => $code) {
// خودِ schema فقط نام را اعلام می‌کند؛ پر کردنش جای دیگری است.
if (str_ends_with($file, 'PolicySchema.php') || str_ends_with($file, 'FieldRegistry.php')) {
continue;
}
if (str_contains($code, sprintf("'%s'", $field)) && str_contains($code, '=>')) {
$found = true;
break;
}
}
if (!$found) {
$missing[$field] = $categories;
}
}
self::assertSame(
[],
$missing,
'این فیلدها در schema هستند ولی هیچ‌جا در context پر نمی‌شوند: ' .
json_encode($missing, JSON_UNESCAPED_UNICODE),
);
}
/** @return array<string, string> مسیر => محتوا */
private function sourceFiles(string $root): array
{
$files = [];
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($root));
foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$files[$file->getPathname()] = (string) file_get_contents($file->getPathname());
}
}
return $files;
}
}