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 "account_selection" Content Type Resolver #43

Merged
merged 3 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
63 changes: 63 additions & 0 deletions Content/ContentTypeResolver/AccountSelectionResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\ContactBundle\Contact\AccountManager;
use Sulu\Bundle\HeadlessBundle\Content\ContentView;
use Sulu\Bundle\HeadlessBundle\Content\Serializer\AccountSerializerInterface;
use Sulu\Component\Content\Compat\PropertyInterface;

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

/**
* @var AccountManager
*/
private $accountManager;

/**
* @var AccountSerializerInterface
*/
private $accountSerializer;

public function __construct(
AccountManager $accountManager,
AccountSerializerInterface $accountSerializer
) {
$this->accountManager = $accountManager;
$this->accountSerializer = $accountSerializer;
}

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

$content = [];
foreach ($this->accountManager->getByIds($data, $locale) as $account) {
$serializationContext = new SerializationContext();
$serializationContext->setGroups(['partialAccount']);

$content[] = $this->accountSerializer->serialize($account->getEntity(), $locale, $serializationContext);
}

return new ContentView($content, [$data]);
alexander-schranz marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 11 additions & 0 deletions Resources/config/content-type-resolvers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,16 @@

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

<service
id="sulu_headless.content_resolver.account_selection"
class="Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\AccountSelectionResolver"
lazy="true"
>
<argument type="service" id="sulu_contact.account_manager"/>
<argument type="service" id="sulu_headless.serializer.account"/>

<tag name="sulu_headless.content_type_resolver"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?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\ContactBundle\Api\Account;
use Sulu\Bundle\ContactBundle\Contact\AccountManager;
use Sulu\Bundle\ContactBundle\Entity\AccountInterface;
use Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\AccountSelectionResolver;
use Sulu\Bundle\HeadlessBundle\Content\ContentView;
use Sulu\Bundle\HeadlessBundle\Content\Serializer\AccountSerializerInterface;
use Sulu\Component\Content\Compat\PropertyInterface;

class AccountSelectionResolverTest extends TestCase
{
/**
* @var AccountManager|ObjectProphecy
*/
private $accountManager;

/**
* @var AccountSerializerInterface|ObjectProphecy
*/
private $accountSerializer;

/**
* @var AccountSelectionResolver
*/
private $accountSelectionResolver;

protected function setUp(): void
{
$this->accountManager = $this->prophesize(AccountManager::class);
$this->accountSerializer = $this->prophesize(AccountSerializerInterface::class);

$this->accountSelectionResolver = new AccountSelectionResolver(
$this->accountManager->reveal(),
$this->accountSerializer->reveal()
);
}

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

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

$account = $this->prophesize(AccountInterface::class);
$apiAccount = $this->prophesize(Account::class);
$apiAccount->getEntity()->willReturn($account->reveal());

$data = [3];

$this->accountManager->getByIds($data, $locale)->willReturn([$apiAccount->reveal()]);
$this->accountSerializer->serialize($account, $locale, Argument::type(SerializationContext::class))->willReturn(
[
'id' => 3,
'depth' => 1,
'name' => 'Sulu GmbH',
'corporation' => 'Digital Agency',
'logo' => [
'id' => 2,
'formatUri' => '/media/2/{format}/media-2.jpg?v=1-0',
],
]
);

$property = $this->prophesize(PropertyInterface::class);
$result = $this->accountSelectionResolver->resolve($data, $property->reveal(), $locale);

$this->assertInstanceOf(ContentView::class, $result);
$this->assertSame(
[
[
'id' => 3,
'depth' => 1,
'name' => 'Sulu GmbH',
'corporation' => 'Digital Agency',
'logo' => [
'id' => 2,
'formatUri' => '/media/2/{format}/media-2.jpg?v=1-0',
],
],
],
$result->getContent()
);

$this->assertSame(
[
alexander-schranz marked this conversation as resolved.
Show resolved Hide resolved
[3],
],
$result->getView()
);
}

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

$account = $this->prophesize(AccountInterface::class);
$apiAccount = $this->prophesize(Account::class);
$apiAccount->getEntity()->willReturn($account->reveal());

$data = [3, 4, 5];

$this->accountManager->getByIds($data, $locale)->willReturn([$apiAccount->reveal(), $apiAccount->reveal(), $apiAccount->reveal()]);
$this->accountSerializer->serialize($account, $locale, Argument::type(SerializationContext::class))->willReturn(
[
'id' => 3,
'depth' => 1,
'name' => 'Sulu GmbH',
'corporation' => 'Digital Agency',
'logo' => [
'id' => 2,
'formatUri' => '/media/2/{format}/media-2.jpg?v=1-0',
],
],
[
'id' => 4,
'depth' => 1,
'name' => 'Test GmbH',
'corporation' => 'Digital Agency',
'logo' => [
'id' => 3,
'formatUri' => '/media/3/{format}/media-2.jpg?v=1-0',
],
],
[
'id' => 5,
'depth' => 1,
'name' => 'Test Inc',
'corporation' => 'Fancy big incorporated',
'logo' => [
'id' => 3,
'formatUri' => '/media/3/{format}/media-2.jpg?v=1-0',
],
]
);

$property = $this->prophesize(PropertyInterface::class);
$result = $this->accountSelectionResolver->resolve($data, $property->reveal(), $locale);

$this->assertInstanceOf(ContentView::class, $result);
$this->assertSame(
[
[
'id' => 3,
'depth' => 1,
'name' => 'Sulu GmbH',
'corporation' => 'Digital Agency',
'logo' => [
'id' => 2,
'formatUri' => '/media/2/{format}/media-2.jpg?v=1-0',
],
],
[
'id' => 4,
'depth' => 1,
'name' => 'Test GmbH',
'corporation' => 'Digital Agency',
'logo' => [
'id' => 3,
'formatUri' => '/media/3/{format}/media-2.jpg?v=1-0',
],
],
[
'id' => 5,
'depth' => 1,
'name' => 'Test Inc',
'corporation' => 'Fancy big incorporated',
'logo' => [
'id' => 3,
'formatUri' => '/media/3/{format}/media-2.jpg?v=1-0',
],
],
],
$result->getContent()
);

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

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

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

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

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