2014-10-05 14:54:21 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Dao;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Dao\ScoreDao;
|
|
|
|
use Szurubooru\Entities\Post;
|
|
|
|
use Szurubooru\Entities\Score;
|
|
|
|
use Szurubooru\Entities\User;
|
|
|
|
use Szurubooru\Services\TimeService;
|
|
|
|
use Szurubooru\Tests\AbstractDatabaseTestCase;
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
final class ScoreDaoTest extends AbstractDatabaseTestCase
|
2014-10-05 14:54:21 +02:00
|
|
|
{
|
|
|
|
private $timeServiceMock;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->timeServiceMock = $this->mock(TimeService::class);
|
2014-10-05 14:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSaving()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$user = new User(1);
|
2014-10-05 14:54:21 +02:00
|
|
|
$user->setName('olivia');
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$post = new Post(2);
|
2014-10-05 14:54:21 +02:00
|
|
|
$post->setName('sword');
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$score = new Score();
|
2014-10-05 15:15:03 +02:00
|
|
|
$score->setUserId($user->getId());
|
|
|
|
$score->setPostId($post->getId());
|
2014-10-05 14:54:21 +02:00
|
|
|
$score->setTime(date('c'));
|
|
|
|
$score->setScore(1);
|
|
|
|
$scoreDao = $this->getScoreDao();
|
|
|
|
$scoreDao->save($score);
|
|
|
|
|
|
|
|
$savedScore = $scoreDao->findById($score->getId());
|
|
|
|
$this->assertEquals(1, $savedScore->getUserId());
|
|
|
|
$this->assertEquals(2, $savedScore->getPostId());
|
|
|
|
$this->assertEquals($score->getTime(), $savedScore->getTime());
|
2014-10-05 15:15:03 +02:00
|
|
|
$this->assertEquals($user->getId(), $savedScore->getUserId());
|
|
|
|
$this->assertEquals($post->getId(), $savedScore->getPostId());
|
2014-10-05 14:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindingByUserAndPost()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$post1 = new Post(1);
|
|
|
|
$post2 = new Post(2);
|
|
|
|
$user1 = new User(3);
|
|
|
|
$user2 = new User(4);
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$score1 = new Score();
|
2014-10-05 15:15:03 +02:00
|
|
|
$score1->setUserId($user1->getId());
|
|
|
|
$score1->setPostId($post1->getId());
|
2014-10-05 14:54:21 +02:00
|
|
|
$score1->setTime(date('c', mktime(1)));
|
|
|
|
$score1->setScore(1);
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$score2 = new Score();
|
2014-10-05 15:15:03 +02:00
|
|
|
$score2->setUserId($user2->getId());
|
|
|
|
$score2->setPostId($post2->getId());
|
2014-10-05 14:54:21 +02:00
|
|
|
$score2->setTime(date('c', mktime(2)));
|
|
|
|
$score2->setScore(0);
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$score3 = new Score();
|
2014-10-05 15:15:03 +02:00
|
|
|
$score3->setUserId($user1->getId());
|
|
|
|
$score3->setPostId($post2->getId());
|
2014-10-05 14:54:21 +02:00
|
|
|
$score3->setTime(date('c', mktime(3)));
|
|
|
|
$score3->setScore(-1);
|
|
|
|
|
|
|
|
$scoreDao = $this->getScoreDao();
|
|
|
|
$scoreDao->save($score1);
|
|
|
|
$scoreDao->save($score2);
|
|
|
|
$scoreDao->save($score3);
|
|
|
|
|
2014-11-10 15:54:08 +01:00
|
|
|
$this->assertEntitiesEqual($score1, $scoreDao->getUserScore($user1, $post1));
|
|
|
|
$this->assertEntitiesEqual($score2, $scoreDao->getUserScore($user2, $post2));
|
|
|
|
$this->assertEntitiesEqual($score3, $scoreDao->getUserScore($user1, $post2));
|
|
|
|
$this->assertNull($scoreDao->getUserScore($user2, $post1));
|
2014-10-05 14:54:21 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function findByPost(Post $post)
|
2014-10-05 14:54:21 +02:00
|
|
|
{
|
|
|
|
return $this->findOneBy('postId', $post->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getScoreDao()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
return new ScoreDao(
|
2014-10-05 14:54:21 +02:00
|
|
|
$this->databaseConnection,
|
|
|
|
$this->timeServiceMock);
|
|
|
|
}
|
|
|
|
}
|