This commit is contained in:
@ -111,4 +111,59 @@ final class ClientsLifecycleTest extends QdbTestCase
|
||||
$codes = array_column($list['data']['clients'], 'clientCode');
|
||||
$this->assertNotContains($code, $codes);
|
||||
}
|
||||
|
||||
public function testAdminCreatesClientRange(): void
|
||||
{
|
||||
$token = $this->api()->loginWebAndGetToken(
|
||||
DatabaseSeeder::ADMIN_USERNAME,
|
||||
DatabaseSeeder::PASSWORD
|
||||
)['token'];
|
||||
$prefix = 'RANGE-' . bin2hex(random_bytes(2)) . '-';
|
||||
|
||||
$res = $this->api()->withToken($token, 'POST', 'clients', [
|
||||
'bulk' => true,
|
||||
'fromCode' => $prefix . '001',
|
||||
'toCode' => $prefix . '003',
|
||||
'coachID' => $this->fixture()->coachId,
|
||||
]);
|
||||
$this->assertApiOk($res);
|
||||
$this->assertSame(3, $res['data']['createdCount']);
|
||||
$this->assertSame(
|
||||
[$prefix . '001', $prefix . '002', $prefix . '003'],
|
||||
$res['data']['created'],
|
||||
);
|
||||
|
||||
$list = $this->api()->withToken($token, 'GET', 'clients');
|
||||
$codes = array_column($list['data']['clients'], 'clientCode');
|
||||
foreach ([$prefix . '001', $prefix . '002', $prefix . '003'] as $code) {
|
||||
$this->assertContains($code, $codes);
|
||||
}
|
||||
}
|
||||
|
||||
public function testBulkCreateSkipsDuplicates(): void
|
||||
{
|
||||
$token = $this->api()->loginWebAndGetToken(
|
||||
DatabaseSeeder::ADMIN_USERNAME,
|
||||
DatabaseSeeder::PASSWORD
|
||||
)['token'];
|
||||
$prefix = 'DUP-' . bin2hex(random_bytes(2)) . '-';
|
||||
$existing = $prefix . '002';
|
||||
|
||||
$single = $this->api()->withToken($token, 'POST', 'clients', [
|
||||
'clientCode' => $existing,
|
||||
'coachID' => $this->fixture()->coachId,
|
||||
]);
|
||||
$this->assertApiOk($single);
|
||||
|
||||
$res = $this->api()->withToken($token, 'POST', 'clients', [
|
||||
'bulk' => true,
|
||||
'fromCode' => $prefix . '001',
|
||||
'toCode' => $prefix . '003',
|
||||
'coachID' => $this->fixture()->coachId,
|
||||
]);
|
||||
$this->assertApiOk($res);
|
||||
$this->assertSame(2, $res['data']['createdCount']);
|
||||
$this->assertSame(1, $res['data']['skippedCount']);
|
||||
$this->assertSame($existing, $res['data']['skipped'][0]['clientCode']);
|
||||
}
|
||||
}
|
||||
|
||||
67
tests/Unit/ClientCodeRangeTest.php
Normal file
67
tests/Unit/ClientCodeRangeTest.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ClientCodeRangeTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
require_once dirname(__DIR__, 2) . '/lib/client_code_range.php';
|
||||
}
|
||||
|
||||
public function testExpandsZeroPaddedPrefixRange(): void
|
||||
{
|
||||
$codes = qdb_expand_client_code_range('C001', 'C005');
|
||||
$this->assertSame(['C001', 'C002', 'C003', 'C004', 'C005'], $codes);
|
||||
}
|
||||
|
||||
public function testExpandsSuffixRange(): void
|
||||
{
|
||||
$codes = qdb_expand_client_code_range('A01Demo', 'A03Demo');
|
||||
$this->assertSame(['A01Demo', 'A02Demo', 'A03Demo'], $codes);
|
||||
}
|
||||
|
||||
public function testExpandsDemoSuffixWithTwoDigitPadding(): void
|
||||
{
|
||||
$codes = qdb_expand_client_code_range('C01Demo', 'C05Demo');
|
||||
$this->assertSame(['C01Demo', 'C02Demo', 'C03Demo', 'C04Demo', 'C05Demo'], $codes);
|
||||
$this->assertSame(
|
||||
array_map(static fn (int $n): string => 'C' . str_pad((string) $n, 2, '0', STR_PAD_LEFT) . 'Demo', range(1, 35)),
|
||||
qdb_expand_client_code_range('C01Demo', 'C35Demo'),
|
||||
);
|
||||
}
|
||||
|
||||
public function testUsesLastNumericSegmentWhenMultiplePresent(): void
|
||||
{
|
||||
$codes = qdb_expand_client_code_range('RANGE-ab12-001', 'RANGE-ab12-003');
|
||||
$this->assertSame(['RANGE-ab12-001', 'RANGE-ab12-002', 'RANGE-ab12-003'], $codes);
|
||||
}
|
||||
|
||||
public function testExpandsSingleCodeRange(): void
|
||||
{
|
||||
$codes = qdb_expand_client_code_range('X9Y', 'X9Y');
|
||||
$this->assertSame(['X9Y'], $codes);
|
||||
}
|
||||
|
||||
public function testRejectsMismatchedPrefix(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
qdb_expand_client_code_range('C001', 'D001');
|
||||
}
|
||||
|
||||
public function testRejectsDescendingRange(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
qdb_expand_client_code_range('C050', 'C001');
|
||||
}
|
||||
|
||||
public function testRejectsRangeWithoutDigits(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
qdb_expand_client_code_range('CLIENT', 'CLIENT');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user