Skip to content

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silasjoisten committed Jan 17, 2025
1 parent 219c4f7 commit ae03ded
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 20 deletions.
27 changes: 14 additions & 13 deletions src/Bridge/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@
/**
* @author Silas Joisten <[email protected]>
*
* @method array assetResponse(array $overrides = [])
* @method array datasourceDimensionResponse(array $overrides = [])
* @method array datasourceEntriesResponse(array $overrides = [])
* @method array datasourceEntryResponse(array $overrides = [])
* @method array datasourceResponse(array $overrides = [])
* @method array datasourcesResponse(array $overrides = [])
* @method array linkAlternateResponse(array $overrides = [])
* @method array linkResponse(array $overrides = [])
* @method array linksResponse(array $overrides = [])
* @method array spaceResponse(array $overrides = [])
* @method array storiesResponse(array $overrides = [])
* @method array storyResponse(array $overrides = [])
* @method array tagsResponse(array $overrides = [])
* @method array assetResponse(array $overrides = [])
* @method array datasourceDimensionResponse(array $overrides = [])
* @method array datasourceEntriesResponse(array $overrides = [])
* @method array datasourceEntryResponse(array $overrides = [])
* @method array datasourceResponse(array $overrides = [])
* @method array datasourcesResponse(array $overrides = [])
* @method array linkAlternateResponse(array $overrides = [])
* @method array linkResponse(array $overrides = [])
* @method array linksResponse(array $overrides = [])
* @method string relation()
* @method array spaceResponse(array $overrides = [])
* @method array storiesResponse(array $overrides = [])
* @method array storyResponse(array $overrides = [])
* @method array tagsResponse(array $overrides = [])
*/
final class Generator extends BaseGenerator
{
Expand Down
9 changes: 9 additions & 0 deletions src/Bridge/Faker/Provider/StoryblokProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,13 @@ public function assetResponse(array $overrides = []): array
$overrides,
);
}

public function relation(): string
{
return \sprintf(
'%s.%s',
$this->generator->word(),
$this->generator->word(),
);
}
}
2 changes: 1 addition & 1 deletion src/Domain/Value/Resolver/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public function __construct(
public string $value,
) {
TrimmedNonEmptyString::fromString($value);
Assert::contains($value, '.');
Assert::regex($value, '/^([a-zA-Z].+)\.([a-zA-Z].+)$/');
}
}
7 changes: 7 additions & 0 deletions src/Request/StoryRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Storyblok\Api\Request;

use Storyblok\Api\Domain\Value\Dto\Version;
use Storyblok\Api\Domain\Value\Resolver\RelationCollection;
use Webmozart\Assert\Assert;

/**
Expand All @@ -24,6 +25,7 @@
public function __construct(
public string $language = 'default',
public ?Version $version = null,
public ?RelationCollection $withRelations = null,
) {
Assert::stringNotEmpty($language);
}
Expand All @@ -32,6 +34,7 @@ public function __construct(
* @return array{
* language: string,
* version?: string,
* resolve_relations?: string,
* }
*/
public function toArray(): array
Expand All @@ -44,6 +47,10 @@ public function toArray(): array
$array['version'] = $this->version->value;
}

if (null !== $this->withRelations && $this->withRelations->count() > 0) {
$array['resolve_relations'] = $this->withRelations->toString();
}

return $array;
}
}
4 changes: 2 additions & 2 deletions src/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ interface ResolverInterface
/**
* Resolves relations in the target content using the given relations collection.
*
* @param array<string, mixed> $target The target story content containing UUIDs to resolve.
* @param array<int, array<string, mixed>> $relations The target story content containing UUIDs to resolve.
* @param array<string, mixed> $target the target story content containing UUIDs to resolve
* @param array<int, array<string, mixed>> $relations the target story content containing UUIDs to resolve
*
* @return array<string, mixed>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Resolver/StoryResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public function resolve(array $target, array $relations): array
}

foreach ($target as &$value) {
if (is_string($value) && array_key_exists($value, $relationMap)) {
if (\is_string($value) && \array_key_exists($value, $relationMap)) {
$value = $relationMap[$value];

continue;
}

if (is_array($value)) {
if (\is_array($value)) {
$value = $this->resolve($value, $relations);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/StoriesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Storyblok\Api\Domain\Value\Dto\Version;
use Storyblok\Api\Domain\Value\Id;
use Storyblok\Api\Domain\Value\Resolver\RelationCollection;
use Storyblok\Api\Domain\Value\Total;
use Storyblok\Api\Domain\Value\Uuid;
use Storyblok\Api\Request\StoriesRequest;
Expand Down
1 change: 0 additions & 1 deletion src/StoriesResolvedApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Storyblok\Api\Resolver\ResolverInterface;
use Storyblok\Api\Response\StoriesResponse;
use Storyblok\Api\Response\StoryResponse;
use Webmozart\Assert\Assert;

/**
* @author Silas Joisten <[email protected]>
Expand Down
130 changes: 130 additions & 0 deletions tests/Unit/Domain/Value/Resolver/RelationCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

/**
* This file is part of Storyblok-Api.
*
* (c) SensioLabs Deutschland <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use PHPUnit\Framework\TestCase;
use Storyblok\Api\Domain\Value\Resolver\Relation;
use Storyblok\Api\Domain\Value\Resolver\RelationCollection;
use Storyblok\Api\Tests\Util\FakerTrait;

/**
* @author Silas Joisten <[email protected]>
*/
final class RelationCollectionTest extends TestCase
{
use FakerTrait;

/**
* @test
*/
public function add(): void
{
$faker = self::faker();

$collection = new RelationCollection();
self::assertEmpty($collection);

$collection->add(new Relation($faker->relation()));
self::assertCount(1, $collection);
}

/**
* @test
*/
public function remove(): void
{
$faker = self::faker();

$relation = new Relation($faker->relation());

$collection = new RelationCollection([$relation]);
self::assertCount(1, $collection);

$collection->remove($relation);
self::assertEmpty($collection);
}

/**
* @test
*/
public function hasReturnsTrue(): void
{
$faker = self::faker();

$relation = new Relation($faker->relation());

$collection = new RelationCollection([$relation, new Relation($faker->relation())]);

self::assertTrue($collection->has($relation));
}

/**
* @test
*/
public function hasReturnsFalse(): void
{
$faker = self::faker();

$collection = new RelationCollection([new Relation($faker->relation())]);

self::assertFalse($collection->has(new Relation($faker->relation())));
}

/**
* @test
*/
public function isCountable(): void
{
$faker = self::faker();

$relation = new Relation($faker->relation());

$collection = new RelationCollection([$relation]);

self::assertSame(1, $collection->count());
}

/**
* @test
*/
public function toStringMethod(): void
{
$faker = self::faker();

$relations = [
new Relation($relation1 = $faker->relation()),
new Relation($relation2 = $faker->relation()),
new Relation($relation3 = $faker->relation()),
];

$collection = new RelationCollection($relations);

self::assertSame(implode(',', [$relation1, $relation2, $relation3]), $collection->toString());
}

/**
* @test
*/
public function getIterator(): void
{
$faker = self::faker();

$relations = [
new Relation($faker->relation()),
new Relation($faker->relation()),
];

self::assertInstanceOf(\ArrayIterator::class, (new RelationCollection($relations))->getIterator());
}
}
50 changes: 50 additions & 0 deletions tests/Unit/Domain/Value/Resolver/RelationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* This file is part of Storyblok-Api.
*
* (c) SensioLabs Deutschland <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use PHPUnit\Framework\TestCase;
use Storyblok\Api\Domain\Value\Resolver\Relation;
use Storyblok\Api\Tests\Util\FakerTrait;

/**
* @author Silas Joisten <[email protected]>
*/
final class RelationTest extends TestCase
{
use FakerTrait;

/**
* @test
*/
public function value(): void
{
$value = self::faker()->relation();

self::assertSame($value, (new Relation($value))->value);
}

/**
* @test
*
* @dataProvider \Ergebnis\DataProvider\StringProvider::arbitrary()
* @dataProvider \Ergebnis\DataProvider\StringProvider::blank()
* @dataProvider \Ergebnis\DataProvider\StringProvider::empty()
*/
public function valueInvalid(string $value): void
{
self::expectException(\InvalidArgumentException::class);

new Relation($value);
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Request/StoryRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use PHPUnit\Framework\TestCase;
use Storyblok\Api\Domain\Value\Dto\Version;
use Storyblok\Api\Domain\Value\Resolver\Relation;
use Storyblok\Api\Domain\Value\Resolver\RelationCollection;
use Storyblok\Api\Request\StoryRequest;
use Storyblok\Api\Tests\Util\FakerTrait;

Expand Down Expand Up @@ -67,4 +69,22 @@ public function toArrayWithVersion(): void
'version' => $version->value,
], $request->toArray());
}

/**
* @test
*/
public function toArrayWithRelations(): void
{
$request = new StoryRequest(
withRelations: new RelationCollection([
new Relation('root.relation'),
new Relation('root.another_relation'),
]),
);

self::assertSame([
'language' => 'default',
'resolve_relations' => 'root.relation,root.another_relation',
], $request->toArray());
}
}

0 comments on commit ae03ded

Please sign in to comment.