Skip to content

Commit

Permalink
OXDEV-8778 Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
tkcreateit committed Nov 13, 2024
1 parent 926c6e7 commit 6d23b03
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 317 deletions.
3 changes: 2 additions & 1 deletion src/ProductVote/DataMapper/ProductVoteDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
namespace OxidEsales\ModuleTemplate\ProductVote\DataMapper;

use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVote;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVoteInterface;
use OxidEsales\ModuleTemplate\ProductVote\Exception\MapDataTypeException;

readonly class ProductVoteDataMapper implements ProductVoteDataMapperInterface
{
public function mapFromDbRow(array $data): ProductVote
public function mapFromDbRow(array $data): ProductVoteInterface
{
if (!isset($data['ProductId']) || !isset($data['UserId']) || !isset($data['Vote'])) {
throw new MapDataTypeException();
Expand Down
4 changes: 2 additions & 2 deletions src/ProductVote/DataMapper/ProductVoteDataMapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace OxidEsales\ModuleTemplate\ProductVote\DataMapper;

use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVote;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVoteInterface;

interface ProductVoteDataMapperInterface
{
public function mapFromDbRow(array $data): ProductVote;
public function mapFromDbRow(array $data): ProductVoteInterface;
}
3 changes: 2 additions & 1 deletion src/ProductVote/DataMapper/VoteResultDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
namespace OxidEsales\ModuleTemplate\ProductVote\DataMapper;

use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResult;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResultInterface;
use OxidEsales\ModuleTemplate\ProductVote\Exception\MapDataTypeException;

readonly class VoteResultDataMapper implements VoteResultDataMapperInterface
{
public function mapFromDbRow(array $data): VoteResult
public function mapFromDbRow(array $data): VoteResultInterface
{
if (!isset($data['ProductId']) || !isset($data['VoteUp']) || !isset($data['VoteDown'])) {
throw new MapDataTypeException();
Expand Down
4 changes: 2 additions & 2 deletions src/ProductVote/DataMapper/VoteResultDataMapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace OxidEsales\ModuleTemplate\ProductVote\DataMapper;

use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResult;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResultInterface;

interface VoteResultDataMapperInterface
{
public function mapFromDbRow(array $data): VoteResult;
public function mapFromDbRow(array $data): VoteResultInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,115 +24,89 @@ 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
->expects($this->never())
->method('setProductVote');
$voteServiceSpy
->expects($this->never())
->method('resetProductVote');

$sut = $this
->getMockBuilder(ArticleDetailsController::class)
->onlyMethods(['getService', 'getProduct', 'getUser'])
->getMock();
$sut
->method('getService')
->with(VoteServiceInterface::class)
->willReturn($voteServiceSpy);
$sut
->method('getUser')
->willReturn(null);
$sut
->method('getProduct')
->willReturn($this->getProductStub());
$voteServiceSpy->expects($this->never())->method('setProductVote');
$voteServiceSpy->expects($this->never())->method('resetProductVote');

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

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

$sut->voteUp();
$sut->voteDown();
$sut->resetVote();
}

#[Test]
public function voteUp(): void
public function voteUpForLoggedInUserCallsServiceMethod(): 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('setProductVote')
->with($this->getProductStub());
$voteServiceSpy->expects($this->once())->method('setProductVote')->with($productStub, $userStub, true);

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

$sut->voteUp();
}

#[Test]
public function voteDown(): void
public function voteDownForLoggedInUserCallsServiceMethod(): 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('setProductVote')
->with($this->getProductStub());
$voteServiceSpy->expects($this->once())->method('setProductVote')->with($productStub, $userStub, false);

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

$sut->voteDown();
}

#[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();
}

private function getProductStub(): Article
{
$productStub = $this->createStub(Article::class);
$productStub
->method('getId')
->willReturn(self::TEST_PRODUCT_ID);

return $productStub;
}

private function getUserStub(): User
{
$userStub = $this->createStub(User::class);
$userStub
->method('getId')
->willReturn(self::TEST_USER_ID);

return $userStub;
}

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 = $this->getMockBuilder(ArticleDetailsController::class)
->onlyMethods(['getService', 'getProduct', 'getUser'])->getMock();

$sut->method('getService')->with(VoteServiceInterface::class)->willReturn($voteServiceSpy);
$sut->method('getUser')->willReturn($user);
$sut->method('getProduct')->willReturn($product);

return $sut;
}
Expand Down
57 changes: 0 additions & 57 deletions tests/Integration/ProductVote/Dao/DaoTestTrait.php

This file was deleted.

Loading

0 comments on commit 6d23b03

Please sign in to comment.