Skip to content

Commit

Permalink
Add "single_category_selection" Content Type Resolver (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCadien authored Aug 18, 2020
1 parent fd6fd7f commit cf25058
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Content/ContentTypeResolver/SingleCategorySelectionResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\HeadlessBundle\Content\ContentTypeResolver;

use JMS\Serializer\SerializationContext;
use Sulu\Bundle\CategoryBundle\Category\CategoryManagerInterface;
use Sulu\Bundle\HeadlessBundle\Content\ContentView;
use Sulu\Bundle\HeadlessBundle\Content\Serializer\CategorySerializerInterface;
use Sulu\Component\Content\Compat\PropertyInterface;

class SingleCategorySelectionResolver implements ContentTypeResolverInterface
{
public static function getContentType(): string
{
return 'single_category_selection';
}

/**
* @var CategoryManagerInterface
*/
private $categoryManager;

/**
* @var CategorySerializerInterface
*/
private $categorySerializer;

public function __construct(
CategoryManagerInterface $categoryManager,
CategorySerializerInterface $categorySerializer
) {
$this->categoryManager = $categoryManager;
$this->categorySerializer = $categorySerializer;
}

public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView
{
if (null === $data) {
return new ContentView(null);
}

$category = $this->categoryManager->findById((int) $data);
$serializationContext = new SerializationContext();
$serializationContext->setGroups(['partialCategory']);

$content = $this->categorySerializer->serialize($category, $locale, $serializationContext);

return new ContentView($content, [$data]);
}
}
10 changes: 10 additions & 0 deletions Resources/config/content-type-resolvers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@
<tag name="sulu_headless.content_type_resolver"/>
</service>

<service
id="sulu_headless.content_resolver.single_category_selection"
class="Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\SingleCategorySelectionResolver"
>
<argument type="service" id="sulu_category.category_manager"/>
<argument type="service" id="sulu_headless.serializer.category"/>

<tag name="sulu_headless.content_type_resolver"/>
</service>

<service
id="sulu_headless.content_resolver.single_media_selection"
class="Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\SingleMediaSelectionResolver"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?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\HeadlessBundle\Tests\Unit\Content\ContentTypeResolver;

use JMS\Serializer\SerializationContext;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Bundle\CategoryBundle\Category\CategoryManagerInterface;
use Sulu\Bundle\CategoryBundle\Entity\CategoryInterface;
use Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\SingleCategorySelectionResolver;
use Sulu\Bundle\HeadlessBundle\Content\ContentView;
use Sulu\Bundle\HeadlessBundle\Content\Serializer\CategorySerializerInterface;
use Sulu\Component\Content\Compat\PropertyInterface;

class SingleCategorySelectionResolverTest extends TestCase
{
/**
* @var CategoryManagerInterface|ObjectProphecy
*/
private $categoryManager;

/**
* @var CategorySerializerInterface|ObjectProphecy
*/
private $categorySerializer;

/**
* @var SingleCategorySelectionResolver
*/
private $singleCategorySelectionResolver;

protected function setUp(): void
{
$this->categoryManager = $this->prophesize(CategoryManagerInterface::class);
$this->categorySerializer = $this->prophesize(CategorySerializerInterface::class);

$this->singleCategorySelectionResolver = new SingleCategorySelectionResolver(
$this->categoryManager->reveal(),
$this->categorySerializer->reveal()
);
}

public function testGetContentType(): void
{
self::assertSame('single_category_selection', $this->singleCategorySelectionResolver::getContentType());
}

public function testResolve(): void
{
$locale = 'en';

$category = $this->prophesize(CategoryInterface::class);

$this->categoryManager->findById(1)->willReturn($category->reveal());

$this->categorySerializer->serialize(
$category->reveal(),
$locale,
Argument::type(SerializationContext::class)
)->willReturn([
'id' => 1,
'locale' => 'en',
'key' => 'key-1',
'name' => 'cat-1',
'desc' => 'desc-1',
'medias' => [
[
'id' => 1,
'formatUri' => '/media/1/{format}/media-1.jpg?=v1-0',
],
],
]);

$property = $this->prophesize(PropertyInterface::class);

$result = $this->singleCategorySelectionResolver->resolve([1], $property->reveal(), $locale);

$this->assertInstanceOf(ContentView::class, $result);

$this->assertSame(
[
'id' => 1,
'locale' => 'en',
'key' => 'key-1',
'name' => 'cat-1',
'desc' => 'desc-1',
'medias' => [
[
'id' => 1,
'formatUri' => '/media/1/{format}/media-1.jpg?=v1-0',
],
],
],
$result->getContent()
);

$this->assertSame(
[[1]],
$result->getView()
);
}

public function testResolveDataIsNull(): void
{
$locale = 'en';
$property = $this->prophesize(PropertyInterface::class);

$result = $this->singleCategorySelectionResolver->resolve(null, $property->reveal(), $locale);

$this->assertNull($result->getContent());

$this->assertSame([], $result->getView());
}

public function testResolveDataIsEmptyArray(): void
{
$locale = 'en';
$property = $this->prophesize(PropertyInterface::class);

$result = $this->singleCategorySelectionResolver->resolve(null, $property->reveal(), $locale);

$this->assertNull($result->getContent());

$this->assertSame([], $result->getView());
}
}

0 comments on commit cf25058

Please sign in to comment.