diff --git a/Content/ContentTypeResolver/AccountSelectionResolver.php b/Content/ContentTypeResolver/AccountSelectionResolver.php new file mode 100644 index 0000000..0a55995 --- /dev/null +++ b/Content/ContentTypeResolver/AccountSelectionResolver.php @@ -0,0 +1,63 @@ +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 ?: []); + } +} diff --git a/Resources/config/content-type-resolvers.xml b/Resources/config/content-type-resolvers.xml index bc03bbc..4751718 100644 --- a/Resources/config/content-type-resolvers.xml +++ b/Resources/config/content-type-resolvers.xml @@ -125,5 +125,16 @@ + + + + + + + diff --git a/Tests/Unit/Content/ContentTypeResolver/AccountSelectionResolverTest.php b/Tests/Unit/Content/ContentTypeResolver/AccountSelectionResolverTest.php new file mode 100644 index 0000000..9d78646 --- /dev/null +++ b/Tests/Unit/Content/ContentTypeResolver/AccountSelectionResolverTest.php @@ -0,0 +1,212 @@ +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( + [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()); + } +}