-
Notifications
You must be signed in to change notification settings - Fork 0
Chore/434 parent title interface #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
74cd229
c530e65
8d05f38
6f29380
0ac7454
08dcabc
6ebabea
9001f02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
|
|
||
| describe(\LongReadPlugin\MultipageParentTitle::class, function () { | ||
| beforeEach(function () { | ||
| $this->parentTitle = new \LongReadPlugin\MultipageParentTitle(); | ||
| }); | ||
| it('implements the ParentTitleRetrieverInterface', function () { | ||
| expect($this->parentTitle)->toBeAnInstanceOf(\LongReadPlugin\ParentTitleRetrieverInterface::class); | ||
| }); | ||
|
|
||
| describe('->get()', function () { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be useful to have some
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok - have added something in |
||
| it('returns null when post has no ancestors', function () { | ||
| global $post; | ||
| $post = (object) [ | ||
| 'ID' => 123 | ||
| ]; | ||
|
|
||
| allow('get_post_ancestors')->toBeCalled()->andReturn([]); | ||
|
|
||
| $result = $this->parentTitle->get(); | ||
|
|
||
| expect($result)->toBeNull(); | ||
| }); | ||
|
|
||
| it('returns the top-level ancestor title', function () { | ||
| global $post; | ||
| $post = (object) [ | ||
| 'ID' => 789 | ||
| ]; | ||
|
|
||
| allow('get_post_ancestors')->toBeCalled()->andReturn([456, 123]); | ||
| allow('get_the_title')->toBeCalled()->andReturn('Top Level Post'); | ||
| expect('get_the_title')->toBeCalled()->with(123); | ||
|
|
||
| $result = $this->parentTitle->get(); | ||
| expect($result)->toEqual('Top Level Post'); | ||
| }); | ||
|
|
||
| it('returns null when get_the_title returns false', function () { | ||
| global $post; | ||
| $post = (object) [ | ||
| 'ID' => 789 | ||
| ]; | ||
|
|
||
| allow('get_post_ancestors')->toBeCalled()->andReturn([456, 123]); | ||
| allow('get_the_title')->toBeCalled()->andReturn(false); | ||
|
|
||
| $result = $this->parentTitle->get(); | ||
|
|
||
| expect($result)->toBeNull(); | ||
| }); | ||
|
|
||
| it('handles single-level hierarchy', function () { | ||
| global $post; | ||
| $post = (object) [ | ||
| 'ID' => 456 | ||
| ]; | ||
|
|
||
| allow('get_post_ancestors')->toBeCalled()->andReturn([123]); | ||
| allow('get_the_title')->toBeCalled()->andReturn('Parent Post'); | ||
|
|
||
| $result = $this->parentTitle->get(); | ||
|
|
||
| expect($result)->toEqual('Parent Post'); | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,29 @@ | ||
| <?php | ||
|
|
||
| describe(\LongReadPlugin\ParentTitle::class, function () { | ||
| describe('::get', function () { | ||
| context('there is no post parent', function () { | ||
| it('returns null', function () { | ||
| global $post; | ||
| $post = new stdClass(); | ||
| allow('get_post_ancestors')->toBeCalled()->andReturn([]); | ||
| $result = \LongReadPlugin\ParentTitle::get(); | ||
| expect($result)->toEqual(null); | ||
| }); | ||
| }); | ||
| describe('::get()', function () { | ||
| it('calls the getter instance method and returns its result', function () { | ||
| $mockGetter = \Kahlan\Plugin\Double::instance([ | ||
| 'implements' => \LongReadPlugin\ParentTitleRetrieverInterface::class | ||
| ]); | ||
| allow($mockGetter)->toReceive('get')->andReturn('Parent Title'); | ||
|
|
||
| new \LongReadPlugin\ParentTitle($mockGetter); | ||
| $result = \LongReadPlugin\ParentTitle::get(); | ||
|
|
||
| context('there is only one post ancestors', function () { | ||
| it('returns the title of that parent', function () { | ||
| global $post; | ||
| $post = new stdClass(); | ||
| allow('get_post_ancestors')->toBeCalled()->andReturn([123]); | ||
| allow('get_the_title')->toBeCalled()->andReturn('The parent title'); | ||
| expect('get_the_title')->toBeCalled()->once()->with(123); | ||
| $result = \LongReadPlugin\ParentTitle::get(); | ||
| expect($result)->toEqual('The parent title'); | ||
| }); | ||
| expect($result)->toEqual('Parent Title'); | ||
| }); | ||
|
|
||
| context('there is only multiple post ancestors', function () { | ||
| it('returns the title of the top level post ancestor', function () { | ||
| global $post; | ||
| $post = new stdClass(); | ||
| allow('get_post_ancestors')->toBeCalled()->andReturn([123, 456, 789]); | ||
| allow('get_the_title')->toBeCalled()->andReturn('The top ancestor title'); | ||
| expect('get_the_title')->toBeCalled()->once()->with(789); | ||
| $result = \LongReadPlugin\ParentTitle::get(); | ||
| expect($result)->toEqual('The top ancestor title'); | ||
| }); | ||
| it('returns null when getter returns null', function () { | ||
| $mockGetter = \Kahlan\Plugin\Double::instance([ | ||
| 'implements' => \LongReadPlugin\ParentTitleRetrieverInterface::class | ||
| ]); | ||
| allow($mockGetter)->toReceive('get')->andReturn(null); | ||
|
|
||
| new \LongReadPlugin\ParentTitle($mockGetter); | ||
| $result = \LongReadPlugin\ParentTitle::get(); | ||
|
|
||
| expect($result)->toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| namespace LongReadPlugin; | ||
|
|
||
| class MultipageParentTitle implements ParentTitleRetrieverInterface | ||
| { | ||
| public function get(): ?string | ||
| { | ||
| global $post; | ||
| $ancestors = get_post_ancestors($post); | ||
| $topLevelAncestor = array_pop($ancestors); | ||
| return $topLevelAncestor ? get_the_title($topLevelAncestor) ?: null : null; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
|
|
||
| namespace LongReadPlugin; | ||
|
|
||
| interface ParentTitleRetrieverInterface | ||
| { | ||
| public function get(): ?string; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.