diff --git a/Content/ContentTypeResolver/ContactSelectionResolver.php b/Content/ContentTypeResolver/ContactSelectionResolver.php new file mode 100644 index 0000000..4fc4e37 --- /dev/null +++ b/Content/ContentTypeResolver/ContactSelectionResolver.php @@ -0,0 +1,63 @@ +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 ?: []); + } +} diff --git a/Resources/config/content-type-resolvers.xml b/Resources/config/content-type-resolvers.xml index bc03bbc..a26780b 100644 --- a/Resources/config/content-type-resolvers.xml +++ b/Resources/config/content-type-resolvers.xml @@ -49,6 +49,17 @@ + + + + + + + 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()); + } +}