Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Domain/Value/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public function __construct(array $values)
{
Assert::keyExists($values, 'id');
Assert::integer($values['id']);
Assert::integer($values['id'], 'The value of the "id" key must be an integer. Got: %s');
$this->id = new Id($values['id']);

Assert::keyExists($values, 'parent_id');
Expand All @@ -64,24 +64,24 @@ public function __construct(array $values)
$this->uuid = new Uuid($values['uuid']);

Assert::keyExists($values, 'name');
$this->name = TrimmedNonEmptyString::fromString($values['name'])->toString();
$this->name = TrimmedNonEmptyString::fromString($values['name'], 'The value of the "name" key must be a non-empty, trimmed string. Got: %s')->toString();

Assert::keyExists($values, 'slug');
$this->slug = TrimmedNonEmptyString::fromString($values['slug'])->toString();
$this->slug = TrimmedNonEmptyString::fromString($values['slug'], 'The value of the "slug" key must be a non-empty, trimmed string. Got: %s')->toString();

Assert::keyExists($values, 'path');

if (null !== $values['path']) {
$path = TrimmedNonEmptyString::fromString($values['path'])->toString();
if (null !== $values['path'] && '' !== $values['path']) {
$path = TrimmedNonEmptyString::fromString($values['path'], 'The value of the "path" key must be a non-empty, trimmed string. Got: %s')->toString();
}

$this->path = $path ?? null;

Assert::keyExists($values, 'real_path');
$this->realPath = TrimmedNonEmptyString::fromString($values['real_path'])->toString();
$this->realPath = TrimmedNonEmptyString::fromString($values['real_path'], 'The value of the "real_path" key must be a non-empty, trimmed string. Got: %s')->toString();

Assert::keyExists($values, 'position');
Assert::integer($values['position']);
Assert::integer($values['position'], 'The value of the "position" key must be an integer. Got: %s');
$this->position = $values['position'];

Assert::keyExists($values, 'is_folder');
Expand All @@ -94,7 +94,7 @@ public function __construct(array $values)
$this->isPublished = true === $values['published'];

Assert::keyExists($values, 'alternates');
Assert::isArray($values['alternates']);
Assert::isArray($values['alternates'], 'The value of the "alternates" key must be an array. Got: %s');
$this->alternates = array_map(static fn (array $values) => new LinkAlternate($values), $values['alternates']);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/Domain/Value/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace Storyblok\Api\Tests\Unit\Domain\Value;

use Ergebnis\DataProvider\StringProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Storyblok\Api\Domain\Value\Link;
Expand Down Expand Up @@ -153,6 +155,18 @@ public function pathKeyMustExist(): void
new Link($values);
}

#[DataProviderExternal(StringProvider::class, 'blank')]
#[Test]
public function pathMustBeValidString(string $value): void
{
$values = self::faker()->linkResponse(['path' => $value]);

self::expectExceptionMessage('The value of the "path" key must be a non-empty, trimmed string. Got: ""');
self::expectException(\InvalidArgumentException::class);

new Link($values);
}

#[Test]
public function isFolder(): void
{
Expand Down