Skip to content

Commit cc64dc8

Browse files
authored
Add new full_slug property (#89)
1 parent 296f40c commit cc64dc8

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

src/Domain/Value/Link.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public Uuid $uuid;
2727
public ?Id $parentId;
2828
public string $name;
29+
public ?string $fullSlug;
2930
public string $slug;
3031
public ?string $path;
3132
public string $realPath;
@@ -66,6 +67,15 @@ public function __construct(array $values)
6667
Assert::keyExists($values, 'name');
6768
$this->name = TrimmedNonEmptyString::fromString($values['name'], 'The value of the "name" key must be a non-empty, trimmed string. Got: %s')->toString();
6869

70+
// This field is only set when using the stories api with resolve_links flag.
71+
$fullSlug = null;
72+
73+
if (\array_key_exists('full_slug', $values) && null !== $values['full_slug']) {
74+
$fullSlug = TrimmedNonEmptyString::fromString($values['full_slug'], 'The value of the "full_slug" key must be a non-empty, trimmed string. Got: %s')->toString();
75+
}
76+
77+
$this->fullSlug = $fullSlug;
78+
6979
Assert::keyExists($values, 'slug');
7080
$this->slug = TrimmedNonEmptyString::fromString($values['slug'], 'The value of the "slug" key must be a non-empty, trimmed string. Got: %s')->toString();
7181

@@ -130,7 +140,7 @@ public function isStory(): bool
130140
public function getSlug(?string $lang = null): string
131141
{
132142
if (null === $lang) {
133-
return $this->slug;
143+
return $this->fullSlug ?? $this->slug;
134144
}
135145

136146
return $this->getAlternate($lang)->slug;

tests/Unit/Domain/Value/LinkTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,73 @@ public function getSlugWithLangAndAlternate(): void
437437
self::assertSame($slug, (new Link($values))->getSlug($lang));
438438
}
439439

440+
#[Test]
441+
public function fullSlug(): void
442+
{
443+
$faker = self::faker();
444+
445+
$values = $faker->linkResponse(['full_slug' => $fullSlug = $faker->slug()]);
446+
447+
self::assertSame($fullSlug, (new Link($values))->fullSlug);
448+
}
449+
450+
#[Test]
451+
public function fullSlugIsOptional(): void
452+
{
453+
$faker = self::faker();
454+
455+
$values = $faker->linkResponse();
456+
unset($values['full_slug']);
457+
458+
self::assertNull((new Link($values))->fullSlug);
459+
}
460+
461+
#[Test]
462+
public function fullSlugIsOptionalWithNull(): void
463+
{
464+
$faker = self::faker();
465+
466+
$values = $faker->linkResponse(['full_slug' => null]);
467+
468+
self::assertNull((new Link($values))->fullSlug);
469+
}
470+
471+
#[DataProviderExternal(StringProvider::class, 'blank')]
472+
#[Test]
473+
public function fullSlugMustBeValidString(string $value): void
474+
{
475+
$values = self::faker()->linkResponse(['full_slug' => $value]);
476+
477+
self::expectExceptionMessage('The value of the "full_slug" key must be a non-empty, trimmed string. Got: ""');
478+
self::expectException(\InvalidArgumentException::class);
479+
480+
new Link($values);
481+
}
482+
483+
#[Test]
484+
public function getSlugWithNullReturnsFullSlugWhenAvailable(): void
485+
{
486+
$faker = self::faker();
487+
488+
$values = $faker->linkResponse([
489+
'slug' => $faker->slug(),
490+
'full_slug' => $fullSlug = $faker->slug(),
491+
]);
492+
493+
self::assertSame($fullSlug, (new Link($values))->getSlug());
494+
}
495+
496+
#[Test]
497+
public function getSlugWithNullReturnsSlugWhenFullSlugIsNull(): void
498+
{
499+
$faker = self::faker();
500+
501+
$values = $faker->linkResponse(['slug' => $slug = $faker->word()]);
502+
unset($values['full_slug']);
503+
504+
self::assertSame($slug, (new Link($values))->getSlug());
505+
}
506+
440507
#[Test]
441508
public function getSlugWithNullReturnsDefaultName(): void
442509
{

0 commit comments

Comments
 (0)