expectException(ShortCodeCannotBeRegeneratedException::class); $this->expectExceptionMessage($expectedMessage); $shortUrl->regenerateShortCode(); } public function provideInvalidShortUrls(): iterable { yield 'with custom slug' => [ ShortUrl::fromMeta(ShortUrlMeta::fromRawData(['customSlug' => 'custom-slug', 'longUrl' => ''])), 'The short code cannot be regenerated on ShortUrls where a custom slug was provided.', ]; yield 'already persisted' => [ ShortUrl::createEmpty()->setId('1'), 'The short code can be regenerated only on new ShortUrls which have not been persisted yet.', ]; } /** * @test * @dataProvider provideValidShortUrls */ public function regenerateShortCodeProperlyChangesTheValueOnValidShortUrls(ShortUrl $shortUrl): void { $firstShortCode = $shortUrl->getShortCode(); $shortUrl->regenerateShortCode(); $secondShortCode = $shortUrl->getShortCode(); self::assertNotEquals($firstShortCode, $secondShortCode); } public function provideValidShortUrls(): iterable { yield 'no custom slug' => [ShortUrl::createEmpty()]; yield 'imported with custom slug' => [ ShortUrl::fromImport(new ImportedShlinkUrl('', '', [], Chronos::now(), null, 'custom-slug'), true), ]; } /** * @test * @dataProvider provideLengths */ public function shortCodesHaveExpectedLength(?int $length, int $expectedLength): void { $shortUrl = ShortUrl::fromMeta(ShortUrlMeta::fromRawData( [ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $length, 'longUrl' => ''], )); self::assertEquals($expectedLength, strlen($shortUrl->getShortCode())); } public function provideLengths(): iterable { yield [null, DEFAULT_SHORT_CODES_LENGTH]; yield from map(range(4, 10), fn (int $value) => [$value, $value]); } }