Files
api/services/nginx/app/tests/Unit/Auth/CreateTokenTest.php
T

60 lines
1.6 KiB
PHP

<?php
namespace objects {
class users_o
{
public int $id;
public static array $existing = [];
public function getUserByCustomerNumber(int $customer_number): self
{
if (in_array($customer_number, self::$existing, true)) {
$this->id = 123;
}
return $this;
}
public function exists(): bool
{
return isset($this->id) && $this->id > 0;
}
}
class tokens_o
{
public static array $created = [];
public function create(int $user_id, string $token, string $type = 'AUTH_TOKEN'): void
{
self::$created[] = compact('user_id', 'token', 'type');
}
}
}
namespace {
app_require('interfaces/authentication_i.php');
app_require('classes/authentication.php');
}
namespace {
it('creates a 64-char auth token for an existing user', function (): void {
\objects\users_o::$existing = [111111];
\objects\tokens_o::$created = [];
$token = (new \classes\authentication())->create_token(111111);
expect($token)->toBeString()->toMatch('/^[a-f0-9]{64}$/');
expect(\objects\tokens_o::$created)->toHaveCount(1);
expect(\objects\tokens_o::$created[0]['user_id'])->toBe(123);
expect(\objects\tokens_o::$created[0]['type'])->toBe('AUTH_TOKEN');
});
it('throws a clear exception when customer user is missing', function (): void {
\objects\users_o::$existing = [];
(new \classes\authentication())->create_token(222222);
})->throws(\Exception::class, 'User not found for customer number: 222222');
}