Skip to content

Commit

Permalink
Add single collection and multi collection property resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Oct 18, 2024
1 parent 3ced396 commit 1246a3f
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\PropertyResolver\CategorySelectionPropertyResolver;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource;
use Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\PropertyResolver\CategorySelectionPropertyResolver;

#[CoversClass(CategorySelectionPropertyResolver::class)]
class CategorySelectionPropertyResolverTest extends TestCase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\PropertyResolver;

use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\ResourceLoader\CollectionResourceLoader;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class CollectionSelectionPropertyResolver implements PropertyResolverInterface
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
{
if (!\is_array($data)
|| 0 === \count($data)
|| !\array_is_list($data)
) {
return ContentView::create([], ['ids' => [], ...$params]);
}

/** @var string $resourceLoaderKey */
$resourceLoaderKey = $params['resourceLoader'] ?? CollectionResourceLoader::getKey();

return ContentView::createResolvables(
$data,
$resourceLoaderKey,
['ids' => $data, ...$params],
);
}

public static function getType(): string
{
return 'collection_selection';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\PropertyResolver;

use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\ResourceLoader\CollectionResourceLoader;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class SingleCollectionSelectionPropertyResolver implements PropertyResolverInterface
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
{
if (!\is_numeric($data)) {
return ContentView::create(null, ['id' => null, ...$params]);
}

/** @var string $resourceLoaderKey */
$resourceLoaderKey = $params['resourceLoader'] ?? CollectionResourceLoader::getKey();

return ContentView::createResolvable(
(int) $data,
$resourceLoaderKey,
[
'id' => $data,
...$params,
],
);
}

public static function getType(): string
{
return 'single_collection_selection';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\ResourceLoader;

use Sulu\Bundle\ContentBundle\Content\Application\ResourceLoader\ResourceLoaderInterface;
use Sulu\Bundle\MediaBundle\Collection\Manager\CollectionManagerInterface;
use Sulu\Bundle\MediaBundle\Media\Exception\CollectionNotFoundException;

/**
* @internal if you need to override this service, create a new service with based on ResourceLoaderInterface instead of extending this class
*
* @final
*/
class CollectionResourceLoader implements ResourceLoaderInterface
{
public const RESOURCE_LOADER_KEY = 'collection';

public function __construct(
private CollectionManagerInterface $collectionManager,
) {
}

public function load(array $ids, ?string $locale, array $params = []): array
{
$mappedResult = [];
foreach ($ids as $id) {
try {
$collection = $this->collectionManager->getById($id, $locale); // TODO load all over one query
$mappedResult[$collection->getId()] = $collection;
} catch (CollectionNotFoundException $e) {
// @ignoreException: do not crash page if selected collection is deleted
}
}

return $mappedResult;
}

public static function getKey(): string
{
return self::RESOURCE_LOADER_KEY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\MediaBundle\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource;
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\PropertyResolver\CollectionSelectionPropertyResolver;

#[CoversClass(CollectionSelectionPropertyResolver::class)]
class CollectionSelectionPropertyResolverTest extends TestCase
{
private CollectionSelectionPropertyResolver $resolver;

public function setUp(): void
{
$this->resolver = new CollectionSelectionPropertyResolver();
}

public function testResolveEmpty(): void
{
$contentView = $this->resolver->resolve([], 'en');

$this->assertSame([], $contentView->getContent());
$this->assertSame(['ids' => []], $contentView->getView());
}

public function testResolveParams(): void
{
$contentView = $this->resolver->resolve([], 'en', ['custom' => 'params']);

$this->assertSame([], $contentView->getContent());
$this->assertSame([
'ids' => [],
'custom' => 'params',
], $contentView->getView());
}

#[DataProvider('provideUnresolvableData')]
public function testResolveUnresolvableData(mixed $data): void
{
$contentView = $this->resolver->resolve($data, 'en');

$this->assertSame([], $contentView->getContent());
$this->assertSame(['ids' => []], $contentView->getView());
}

/**
* @return iterable<array{
* 0: mixed,
* }>
*/
public static function provideUnresolvableData(): iterable
{
yield 'null' => [null];
yield 'smart_content' => [['source' => '123']];
yield 'single_value' => [1];
yield 'object' => [(object) [1, 2]];
}

/**
* @param array<string|int> $data
*/
#[DataProvider('provideResolvableData')]
public function testResolveResolvableData(array $data): void
{
$contentView = $this->resolver->resolve($data, 'en');

$content = $contentView->getContent();
$this->assertIsArray($content);
foreach ($data as $key => $value) {
$resolvable = $content[$key] ?? null;
$this->assertInstanceOf(ResolvableResource::class, $resolvable);
$this->assertSame($value, $resolvable->getId());
$this->assertSame('collection', $resolvable->getResourceLoaderKey());
}

$this->assertSame(['ids' => $data], $contentView->getView());
}

/**
* @return iterable<array{
* 0: array<string|int>,
* }>
*/
public static function provideResolvableData(): iterable
{
yield 'empty' => [[]];
yield 'int_list' => [[1, 2]];
yield 'string_list' => [['1', '2']];
}

public function testCustomResourceLoader(): void
{
$contentView = $this->resolver->resolve([1], 'en', ['resourceLoader' => 'custom_collection']);

$content = $contentView->getContent();
$this->assertIsArray($content);
$resolvable = $content[0] ?? null;
$this->assertInstanceOf(ResolvableResource::class, $resolvable);
$this->assertSame(1, $resolvable->getId());
$this->assertSame('custom_collection', $resolvable->getResourceLoaderKey());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ public static function provideResolvableData(): iterable
{
yield 'empty' => [[]];
yield 'int_id' => [['imageId' => 1]];
yield 'int_id_with_hotspots' => [['imageId' => 1, 'hotspots' => [['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 1'], ['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 2']]]];
yield 'int_id_with_hotspots' => [
['imageId' => 1, 'hotspots' => [['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 1'], ['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 2']]],
];
yield 'string_id' => [['imageId' => '1']];
yield 'string_id_with_hotspots' => [['imageId' => '1', 'hotspots' => [['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 1'], ['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 2']]]];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\MediaBundle\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource;
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\PropertyResolver\SingleCollectionSelectionPropertyResolver;

#[CoversClass(SingleCollectionSelectionPropertyResolver::class)]
class SingleCollectionSelectionPropertyResolverTest extends TestCase
{
private SingleCollectionSelectionPropertyResolver $resolver;

public function setUp(): void
{
$this->resolver = new SingleCollectionSelectionPropertyResolver();
}

public function testResolveEmpty(): void
{
$contentView = $this->resolver->resolve(null, 'en');

$this->assertNull($contentView->getContent());
$this->assertSame(['id' => null], $contentView->getView());
}

public function testResolveParams(): void
{
$contentView = $this->resolver->resolve(null, 'en', ['custom' => 'params']);

$this->assertNull($contentView->getContent());
$this->assertSame([
'id' => null,
'custom' => 'params',
], $contentView->getView());
}

#[DataProvider('provideUnresolvableData')]
public function testResolveUnresolvableData(mixed $data): void
{
$contentView = $this->resolver->resolve($data, 'en');

$this->assertNull($contentView->getContent());
$this->assertSame(['id' => null], $contentView->getView());
}

/**
* @return iterable<array{
* 0: mixed,
* }>
*/
public static function provideUnresolvableData(): iterable
{
yield 'null' => [null];
yield 'smart_content' => [['source' => '123']];
yield 'multi_value' => [[1]];
yield 'object' => [(object) [1]];
}

#[DataProvider('provideResolvableData')]
public function testResolveResolvableData(int|string $data): void
{
$contentView = $this->resolver->resolve($data, 'en');

$content = $contentView->getContent();
$this->assertInstanceOf(ResolvableResource::class, $content);
$this->assertSame((int) $data, $content->getId());
$this->assertSame('collection', $content->getResourceLoaderKey());

$this->assertSame(['id' => $data], $contentView->getView());
}

/**
* @return iterable<array{
* 0: int|string,
* }>
*/
public static function provideResolvableData(): iterable
{
yield 'int' => [1];
yield 'string' => ['2'];
}

public function testCustomResourceLoader(): void
{
$contentView = $this->resolver->resolve(1, 'en', ['resourceLoader' => 'custom_collection']);

$content = $contentView->getContent();

$this->assertInstanceOf(ResolvableResource::class, $content);
$this->assertSame(1, $content->getId());
$this->assertSame('custom_collection', $content->getResourceLoaderKey());
}
}
Loading

0 comments on commit 1246a3f

Please sign in to comment.