From cf25058ba6ed7f45a6644694981a18b5bae1210b Mon Sep 17 00:00:00 2001 From: Oliver Kossin Date: Tue, 18 Aug 2020 11:16:08 +0200 Subject: [PATCH] Add "single_category_selection" Content Type Resolver (#45) --- .../SingleCategorySelectionResolver.php | 61 ++++++++ Resources/config/content-type-resolvers.xml | 10 ++ .../SingleCategorySelectionResolverTest.php | 138 ++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 Content/ContentTypeResolver/SingleCategorySelectionResolver.php create mode 100644 Tests/Unit/Content/ContentTypeResolver/SingleCategorySelectionResolverTest.php diff --git a/Content/ContentTypeResolver/SingleCategorySelectionResolver.php b/Content/ContentTypeResolver/SingleCategorySelectionResolver.php new file mode 100644 index 0000000..1d1dcbd --- /dev/null +++ b/Content/ContentTypeResolver/SingleCategorySelectionResolver.php @@ -0,0 +1,61 @@ +categoryManager = $categoryManager; + $this->categorySerializer = $categorySerializer; + } + + public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView + { + if (null === $data) { + return new ContentView(null); + } + + $category = $this->categoryManager->findById((int) $data); + $serializationContext = new SerializationContext(); + $serializationContext->setGroups(['partialCategory']); + + $content = $this->categorySerializer->serialize($category, $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..8f29d34 100644 --- a/Resources/config/content-type-resolvers.xml +++ b/Resources/config/content-type-resolvers.xml @@ -82,6 +82,16 @@ + + + + + + + categoryManager = $this->prophesize(CategoryManagerInterface::class); + $this->categorySerializer = $this->prophesize(CategorySerializerInterface::class); + + $this->singleCategorySelectionResolver = new SingleCategorySelectionResolver( + $this->categoryManager->reveal(), + $this->categorySerializer->reveal() + ); + } + + public function testGetContentType(): void + { + self::assertSame('single_category_selection', $this->singleCategorySelectionResolver::getContentType()); + } + + public function testResolve(): void + { + $locale = 'en'; + + $category = $this->prophesize(CategoryInterface::class); + + $this->categoryManager->findById(1)->willReturn($category->reveal()); + + $this->categorySerializer->serialize( + $category->reveal(), + $locale, + Argument::type(SerializationContext::class) + )->willReturn([ + 'id' => 1, + 'locale' => 'en', + 'key' => 'key-1', + 'name' => 'cat-1', + 'desc' => 'desc-1', + 'medias' => [ + [ + 'id' => 1, + 'formatUri' => '/media/1/{format}/media-1.jpg?=v1-0', + ], + ], + ]); + + $property = $this->prophesize(PropertyInterface::class); + + $result = $this->singleCategorySelectionResolver->resolve([1], $property->reveal(), $locale); + + $this->assertInstanceOf(ContentView::class, $result); + + $this->assertSame( + [ + 'id' => 1, + 'locale' => 'en', + 'key' => 'key-1', + 'name' => 'cat-1', + 'desc' => 'desc-1', + 'medias' => [ + [ + 'id' => 1, + 'formatUri' => '/media/1/{format}/media-1.jpg?=v1-0', + ], + ], + ], + $result->getContent() + ); + + $this->assertSame( + [[1]], + $result->getView() + ); + } + + public function testResolveDataIsNull(): void + { + $locale = 'en'; + $property = $this->prophesize(PropertyInterface::class); + + $result = $this->singleCategorySelectionResolver->resolve(null, $property->reveal(), $locale); + + $this->assertNull($result->getContent()); + + $this->assertSame([], $result->getView()); + } + + public function testResolveDataIsEmptyArray(): void + { + $locale = 'en'; + $property = $this->prophesize(PropertyInterface::class); + + $result = $this->singleCategorySelectionResolver->resolve(null, $property->reveal(), $locale); + + $this->assertNull($result->getContent()); + + $this->assertSame([], $result->getView()); + } +}