2014-10-05 14:54:21 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Dao;
|
2015-11-25 11:39:32 +01:00
|
|
|
use Szurubooru\Dao\PostDao;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Dao\ScoreDao;
|
2015-11-25 11:39:32 +01:00
|
|
|
use Szurubooru\Dao\UserDao;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Entities\Post;
|
|
|
|
use Szurubooru\Entities\Score;
|
|
|
|
use Szurubooru\Entities\User;
|
2015-11-25 11:39:32 +01:00
|
|
|
use Szurubooru\Injector;
|
2014-10-08 14:47:47 +02:00
|
|
|
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
|
|
|
{
|
2015-11-25 09:48:03 +01:00
|
|
|
public function testSaving()
|
|
|
|
{
|
2015-11-25 11:39:32 +01:00
|
|
|
$userDao = Injector::get(UserDao::class);
|
|
|
|
$postDao = Injector::get(PostDao::class);
|
|
|
|
$scoreDao = Injector::get(ScoreDao::class);
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2015-11-25 11:39:32 +01:00
|
|
|
$user = self::getTestUser('olivia');
|
|
|
|
$userDao->save($user);
|
|
|
|
|
|
|
|
$post = self::getTestPost();
|
|
|
|
$postDao->save($post);
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$score = new Score();
|
|
|
|
$score->setUserId($user->getId());
|
|
|
|
$score->setPostId($post->getId());
|
|
|
|
$score->setTime(date('c'));
|
|
|
|
$score->setScore(1);
|
|
|
|
$scoreDao->save($score);
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$savedScore = $scoreDao->findById($score->getId());
|
2015-11-25 11:39:32 +01:00
|
|
|
$this->assertNotNull($savedScore->getUserId());
|
|
|
|
$this->assertNotNull($savedScore->getPostId());
|
2015-11-25 09:48:03 +01:00
|
|
|
$this->assertEquals($score->getTime(), $savedScore->getTime());
|
|
|
|
$this->assertEquals($user->getId(), $savedScore->getUserId());
|
|
|
|
$this->assertEquals($post->getId(), $savedScore->getPostId());
|
|
|
|
}
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
public function testFindingByUserAndPost()
|
|
|
|
{
|
2015-11-25 11:39:32 +01:00
|
|
|
$userDao = Injector::get(UserDao::class);
|
|
|
|
$postDao = Injector::get(PostDao::class);
|
|
|
|
$scoreDao = Injector::get(ScoreDao::class);
|
|
|
|
|
|
|
|
$user1 = self::getTestUser('olivia');
|
|
|
|
$user2 = self::getTestUser('victoria');
|
|
|
|
$userDao->save($user1);
|
|
|
|
$userDao->save($user2);
|
|
|
|
|
|
|
|
$post1 = self::getTestPost();
|
|
|
|
$post2 = self::getTestPost();
|
|
|
|
$postDao->save($post1);
|
|
|
|
$postDao->save($post2);
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$score1 = new Score();
|
|
|
|
$score1->setUserId($user1->getId());
|
2015-11-25 11:39:32 +01:00
|
|
|
$score1->setPostId($post1->geTId());
|
2015-11-25 09:48:03 +01:00
|
|
|
$score1->setTime(date('c', mktime(1)));
|
|
|
|
$score1->setScore(1);
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$score2 = new Score();
|
|
|
|
$score2->setUserId($user2->getId());
|
|
|
|
$score2->setPostId($post2->getId());
|
|
|
|
$score2->setTime(date('c', mktime(2)));
|
|
|
|
$score2->setScore(0);
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$score3 = new Score();
|
|
|
|
$score3->setUserId($user1->getId());
|
|
|
|
$score3->setPostId($post2->getId());
|
|
|
|
$score3->setTime(date('c', mktime(3)));
|
|
|
|
$score3->setScore(-1);
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$scoreDao->save($score1);
|
|
|
|
$scoreDao->save($score2);
|
|
|
|
$scoreDao->save($score3);
|
2014-10-05 14:54:21 +02:00
|
|
|
|
2015-11-25 09:48:03 +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
|
|
|
}
|