You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
4.7 KiB
PHTML
82 lines
4.7 KiB
PHTML
|
2 months ago
|
<?php
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
|
||
|
|
use App\Services\ReferralService;
|
||
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
||
|
|
use Illuminate\Database\Schema\Blueprint;
|
||
|
|
|
||
|
|
$assert = static function ($condition, string $message): void {
|
||
|
|
if (!$condition) {
|
||
|
|
throw new RuntimeException($message);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// Keep this regression test self-contained: no Redis or production database
|
||
|
|
// is needed to exercise the code/lock invariants.
|
||
|
|
$capsule = new Capsule();
|
||
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
||
|
|
$capsule->setAsGlobal();
|
||
|
|
$capsule->bootEloquent();
|
||
|
|
$schema = $capsule->schema('default');
|
||
|
|
$schema->create('ss_invite_code', static function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->string('code', 128);
|
||
|
|
$table->integer('user_id');
|
||
|
|
});
|
||
|
|
$schema->create('referral_code_generation_locks', static function (Blueprint $table): void {
|
||
|
|
$table->unsignedTinyInteger('id')->primary();
|
||
|
|
});
|
||
|
|
Capsule::table('referral_code_generation_locks')->insert(['id' => 1]);
|
||
|
|
|
||
|
|
Capsule::table('ss_invite_code')->insert([
|
||
|
|
['id' => 1, 'code' => 'Ab3dE9x0', 'user_id' => 1],
|
||
|
|
['id' => 2, 'code' => 'aB3dE9x0', 'user_id' => 2],
|
||
|
|
['id' => 3, 'code' => 'legacy-long-code', 'user_id' => 3],
|
||
|
|
]);
|
||
|
|
|
||
|
|
$assert(ReferralService::normalizeCode('Ab3dE9x0') === 'Ab3dE9x0', 'Valid mixed-case code was rejected.');
|
||
|
|
$assert(ReferralService::normalizeCode(' Ab3dE9x0 ') === 'Ab3dE9x0', 'Whitespace was not trimmed.');
|
||
|
|
$assert(ReferralService::normalizeCode('Ab3dE9x') === '', 'Short code was accepted.');
|
||
|
|
$assert(ReferralService::normalizeCode('Ab3dE9x!') === '', 'Non-alphanumeric code was accepted.');
|
||
|
|
$assert(ReferralService::findInvitation('Ab3dE9x0')->user_id === 1, 'Exact mixed-case lookup chose the wrong inviter.');
|
||
|
|
$assert(ReferralService::findInvitation('aB3dE9x0')->user_id === 2, 'Case-sensitive lookup did not preserve the code owner.');
|
||
|
|
$assert(ReferralService::findInvitation('AB3DE9X0') === null, 'A differently cased code resolved unexpectedly.');
|
||
|
|
$assert(ReferralService::findInvitation('legacy-long-code') === null, 'Legacy long code was accepted by the new lookup.');
|
||
|
|
|
||
|
|
// Query-string links win over a stale cookie; `code` remains a compatibility
|
||
|
|
// alias, and a valid cookie is used only when neither query key is valid.
|
||
|
|
$assert(ReferralService::registrationCode(['aff' => 'Ab3dE9x0'], 'aB3dE9x0') === 'Ab3dE9x0', 'aff did not win over cookie.');
|
||
|
|
$assert(ReferralService::registrationCode(['aff' => 'invalid', 'code' => 'aB3dE9x0'], 'Ab3dE9x0') === 'aB3dE9x0', 'Legacy code query alias was not accepted.');
|
||
|
|
$assert(ReferralService::registrationCode([], 'aB3dE9x0') === 'aB3dE9x0', 'Valid cookie was not used as fallback.');
|
||
|
|
$assert(ReferralService::registrationCode(['aff' => 'invalid', 'code' => 'invalid'], 'invalid') === '', 'Invalid referral values were accepted.');
|
||
|
|
|
||
|
|
$first = ReferralService::codeForUser((object) ['id' => 4]);
|
||
|
|
$again = ReferralService::codeForUser((object) ['id' => 4]);
|
||
|
|
$other = ReferralService::codeForUser((object) ['id' => 5]);
|
||
|
|
$assert($first !== null && $again !== null && $other !== null, 'Code generation returned no model.');
|
||
|
|
$assert($first->code === $again->code, 'Repeated codeForUser calls generated duplicate rows.');
|
||
|
|
$assert($first->code !== $other->code, 'Different users received the same generated code.');
|
||
|
|
$assert(ReferralService::normalizeCode($first->code) === $first->code, 'Generated code does not match the eight-character contract.');
|
||
|
|
$assert(Capsule::table('ss_invite_code')->where('code', $first->code)->count() === 1, 'Generated code was not persisted once.');
|
||
|
|
$candidate = ReferralService::generateCode();
|
||
|
|
$assert(ReferralService::normalizeCode($candidate) === $candidate
|
||
|
|
&& Capsule::table('ss_invite_code')->where('code', $candidate)->count() === 0,
|
||
|
|
'The locked candidate generator returned an invalid or already-used code.');
|
||
|
|
|
||
|
|
Capsule::table('referral_code_generation_locks')->delete();
|
||
|
|
try {
|
||
|
|
ReferralService::codeForUser((object) ['id' => 6]);
|
||
|
|
throw new RuntimeException('Missing referral generation lock was not rejected.');
|
||
|
|
} catch (RuntimeException $exception) {
|
||
|
|
$assert(str_contains($exception->getMessage(), 'referral_affiliate_code.sql'), 'Missing lock error was not actionable.');
|
||
|
|
}
|
||
|
|
Capsule::table('referral_code_generation_locks')->insert(['id' => 1]);
|
||
|
|
|
||
|
|
$rotated = ReferralService::rotateForUser((object) ['id' => 4]);
|
||
|
|
$assert($rotated !== null && $rotated->code !== $first->code, 'Code rotation did not issue a replacement.');
|
||
|
|
$assert(Capsule::table('ss_invite_code')->where('user_id', 4)->count() === 1, 'Code rotation left multiple rows for one user.');
|
||
|
|
$assert(Capsule::table('ss_invite_code')->where('code', $first->code)->count() === 0, 'Rotated code remained active.');
|
||
|
|
|
||
|
|
echo "Referral service checks passed.\n";
|