Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags resolving and fix category resolving #2

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CategoryBundle\Infrastructure\Content\PropertyResolver\Resolver;
namespace Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\PropertyResolver;

use Sulu\Bundle\CategoryBundle\Infrastructure\Content\ResourceLoader\CategoryResourceLoader;
use Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\ResourceLoader\CategoryResourceLoader;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
* with this source code in the file LICENSE.
*/

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

use Sulu\Bundle\CategoryBundle\Category\CategoryManagerInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ResourceLoader\ResourceLoaderInterface;

/**
* @internal if you need to override this service, create a new service with based on ResourceLoaderInterface instead of extending this class
*
* @final
*/
class CategoryResourceLoader implements ResourceLoaderInterface
{
public const RESOURCE_LOADER_KEY = 'category';
Expand Down
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\CategoryBundle\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\CategoryBundle\Infrastructure\Sulu\Content\PropertyResolver\CategorySelectionPropertyResolver;

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

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

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('category', $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_category']);

$content = $contentView->getContent();
$this->assertIsArray($content);
$resolvable = $content[0] ?? null;
$this->assertInstanceOf(ResolvableResource::class, $resolvable);
$this->assertSame(1, $resolvable->getId());
$this->assertSame('custom_category', $resolvable->getResourceLoaderKey());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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\CategoryBundle\Tests\Unit\Infrastructure\Sulu\Content\ResourceLoader;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Bundle\CategoryBundle\Category\CategoryManagerInterface;
use Sulu\Bundle\CategoryBundle\Entity\Category;
use Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\ResourceLoader\CategoryResourceLoader;
use Sulu\Bundle\TestBundle\Testing\SetGetPrivatePropertyTrait;

class CategoryResourceLoaderTest extends TestCase
{
use ProphecyTrait;
use SetGetPrivatePropertyTrait;

/**
* @var ObjectProphecy<CategoryManagerInterface>
*/
private ObjectProphecy $categoryManager;

private CategoryResourceLoader $loader;

public function setUp(): void
{
$this->categoryManager = $this->prophesize(CategoryManagerInterface::class);
$this->loader = new CategoryResourceLoader($this->categoryManager->reveal());
}

public function testGetKey(): void
{
$this->assertSame('category', $this->loader::getKey());
}

public function testLoad(): void
{
$category1 = $this->createCategory(1);
$category2 = $this->createCategory(3);

$this->categoryManager->findByIds([1, 3])->willReturn([
$category1,
$category2,
])
->shouldBeCalled();

$result = $this->loader->load([1, 3], 'en', []);

$this->assertSame([
1 => $category1,
3 => $category2,
], $result);
}

private static function createCategory(int $id): Category
{
$category = new Category();
static::setPrivateProperty($category, 'id', $id);
$category->setKey('category-' . $id);

return $category;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class AccountSelectionPropertyResolver implements PropertyResolverInterface
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class ContactAccountSelectionPropertyResolver implements PropertyResolverInterface
{
public const PREFIX_CONTACT = 'c';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class ContactSelectionPropertyResolver implements PropertyResolverInterface
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class SingleAccountSelectionPropertyResolver implements PropertyResolverInterface
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class SingleContactSelectionPropertyResolver implements PropertyResolverInterface
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
use Sulu\Bundle\ContactBundle\Entity\AccountInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ResourceLoader\ResourceLoaderInterface;

/**
* @internal if you need to override this service, create a new service with based on ResourceLoaderInterface instead of extending this class
*
* @final
*/
class AccountResourceLoader implements ResourceLoaderInterface
{
public const RESOURCE_LOADER_KEY = 'account';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
use Sulu\Bundle\ContactBundle\Entity\ContactInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ResourceLoader\ResourceLoaderInterface;

/**
* @internal if you need to override this service, create a new service with based on ResourceLoaderInterface instead of extending this class
*
* @final
*/
class ContactResourceLoader implements ResourceLoaderInterface
{
public const RESOURCE_LOADER_KEY = 'contact';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\ResourceLoader\MediaResourceLoader;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class MediaSelectionPropertyResolver implements PropertyResolverInterface
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
use Sulu\Bundle\ContentBundle\Content\Application\ResourceLoader\ResourceLoaderInterface;
use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;

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