Files
JobcardSystem/tests/ClientRecordTest.php

81 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/../app/Domain/Client/ClientRecord.php';
use App\Domain\Client\ClientRecord;
function client_record_assert_same(mixed $expected, mixed $actual, string $message): void
{
if ($expected !== $actual) {
throw new RuntimeException($message . "\nExpected: " . var_export($expected, true) . "\nActual: " . var_export($actual, true));
}
}
$service = new ClientRecord();
$normalized = $service->normalize([
'name' => ' Acme IT ',
'registration_number' => ' REG-42 ',
'status' => ' ACTIVE ',
'support_email' => ' SUPPORT@EXAMPLE.TEST ',
'support_phone' => ' +27 11 555 0100 ',
'preferred_contact_method' => ' email ',
'physical_address' => ' 1 Main Street ',
'postal_address' => '',
'general_notes' => ' Call first ',
]);
client_record_assert_same([
'name' => 'Acme IT',
'registration_number' => 'REG-42',
'status' => 'active',
'support_email' => 'support@example.test',
'support_phone' => '+27 11 555 0100',
'preferred_contact_method' => 'email',
'physical_address' => '1 Main Street',
'postal_address' => null,
'general_notes' => 'Call first',
], $normalized, 'Client records should normalize accepted fields deterministically.');
$invalid = $service->validate([
'name' => 'Client',
'support_email' => 'not-an-email',
'support_phone' => 'abc',
'registration_number' => str_repeat('R', 121),
]);
if ($invalid['valid'] !== false || !isset($invalid['errors']['support_email'])
|| !isset($invalid['errors']['support_phone']) || !isset($invalid['errors']['registration_number'])) {
throw new RuntimeException('Expected optional contact and registration fields to be validated.');
}
$valid = $service->validate(['name' => ' Client ']);
client_record_assert_same([], $valid['errors'], 'A client may omit optional contact and registration fields.');
client_record_assert_same(true, $valid['valid'], 'Valid client records should report valid=true.');
client_record_assert_same(null, $valid['support_email'], 'Missing email should normalize to null.');
client_record_assert_same(null, $valid['support_phone'], 'Missing phone should normalize to null.');
client_record_assert_same(null, $valid['registration_number'], 'Missing registration should normalize to null.');
$display = $service->display([
'id' => 7,
'name' => 'Acme IT',
'registration_number' => 'REG-42',
'status' => 'active',
'support_email' => 'support@example.test',
'support_phone' => '+27 11 555 0100',
'password' => 'secret',
'password_hash' => 'hash',
'credentials' => 'token',
'created_by' => 99,
'unknown_field' => 'omit',
]);
client_record_assert_same([
'id' => 7,
'name' => 'Acme IT',
'registration_number' => 'REG-42',
'status' => 'active',
'support_email' => 'support@example.test',
'support_phone' => '+27 11 555 0100',
], $display, 'Display projection must be explicitly allow-listed.');
printf("Client record tests: 4 passed\n");