From dd8e401dbfaa0a4b1dfa2bdecb2ad4aad50b704c Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 12 Feb 2019 19:03:20 +0700 Subject: [PATCH] NEW Add tests for blog archive links and tidy up code re-use a little --- src/Model/BlogPost.php | 16 ++++------------ tests/BlogPostTest.php | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/Model/BlogPost.php b/src/Model/BlogPost.php index ad071d1..758d004 100644 --- a/src/Model/BlogPost.php +++ b/src/Model/BlogPost.php @@ -609,18 +609,15 @@ class BlogPost extends Page * Returns a monthly archive link for the current blog post. * * @param string $type - * * @return string */ public function getMonthlyArchiveLink($type = 'day') { - /** - * @var DBDatetime $date - */ + /** @var DBDatetime $date */ $date = $this->dbObject('PublishDate'); - if ($type != 'year') { - if ($type == 'day') { + if ($type !== 'year') { + if ($type === 'day') { return Controller::join_links( $this->Parent()->Link('archive'), $date->format('Y'), @@ -642,12 +639,7 @@ class BlogPost extends Page */ public function getYearlyArchiveLink() { - /** - * @var DBDatetime $date - */ - $date = $this->dbObject('PublishDate'); - - return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y')); + return $this->getMonthlyArchiveLink('year'); } /** diff --git a/tests/BlogPostTest.php b/tests/BlogPostTest.php index 899bad5..4216af2 100644 --- a/tests/BlogPostTest.php +++ b/tests/BlogPostTest.php @@ -146,4 +146,42 @@ class BlogPostTest extends SapphireTest $this->expectException(\InvalidArgumentException::class); $blogPost->MinutesToRead('not-a-number'); } + + /** + * @param string $type + * @param string $expected + * @dataProvider monthlyArchiveLinkProvider + * @group wip + */ + public function testGetMonthlyArchiveLink($type, $expected) + { + /** @var BlogPost $blogPost */ + $blogPost = $this->objFromFixture(BlogPost::class, 'FirstBlogPost'); + + $archiveLink = $blogPost->getMonthlyArchiveLink($type); + $this->assertContains('archive/', $archiveLink); + $this->assertContains($expected, $archiveLink); + } + + /** + * @return array[] + */ + public function monthlyArchiveLinkProvider() + { + return [ + ['day', '/2013/10/1'], + ['month', '/2013/10'], + ['year', '/2013'], + ]; + } + + public function testGetYearlyArchiveLink() + { + /** @var BlogPost $blogPost */ + $blogPost = $this->objFromFixture(BlogPost::class, 'FirstBlogPost'); + + $archiveLink = $blogPost->getYearlyArchiveLink(); + $this->assertContains('archive/', $archiveLink); + $this->assertContains('/2013', $archiveLink); + } }