From b79f4f69481643d585b520eb0032cb10d2f6ce72 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 11 Nov 2025 03:12:23 +0000 Subject: [PATCH 1/3] Initial plan From b9f0e5d9b0d1d21c80b414ca0dfd7b0c4842a703 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 11 Nov 2025 03:15:44 +0000 Subject: [PATCH 2/3] Add error handling for repository persistence failures in Publisher service Co-authored-by: ljonesfl <1099983+ljonesfl@users.noreply.github.com> --- src/Cms/Services/Post/Publisher.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Cms/Services/Post/Publisher.php b/src/Cms/Services/Post/Publisher.php index fb89fc5..4fc2507 100644 --- a/src/Cms/Services/Post/Publisher.php +++ b/src/Cms/Services/Post/Publisher.php @@ -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 { @@ -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; } @@ -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 { @@ -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; } @@ -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 { @@ -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; } From 8a50ee3effc00d72efb0a88a94904accfdb2bac3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 11 Nov 2025 03:32:24 +0000 Subject: [PATCH 3/3] Fix PublisherTest mocks to return true for update() calls Co-authored-by: ljonesfl <1099983+ljonesfl@users.noreply.github.com> --- tests/Cms/Services/Post/PublisherTest.php | 72 +++++++++++++++++++++-- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/tests/Cms/Services/Post/PublisherTest.php b/tests/Cms/Services/Post/PublisherTest.php index c6b0c17..95a0962 100644 --- a/tests/Cms/Services/Post/PublisherTest.php +++ b/tests/Cms/Services/Post/PublisherTest.php @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); + } }