Skip to content

Commit

Permalink
Add Tag and add Category loader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Oct 17, 2024
1 parent 32e3639 commit 02dadf0
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* 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;

Expand All @@ -26,17 +26,20 @@ 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,7 +11,7 @@
* 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;
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 @@ -11,11 +11,11 @@
* with this source code in the file LICENSE.
*/

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

use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;
use Sulu\Bundle\TagBundle\Infrastructure\Content\ResourceLoader\TagResourceLoader;
use Sulu\Bundle\TagBundle\Infrastructure\Sulu\Content\ResourceLoader\TagResourceLoader;

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

/** @var string $resourceLoaderKey */
Expand All @@ -39,7 +39,7 @@ public function resolve(mixed $data, string $locale, array $params = []): Conten
return ContentView::createResolvables(
$data,
$resourceLoaderKey,
['ids' => $data],
['ids' => $data, ...$params],
);
}

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

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

use Sulu\Bundle\ContentBundle\Content\Application\ResourceLoader\ResourceLoaderInterface;
use Sulu\Bundle\TagBundle\Tag\TagRepositoryInterface;

/**
* @internal if you need to override this service, create a new service with based on ResourceLoaderInterface instead of extending this class
*
* @final
*/
class TagResourceLoader implements ResourceLoaderInterface
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\TagBundle\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\TagBundle\Infrastructure\Sulu\Content\PropertyResolver\TagSelectionPropertyResolver;

#[CoversClass(TagSelectionPropertyResolver::class)]
class TagSelectionPropertyResolverTest extends TestCase
{
private TagSelectionPropertyResolver $resolver;

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

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('tag', $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_tag']);

$content = $contentView->getContent();
$this->assertIsArray($content);
$resolvable = $content[0] ?? null;
$this->assertInstanceOf(ResolvableResource::class, $resolvable);
$this->assertSame(1, $resolvable->getId());
$this->assertSame('custom_tag', $resolvable->getResourceLoaderKey());
}
}
Loading

0 comments on commit 02dadf0

Please sign in to comment.