Removed unused lazy loaders

This commit is contained in:
Marcin Kurczewski 2014-10-05 15:15:03 +02:00
parent edb9055299
commit 28e87dca93
5 changed files with 18 additions and 107 deletions

View file

@ -3,14 +3,10 @@ namespace Szurubooru\Dao;
class FavoritesDao extends AbstractDao implements ICrudDao
{
private $userDao;
private $postDao;
private $timeService;
public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection,
\Szurubooru\Dao\UserDao $userDao,
\Szurubooru\Dao\PostDao $postDao,
\Szurubooru\Services\TimeService $timeService)
{
parent::__construct(
@ -18,8 +14,6 @@ class FavoritesDao extends AbstractDao implements ICrudDao
'favorites',
new \Szurubooru\Dao\EntityConverters\FavoriteEntityConverter());
$this->userDao = $userDao;
$this->postDao = $postDao;
$this->timeService = $timeService;
}
@ -38,10 +32,10 @@ class FavoritesDao extends AbstractDao implements ICrudDao
{
$favorite = new \Szurubooru\Entities\Favorite();
$favorite->setTime($this->timeService->getCurrentTime());
$favorite->setUser($user);
$favorite->setUserId($user->getId());
if ($entity instanceof \Szurubooru\Entities\Post)
$favorite->setPost($entity);
$favorite->setPostId($entity->getId());
else
throw new \InvalidArgumentException();
@ -57,23 +51,6 @@ class FavoritesDao extends AbstractDao implements ICrudDao
$this->deleteById($favorite->getId());
}
protected function afterLoad(\Szurubooru\Entities\Entity $favorite)
{
$favorite->setLazyLoader(
\Szurubooru\Entities\Favorite::LAZY_LOADER_USER,
function (\Szurubooru\Entities\Favorite $favorite)
{
return $this->userDao->findById($favorite->getUserId());
});
$favorite->setLazyLoader(
\Szurubooru\Entities\Favorite::LAZY_LOADER_POST,
function (\Szurubooru\Entities\Favorite $favorite)
{
return $this->postDao->findById($favorite->getPostId());
});
}
private function get(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
{
$query = $this->fpdo->from($this->tableName)->where('userId', $user->getId());

View file

@ -3,14 +3,10 @@ namespace Szurubooru\Dao;
class ScoreDao extends AbstractDao implements ICrudDao
{
private $userDao;
private $postDao;
private $timeService;
public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection,
\Szurubooru\Dao\UserDao $userDao,
\Szurubooru\Dao\PostDao $postDao,
\Szurubooru\Services\TimeService $timeService)
{
parent::__construct(
@ -18,8 +14,6 @@ class ScoreDao extends AbstractDao implements ICrudDao
'scores',
new \Szurubooru\Dao\EntityConverters\ScoreEntityConverter());
$this->userDao = $userDao;
$this->postDao = $postDao;
$this->timeService = $timeService;
}
@ -44,10 +38,10 @@ class ScoreDao extends AbstractDao implements ICrudDao
{
$score = new \Szurubooru\Entities\Score();
$score->setTime($this->timeService->getCurrentTime());
$score->setUser($user);
$score->setUserId($user->getId());
if ($entity instanceof \Szurubooru\Entities\Post)
$score->setPost($entity);
$score->setPostId($entity->getId());
else
throw new \InvalidArgumentException();
}
@ -55,21 +49,4 @@ class ScoreDao extends AbstractDao implements ICrudDao
$this->save($score);
return $score;
}
protected function afterLoad(\Szurubooru\Entities\Entity $score)
{
$score->setLazyLoader(
\Szurubooru\Entities\Score::LAZY_LOADER_USER,
function (\Szurubooru\Entities\Score $score)
{
return $this->userDao->findById($score->getUserId());
});
$score->setLazyLoader(
\Szurubooru\Entities\Score::LAZY_LOADER_POST,
function (\Szurubooru\Entities\Score $score)
{
return $this->postDao->findById($score->getPostId());
});
}
}

View file

@ -8,9 +8,6 @@ class Score extends Entity
private $time;
private $score;
const LAZY_LOADER_USER = 'user';
const LAZY_LOADER_POST = 'post';
public function getUserId()
{
return $this->userId;
@ -50,26 +47,4 @@ class Score extends Entity
{
$this->score = $score;
}
public function getUser()
{
return $this->lazyLoad(self::LAZY_LOADER_USER, null);
}
public function setUser(\Szurubooru\Entities\User $user)
{
$this->lazySave(self::LAZY_LOADER_USER, $user);
$this->userId = $user->getId();
}
public function getPost()
{
return $this->lazyLoad(self::LAZY_LOADER_POST, null);
}
public function setPost(\Szurubooru\Entities\Post $post)
{
$this->lazySave(self::LAZY_LOADER_POST, $post);
$this->postId = $post->getId();
}
}

View file

@ -3,15 +3,11 @@ namespace Szurubooru\Tests\Dao;
class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{
private $userDaoMock;
private $postDaoMock;
private $timeServiceMock;
public function setUp()
{
parent::setUp();
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
$this->postDaoMock = $this->mock(\Szurubooru\Dao\PostDao::class);
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
}
@ -24,29 +20,24 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$post->setName('sword');
$favorite = new \Szurubooru\Entities\Favorite();
$favorite->setUser($user);
$favorite->setPost($post);
$favorite->setUserId($user->getId());
$favorite->setPostId($post->getId());
$favorite->setTime(date('c'));
$favoritesDao = $this->getFavoritesDao();
$favoritesDao->save($favorite);
$this->userDaoMock->expects($this->once())->method('findById')->with(1)->willReturn($user);
$this->postDaoMock->expects($this->once())->method('findById')->with(2)->willReturn($post);
$savedFavorite = $favoritesDao->findById($favorite->getId());
$this->assertEquals(1, $savedFavorite->getUserId());
$this->assertEquals(2, $savedFavorite->getPostId());
$this->assertEquals($favorite->getTime(), $savedFavorite->getTime());
$this->assertEntitiesEqual($user, $savedFavorite->getUser());
$this->assertEntitiesEqual($post, $savedFavorite->getPost());
$this->assertEquals($user->getId(), $savedFavorite->getUserId());
$this->assertEquals($post->getId(), $savedFavorite->getPostId());
}
private function getFavoritesDao()
{
return new \Szurubooru\Dao\FavoritesDao(
$this->databaseConnection,
$this->userDaoMock,
$this->postDaoMock,
$this->timeServiceMock);
}
}

View file

@ -3,15 +3,11 @@ namespace Szurubooru\Tests\Dao;
class ScoreDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{
private $userDaoMock;
private $postDaoMock;
private $timeServiceMock;
public function setUp()
{
parent::setUp();
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
$this->postDaoMock = $this->mock(\Szurubooru\Dao\PostDao::class);
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
}
@ -24,22 +20,19 @@ class ScoreDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$post->setName('sword');
$score = new \Szurubooru\Entities\Score();
$score->setUser($user);
$score->setPost($post);
$score->setUserId($user->getId());
$score->setPostId($post->getId());
$score->setTime(date('c'));
$score->setScore(1);
$scoreDao = $this->getScoreDao();
$scoreDao->save($score);
$this->userDaoMock->expects($this->once())->method('findById')->with(1)->willReturn($user);
$this->postDaoMock->expects($this->once())->method('findById')->with(2)->willReturn($post);
$savedScore = $scoreDao->findById($score->getId());
$this->assertEquals(1, $savedScore->getUserId());
$this->assertEquals(2, $savedScore->getPostId());
$this->assertEquals($score->getTime(), $savedScore->getTime());
$this->assertEntitiesEqual($user, $savedScore->getUser());
$this->assertEntitiesEqual($post, $savedScore->getPost());
$this->assertEquals($user->getId(), $savedScore->getUserId());
$this->assertEquals($post->getId(), $savedScore->getPostId());
}
public function testFindingByUserAndPost()
@ -50,20 +43,20 @@ class ScoreDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$user2 = new \Szurubooru\Entities\User(4);
$score1 = new \Szurubooru\Entities\Score();
$score1->setUser($user1);
$score1->setPost($post1);
$score1->setUserId($user1->getId());
$score1->setPostId($post1->getId());
$score1->setTime(date('c', mktime(1)));
$score1->setScore(1);
$score2 = new \Szurubooru\Entities\Score();
$score2->setUser($user2);
$score2->setPost($post2);
$score2->setUserId($user2->getId());
$score2->setPostId($post2->getId());
$score2->setTime(date('c', mktime(2)));
$score2->setScore(0);
$score3 = new \Szurubooru\Entities\Score();
$score3->setUser($user1);
$score3->setPost($post2);
$score3->setUserId($user1->getId());
$score3->setPostId($post2->getId());
$score3->setTime(date('c', mktime(3)));
$score3->setScore(-1);
@ -87,8 +80,6 @@ class ScoreDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{
return new \Szurubooru\Dao\ScoreDao(
$this->databaseConnection,
$this->userDaoMock,
$this->postDaoMock,
$this->timeServiceMock);
}
}