From a0ad50c5b441b4985b76e74ca022901ef3a13855 Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Tue, 12 Nov 2024 17:49:53 +0200 Subject: [PATCH] OXDEV-8778 Test example Signed-off-by: Anton Fedurtsya --- .../ArticleDetailsControllerTest.php | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/Integration/ProductVote/Controller/ArticleDetailsControllerTest.php b/tests/Integration/ProductVote/Controller/ArticleDetailsControllerTest.php index eaed3a8..682fb1f 100644 --- a/tests/Integration/ProductVote/Controller/ArticleDetailsControllerTest.php +++ b/tests/Integration/ProductVote/Controller/ArticleDetailsControllerTest.php @@ -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 @@ -55,7 +55,7 @@ public function voteNotLoggedIn(): void } #[Test] - public function voteUp(): void + public function voteUpForLoggedInUserCallsServiceMethod(): void { $voteServiceSpy = $this->createMock(VoteServiceInterface::class); $voteServiceSpy @@ -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(); } @@ -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; }