diff --git a/library/Helper/Post.php b/library/Helper/Post.php index 5adba1b36..3bd9bee6a 100644 --- a/library/Helper/Post.php +++ b/library/Helper/Post.php @@ -9,8 +9,10 @@ use ComponentLibrary\Integrations\Image\Image as ImageComponentContract; use Municipio\PostObject\TermIcon\TryGetTermIcon; use Municipio\PostObject\Decorators\BackwardsCompatiblePostObject; +use Municipio\PostObject\Decorators\IconResolvingPostObject; use Municipio\PostObject\Decorators\PostObjectFromWpPost; use Municipio\PostObject\Decorators\PostObjectWithTermIcons; +use Municipio\PostObject\Icon\Resolvers\NullIconResolver; use Municipio\PostObject\PostObject; use Municipio\PostObject\PostObjectInterface; @@ -151,6 +153,7 @@ private static function convertWpPostToPostObject(WP_Post $post, string $cacheGr $postObject = new PostObjectFromWpPost(new PostObject(), $post, $wpService); $postObject = new PostObjectWithTermIcons($postObject, $wpService, new TryGetTermIcon()); + $postObject = new IconResolvingPostObject($postObject, new NullIconResolver()); $postObject = new BackwardsCompatiblePostObject($postObject, $camelCasedPost); self::$runtimeCache[$cacheGroup][$cacheKey] = $postObject; diff --git a/library/PostObject/Decorators/AbstractPostObjectDecorator.php b/library/PostObject/Decorators/AbstractPostObjectDecorator.php index bcdff0cd7..b1f654145 100644 --- a/library/PostObject/Decorators/AbstractPostObjectDecorator.php +++ b/library/PostObject/Decorators/AbstractPostObjectDecorator.php @@ -2,6 +2,7 @@ namespace Municipio\PostObject\Decorators; +use Municipio\PostObject\Icon\IconInterface; use Municipio\PostObject\PostObjectInterface; use Municipio\PostObject\TermIcon\TermIconInterface; @@ -67,4 +68,12 @@ public function getTermIcon(?string $taxonomy = null): ?TermIconInterface { return $this->postObject->getTermIcon($taxonomy); } + + /** + * @inheritDoc + */ + public function getIcon(): ?IconInterface + { + return $this->postObject->getIcon(); + } } diff --git a/library/PostObject/Decorators/IconResolvingPostObject.php b/library/PostObject/Decorators/IconResolvingPostObject.php new file mode 100644 index 000000000..0703b6e5a --- /dev/null +++ b/library/PostObject/Decorators/IconResolvingPostObject.php @@ -0,0 +1,29 @@ +postObject = $inner; + } + + /** + * @inheritDoc + */ + public function getIcon(): ?IconInterface + { + return $this->iconResolver->resolve(); + } +} diff --git a/library/PostObject/Decorators/IconResolvingPostObject.test.php b/library/PostObject/Decorators/IconResolvingPostObject.test.php new file mode 100644 index 000000000..086cd76db --- /dev/null +++ b/library/PostObject/Decorators/IconResolvingPostObject.test.php @@ -0,0 +1,38 @@ +assertInstanceOf(IconResolvingPostObject::class, $decorator); + } + + /** + * @testdox getIcon() calls provided icon resolver + */ + public function testGetIconCallsProvidedIconResolver() + { + $icon = $this->createMock(IconInterface::class); + $iconResolver = $this->createMock(IconResolverInterface::class); + + $icon->method('getIcon')->willReturn('test-icon'); + $iconResolver->method('resolve')->willReturn($icon); + + $postObject = new IconResolvingPostObject(new PostObject(), $iconResolver); + + $this->assertEquals('test-icon', $postObject->getIcon()->getIcon()); + } +} diff --git a/library/PostObject/Icon/IconInterface.php b/library/PostObject/Icon/IconInterface.php new file mode 100644 index 000000000..853503ae6 --- /dev/null +++ b/library/PostObject/Icon/IconInterface.php @@ -0,0 +1,55 @@ +assertInstanceOf(NullIconResolver::class, new NullIconResolver()); + } + + /** + * @testdox resolve() returns null + */ + public function testResolveReturnsNull() + { + $resolver = new NullIconResolver(); + $this->assertNull($resolver->resolve()); + } +} diff --git a/library/PostObject/PostObject.php b/library/PostObject/PostObject.php index 11e63da4c..d5e569d47 100644 --- a/library/PostObject/PostObject.php +++ b/library/PostObject/PostObject.php @@ -2,6 +2,7 @@ namespace Municipio\PostObject; +use Municipio\PostObject\Icon\IconInterface; use Municipio\PostObject\PostObjectInterface; use Municipio\PostObject\TermIcon\TermIconInterface; @@ -65,4 +66,12 @@ public function getTermIcon(?string $taxonomy = null): ?TermIconInterface { return null; } + + /** + * @inheritDoc + */ + public function getIcon(): ?IconInterface + { + return null; + } } diff --git a/library/PostObject/PostObject.test.php b/library/PostObject/PostObject.test.php index a2da74fc6..e9030644a 100644 --- a/library/PostObject/PostObject.test.php +++ b/library/PostObject/PostObject.test.php @@ -71,4 +71,12 @@ public function testGetTermIconReturnsNull() { $this->assertNull($this->instance->getTermIcon()); } + + /** + * @testdox getIcon() returns null + */ + public function testGetIconReturnsNull() + { + $this->assertNull($this->instance->getIcon()); + } } diff --git a/library/PostObject/PostObjectInterface.php b/library/PostObject/PostObjectInterface.php index bb0f22555..181cb01e9 100644 --- a/library/PostObject/PostObjectInterface.php +++ b/library/PostObject/PostObjectInterface.php @@ -2,6 +2,7 @@ namespace Municipio\PostObject; +use Municipio\PostObject\Icon\IconInterface; use Municipio\PostObject\TermIcon\TermIconInterface; interface PostObjectInterface @@ -54,4 +55,11 @@ public function getTermIcon(?string $taxonomy = null): ?TermIconInterface; * Get the post type. */ public function getPostType(): string; + + /** + * Get the post object icon. + * + * @return IconInterface|null The post object icon or null if none is found. + */ + public function getIcon(): ?IconInterface; }