Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/Cms/Services/Post/Publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct( IPostRepository $postRepository )
*
* @param Post $post The post to publish
* @return Post
* @throws Exception if post is already published
* @throws Exception if post is already published or persistence fails
*/
public function publish( Post $post ): Post
{
Expand All @@ -41,7 +41,10 @@ public function publish( Post $post ): Post
$post->setStatus( Post::STATUS_PUBLISHED );
$post->setPublishedAt( new DateTimeImmutable() );

$this->_postRepository->update( $post );
if( !$this->_postRepository->update( $post ) )
{
throw new Exception( 'Failed to persist post changes' );
}

return $post;
}
Expand All @@ -51,7 +54,7 @@ public function publish( Post $post ): Post
*
* @param Post $post The post to unpublish
* @return Post
* @throws Exception if post is not published
* @throws Exception if post is not published or persistence fails
*/
public function unpublish( Post $post ): Post
{
Expand All @@ -64,7 +67,10 @@ public function unpublish( Post $post ): Post
$post->setStatus( Post::STATUS_DRAFT );
$post->setPublishedAt( null );

$this->_postRepository->update( $post );
if( !$this->_postRepository->update( $post ) )
{
throw new Exception( 'Failed to persist post changes' );
}

return $post;
}
Expand All @@ -75,7 +81,7 @@ public function unpublish( Post $post ): Post
* @param Post $post The post to schedule
* @param DateTimeImmutable $publishAt When to publish
* @return Post
* @throws Exception if scheduled date is in the past
* @throws Exception if scheduled date is in the past or persistence fails
*/
public function schedule( Post $post, DateTimeImmutable $publishAt ): Post
{
Expand All @@ -88,7 +94,10 @@ public function schedule( Post $post, DateTimeImmutable $publishAt ): Post
$post->setStatus( Post::STATUS_SCHEDULED );
$post->setPublishedAt( $publishAt );

$this->_postRepository->update( $post );
if( !$this->_postRepository->update( $post ) )
{
throw new Exception( 'Failed to persist post changes' );
}

return $post;
}
Expand Down
72 changes: 66 additions & 6 deletions tests/Cms/Services/Post/PublisherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function testPublishesDraftPost(): void
->with( $this->callback( function( Post $p ) {
return $p->getStatus() === Post::STATUS_PUBLISHED
&& $p->getPublishedAt() instanceof DateTimeImmutable;
} ) );
} ) )
->willReturn( true );

$result = $this->_publisher->publish( $post );

Expand Down Expand Up @@ -68,7 +69,8 @@ public function testUnpublishesPublishedPost(): void
->with( $this->callback( function( Post $p ) {
return $p->getStatus() === Post::STATUS_DRAFT
&& $p->getPublishedAt() === null;
} ) );
} ) )
->willReturn( true );

$result = $this->_publisher->unpublish( $post );

Expand Down Expand Up @@ -107,7 +109,8 @@ public function testSchedulesPostForFuturePublication(): void
return $p->getStatus() === Post::STATUS_SCHEDULED
&& $p->getPublishedAt() instanceof DateTimeImmutable
&& $p->getPublishedAt()->getTimestamp() === $futureDate->getTimestamp();
} ) );
} ) )
->willReturn( true );

$result = $this->_publisher->schedule( $post, $futureDate );

Expand Down Expand Up @@ -159,7 +162,8 @@ public function testReturnsUpdatedPostAfterPublishing(): void
$post->setStatus( Post::STATUS_DRAFT );

$this->_mockPostRepository
->method( 'update' );
->method( 'update' )
->willReturn( true );

$result = $this->_publisher->publish( $post );

Expand All @@ -174,7 +178,8 @@ public function testReturnsUpdatedPostAfterUnpublishing(): void
$post->setPublishedAt( new DateTimeImmutable() );

$this->_mockPostRepository
->method( 'update' );
->method( 'update' )
->willReturn( true );

$result = $this->_publisher->unpublish( $post );

Expand All @@ -190,10 +195,65 @@ public function testReturnsUpdatedPostAfterScheduling(): void
$futureDate = ( new DateTimeImmutable() )->modify( '+1 day' );

$this->_mockPostRepository
->method( 'update' );
->method( 'update' )
->willReturn( true );

$result = $this->_publisher->schedule( $post, $futureDate );

$this->assertSame( $post, $result );
}

public function testThrowsExceptionWhenPublishPersistenceFails(): void
{
$post = new Post();
$post->setId( 1 );
$post->setStatus( Post::STATUS_DRAFT );

$this->_mockPostRepository
->expects( $this->once() )
->method( 'update' )
->willReturn( false );

$this->expectException( \Exception::class );
$this->expectExceptionMessage( 'Failed to persist post changes' );

$this->_publisher->publish( $post );
}

public function testThrowsExceptionWhenUnpublishPersistenceFails(): void
{
$post = new Post();
$post->setId( 1 );
$post->setStatus( Post::STATUS_PUBLISHED );
$post->setPublishedAt( new DateTimeImmutable() );

$this->_mockPostRepository
->expects( $this->once() )
->method( 'update' )
->willReturn( false );

$this->expectException( \Exception::class );
$this->expectExceptionMessage( 'Failed to persist post changes' );

$this->_publisher->unpublish( $post );
}

public function testThrowsExceptionWhenSchedulePersistenceFails(): void
{
$post = new Post();
$post->setId( 1 );
$post->setStatus( Post::STATUS_DRAFT );

$futureDate = ( new DateTimeImmutable() )->modify( '+1 day' );

$this->_mockPostRepository
->expects( $this->once() )
->method( 'update' )
->willReturn( false );

$this->expectException( \Exception::class );
$this->expectExceptionMessage( 'Failed to persist post changes' );

$this->_publisher->schedule( $post, $futureDate );
}
}