From 28b240609b146c93f86f18b8d46200388621e475 Mon Sep 17 00:00:00 2001 From: IanM Date: Tue, 12 Nov 2024 08:58:32 +0000 Subject: [PATCH] chore: fix unit test --- src/Repository/BestAnswerRepository.php | 4 +- tests/unit/SaveBestAnswerToDatabaseTest.php | 54 ++++++++++----------- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/Repository/BestAnswerRepository.php b/src/Repository/BestAnswerRepository.php index 2b6d385..1195e34 100644 --- a/src/Repository/BestAnswerRepository.php +++ b/src/Repository/BestAnswerRepository.php @@ -125,7 +125,7 @@ public function calculateBestAnswersForUser(User $user): int return $count; } - protected function removeBestAnswer(Discussion $discussion, User $actor): void + public function removeBestAnswer(Discussion $discussion, User $actor): void { if (!$this->canRemoveBestAnswer($actor, $discussion)) { throw new PermissionDeniedException(); @@ -151,7 +151,7 @@ protected function removeBestAnswer(Discussion $discussion, User $actor): void }); } - protected function setBestAnswer(Discussion $discussion, User $actor, int $id): void + public function setBestAnswer(Discussion $discussion, User $actor, int $id): void { /** @var Post|null $post */ $post = $discussion->posts()->find($id); diff --git a/tests/unit/SaveBestAnswerToDatabaseTest.php b/tests/unit/SaveBestAnswerToDatabaseTest.php index badc363..7ead57b 100644 --- a/tests/unit/SaveBestAnswerToDatabaseTest.php +++ b/tests/unit/SaveBestAnswerToDatabaseTest.php @@ -24,7 +24,7 @@ class SaveBestAnswerToDatabaseTest extends TestCase { /** - * @var m\MockInterface|SaveBestAnswerToDatabase + * @var SaveBestAnswerToDatabase */ protected $sut; // System Under Test @@ -34,22 +34,20 @@ public function tearDown(): void parent::tearDown(); } - // testing the `handle()` method - public function testHandle_WhenKeyIsMissing_ReturnsWithoutAction() { $event = m::mock(Saving::class); $event->data = []; - $this->sut = m::mock(SaveBestAnswerToDatabase::class.'[removeBestAnswer,setBestAnswer]', [ - m::mock(NotificationSyncer::class), - m::mock(BestAnswerRepository::class), - ])->shouldAllowMockingProtectedMethods(); + $notifications = m::mock(NotificationSyncer::class); + $repository = m::mock(BestAnswerRepository::class); + + $this->sut = new SaveBestAnswerToDatabase($notifications, $repository); - $this->sut->shouldNotReceive('removeBestAnswer'); - $this->sut->shouldNotReceive('setBestAnswer'); + $result = $this->sut->handle($event); - $this->sut->handle($event); + // Assert that no actions were taken + $this->assertNull($result); } public function testHandle_DiscussionDoesNotExistOrMatches_ReturnsWithoutAction() @@ -62,18 +60,18 @@ public function testHandle_DiscussionDoesNotExistOrMatches_ReturnsWithoutAction( $event->discussion->exists = false; $event->discussion->best_answer_post_id = 1; - $this->sut = m::mock(SaveBestAnswerToDatabase::class.'[removeBestAnswer,setBestAnswer]', [ - m::mock(NotificationSyncer::class), - m::mock(BestAnswerRepository::class), - ])->shouldAllowMockingProtectedMethods(); + $notifications = m::mock(NotificationSyncer::class); + $repository = m::mock(BestAnswerRepository::class); - $this->sut->shouldNotReceive('removeBestAnswer'); - $this->sut->shouldNotReceive('setBestAnswer'); + $this->sut = new SaveBestAnswerToDatabase($notifications, $repository); - $this->sut->handle($event); + $result = $this->sut->handle($event); + + // Assert that no actions were taken + $this->assertNull($result); } - public function testHandle_IdIsZero_CallsRemoveBestAnswer() + public function testHandle_IdIsZero_CallsHandleRemoveBestAnswer() { $event = m::mock(Saving::class); $event->data = ['attributes.bestAnswerPostId' => 0]; @@ -86,17 +84,16 @@ public function testHandle_IdIsZero_CallsRemoveBestAnswer() $notifications = m::mock(NotificationSyncer::class); $notifications->shouldReceive('delete')->with(m::type(SelectBestAnswerBlueprint::class))->once(); - $this->sut = m::mock(SaveBestAnswerToDatabase::class.'[removeBestAnswer]', [ - $notifications, - m::mock(BestAnswerRepository::class), - ])->shouldAllowMockingProtectedMethods(); + $repository = m::mock(BestAnswerRepository::class); + $repository->shouldReceive('canRemoveBestAnswer')->with($event->actor, $event->discussion)->andReturn(true); + $repository->shouldReceive('removeBestAnswer')->with($event->discussion, $event->actor, 0)->once(); - $this->sut->shouldReceive('removeBestAnswer')->once(); + $this->sut = new SaveBestAnswerToDatabase($notifications, $repository); $this->sut->handle($event); } - public function testHandle_IdIsNotZero_CallsSetBestAnswer() + public function testHandle_IdIsNotZero_CallsHandleSetBestAnswer() { $event = m::mock(Saving::class); $event->data = ['attributes.bestAnswerPostId' => 2]; @@ -109,12 +106,11 @@ public function testHandle_IdIsNotZero_CallsSetBestAnswer() $notifications = m::mock(NotificationSyncer::class); $notifications->shouldReceive('delete')->with(m::type(SelectBestAnswerBlueprint::class))->once(); - $this->sut = m::mock(SaveBestAnswerToDatabase::class.'[setBestAnswer]', [ - $notifications, - m::mock(BestAnswerRepository::class), - ])->shouldAllowMockingProtectedMethods(); + $repository = m::mock(BestAnswerRepository::class); + $repository->shouldReceive('canSelectPostAsBestAnswer')->with($event->actor, m::type(Discussion::class))->andReturn(true); + $repository->shouldReceive('setBestAnswer')->with($event->discussion, $event->actor, 2)->once(); - $this->sut->shouldReceive('setBestAnswer')->once(); + $this->sut = new SaveBestAnswerToDatabase($notifications, $repository); $this->sut->handle($event); }