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 12, 2024
1 parent 2e9f009 commit 926c6e7
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/ProductVote/Dao/ProductVoteDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getProductVote(string $productId, string $userId): ?ProductVoteI
return null;
}

return $this->dataMapper->map($row);
return $this->dataMapper->mapFromDbRow($row);
}

public function setProductVote(ProductVoteInterface $vote): void
Expand Down
2 changes: 1 addition & 1 deletion src/ProductVote/Dao/VoteResultDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public function getProductVoteResult(string $productId): VoteResultInterface
if (!$row) {
return new VoteResult($productId, 0, 0);
}
return $this->dataMapper->map($row);
return $this->dataMapper->mapFromDbRow($row);
}
}
2 changes: 1 addition & 1 deletion src/ProductVote/DataMapper/ProductVoteDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

readonly class ProductVoteDataMapper implements ProductVoteDataMapperInterface
{
public function map(array $data): ProductVote
public function mapFromDbRow(array $data): ProductVote
{
if (!isset($data['ProductId']) || !isset($data['UserId']) || !isset($data['Vote'])) {
throw new MapDataTypeException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@

interface ProductVoteDataMapperInterface
{
public function map(array $data): ProductVote;
public function mapFromDbRow(array $data): ProductVote;
}
2 changes: 1 addition & 1 deletion src/ProductVote/DataMapper/VoteResultDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

readonly class VoteResultDataMapper implements VoteResultDataMapperInterface
{
public function map(array $data): VoteResult
public function mapFromDbRow(array $data): VoteResult
{
if (!isset($data['ProductId']) || !isset($data['VoteUp']) || !isset($data['VoteDown'])) {
throw new MapDataTypeException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@

interface VoteResultDataMapperInterface
{
public function map(array $data): VoteResult;
public function mapFromDbRow(array $data): VoteResult;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use OxidEsales\ModuleTemplate\ProductVote\Service\VoteServiceInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;

#[CoversClass(ArticleDetailsController::class)]
Expand Down Expand Up @@ -58,12 +57,12 @@ public function voteNotLoggedIn(): void
#[Test]
public function voteUp(): void
{
$voteServiceSpy = $this->getVoteServiceSpy(
'setProductVote',
$this->getProductStub(),
$this->getUserStub(),
true,
);
$voteServiceSpy = $this->createMock(VoteServiceInterface::class);
$voteServiceSpy
->expects($this->once())
->method('setProductVote')
->with($this->getProductStub());

$sut = $this->getSutMock($voteServiceSpy);

$sut->voteUp();
Expand All @@ -72,12 +71,12 @@ public function voteUp(): void
#[Test]
public function voteDown(): void
{
$voteServiceSpy = $this->getVoteServiceSpy(
'setProductVote',
$this->getProductStub(),
$this->getUserStub(),
false,
);
$voteServiceSpy = $this->createMock(VoteServiceInterface::class);
$voteServiceSpy
->expects($this->once())
->method('setProductVote')
->with($this->getProductStub());

$sut = $this->getSutMock($voteServiceSpy);

$sut->voteDown();
Expand All @@ -86,28 +85,18 @@ public function voteDown(): void
#[Test]
public function resetVote(): void
{
$voteServiceSpy = $this->getVoteServiceSpy(
'resetProductVote',
$this->getProductStub(),
$this->getUserStub(),
);
$voteServiceSpy = $this->createMock(VoteServiceInterface::class);
$voteServiceSpy
->expects($this->once())
->method('resetProductVote')
->with($this->getProductStub());

$sut = $this->getSutMock($voteServiceSpy);

$sut->resetVote();
}

private function getVoteServiceSpy(string $method, mixed ...$arguments): VoteServiceInterface
{
$daoSpy = $this->createMock(VoteServiceInterface::class);
$daoSpy
->expects($this->once())
->method($method)
->with(...$arguments);

return $daoSpy;
}

private function getProductStub(): Article|Stub
private function getProductStub(): Article
{
$productStub = $this->createStub(Article::class);
$productStub
Expand All @@ -117,7 +106,7 @@ private function getProductStub(): Article|Stub
return $productStub;
}

private function getUserStub(): User|Stub
private function getUserStub(): User
{
$userStub = $this->createStub(User::class);
$userStub
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/ProductVote/Dao/DaoTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait DaoTestTrait
protected const TEST_USER_ID = '_testuser';
protected const TEST_PRODUCT_ID = '_testproduct';

protected function getVoteQueryResult(): Result
protected function getProductVoteQueryResult(): Result
{
$queryBuilder = $this->get(QueryBuilderFactoryInterface::class)->create();
$queryBuilder
Expand All @@ -35,7 +35,7 @@ protected function getVoteQueryResult(): Result
return $result;
}

protected function executeInsertVoteQuery(bool $isVoteUp, string $userId = self::TEST_USER_ID): void
protected function executeInsertProductVoteQuery(bool $isVoteUp, string $userId = self::TEST_USER_ID): void
{
$queryBuilder = $this->get(QueryBuilderFactoryInterface::class)->create();
$queryBuilder
Expand Down
12 changes: 6 additions & 6 deletions tests/Integration/ProductVote/Dao/ProductVoteDaoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function noVoteForNoRecord(): void
public function getUpVote(bool $value): void
{
$isVoteUp = $value;
$this->executeInsertVoteQuery($isVoteUp);
$this->executeInsertProductVoteQuery($isVoteUp);

$sut = $this->get(ProductVoteDaoInterface::class);

Expand All @@ -58,7 +58,7 @@ public function setVote(bool $value): void
$sut = $this->get(ProductVoteDaoInterface::class);
$sut->setProductVote($vote);

$result = $this->getVoteQueryResult();
$result = $this->getProductVoteQueryResult();
$this->assertEquals(1, $result->rowCount());
$this->assertEquals(
[
Expand Down Expand Up @@ -88,7 +88,7 @@ public function replaceVote(): void
$sut->setProductVote($upVote);
$sut->setProductVote($downVote);

$result = $this->getVoteQueryResult();
$result = $this->getProductVoteQueryResult();
$this->assertEquals(1, $result->rowCount());
$this->assertEquals(
[
Expand All @@ -106,19 +106,19 @@ public function resetNonExistingVote(): void
$sut = $this->get(ProductVoteDaoInterface::class);
$sut->resetProductVote(self::TEST_PRODUCT_ID, self::TEST_USER_ID);

$result = $this->getVoteQueryResult();
$result = $this->getProductVoteQueryResult();
$this->assertEquals(0, $result->rowCount());
}

#[Test]
public function resetVote(): void
{
$this->executeInsertVoteQuery(true);
$this->executeInsertProductVoteQuery(true);

$sut = $this->get(ProductVoteDaoInterface::class);
$sut->resetProductVote(self::TEST_PRODUCT_ID, self::TEST_USER_ID);

$result = $this->getVoteQueryResult();
$result = $this->getProductVoteQueryResult();
$this->assertEquals(0, $result->rowCount());
}
}
16 changes: 8 additions & 8 deletions tests/Integration/ProductVote/Dao/ResultDaoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function calculateNoVotes(): void
#[Test]
public function calculateVoteResult(): void
{
$this->executeInsertVoteQuery(true, 'user_1');
$this->executeInsertProductVoteQuery(true, 'user_1');

$sut = $this->get(VoteResultDaoInterface::class);
$result = $sut->getProductVoteResult(self::TEST_PRODUCT_ID);
Expand All @@ -43,13 +43,13 @@ public function calculateVoteResult(): void
#[Test]
public function calculateVotesResult(): void
{
$this->executeInsertVoteQuery(true, 'user_1'); // 1/0
$this->executeInsertVoteQuery(false, 'user_2');// 1/1
$this->executeInsertVoteQuery(false, 'user_3');// 1/2
$this->executeInsertVoteQuery(false, 'user_4');// 1/3
$this->executeInsertVoteQuery(true, 'user_5'); // 2/3
$this->executeInsertVoteQuery(true, 'user_6'); // 3/3
$this->executeInsertVoteQuery(true, 'user_7'); // 4/3
$this->executeInsertProductVoteQuery(true, 'user_1'); // 1/0
$this->executeInsertProductVoteQuery(false, 'user_2');// 1/1
$this->executeInsertProductVoteQuery(false, 'user_3');// 1/2
$this->executeInsertProductVoteQuery(false, 'user_4');// 1/3
$this->executeInsertProductVoteQuery(true, 'user_5'); // 2/3
$this->executeInsertProductVoteQuery(true, 'user_6'); // 3/3
$this->executeInsertProductVoteQuery(true, 'user_7'); // 4/3

$sut = $this->get(VoteResultDaoInterface::class);
$result = $sut->getProductVoteResult(self::TEST_PRODUCT_ID);
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/ProductVote/Widget/ArticleDetailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function getSutMock(): ArticleDetails|MockObject
return $sut;
}

private function getProductStub(): Article|Stub
private function getProductStub(): Article
{
$productStub = $this->createStub(Article::class);
$productStub
Expand All @@ -88,7 +88,7 @@ private function getProductStub(): Article|Stub
return $productStub;
}

private function getUserStub(): User|Stub
private function getUserStub(): User
{
$userStub = $this->createStub(User::class);
$userStub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function mapMalformedData(array $data): void
$sut = new ProductVoteDataMapper();

$this->expectException(MapDataTypeException::class);
$sut->map($data);
$sut->mapFromDbRow($data);
}

public static function mapMalformedDataProvider(): Generator
Expand All @@ -47,7 +47,7 @@ public static function mapMalformedDataProvider(): Generator
public function mapData(ProductVote $expectedVote, array $data): void
{
$sut = new ProductVoteDataMapper();
$this->assertEquals($expectedVote, $sut->map($data));
$this->assertEquals($expectedVote, $sut->mapFromDbRow($data));
}

public static function mapDataProvider(): Generator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use PHPUnit\Framework\TestCase;

#[CoversClass(VoteResultDataMapper::class)]
final class ResultDataMapperTest extends TestCase
final class VoteResultDataMapperTest extends TestCase
{
#[Test]
#[DataProvider('mapMalformedDataProvider')]
Expand All @@ -28,7 +28,7 @@ public function mapMalformedData(array $data): void
$sut = new VoteResultDataMapper();

$this->expectException(MapDataTypeException::class);
$sut->map($data);
$sut->mapFromDbRow($data);
}

public static function mapMalformedDataProvider(): Generator
Expand All @@ -48,7 +48,7 @@ public static function mapMalformedDataProvider(): Generator
public function mapData(VoteResult $expectedResult, array $data): void
{
$sut = new VoteResultDataMapper();
$this->assertEquals($expectedResult, $sut->map($data));
$this->assertEquals($expectedResult, $sut->mapFromDbRow($data));
}

public static function mapDataProvider(): Generator
Expand Down

0 comments on commit 926c6e7

Please sign in to comment.