Skip to content

Commit

Permalink
OXDEV-8778 Test example
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 12, 2024
1 parent 926c6e7 commit a0ad50c
Showing 1 changed file with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class ArticleDetailsControllerTest extends TestCase
private const TEST_USER_ID = 'test_user_id';

#[Test]
public function voteNotLoggedIn(): void
public function ensureNotLoggedInDoesNotCallServiceMethods(): void
{
$voteServiceSpy = $this->createMock(VoteServiceInterface::class);
$voteServiceSpy
Expand Down Expand Up @@ -55,7 +55,7 @@ public function voteNotLoggedIn(): void
}

#[Test]
public function voteUp(): void
public function voteUpForLoggedInUserCallsServiceMethod(): void
{
$voteServiceSpy = $this->createMock(VoteServiceInterface::class);
$voteServiceSpy
Expand Down Expand Up @@ -83,15 +83,19 @@ public function voteDown(): void
}

#[Test]
public function resetVote(): void
public function resetVoteTriggersServiceWithExpectedProductAndUser(): void
{
$productStub = $this->createConfiguredStub(Article::class, ['getId' => self::TEST_PRODUCT_ID]);
$userStub = $this->createConfiguredStub(User::class, ['getId' => self::TEST_USER_ID]);

$voteServiceSpy = $this->createMock(VoteServiceInterface::class);
$voteServiceSpy
->expects($this->once())
->method('resetProductVote')
->with($this->getProductStub());
$voteServiceSpy->expects($this->once())->method('resetProductVote')->with($productStub, $userStub);

$sut = $this->getSutMock($voteServiceSpy);
$sut = $this->getSutMock(
voteServiceSpy: $voteServiceSpy,
user: $userStub,
product: $productStub
);

$sut->resetVote();
}
Expand All @@ -118,21 +122,21 @@ private function getUserStub(): User

private function getSutMock(
VoteServiceInterface $voteServiceSpy,
User $user = null,
Article $product = null,
): ArticleDetailsController {
$sut = $this
->getMockBuilder(ArticleDetailsController::class)
->onlyMethods(['getService', 'getProduct', 'getUser'])
->getMock();

$sut
->method('getService')
->with(VoteServiceInterface::class)
->willReturn($voteServiceSpy);
$sut
->method('getUser')
->willReturn($this->getUserStub());
$sut
->method('getProduct')
->willReturn($this->getProductStub());

$sut->method('getUser')->willReturn($user);
$sut->method('getProduct')->willReturn($product);

return $sut;
}
Expand Down

0 comments on commit a0ad50c

Please sign in to comment.