Skip to content

Commit

Permalink
chore: fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 12, 2024
1 parent f4a8a3d commit 28b2406
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/Repository/BestAnswerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
54 changes: 25 additions & 29 deletions tests/unit/SaveBestAnswerToDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class SaveBestAnswerToDatabaseTest extends TestCase
{
/**
* @var m\MockInterface|SaveBestAnswerToDatabase
* @var SaveBestAnswerToDatabase
*/
protected $sut; // System Under Test

Expand All @@ -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()
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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);
}
Expand Down

0 comments on commit 28b2406

Please sign in to comment.