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 e2f600a
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CategoryBundle\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver;
namespace Sulu\Bundle\MedaiBundle\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver;

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\MedaiBundle\Infrastructure\Sulu\Content\PropertyResolver\CollectionSelectionPropertyResolver;

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

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

public function testResolveEmpty(): void
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
Loading

0 comments on commit e2f600a

Please sign in to comment.