Ensure auto-generated name API keys do not throw duplicated name

This commit is contained in:
Alejandro Celaya
2024-11-09 12:07:07 +01:00
parent d228b88e51
commit 3c6f12aec6
5 changed files with 53 additions and 12 deletions

View File

@@ -78,7 +78,23 @@ class ApiKeyServiceTest extends TestCase
}
#[Test]
public function exceptionIsThrownWhileCreatingIfNameIsInUse(): void
public function autoGeneratedNameIsRegeneratedIfAlreadyExists(): void
{
$callCount = 0;
$this->repo->expects($this->exactly(3))->method('nameExists')->with(
$this->isType('string'),
)->willReturnCallback(function () use (&$callCount): bool {
$callCount++;
return $callCount < 3;
});
$this->em->expects($this->once())->method('flush');
$this->em->expects($this->once())->method('persist')->with($this->isInstanceOf(ApiKey::class));
$this->service->create(ApiKeyMeta::create());
}
#[Test]
public function exceptionIsThrownWhileCreatingIfExplicitlyProvidedNameIsInUse(): void
{
$this->repo->expects($this->once())->method('nameExists')->with('the_name')->willReturn(true);
$this->em->expects($this->never())->method('flush');