From 52f86e4136b25c6859f2c8bbf20497073292b997 Mon Sep 17 00:00:00 2001 From: Oliver Kossin Date: Sat, 1 Aug 2020 19:06:53 +0200 Subject: [PATCH 1/3] Add "contact_selection" Content Type Resolver --- .../ContactSelectionResolver.php | 64 +++++++++ Resources/config/content-type-resolvers.xml | 11 ++ .../ContactSelectionResolverTest.php | 126 ++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 Content/ContentTypeResolver/ContactSelectionResolver.php create mode 100644 Tests/Unit/Content/ContentTypeResolver/ContactSelectionResolverTest.php diff --git a/Content/ContentTypeResolver/ContactSelectionResolver.php b/Content/ContentTypeResolver/ContactSelectionResolver.php new file mode 100644 index 0000000..15359ca --- /dev/null +++ b/Content/ContentTypeResolver/ContactSelectionResolver.php @@ -0,0 +1,64 @@ +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 ($data as $key => $contactId) { + $contact = $this->contactManager->getById($contactId, $locale); + $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 testResolve(): void + { + $locale = 'en'; + + $contact = $this->prophesize(ContactInterface::class); + $apiContact = $this->prophesize(Contact::class); + $apiContact->getEntity()->willReturn($contact->reveal()); + + $data = [2]; + + $this->contactManager->getById(2, $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 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()); + } +} From 30a5efce289686b7808941f06cd2503caa460f67 Mon Sep 17 00:00:00 2001 From: Oliver Kossin Date: Sat, 1 Aug 2020 19:42:08 +0200 Subject: [PATCH 2/3] refactor "contact_selection" and add more unit tests --- .../ContactSelectionResolver.php | 3 +- .../ContactSelectionResolverTest.php | 132 ++++++++++++++++-- 2 files changed, 120 insertions(+), 15 deletions(-) diff --git a/Content/ContentTypeResolver/ContactSelectionResolver.php b/Content/ContentTypeResolver/ContactSelectionResolver.php index 15359ca..7fbf641 100644 --- a/Content/ContentTypeResolver/ContactSelectionResolver.php +++ b/Content/ContentTypeResolver/ContactSelectionResolver.php @@ -51,8 +51,7 @@ public function resolve($data, PropertyInterface $property, string $locale, arra } $content = []; - foreach ($data as $key => $contactId) { - $contact = $this->contactManager->getById($contactId, $locale); + foreach ($this->contactManager->getByIds($data, $locale) as $contact) { $serializationContext = new SerializationContext(); $serializationContext->setGroups(['partialContact']); diff --git a/Tests/Unit/Content/ContentTypeResolver/ContactSelectionResolverTest.php b/Tests/Unit/Content/ContentTypeResolver/ContactSelectionResolverTest.php index 4b9d598..00775df 100644 --- a/Tests/Unit/Content/ContentTypeResolver/ContactSelectionResolverTest.php +++ b/Tests/Unit/Content/ContentTypeResolver/ContactSelectionResolverTest.php @@ -58,7 +58,7 @@ public function testGetContentType(): void self::assertSame('contact_selection', $this->contactSelectionResolver::getContentType()); } - public function testResolve(): void + public function testResolveWithOneContact(): void { $locale = 'en'; @@ -68,19 +68,21 @@ public function testResolve(): void $data = [2]; - $this->contactManager->getById(2, $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' => [ + $this->contactManager->getByIds($data, $locale)->willReturn([$apiContact->reveal()]); + $this->contactSerializer->serialize($contact, $locale, Argument::type(SerializationContext::class))->willReturn( + [ 'id' => 2, - 'formatUri' => '/media/2/{format}/media-2.jpg?v=1-0', - ], - ]); + '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); @@ -112,6 +114,110 @@ public function testResolve(): void ); } + 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'; From d73de07fd7b83a3140fcacf5e6050f2b960cab19 Mon Sep 17 00:00:00 2001 From: Oliver Kossin Date: Tue, 18 Aug 2020 19:14:42 +0200 Subject: [PATCH 3/3] Add Review --- Content/ContentTypeResolver/ContactSelectionResolver.php | 2 +- .../ContentTypeResolver/ContactSelectionResolverTest.php | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Content/ContentTypeResolver/ContactSelectionResolver.php b/Content/ContentTypeResolver/ContactSelectionResolver.php index 7fbf641..4fc4e37 100644 --- a/Content/ContentTypeResolver/ContactSelectionResolver.php +++ b/Content/ContentTypeResolver/ContactSelectionResolver.php @@ -58,6 +58,6 @@ public function resolve($data, PropertyInterface $property, string $locale, arra $content[] = $this->contactSerializer->serialize($contact->getEntity(), $locale, $serializationContext); } - return new ContentView($content, [$data]); + return new ContentView($content, $data ?: []); } } diff --git a/Tests/Unit/Content/ContentTypeResolver/ContactSelectionResolverTest.php b/Tests/Unit/Content/ContentTypeResolver/ContactSelectionResolverTest.php index 00775df..22ef7b0 100644 --- a/Tests/Unit/Content/ContentTypeResolver/ContactSelectionResolverTest.php +++ b/Tests/Unit/Content/ContentTypeResolver/ContactSelectionResolverTest.php @@ -107,9 +107,7 @@ public function testResolveWithOneContact(): void ); $this->assertSame( - [ - [2], - ], + [2], $result->getView() ); } @@ -211,9 +209,7 @@ public function testResolveWithManyContacts(): void ); $this->assertSame( - [ - [2, 3, 4], - ], + [2, 3, 4], $result->getView() ); }