-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
252 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/** | ||
* Vote entity class. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* @package phpMyFAQ | ||
* @author Thorsten Rinne <thorsten@phpmyfaq.de> | ||
* @copyright 2024 phpMyFAQ Team | ||
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 | ||
* @link https://www.phpmyfaq.de | ||
* @since 2024-03-29 | ||
*/ | ||
|
||
namespace phpMyFAQ\Entity; | ||
|
||
use DateTime; | ||
|
||
class Vote | ||
{ | ||
private int $id; | ||
|
||
private int $faqId; | ||
|
||
private int $vote; | ||
|
||
private int $users; | ||
|
||
private DateTime $createdAt; | ||
|
||
private string $ip; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
public function setId(int $id): Vote | ||
{ | ||
$this->id = $id; | ||
return $this; | ||
} | ||
public function getFaqId(): int | ||
{ | ||
return $this->faqId; | ||
} | ||
public function setFaqId(int $faqId): Vote | ||
{ | ||
$this->faqId = $faqId; | ||
return $this; | ||
} | ||
public function getVote(): int | ||
{ | ||
return $this->vote; | ||
} | ||
public function setVote(int $vote): Vote | ||
{ | ||
$this->vote = $vote; | ||
return $this; | ||
} | ||
public function getUsers(): int | ||
{ | ||
return $this->users; | ||
} | ||
public function setUsers(int $users): Vote | ||
{ | ||
$this->users = $users; | ||
return $this; | ||
} | ||
public function getCreatedAt(): DateTime | ||
{ | ||
return $this->createdAt; | ||
} | ||
public function setCreatedAt(DateTime $createdAt): Vote | ||
{ | ||
$this->createdAt = $createdAt; | ||
return $this; | ||
} | ||
public function getIp(): string | ||
{ | ||
return $this->ip; | ||
} | ||
public function setIp(string $ip): Vote | ||
{ | ||
$this->ip = $ip; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace phpMyFAQ; | ||
|
||
use phpMyFAQ\Core\Exception; | ||
use phpMyFAQ\Database\Sqlite3; | ||
use phpMyFAQ\Entity\Vote; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RatingTest extends TestCase | ||
{ | ||
private Sqlite3 $dbHandle; | ||
private Rating $rating; | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Translation::create() | ||
->setLanguagesDir(PMF_TRANSLATION_DIR) | ||
->setDefaultLanguage('en') | ||
->setCurrentLanguage('en') | ||
->setMultiByteLanguage(); | ||
|
||
$this->dbHandle = new Sqlite3(); | ||
$this->dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); | ||
$configuration = new Configuration($this->dbHandle); | ||
$language = new Language($configuration); | ||
$language->setLanguage(false, 'en'); | ||
$configuration->setLanguage($language); | ||
|
||
$this->rating = new Rating($configuration); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->dbHandle->query('DELETE FROM faqvoting'); | ||
} | ||
|
||
public function testCreate(): void | ||
{ | ||
$votingData = new Vote(); | ||
$votingData | ||
->setFaqId(1) | ||
->setVote(5) | ||
->setIp('127.0.0.1'); | ||
|
||
$this->assertTrue($this->rating->create($votingData)); | ||
} | ||
|
||
public function testGet(): void | ||
{ | ||
$votingData = new Vote(); | ||
$votingData | ||
->setFaqId(1) | ||
->setVote(5) | ||
->setIp('127.0.0.1'); | ||
|
||
$this->rating->create($votingData); | ||
|
||
$this->assertEquals(' <span data-rating="5">5</span> (1 Vote)', $this->rating->get(1)); | ||
} | ||
|
||
public function testCheck(): void | ||
{ | ||
$votingData = new Vote(); | ||
$votingData | ||
->setFaqId(1) | ||
->setVote(5) | ||
->setIp('127.0.0.1'); | ||
|
||
$this->rating->create($votingData); | ||
|
||
$this->assertFalse($this->rating->check(1, '127.0.0.1')); | ||
} | ||
|
||
public function testGetNumberOfVotings(): void | ||
{ | ||
$votingData = new Vote(); | ||
$votingData | ||
->setFaqId(1) | ||
->setVote(5) | ||
->setIp('127.0.0.1'); | ||
|
||
$this->rating->create($votingData); | ||
|
||
$votingData = new Vote(); | ||
$votingData | ||
->setFaqId(1) | ||
->setVote(4) | ||
->setIp('127.0.0.2'); | ||
|
||
$this->rating->update($votingData); | ||
|
||
$this->assertEquals(2, $this->rating->getNumberOfVotings(1)); | ||
} | ||
|
||
public function testUpdate(): void | ||
{ | ||
$votingData = new Vote(); | ||
$votingData | ||
->setFaqId(1) | ||
->setVote(5) | ||
->setIp('127.0.0.1'); | ||
|
||
$this->rating->create($votingData); | ||
|
||
$votingData = new Vote(); | ||
$votingData | ||
->setFaqId(1) | ||
->setVote(4) | ||
->setIp('127.0.0.2'); | ||
|
||
$this->assertTrue($this->rating->update($votingData)); | ||
} | ||
|
||
public function testDeleteAll(): void | ||
{ | ||
$votingData = new Vote(); | ||
$votingData->setFaqId(1)->setVote(5)->setIp('127.0.0.1'); | ||
|
||
$this->rating->create($votingData); | ||
|
||
$this->assertTrue($this->rating->deleteAll()); | ||
$this->assertEquals(' <span data-rating="0">0</span> (0 Votes)', $this->rating->get(1)); | ||
} | ||
} |