-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "contact_selection" Content Type Resolver (#44)
- Loading branch information
Showing
3 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\ContactManager; | ||
use Sulu\Bundle\HeadlessBundle\Content\ContentView; | ||
use Sulu\Bundle\HeadlessBundle\Content\Serializer\ContactSerializerInterface; | ||
use Sulu\Component\Content\Compat\PropertyInterface; | ||
|
||
class ContactSelectionResolver implements ContentTypeResolverInterface | ||
{ | ||
public static function getContentType(): string | ||
{ | ||
return 'contact_selection'; | ||
} | ||
|
||
/** | ||
* @var ContactManager | ||
*/ | ||
private $contactManager; | ||
|
||
/** | ||
* @var ContactSerializerInterface | ||
*/ | ||
private $contactSerializer; | ||
|
||
public function __construct( | ||
ContactManager $contactManager, | ||
ContactSerializerInterface $contactSerializer | ||
) { | ||
$this->contactManager = $contactManager; | ||
$this->contactSerializer = $contactSerializer; | ||
} | ||
|
||
public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView | ||
{ | ||
if (null === $data) { | ||
return new ContentView(null); | ||
} | ||
|
||
$content = []; | ||
foreach ($this->contactManager->getByIds($data, $locale) as $contact) { | ||
$serializationContext = new SerializationContext(); | ||
$serializationContext->setGroups(['partialContact']); | ||
|
||
$content[] = $this->contactSerializer->serialize($contact->getEntity(), $locale, $serializationContext); | ||
} | ||
|
||
return new ContentView($content, $data ?: []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 228 additions & 0 deletions
228
Tests/Unit/Content/ContentTypeResolver/ContactSelectionResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
<?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\Contact; | ||
use Sulu\Bundle\ContactBundle\Contact\ContactManager; | ||
use Sulu\Bundle\ContactBundle\Entity\ContactInterface; | ||
use Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\ContactSelectionResolver; | ||
use Sulu\Bundle\HeadlessBundle\Content\ContentView; | ||
use Sulu\Bundle\HeadlessBundle\Content\Serializer\ContactSerializerInterface; | ||
use Sulu\Component\Content\Compat\PropertyInterface; | ||
|
||
class ContactSelectionResolverTest extends TestCase | ||
{ | ||
/** | ||
* @var ContactManager|ObjectProphecy | ||
*/ | ||
private $contactManager; | ||
|
||
/** | ||
* @var ContactSerializerInterface|ObjectProphecy | ||
*/ | ||
private $contactSerializer; | ||
|
||
/** | ||
* @var ContactSelectionResolver | ||
*/ | ||
private $contactSelectionResolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->contactManager = $this->prophesize(ContactManager::class); | ||
$this->contactSerializer = $this->prophesize(ContactSerializerInterface::class); | ||
|
||
$this->contactSelectionResolver = new ContactSelectionResolver( | ||
$this->contactManager->reveal(), | ||
$this->contactSerializer->reveal() | ||
); | ||
} | ||
|
||
public function testGetContentType(): void | ||
{ | ||
self::assertSame('contact_selection', $this->contactSelectionResolver::getContentType()); | ||
} | ||
|
||
public function testResolveWithOneContact(): void | ||
{ | ||
$locale = 'en'; | ||
|
||
$contact = $this->prophesize(ContactInterface::class); | ||
$apiContact = $this->prophesize(Contact::class); | ||
$apiContact->getEntity()->willReturn($contact->reveal()); | ||
|
||
$data = [2]; | ||
|
||
$this->contactManager->getByIds($data, $locale)->willReturn([$apiContact->reveal()]); | ||
$this->contactSerializer->serialize($contact, $locale, Argument::type(SerializationContext::class))->willReturn( | ||
[ | ||
'id' => 2, | ||
'firstName' => 'John', | ||
'lastName' => 'Doe', | ||
'fullName' => 'John Doe', | ||
'title' => 'fancyTitle', | ||
'position' => 'CEO', | ||
'avatar' => [ | ||
'id' => 2, | ||
'formatUri' => '/media/2/{format}/media-2.jpg?v=1-0', | ||
], | ||
] | ||
); | ||
|
||
$property = $this->prophesize(PropertyInterface::class); | ||
$result = $this->contactSelectionResolver->resolve($data, $property->reveal(), $locale); | ||
|
||
$this->assertInstanceOf(ContentView::class, $result); | ||
$this->assertSame( | ||
[ | ||
[ | ||
'id' => 2, | ||
'firstName' => 'John', | ||
'lastName' => 'Doe', | ||
'fullName' => 'John Doe', | ||
'title' => 'fancyTitle', | ||
'position' => 'CEO', | ||
'avatar' => [ | ||
'id' => 2, | ||
'formatUri' => '/media/2/{format}/media-2.jpg?v=1-0', | ||
], | ||
], | ||
], | ||
$result->getContent() | ||
); | ||
|
||
$this->assertSame( | ||
[2], | ||
$result->getView() | ||
); | ||
} | ||
|
||
public function testResolveWithManyContacts(): void | ||
{ | ||
$locale = 'en'; | ||
|
||
$contact = $this->prophesize(ContactInterface::class); | ||
$apiContact = $this->prophesize(Contact::class); | ||
$apiContact->getEntity()->willReturn($contact->reveal()); | ||
|
||
$data = [2, 3, 4]; | ||
|
||
$this->contactManager->getByIds($data, $locale)->willReturn([$apiContact->reveal(), $apiContact->reveal(), $apiContact->reveal()]); | ||
$this->contactSerializer->serialize($contact, $locale, Argument::type(SerializationContext::class))->willReturn( | ||
[ | ||
'id' => 2, | ||
'firstName' => 'John', | ||
'lastName' => 'Doe', | ||
'fullName' => 'John Doe', | ||
'title' => 'fancyTitle', | ||
'position' => 'CEO', | ||
'avatar' => [ | ||
'id' => 2, | ||
'formatUri' => '/media/2/{format}/media-2.jpg?v=1-0', | ||
], | ||
], | ||
[ | ||
'id' => 3, | ||
'firstName' => 'Max', | ||
'lastName' => 'Mustermann', | ||
'fullName' => 'Max Mustermann', | ||
'title' => 'fancyTitle', | ||
'position' => 'CTO', | ||
'avatar' => [ | ||
'id' => 3, | ||
'formatUri' => '/media/3/{format}/media-3.jpg?v=1-0', | ||
], | ||
], | ||
[ | ||
'id' => 4, | ||
'firstName' => 'Diana', | ||
'lastName' => 'Doe', | ||
'fullName' => 'Diana Doe', | ||
'title' => 'fancyTitle', | ||
'position' => 'CFO', | ||
'avatar' => [ | ||
'id' => 4, | ||
'formatUri' => '/media/4/{format}/media-2.jpg?v=1-0', | ||
], | ||
] | ||
); | ||
|
||
$property = $this->prophesize(PropertyInterface::class); | ||
$result = $this->contactSelectionResolver->resolve($data, $property->reveal(), $locale); | ||
|
||
$this->assertInstanceOf(ContentView::class, $result); | ||
$this->assertSame( | ||
[ | ||
[ | ||
'id' => 2, | ||
'firstName' => 'John', | ||
'lastName' => 'Doe', | ||
'fullName' => 'John Doe', | ||
'title' => 'fancyTitle', | ||
'position' => 'CEO', | ||
'avatar' => [ | ||
'id' => 2, | ||
'formatUri' => '/media/2/{format}/media-2.jpg?v=1-0', | ||
], | ||
], | ||
[ | ||
'id' => 3, | ||
'firstName' => 'Max', | ||
'lastName' => 'Mustermann', | ||
'fullName' => 'Max Mustermann', | ||
'title' => 'fancyTitle', | ||
'position' => 'CTO', | ||
'avatar' => [ | ||
'id' => 3, | ||
'formatUri' => '/media/3/{format}/media-3.jpg?v=1-0', | ||
], | ||
], | ||
[ | ||
'id' => 4, | ||
'firstName' => 'Diana', | ||
'lastName' => 'Doe', | ||
'fullName' => 'Diana Doe', | ||
'title' => 'fancyTitle', | ||
'position' => 'CFO', | ||
'avatar' => [ | ||
'id' => 4, | ||
'formatUri' => '/media/4/{format}/media-2.jpg?v=1-0', | ||
], | ||
], | ||
], | ||
$result->getContent() | ||
); | ||
|
||
$this->assertSame( | ||
[2, 3, 4], | ||
$result->getView() | ||
); | ||
} | ||
|
||
public function testResolveDataIsNull(): void | ||
{ | ||
$locale = 'en'; | ||
$property = $this->prophesize(PropertyInterface::class); | ||
|
||
$result = $this->contactSelectionResolver->resolve(null, $property->reveal(), $locale); | ||
|
||
$this->assertNull($result->getContent()); | ||
|
||
$this->assertSame([], $result->getView()); | ||
} | ||
} |