Skip to content

Commit

Permalink
feat: return timestamps from PostObjectFromWpPost
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink committed Jan 23, 2025
1 parent e65ad48 commit 4a0e2e1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions library/PostObject/Decorators/PostObjectFromWpPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ public function getBlogId(): int
*/
public function getPublishedTime(bool $gmt = false): int
{
return $this->postObject->getPublishedTime($gmt);
return strtotime($gmt ? $this->wpPost->post_date_gmt : $this->wpPost->post_date);
}

/**
* @inheritDoc
*/
public function getModifiedTime(bool $gmt = false): int
{
return $this->postObject->getModifiedTime($gmt);
return strtotime($gmt ? $this->wpPost->post_modified_gmt : $this->wpPost->post_modified);
}
}
48 changes: 48 additions & 0 deletions library/PostObject/Decorators/PostObjectFromWpPost.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,52 @@ public function testGetPostTypeReturnsPostType()

$this->assertEquals('post', $instance->getPostType());
}

/**
* @testdox getPublishedTime returns timestamp from the WP_Post->post_date
*/
public function testGetPublishedTimeReturnsTimestampFromTheWpPostPostDate()
{
$dateTimeString = date('Y-m-d H:i:s', $now = time());
$wpPost = WpMockFactory::createWpPost(['post_date' => $dateTimeString]);
$instance = new PostObjectFromWpPost(new PostObject(new FakeWpService()), $wpPost, new FakeWpService());

$this->assertEquals($now, $instance->getPublishedTime());
}

/**
* @testdox getPublishedTime returns gmt timestamp from the WP_Post->post_date_gmt
*/
public function testGetPublishedTimeReturnsGmtTimestampFromTheWpPostPostDateGmt()
{
$dateTimeString = date('Y-m-d H:i:s', $now = time());
$wpPost = WpMockFactory::createWpPost(['post_date_gmt' => $dateTimeString]);
$instance = new PostObjectFromWpPost(new PostObject(new FakeWpService()), $wpPost, new FakeWpService());

$this->assertEquals($now, $instance->getPublishedTime(true));
}

/**
* @testdox getModifiedTime returns timestamp from the WP_Post->post_modified
*/
public function testGetModifiedTimeReturnsTimestampFromTheWpPostPostModified()
{
$dateTimeString = date('Y-m-d H:i:s', $now = time());
$wpPost = WpMockFactory::createWpPost(['post_modified' => $dateTimeString]);
$instance = new PostObjectFromWpPost(new PostObject(new FakeWpService()), $wpPost, new FakeWpService());

$this->assertEquals($now, $instance->getModifiedTime());
}

/**
* @testdox getModifiedTime returns gmt timestamp from the WP_Post->post_modified_gmt
*/
public function testGetModifiedTimeReturnsGmtTimestampFromTheWpPostPostModifiedGmt()
{
$dateTimeString = date('Y-m-d H:i:s', $now = time());
$wpPost = WpMockFactory::createWpPost(['post_modified_gmt' => $dateTimeString]);
$instance = new PostObjectFromWpPost(new PostObject(new FakeWpService()), $wpPost, new FakeWpService());

$this->assertEquals($now, $instance->getModifiedTime(true));
}
}

0 comments on commit 4a0e2e1

Please sign in to comment.