Skip to content

Commit

Permalink
OXDEV-8778 Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tkcreateit committed Nov 14, 2024
1 parent 6d23b03 commit 181e752
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@
#[CoversClass(ArticleDetailsController::class)]
final class ArticleDetailsControllerTest extends TestCase
{
private const TEST_PRODUCT_ID = 'test_product_id';
private const TEST_USER_ID = 'test_user_id';

#[Test]
public function ensureNotLoggedInDoesNotCallServiceMethods(): void
{
$voteServiceSpy = $this->createMock(VoteServiceInterface::class);
$voteServiceSpy->expects($this->never())->method('setProductVote');
$voteServiceSpy->expects($this->never())->method('resetProductVote');

$productStub = $this->createConfiguredStub(Article::class, ['getId' => self::TEST_PRODUCT_ID]);
$productStub = $this->createStub(Article::class);

$sut = $this->getSutMock(
voteServiceSpy: $voteServiceSpy,
Expand All @@ -45,8 +42,8 @@ public function ensureNotLoggedInDoesNotCallServiceMethods(): void
#[Test]
public function voteUpForLoggedInUserCallsServiceMethod(): void
{
$productStub = $this->createConfiguredStub(Article::class, ['getId' => self::TEST_PRODUCT_ID]);
$userStub = $this->createConfiguredStub(User::class, ['getId' => self::TEST_USER_ID]);
$productStub = $this->createStub(Article::class);
$userStub = $this->createStub(User::class);

$voteServiceSpy = $this->createMock(VoteServiceInterface::class);
$voteServiceSpy->expects($this->once())->method('setProductVote')->with($productStub, $userStub, true);
Expand All @@ -63,8 +60,8 @@ public function voteUpForLoggedInUserCallsServiceMethod(): void
#[Test]
public function voteDownForLoggedInUserCallsServiceMethod(): void
{
$productStub = $this->createConfiguredStub(Article::class, ['getId' => self::TEST_PRODUCT_ID]);
$userStub = $this->createConfiguredStub(User::class, ['getId' => self::TEST_USER_ID]);
$productStub = $this->createStub(Article::class);
$userStub = $this->createStub(User::class);

$voteServiceSpy = $this->createMock(VoteServiceInterface::class);
$voteServiceSpy->expects($this->once())->method('setProductVote')->with($productStub, $userStub, false);
Expand All @@ -81,8 +78,8 @@ public function voteDownForLoggedInUserCallsServiceMethod(): void
#[Test]
public function resetVoteTriggersServiceWithExpectedProductAndUser(): void
{
$productStub = $this->createConfiguredStub(Article::class, ['getId' => self::TEST_PRODUCT_ID]);
$userStub = $this->createConfiguredStub(User::class, ['getId' => self::TEST_USER_ID]);
$productStub = $this->createStub(Article::class);
$userStub = $this->createStub(User::class);

$voteServiceSpy = $this->createMock(VoteServiceInterface::class);
$voteServiceSpy->expects($this->once())->method('resetProductVote')->with($productStub, $userStub);
Expand Down
9 changes: 5 additions & 4 deletions tests/Integration/ProductVote/Dao/ProductVoteDaoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ public function setAndGetVoteGivesTheSameVote(bool $isVoteUp): void
$vote = new ProductVote(self::TEST_PRODUCT_ID, self::TEST_USER_ID, $isVoteUp);
$sut->setProductVote($vote);

$vote = $sut->getProductVote(self::TEST_PRODUCT_ID, self::TEST_USER_ID);
$daoVote = $sut->getProductVote(self::TEST_PRODUCT_ID, self::TEST_USER_ID);
$this->assertNotSame($vote, $daoVote);

$this->assertEquals(self::TEST_PRODUCT_ID, $vote->getProductId());
$this->assertEquals(self::TEST_USER_ID, $vote->getUserId());
$this->assertEquals($isVoteUp, $vote->isVoteUp());
$this->assertEquals(self::TEST_PRODUCT_ID, $daoVote->getProductId());
$this->assertEquals(self::TEST_USER_ID, $daoVote->getUserId());
$this->assertEquals($isVoteUp, $daoVote->isVoteUp());
}

public static function isVoteUpProvider(): array
Expand Down
47 changes: 21 additions & 26 deletions tests/Integration/ProductVote/Widget/ArticleDetailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

use OxidEsales\Eshop\Application\Model\Article;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVote;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResult;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVoteInterface;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResultInterface;
use OxidEsales\ModuleTemplate\ProductVote\Service\VoteServiceInterface;
use OxidEsales\ModuleTemplate\ProductVote\Widget\ArticleDetails;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -22,41 +22,46 @@
#[CoversClass(ArticleDetails::class)]
final class ArticleDetailsTest extends TestCase
{
private const TEST_PRODUCT_ID = 'test_product_id';
private const TEST_USER_ID = 'test_user_id';

#[Test]
public function prepareDataForNotLoggedInUserGivesPartialData(): void
{
$sut = $this->getSutMock(user: null);
$voteResultStub = $this->createStub(VoteResultInterface::class);

$sut = $this->getSutMock(voteResult: $voteResultStub);
$sut->oemtPrepareVoteData();

$viewData = $sut->getViewData();
$this->assertEquals($this->getVoteResultDataType(), $viewData['productVoteResult']);
$this->assertEquals($voteResultStub, $viewData['productVoteResult']);
$this->assertArrayNotHasKey('productVote', $viewData);
}

#[Test]
public function prepareDataForLoggedInUserGivesFullData(): void
{
$voteResultStub = $this->createStub(VoteResultInterface::class);
$productVoteStub = $this->createStub(ProductVoteInterface::class);

$sut = $this->getSutMock(
user: $this->createConfiguredStub(user::class, ['getId' => self::TEST_USER_ID])
voteResult: $voteResultStub,
productVote: $productVoteStub,
user: $this->createStub(user::class)
);

$sut->oemtPrepareVoteData();

$viewData = $sut->getViewData();
$this->assertEquals($this->getVoteResultDataType(), $viewData['productVoteResult']);
$this->assertEquals($this->getProductVoteDataType(), $viewData['productVote']);
$this->assertEquals($voteResultStub, $viewData['productVoteResult']);
$this->assertEquals($productVoteStub, $viewData['productVote']);
}

private function getSutMock(?User $user): ArticleDetails
{
$productStub = $this->createConfiguredStub(article::class, ['getId' => self::TEST_PRODUCT_ID]);
private function getSutMock(
VoteResultInterface $voteResult,
?ProductVoteInterface $productVote = null,
?User $user = null,
): ArticleDetails {
$productStub = $this->createStub(Article::class);
$voteServiceStub = $this->createConfiguredStub(VoteServiceInterface::class, [
'getProductVote' => $this->getProductVoteDataType(),
'getProductVoteResult' => $this->getVoteResultDataType(),
'getProductVote' => $productVote,
'getProductVoteResult' => $voteResult,
]);

$sut = $this->getMockBuilder(ArticleDetails::class)
Expand All @@ -67,14 +72,4 @@ private function getSutMock(?User $user): ArticleDetails

return $sut;
}

private function getProductVoteDataType(): ProductVote
{
return new ProductVote(self::TEST_PRODUCT_ID, self::TEST_USER_ID, true);
}

private function getVoteResultDataType(): VoteResult
{
return new VoteResult(self::TEST_PRODUCT_ID, 3, 2);
}
}

0 comments on commit 181e752

Please sign in to comment.