From edb9055299b6b80b9ddb5d749c313dfb60e3fd84 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 5 Oct 2014 14:54:21 +0200 Subject: [PATCH] Changed favorite and score DAO to be more flexible --- ...coreController.php => ScoreController.php} | 38 +++++--- src/Controllers/ViewProxies/PostViewProxy.php | 8 +- ...Converter.php => ScoreEntityConverter.php} | 4 +- src/Dao/FavoritesDao.php | 36 ++++--- src/Dao/PostDao.php | 8 +- src/Dao/PostScoreDao.php | 67 ------------- src/Dao/ScoreDao.php | 75 +++++++++++++++ src/Entities/{PostScore.php => Score.php} | 2 +- src/Services/FavoritesService.php | 26 ++--- ...{PostScoreService.php => ScoreService.php} | 26 ++--- src/Upgrades/Upgrade15.php | 60 ++++++++++++ src/di.php | 3 +- tests/Dao/FavoritesDaoTest.php | 5 - tests/Dao/PostScoreDaoTest.php | 94 ------------------- tests/Dao/ScoreDaoTest.php | 94 +++++++++++++++++++ tests/Services/FavoritesServiceTest.php | 8 +- tests/Services/PostScoreServiceTest.php | 66 ------------- tests/Services/ScoreServiceTest.php | 66 +++++++++++++ 18 files changed, 386 insertions(+), 300 deletions(-) rename src/Controllers/{PostScoreController.php => ScoreController.php} (59%) rename src/Dao/EntityConverters/{PostScoreEntityConverter.php => ScoreEntityConverter.php} (79%) delete mode 100644 src/Dao/PostScoreDao.php create mode 100644 src/Dao/ScoreDao.php rename src/Entities/{PostScore.php => Score.php} (97%) rename src/Services/{PostScoreService.php => ScoreService.php} (65%) create mode 100644 src/Upgrades/Upgrade15.php delete mode 100644 tests/Dao/PostScoreDaoTest.php create mode 100644 tests/Dao/ScoreDaoTest.php delete mode 100644 tests/Services/PostScoreServiceTest.php create mode 100644 tests/Services/ScoreServiceTest.php diff --git a/src/Controllers/PostScoreController.php b/src/Controllers/ScoreController.php similarity index 59% rename from src/Controllers/PostScoreController.php rename to src/Controllers/ScoreController.php index 205c06a9..dd1b2e79 100644 --- a/src/Controllers/PostScoreController.php +++ b/src/Controllers/ScoreController.php @@ -1,50 +1,60 @@ privilegeService = $privilegeService; $this->authService = $authService; $this->postService = $postService; - $this->postScoreService = $postScoreService; + $this->scoreService = $scoreService; $this->inputReader = $inputReader; } public function registerRoutes(\Szurubooru\Router $router) { - $router->get('/api/posts/:postNameOrId/score', [$this, 'getScore']); - $router->post('/api/posts/:postNameOrId/score', [$this, 'setScore']); + $router->get('/api/posts/:postNameOrId/score', [$this, 'getPostScore']); + $router->post('/api/posts/:postNameOrId/score', [$this, 'setPostScore']); } - public function getScore($postNameOrId) + public function getPostScore($postNameOrId) { - $this->privilegeService->assertLoggedIn(); - $user = $this->authService->getLoggedInUser(); $post = $this->postService->getByNameOrId($postNameOrId); - $result = $this->postScoreService->getScore($user, $post); - return ['score' => $result ? $result->getScore() : 0]; + return $this->getScore($post); } - public function setScore($postNameOrId) + public function setPostScore($postNameOrId) + { + $post = $this->postService->getByNameOrId($postNameOrId); + return $this->setScore($post); + } + + private function setScore(\Szurubooru\Entities\Entity $entity) { $this->privilegeService->assertLoggedIn(); $score = intval($this->inputReader->score); $user = $this->authService->getLoggedInUser(); - $post = $this->postService->getByNameOrId($postNameOrId); - $result = $this->postScoreService->setScore($user, $post, $score); + $result = $this->scoreService->setScore($user, $entity, $score); return ['score' => $result->getScore()]; } + + private function getScore(\Szurubooru\Entities\Entity $entity) + { + $this->privilegeService->assertLoggedIn(); + $user = $this->authService->getLoggedInUser(); + $result = $this->scoreService->getScore($user, $entity); + return ['score' => $result ? $result->getScore() : 0]; + } } diff --git a/src/Controllers/ViewProxies/PostViewProxy.php b/src/Controllers/ViewProxies/PostViewProxy.php index c4a3f738..91f49ce9 100644 --- a/src/Controllers/ViewProxies/PostViewProxy.php +++ b/src/Controllers/ViewProxies/PostViewProxy.php @@ -14,7 +14,7 @@ class PostViewProxy extends AbstractViewProxy private $authService; private $historyService; private $favoritesService; - private $postScoreService; + private $scoreService; private $tagViewProxy; private $userViewProxy; private $snapshotViewProxy; @@ -24,7 +24,7 @@ class PostViewProxy extends AbstractViewProxy \Szurubooru\Services\AuthService $authService, \Szurubooru\Services\HistoryService $historyService, \Szurubooru\Services\FavoritesService $favoritesService, - \Szurubooru\Services\PostScoreService $postScoreService, + \Szurubooru\Services\ScoreService $scoreService, TagViewProxy $tagViewProxy, UserViewProxy $userViewProxy, SnapshotViewProxy $snapshotViewProxy) @@ -33,7 +33,7 @@ class PostViewProxy extends AbstractViewProxy $this->authService = $authService; $this->historyService = $historyService; $this->favoritesService = $favoritesService; - $this->postScoreService = $postScoreService; + $this->scoreService = $scoreService; $this->tagViewProxy = $tagViewProxy; $this->userViewProxy = $userViewProxy; $this->snapshotViewProxy = $snapshotViewProxy; @@ -83,7 +83,7 @@ class PostViewProxy extends AbstractViewProxy } if (!empty($config[self::FETCH_OWN_SCORE]) and $this->authService->isLoggedIn()) - $result->ownScore = $this->postScoreService->getScoreValue($this->authService->getLoggedInUser(), $post); + $result->ownScore = $this->scoreService->getScoreValue($this->authService->getLoggedInUser(), $post); if (!empty($config[self::FETCH_FAVORITES])) $result->favorites = $this->userViewProxy->fromArray($this->favoritesService->getFavoriteUsers($post)); diff --git a/src/Dao/EntityConverters/PostScoreEntityConverter.php b/src/Dao/EntityConverters/ScoreEntityConverter.php similarity index 79% rename from src/Dao/EntityConverters/PostScoreEntityConverter.php rename to src/Dao/EntityConverters/ScoreEntityConverter.php index 59cb3885..62e74676 100644 --- a/src/Dao/EntityConverters/PostScoreEntityConverter.php +++ b/src/Dao/EntityConverters/ScoreEntityConverter.php @@ -1,7 +1,7 @@ setUserId($array['userId']); $entity->setPostId($array['postId']); $entity->setTime($this->dbTimeToEntityTime($array['time'])); diff --git a/src/Dao/FavoritesDao.php b/src/Dao/FavoritesDao.php index b76b712d..4776fdf7 100644 --- a/src/Dao/FavoritesDao.php +++ b/src/Dao/FavoritesDao.php @@ -23,28 +23,36 @@ class FavoritesDao extends AbstractDao implements ICrudDao $this->timeService = $timeService; } - public function findByPost(\Szurubooru\Entities\Post $post) + public function findByEntity(\Szurubooru\Entities\Entity $entity) { - return $this->findBy('postId', $post->getId()); + if ($entity instanceof \Szurubooru\Entities\Post) + return $this->findBy('postId', $entity->getId()); + else + throw new \InvalidArgumentException(); } - public function set(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post) + public function set(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity) { - $favorite = $this->get($user, $post); + $favorite = $this->get($user, $entity); if (!$favorite) { $favorite = new \Szurubooru\Entities\Favorite(); - $favorite->setUser($user); - $favorite->setPost($post); $favorite->setTime($this->timeService->getCurrentTime()); + $favorite->setUser($user); + + if ($entity instanceof \Szurubooru\Entities\Post) + $favorite->setPost($entity); + else + throw new \InvalidArgumentException(); + $this->save($favorite); } return $favorite; } - public function delete(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post) + public function delete(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity) { - $favorite = $this->get($user, $post); + $favorite = $this->get($user, $entity); if ($favorite) $this->deleteById($favorite->getId()); } @@ -66,11 +74,15 @@ class FavoritesDao extends AbstractDao implements ICrudDao }); } - private function get(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post) + private function get(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity) { - $query = $this->fpdo->from($this->tableName) - ->where('userId', $user->getId()) - ->where('postId', $post->getId()); + $query = $this->fpdo->from($this->tableName)->where('userId', $user->getId()); + + if ($entity instanceof \Szurubooru\Entities\Post) + $query->where('postId', $entity->getId()); + else + throw new \InvalidArgumentException(); + $arrayEntities = iterator_to_array($query); $entities = $this->arrayToEntities($arrayEntities); return array_shift($entities); diff --git a/src/Dao/PostDao.php b/src/Dao/PostDao.php index b8bd9091..61383be4 100644 --- a/src/Dao/PostDao.php +++ b/src/Dao/PostDao.php @@ -133,11 +133,11 @@ class PostDao extends AbstractDao implements ICrudDao $userName = $values[0]; $score = $values[1]; $sql = 'EXISTS ( - SELECT 1 FROM postScores - INNER JOIN users ON postScores.userId = users.id - WHERE postScores.postId = posts.id + SELECT 1 FROM scores + INNER JOIN users ON scores.userId = users.id + WHERE scores.postId = posts.id AND LOWER(users.name) = LOWER(?) - AND postScores.score = ?)'; + AND scores.score = ?)'; if ($requirement->isnegated()) $sql = 'NOT ' . $sql; $query->where($sql, $userName, $score); diff --git a/src/Dao/PostScoreDao.php b/src/Dao/PostScoreDao.php deleted file mode 100644 index d7179a88..00000000 --- a/src/Dao/PostScoreDao.php +++ /dev/null @@ -1,67 +0,0 @@ -userDao = $userDao; - $this->postDao = $postDao; - $this->timeService = $timeService; - } - - public function getScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post) - { - $query = $this->fpdo->from($this->tableName) - ->where('userId', $user->getId()) - ->where('postId', $post->getId()); - $arrayEntities = iterator_to_array($query); - $entities = $this->arrayToEntities($arrayEntities); - return array_shift($entities); - } - - public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post, $scoreValue) - { - $postScore = $this->getScore($user, $post); - if (!$postScore) - { - $postScore = new \Szurubooru\Entities\PostScore(); - $postScore->setUser($user); - $postScore->setPost($post); - $postScore->setTime($this->timeService->getCurrentTime()); - } - $postScore->setScore($scoreValue); - $this->save($postScore); - return $postScore; - } - - protected function afterLoad(\Szurubooru\Entities\Entity $postScore) - { - $postScore->setLazyLoader( - \Szurubooru\Entities\PostScore::LAZY_LOADER_USER, - function (\Szurubooru\Entities\PostScore $postScore) - { - return $this->userDao->findById($postScore->getUserId()); - }); - - $postScore->setLazyLoader( - \Szurubooru\Entities\PostScore::LAZY_LOADER_POST, - function (\Szurubooru\Entities\PostScore $postScore) - { - return $this->postDao->findById($postScore->getPostId()); - }); - } -} diff --git a/src/Dao/ScoreDao.php b/src/Dao/ScoreDao.php new file mode 100644 index 00000000..c4864456 --- /dev/null +++ b/src/Dao/ScoreDao.php @@ -0,0 +1,75 @@ +userDao = $userDao; + $this->postDao = $postDao; + $this->timeService = $timeService; + } + + public function getScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity) + { + $query = $this->fpdo->from($this->tableName)->where('userId', $user->getId()); + + if ($entity instanceof \Szurubooru\Entities\Post) + $query->where('postId', $entity->getId()); + else + throw new \InvalidArgumentException(); + + $arrayEntities = iterator_to_array($query); + $entities = $this->arrayToEntities($arrayEntities); + return array_shift($entities); + } + + public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity, $scoreValue) + { + $score = $this->getScore($user, $entity); + if (!$score) + { + $score = new \Szurubooru\Entities\Score(); + $score->setTime($this->timeService->getCurrentTime()); + $score->setUser($user); + + if ($entity instanceof \Szurubooru\Entities\Post) + $score->setPost($entity); + else + throw new \InvalidArgumentException(); + } + $score->setScore($scoreValue); + $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()); + }); + } +} diff --git a/src/Entities/PostScore.php b/src/Entities/Score.php similarity index 97% rename from src/Entities/PostScore.php rename to src/Entities/Score.php index b2691983..147838f7 100644 --- a/src/Entities/PostScore.php +++ b/src/Entities/Score.php @@ -1,7 +1,7 @@ favoritesDao = $favoritesDao; - $this->postScoreDao = $postScoreDao; + $this->scoreDao = $scoreDao; $this->userDao = $userDao; $this->transactionManager = $transactionManager; $this->timeService = $timeService; } - public function getFavoriteUsers(\Szurubooru\Entities\Post $post) + public function getFavoriteUsers(\Szurubooru\Entities\Entity $entity) { - $transactionFunc = function() use ($post) + $transactionFunc = function() use ($entity) { - $favorites = $this->favoritesDao->findByPost($post); + $favorites = $this->favoritesDao->findByEntity($entity); $userIds = []; foreach ($favorites as $favorite) { @@ -38,22 +38,22 @@ class FavoritesService return $this->transactionManager->rollback($transactionFunc); } - public function addFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post) + public function addFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity) { - $transactionFunc = function() use ($user, $post) + $transactionFunc = function() use ($user, $entity) { - $this->postScoreDao->setScore($user, $post, 1); + $this->scoreDao->setScore($user, $entity, 1); - return $this->favoritesDao->set($user, $post); + return $this->favoritesDao->set($user, $entity); }; return $this->transactionManager->commit($transactionFunc); } - public function deleteFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post) + public function deleteFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity) { - $transactionFunc = function() use ($user, $post) + $transactionFunc = function() use ($user, $entity) { - $this->favoritesDao->delete($user, $post); + $this->favoritesDao->delete($user, $entity); }; $this->transactionManager->commit($transactionFunc); } diff --git a/src/Services/PostScoreService.php b/src/Services/ScoreService.php similarity index 65% rename from src/Services/PostScoreService.php rename to src/Services/ScoreService.php index baa2dd3a..cc844274 100644 --- a/src/Services/PostScoreService.php +++ b/src/Services/ScoreService.php @@ -1,56 +1,56 @@ postScoreDao = $postScoreDao; + $this->scoreDao = $scoreDao; $this->favoritesDao = $favoritesDao; $this->userDao = $userDao; $this->transactionManager = $transactionManager; $this->timeService = $timeService; } - public function getScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post) + public function getScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity) { - $transactionFunc = function() use ($user, $post) + $transactionFunc = function() use ($user, $entity) { - return $this->postScoreDao->getScore($user, $post); + return $this->scoreDao->getScore($user, $entity); }; return $this->transactionManager->rollback($transactionFunc); } - public function getScoreValue(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post) + public function getScoreValue(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity) { - $score = $this->getScore($user, $post); + $score = $this->getScore($user, $entity); if (!$score) return 0; return $score->getScore(); } - public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post, $scoreValue) + public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity, $scoreValue) { if ($scoreValue !== 1 and $scoreValue !== 0 and $scoreValue !== -1) throw new \DomainException('Bad score'); - $transactionFunc = function() use ($user, $post, $scoreValue) + $transactionFunc = function() use ($user, $entity, $scoreValue) { if ($scoreValue !== 1) - $this->favoritesDao->delete($user, $post); + $this->favoritesDao->delete($user, $entity); - return $this->postScoreDao->setScore($user, $post, $scoreValue); + return $this->scoreDao->setScore($user, $entity, $scoreValue); }; return $this->transactionManager->commit($transactionFunc); } diff --git a/src/Upgrades/Upgrade15.php b/src/Upgrades/Upgrade15.php new file mode 100644 index 00000000..46aa3050 --- /dev/null +++ b/src/Upgrades/Upgrade15.php @@ -0,0 +1,60 @@ +getPDO(); + $driver = $databaseConnection->getDriver(); + + $pdo->exec('DROP TABLE IF EXISTS scores'); + + $pdo->exec('CREATE TABLE scores + ( + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', + userId INTEGER NOT NULL, + postId INTEGER, + time DATETIME NOT NULL, + score INTEGER NOT NULL + )'); + + $pdo->exec('INSERT INTO scores (userId, postId, time, score) SELECT userId, postId, time, score FROM postScores'); + + $pdo->exec('DROP TABLE IF EXISTS postScores'); + $pdo->exec('DROP TRIGGER IF EXISTS postScoresDelete'); + $pdo->exec('DROP TRIGGER IF EXISTS postScoresInsert'); + $pdo->exec('DROP TRIGGER IF EXISTS postScoresUpdate'); + + $pdo->exec('DROP TRIGGER IF EXISTS scoresDelete'); + $pdo->exec('DROP TRIGGER IF EXISTS scoresInsert'); + $pdo->exec('DROP TRIGGER IF EXISTS scoresUpdate'); + + $pdo->exec(' + CREATE TRIGGER scoresDelete AFTER DELETE ON scores + FOR EACH ROW + BEGIN + UPDATE posts SET + score = (SELECT SUM(score) FROM scores WHERE scores.postId = posts.id) + WHERE posts.id = OLD.postId; + END'); + + $pdo->exec(' + CREATE TRIGGER scoresInsert AFTER INSERT ON scores + FOR EACH ROW + BEGIN + UPDATE posts SET + score = (SELECT SUM(score) FROM scores WHERE scores.postId = posts.id) + WHERE posts.id = NEW.postId; + END'); + + $pdo->exec(' + CREATE TRIGGER scoresUpdate AFTER UPDATE ON scores + FOR EACH ROW + BEGIN + UPDATE posts SET + score = (SELECT SUM(score) FROM scores WHERE scores.postId = posts.id) + WHERE posts.id IN (OLD.postId, NEW.postId); + END'); + } +} diff --git a/src/di.php b/src/di.php index 7ae0bc9e..31869f61 100644 --- a/src/di.php +++ b/src/di.php @@ -30,6 +30,7 @@ return [ $container->get(\Szurubooru\Upgrades\Upgrade12::class), $container->get(\Szurubooru\Upgrades\Upgrade13::class), $container->get(\Szurubooru\Upgrades\Upgrade14::class), + $container->get(\Szurubooru\Upgrades\Upgrade15::class), ]; }), @@ -43,7 +44,7 @@ return [ $container->get(\Szurubooru\Controllers\GlobalParamController::class), $container->get(\Szurubooru\Controllers\HistoryController::class), $container->get(\Szurubooru\Controllers\FavoritesController::class), - $container->get(\Szurubooru\Controllers\PostScoreController::class), + $container->get(\Szurubooru\Controllers\ScoreController::class), $container->get(\Szurubooru\Controllers\CommentController::class), ]; }), diff --git a/tests/Dao/FavoritesDaoTest.php b/tests/Dao/FavoritesDaoTest.php index e303b002..993bb646 100644 --- a/tests/Dao/FavoritesDaoTest.php +++ b/tests/Dao/FavoritesDaoTest.php @@ -41,11 +41,6 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $this->assertEntitiesEqual($post, $savedFavorite->getPost()); } - public function findByPost(\Szurubooru\Entities\Post $post) - { - return $this->findOneBy('postId', $post->getId()); - } - private function getFavoritesDao() { return new \Szurubooru\Dao\FavoritesDao( diff --git a/tests/Dao/PostScoreDaoTest.php b/tests/Dao/PostScoreDaoTest.php deleted file mode 100644 index 6cf2059c..00000000 --- a/tests/Dao/PostScoreDaoTest.php +++ /dev/null @@ -1,94 +0,0 @@ -userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class); - $this->postDaoMock = $this->mock(\Szurubooru\Dao\PostDao::class); - $this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class); - } - - public function testSaving() - { - $user = new \Szurubooru\Entities\User(1); - $user->setName('olivia'); - - $post = new \Szurubooru\Entities\Post(2); - $post->setName('sword'); - - $postScore = new \Szurubooru\Entities\PostScore(); - $postScore->setUser($user); - $postScore->setPost($post); - $postScore->setTime(date('c')); - $postScore->setScore(1); - $postScoreDao = $this->getPostScoreDao(); - $postScoreDao->save($postScore); - - $this->userDaoMock->expects($this->once())->method('findById')->with(1)->willReturn($user); - $this->postDaoMock->expects($this->once())->method('findById')->with(2)->willReturn($post); - - $savedPostScore = $postScoreDao->findById($postScore->getId()); - $this->assertEquals(1, $savedPostScore->getUserId()); - $this->assertEquals(2, $savedPostScore->getPostId()); - $this->assertEquals($postScore->getTime(), $savedPostScore->getTime()); - $this->assertEntitiesEqual($user, $savedPostScore->getUser()); - $this->assertEntitiesEqual($post, $savedPostScore->getPost()); - } - - public function testFindingByUserAndPost() - { - $post1 = new \Szurubooru\Entities\Post(1); - $post2 = new \Szurubooru\Entities\Post(2); - $user1 = new \Szurubooru\Entities\User(3); - $user2 = new \Szurubooru\Entities\User(4); - - $postScore1 = new \Szurubooru\Entities\PostScore(); - $postScore1->setUser($user1); - $postScore1->setPost($post1); - $postScore1->setTime(date('c', mktime(1))); - $postScore1->setScore(1); - - $postScore2 = new \Szurubooru\Entities\PostScore(); - $postScore2->setUser($user2); - $postScore2->setPost($post2); - $postScore2->setTime(date('c', mktime(2))); - $postScore2->setScore(0); - - $postScore3 = new \Szurubooru\Entities\PostScore(); - $postScore3->setUser($user1); - $postScore3->setPost($post2); - $postScore3->setTime(date('c', mktime(3))); - $postScore3->setScore(-1); - - $postScoreDao = $this->getPostScoreDao(); - $postScoreDao->save($postScore1); - $postScoreDao->save($postScore2); - $postScoreDao->save($postScore3); - - $this->assertEntitiesEqual($postScore1, $postScoreDao->getScore($user1, $post1)); - $this->assertEntitiesEqual($postScore2, $postScoreDao->getScore($user2, $post2)); - $this->assertEntitiesEqual($postScore3, $postScoreDao->getScore($user1, $post2)); - $this->assertNull($postScoreDao->getScore($user2, $post1)); - } - - public function findByPost(\Szurubooru\Entities\Post $post) - { - return $this->findOneBy('postId', $post->getId()); - } - - private function getPostScoreDao() - { - return new \Szurubooru\Dao\PostScoreDao( - $this->databaseConnection, - $this->userDaoMock, - $this->postDaoMock, - $this->timeServiceMock); - } -} diff --git a/tests/Dao/ScoreDaoTest.php b/tests/Dao/ScoreDaoTest.php new file mode 100644 index 00000000..b3616f29 --- /dev/null +++ b/tests/Dao/ScoreDaoTest.php @@ -0,0 +1,94 @@ +userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class); + $this->postDaoMock = $this->mock(\Szurubooru\Dao\PostDao::class); + $this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class); + } + + public function testSaving() + { + $user = new \Szurubooru\Entities\User(1); + $user->setName('olivia'); + + $post = new \Szurubooru\Entities\Post(2); + $post->setName('sword'); + + $score = new \Szurubooru\Entities\Score(); + $score->setUser($user); + $score->setPost($post); + $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()); + } + + public function testFindingByUserAndPost() + { + $post1 = new \Szurubooru\Entities\Post(1); + $post2 = new \Szurubooru\Entities\Post(2); + $user1 = new \Szurubooru\Entities\User(3); + $user2 = new \Szurubooru\Entities\User(4); + + $score1 = new \Szurubooru\Entities\Score(); + $score1->setUser($user1); + $score1->setPost($post1); + $score1->setTime(date('c', mktime(1))); + $score1->setScore(1); + + $score2 = new \Szurubooru\Entities\Score(); + $score2->setUser($user2); + $score2->setPost($post2); + $score2->setTime(date('c', mktime(2))); + $score2->setScore(0); + + $score3 = new \Szurubooru\Entities\Score(); + $score3->setUser($user1); + $score3->setPost($post2); + $score3->setTime(date('c', mktime(3))); + $score3->setScore(-1); + + $scoreDao = $this->getScoreDao(); + $scoreDao->save($score1); + $scoreDao->save($score2); + $scoreDao->save($score3); + + $this->assertEntitiesEqual($score1, $scoreDao->getScore($user1, $post1)); + $this->assertEntitiesEqual($score2, $scoreDao->getScore($user2, $post2)); + $this->assertEntitiesEqual($score3, $scoreDao->getScore($user1, $post2)); + $this->assertNull($scoreDao->getScore($user2, $post1)); + } + + public function findByPost(\Szurubooru\Entities\Post $post) + { + return $this->findOneBy('postId', $post->getId()); + } + + private function getScoreDao() + { + return new \Szurubooru\Dao\ScoreDao( + $this->databaseConnection, + $this->userDaoMock, + $this->postDaoMock, + $this->timeServiceMock); + } +} diff --git a/tests/Services/FavoritesServiceTest.php b/tests/Services/FavoritesServiceTest.php index 2d7444eb..99d035e3 100644 --- a/tests/Services/FavoritesServiceTest.php +++ b/tests/Services/FavoritesServiceTest.php @@ -4,7 +4,7 @@ namespace Szurubooru\Tests\Services; final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase { private $favoritesDaoMock; - private $postScoreDaoMock; + private $scoreDaoMock; private $userDaoMock; private $transactionManagerMock; private $timeServiceMock; @@ -13,7 +13,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase { parent::setUp(); $this->favoritesDaoMock = $this->mock(\Szurubooru\Dao\FavoritesDao::class); - $this->postScoreDaoMock = $this->mock(\Szurubooru\Dao\PostScoreDao::class); + $this->scoreDaoMock = $this->mock(\Szurubooru\Dao\ScoreDao::class); $this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class); $this->transactionManagerMock = $this->mockTransactionManager(); $this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class); @@ -51,7 +51,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase $fav1->setUser(new \Szurubooru\Entities\User(1)); $fav2->setUser(new \Szurubooru\Entities\User(2)); - $this->favoritesDaoMock->expects($this->once())->method('findByPost')->with($post)->willReturn([$fav1, $fav2]); + $this->favoritesDaoMock->expects($this->once())->method('findByEntity')->with($post)->willReturn([$fav1, $fav2]); $this->userDaoMock->expects($this->once())->method('findByIds')->with([1, 2]); $favoritesService = $this->getFavoritesService(); @@ -62,7 +62,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase { return new \Szurubooru\Services\FavoritesService( $this->favoritesDaoMock, - $this->postScoreDaoMock, + $this->scoreDaoMock, $this->userDaoMock, $this->transactionManagerMock, $this->timeServiceMock); diff --git a/tests/Services/PostScoreServiceTest.php b/tests/Services/PostScoreServiceTest.php deleted file mode 100644 index 0acd0394..00000000 --- a/tests/Services/PostScoreServiceTest.php +++ /dev/null @@ -1,66 +0,0 @@ -postScoreDaoMock = $this->mock(\Szurubooru\Dao\PostScoreDao::class); - $this->favoritesDaoMock = $this->mock(\Szurubooru\Dao\FavoritesDao::class); - $this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class); - $this->transactionManagerMock = $this->mockTransactionManager(); - $this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class); - } - - public function testSetting() - { - $user = new \Szurubooru\Entities\User(1); - $post = new \Szurubooru\Entities\Post(2); - $postScore = new \Szurubooru\Entities\PostScore(); - $postScore->setUserId($user->getId()); - $postScore->setPostId($post->getId()); - $postScore->setScore(1); - $this->postScoreDaoMock->expects($this->once())->method('setScore')->with($user, $post)->willReturn(null); - - $postScoreService = $this->getPostScoreService(); - $postScoreService->setScore($user, $post, 1); - } - - public function testSettingInvalid() - { - $user = new \Szurubooru\Entities\User(1); - $post = new \Szurubooru\Entities\Post(2); - $this->setExpectedException(\Exception::class); - $postScoreService = $this->getPostScoreService(); - $postScoreService->setScore($user, $post, 2); - } - - public function testGetting() - { - $user = new \Szurubooru\Entities\User(); - $post = new \Szurubooru\Entities\Post(); - $postScore = new \Szurubooru\Entities\PostScore(3); - $this->postScoreDaoMock->expects($this->once())->method('getScore')->with($user, $post)->willReturn($postScore); - - $postScoreService = $this->getPostScoreService(); - $retrievedScore = $postScoreService->getScore($user, $post); - $this->assertEquals($postScore, $retrievedScore); - } - - private function getPostScoreService() - { - return new \Szurubooru\Services\PostScoreService( - $this->postScoreDaoMock, - $this->favoritesDaoMock, - $this->userDaoMock, - $this->transactionManagerMock, - $this->timeServiceMock); - } -} diff --git a/tests/Services/ScoreServiceTest.php b/tests/Services/ScoreServiceTest.php new file mode 100644 index 00000000..7626238b --- /dev/null +++ b/tests/Services/ScoreServiceTest.php @@ -0,0 +1,66 @@ +scoreDaoMock = $this->mock(\Szurubooru\Dao\ScoreDao::class); + $this->favoritesDaoMock = $this->mock(\Szurubooru\Dao\FavoritesDao::class); + $this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class); + $this->transactionManagerMock = $this->mockTransactionManager(); + $this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class); + } + + public function testSetting() + { + $user = new \Szurubooru\Entities\User(1); + $post = new \Szurubooru\Entities\Post(2); + $score = new \Szurubooru\Entities\Score(); + $score->setUserId($user->getId()); + $score->setPostId($post->getId()); + $score->setScore(1); + $this->scoreDaoMock->expects($this->once())->method('setScore')->with($user, $post)->willReturn(null); + + $scoreService = $this->getScoreService(); + $scoreService->setScore($user, $post, 1); + } + + public function testSettingInvalid() + { + $user = new \Szurubooru\Entities\User(1); + $post = new \Szurubooru\Entities\Post(2); + $this->setExpectedException(\Exception::class); + $scoreService = $this->getScoreService(); + $scoreService->setScore($user, $post, 2); + } + + public function testGetting() + { + $user = new \Szurubooru\Entities\User(); + $post = new \Szurubooru\Entities\Post(); + $score = new \Szurubooru\Entities\Score(3); + $this->scoreDaoMock->expects($this->once())->method('getScore')->with($user, $post)->willReturn($score); + + $scoreService = $this->getScoreService(); + $retrievedScore = $scoreService->getScore($user, $post); + $this->assertEquals($score, $retrievedScore); + } + + private function getScoreService() + { + return new \Szurubooru\Services\ScoreService( + $this->scoreDaoMock, + $this->favoritesDaoMock, + $this->userDaoMock, + $this->transactionManagerMock, + $this->timeServiceMock); + } +}