repository = new InMemorySmartLinkRepository(); $this->generator = new ShortCodeGenerator($this->repository); }); it('generates unique short code', function () { $shortCode = $this->generator->generateUnique(); expect($shortCode)->toBeInstanceOf(ShortCode::class); expect($shortCode->toString())->toHaveLength(6); }); it('generates alphanumeric codes', function () { $shortCode = $this->generator->generateUnique(); expect($shortCode->toString())->toMatch('/^[a-zA-Z0-9]{6}$/'); }); it('generates different codes on multiple calls', function () { $codes = []; for ($i = 0; $i < 5; $i++) { $codes[] = $this->generator->generateUnique()->toString(); } $uniqueCodes = array_unique($codes); expect(count($uniqueCodes))->toBeGreaterThan(3); }); it('generates code with custom length', function () { $shortCode = $this->generator->generateUnique(length: 8); expect($shortCode->toString())->toHaveLength(8); }); it('returns different code when collision detected', function () { // Generate a code and add it to repository $firstCode = $this->generator->generateUnique(); // Manually add the code to repository to simulate it exists $link = \App\Domain\SmartLink\Entities\SmartLink::create( clock: new \App\Framework\DateTime\SystemClock(), shortCode: $firstCode, type: \App\Domain\SmartLink\Enums\LinkType::RELEASE, title: \App\Domain\SmartLink\ValueObjects\LinkTitle::fromString('Test') ); $this->repository->save($link); // Generate another code - should be different $secondCode = $this->generator->generateUnique(); expect($this->repository->existsShortCode($firstCode))->toBeTrue(); expect($secondCode)->toBeInstanceOf(ShortCode::class); }); });