Fixed tests

This commit is contained in:
rr- 2015-11-25 11:39:32 +01:00
parent ee09a09833
commit ecb3901bbe
12 changed files with 208 additions and 240 deletions

View file

@ -23,7 +23,7 @@ maxCustomThumbnailSize = 1048576 ;1mb
[database.tests] [database.tests]
dsn = mysql:host=localhost dsn = mysql:host=localhost
user = szuru_test user = szuru-test
password = cat password = cat
[security] [security]

View file

@ -41,6 +41,7 @@ abstract class AbstractDatabaseTestCase extends AbstractTestCase
$tag = new Tag(); $tag = new Tag();
$tag->setName($name); $tag->setName($name);
$tag->setCreationTime(date('c')); $tag->setCreationTime(date('c'));
$tag->setLastEditTime(date('c'));
return $tag; return $tag;
} }
@ -48,7 +49,8 @@ abstract class AbstractDatabaseTestCase extends AbstractTestCase
{ {
$post = new Post(); $post = new Post();
$post->setName('test'); $post->setName('test');
$post->setUploadTime(date('c')); $post->setCreationTime(date('c'));
$post->setLastEditTime(date('c'));
$post->setSafety(Post::POST_SAFETY_SAFE); $post->setSafety(Post::POST_SAFETY_SAFE);
$post->setContentType(Post::POST_TYPE_YOUTUBE); $post->setContentType(Post::POST_TYPE_YOUTUBE);
$post->setContentChecksum('whatever'); $post->setContentChecksum('whatever');
@ -61,7 +63,7 @@ abstract class AbstractDatabaseTestCase extends AbstractTestCase
$user->setName($userName); $user->setName($userName);
$user->setPasswordHash('whatever'); $user->setPasswordHash('whatever');
$user->setLastLoginTime(date('c', mktime(1, 2, 3))); $user->setLastLoginTime(date('c', mktime(1, 2, 3)));
$user->setRegistrationTime(date('c', mktime(3, 2, 1))); $user->setCreationTime(date('c', mktime(3, 2, 1)));
$user->setAccessRank(User::ACCESS_RANK_REGULAR_USER); $user->setAccessRank(User::ACCESS_RANK_REGULAR_USER);
return $user; return $user;
} }

View file

@ -11,23 +11,22 @@ use Szurubooru\Tests\AbstractDatabaseTestCase;
final class CommentDaoTest extends AbstractDatabaseTestCase final class CommentDaoTest extends AbstractDatabaseTestCase
{ {
private $userDaoMock;
private $postDaoMock;
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->userDaoMock = $this->mock(UserDao::class);
$this->postDaoMock = $this->mock(PostDao::class);
} }
public function testSaving() public function testSaving()
{ {
$user = new User(1); $userDao = Injector::get(UserDao::class);
$user->setName('olivia'); $postDao = Injector::get(PostDao::class);
$commentDao = Injector::get(CommentDao::class);
$post = new Post(2); $user = self::getTestUser('olivia');
$post->setName('sword'); $userDao->save($user);
$post = self::getTestPost();
$postDao->save($post);
$comment = new Comment(); $comment = new Comment();
$comment->setUser($user); $comment->setUser($user);
@ -35,15 +34,11 @@ final class CommentDaoTest extends AbstractDatabaseTestCase
$comment->setCreationTime(date('c')); $comment->setCreationTime(date('c'));
$comment->setLastEditTime(date('c')); $comment->setLastEditTime(date('c'));
$comment->setText('whatever'); $comment->setText('whatever');
$commentDao = $this->getCommentDao();
$commentDao->save($comment); $commentDao->save($comment);
$this->userDaoMock->expects($this->once())->method('findById')->with(1)->willReturn($user);
$this->postDaoMock->expects($this->once())->method('findById')->with(2)->willReturn($post);
$savedComment = $commentDao->findById($comment->getId()); $savedComment = $commentDao->findById($comment->getId());
$this->assertEquals(1, $savedComment->getUserId()); $this->assertNotNull($savedComment->getUserId());
$this->assertEquals(2, $savedComment->getPostId()); $this->assertNotNull($savedComment->getPostId());
$this->assertEquals($comment->getCreationTime(), $savedComment->getCreationTime()); $this->assertEquals($comment->getCreationTime(), $savedComment->getCreationTime());
$this->assertEquals($comment->getLastEditTime(), $savedComment->getLastEditTime()); $this->assertEquals($comment->getLastEditTime(), $savedComment->getLastEditTime());
$this->assertEquals($comment->getText(), $savedComment->getText()); $this->assertEquals($comment->getText(), $savedComment->getText());
@ -109,17 +104,4 @@ final class CommentDaoTest extends AbstractDatabaseTestCase
$this->assertNotNull($post); $this->assertNotNull($post);
$this->assertEquals(0, $post->getCommentCount()); $this->assertEquals(0, $post->getCommentCount());
} }
public function findByPost(Post $post)
{
return $this->findOneBy('postId', $post->getId());
}
private function getCommentDao()
{
return new CommentDao(
$this->databaseConnection,
$this->userDaoMock,
$this->postDaoMock);
}
} }

View file

@ -1,49 +1,42 @@
<?php <?php
namespace Szurubooru\Tests\Dao; namespace Szurubooru\Tests\Dao;
use Szurubooru\Dao\FavoritesDao; use Szurubooru\Dao\FavoritesDao;
use Szurubooru\Dao\PostDao;
use Szurubooru\Dao\UserDao;
use Szurubooru\Entities\Favorite; use Szurubooru\Entities\Favorite;
use Szurubooru\Entities\Post; use Szurubooru\Entities\Post;
use Szurubooru\Entities\User; use Szurubooru\Entities\User;
use Szurubooru\Injector;
use Szurubooru\Services\TimeService; use Szurubooru\Services\TimeService;
use Szurubooru\Tests\AbstractDatabaseTestCase; use Szurubooru\Tests\AbstractDatabaseTestCase;
final class FavoritesDaoTest extends AbstractDatabaseTestCase final class FavoritesDaoTest extends AbstractDatabaseTestCase
{ {
private $timeServiceMock;
public function setUp()
{
parent::setUp();
$this->timeServiceMock = $this->mock(TimeService::class);
}
public function testSaving() public function testSaving()
{ {
$user = new User(1); $userDao = Injector::get(UserDao::class);
$user->setName('olivia'); $postDao = Injector::get(PostDao::class);
$timeServiceMock = $this->mock(TimeService::class);
$favoritesDao = new FavoritesDao(
$this->databaseConnection, $userDao, $postDao, $timeServiceMock);
$post = new Post(2); $user = self::getTestUser('olivia');
$post->setName('sword'); $userDao->save($user);
$post = self::getTestPost();
$postDao->save($post);
$favorite = new Favorite(); $favorite = new Favorite();
$favorite->setUserId($user->getId()); $favorite->setUserId($user->getId());
$favorite->setPostId($post->getId()); $favorite->setPostId($post->getId());
$favorite->setTime(date('c')); $favorite->setTime(date('c'));
$favoritesDao = $this->getFavoritesDao();
$favoritesDao->save($favorite); $favoritesDao->save($favorite);
$savedFavorite = $favoritesDao->findById($favorite->getId()); $savedFavorite = $favoritesDao->findById($favorite->getId());
$this->assertEquals(1, $savedFavorite->getUserId()); $this->assertNotNull($savedFavorite->getUserId());
$this->assertEquals(2, $savedFavorite->getPostId()); $this->assertNotNull($savedFavorite->getPostId());
$this->assertEquals($favorite->getTime(), $savedFavorite->getTime()); $this->assertEquals($favorite->getTime(), $savedFavorite->getTime());
$this->assertEquals($user->getId(), $savedFavorite->getUserId()); $this->assertEntitiesEqual($user, $savedFavorite->getUser());
$this->assertEquals($post->getId(), $savedFavorite->getPostId()); $this->assertEntitiesEqual($post, $savedFavorite->getPost());
}
private function getFavoritesDao()
{
return new FavoritesDao(
$this->databaseConnection,
$this->timeServiceMock);
} }
} }

View file

@ -6,6 +6,7 @@ use Szurubooru\Dao\TagDao;
use Szurubooru\Dao\UserDao; use Szurubooru\Dao\UserDao;
use Szurubooru\Entities\Tag; use Szurubooru\Entities\Tag;
use Szurubooru\Entities\User; use Szurubooru\Entities\User;
use Szurubooru\Injector;
use Szurubooru\Services\ThumbnailService; use Szurubooru\Services\ThumbnailService;
use Szurubooru\Tests\AbstractDatabaseTestCase; use Szurubooru\Tests\AbstractDatabaseTestCase;
@ -19,20 +20,15 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->fileDaoMock = $this->mock(PublicFileDao::class);
$this->thumbnailServiceMock = $this->mock(ThumbnailService::class);
$this->tagDao = new TagDao($this->databaseConnection); $this->tagDao = new TagDao($this->databaseConnection);
$this->userDao = new UserDao( $fileDaoMock = $this->mock(PublicFileDao::class);
$this->databaseConnection,
$this->fileDaoMock,
$this->thumbnailServiceMock);
} }
public function testCreating() public function testCreating()
{ {
$postDao = $this->getPostDao(); $postDao = Injector::get(PostDao::class);
$post = self::getTestPost(); $post = self::getTestPost();
$savedPost = $postDao->save($post); $savedPost = $postDao->save($post);
@ -42,7 +38,8 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testUpdating() public function testUpdating()
{ {
$postDao = $this->getPostDao(); $postDao = Injector::get(PostDao::class);
$post = self::getTestPost(); $post = self::getTestPost();
$post = $postDao->save($post); $post = $postDao->save($post);
$this->assertEquals('test', $post->getName()); $this->assertEquals('test', $post->getName());
@ -55,7 +52,7 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testGettingAll() public function testGettingAll()
{ {
$postDao = $this->getPostDao(); $postDao = Injector::get(PostDao::class);
$post1 = self::getTestPost(); $post1 = self::getTestPost();
$post2 = self::getTestPost(); $post2 = self::getTestPost();
@ -78,7 +75,7 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testGettingTotalFileSize() public function testGettingTotalFileSize()
{ {
$postDao = $this->getPostDao(); $postDao = Injector::get(PostDao::class);
$post1 = self::getTestPost(); $post1 = self::getTestPost();
$post2 = self::getTestPost(); $post2 = self::getTestPost();
@ -99,7 +96,7 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testGettingById() public function testGettingById()
{ {
$postDao = $this->getPostDao(); $postDao = Injector::get(PostDao::class);
$post1 = self::getTestPost(); $post1 = self::getTestPost();
$post2 = self::getTestPost(); $post2 = self::getTestPost();
@ -114,7 +111,7 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testDeletingAll() public function testDeletingAll()
{ {
$postDao = $this->getPostDao(); $postDao = Injector::get(PostDao::class);
$post1 = self::getTestPost(); $post1 = self::getTestPost();
$post2 = self::getTestPost(); $post2 = self::getTestPost();
@ -132,7 +129,7 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testDeletingById() public function testDeletingById()
{ {
$postDao = $this->getPostDao(); $postDao = Injector::get(PostDao::class);
$post1 = self::getTestPost(); $post1 = self::getTestPost();
$post2 = self::getTestPost(); $post2 = self::getTestPost();
@ -150,16 +147,18 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testFindingByTagName() public function testFindingByTagName()
{ {
$tagDao = Injector::get(TagDao::class);
$postDao = Injector::get(PostDao::class);
$tag1 = new Tag(); $tag1 = new Tag();
$tag1->setName('tag1'); $tag1->setName('tag1');
$tag1->setCreationTime(date('c')); $tag1->setCreationTime(date('c'));
$tag2 = new Tag(); $tag2 = new Tag();
$tag2->setName('tag2'); $tag2->setName('tag2');
$tag2->setCreationTime(date('c')); $tag2->setCreationTime(date('c'));
$this->tagDao->save($tag1); $tagDao->save($tag1);
$this->tagDao->save($tag2); $tagDao->save($tag2);
$postDao = $this->getPostDao();
$post1 = self::getTestPost(); $post1 = self::getTestPost();
$post1->setTags([$tag1]); $post1->setTags([$tag1]);
$postDao->save($post1); $postDao->save($post1);
@ -173,6 +172,9 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testSavingTags() public function testSavingTags()
{ {
$tagDao = Injector::get(TagDao::class);
$postDao = Injector::get(PostDao::class);
$tag1 = new Tag(); $tag1 = new Tag();
$tag1->setName('tag1'); $tag1->setName('tag1');
$tag1->setCreationTime(date('c')); $tag1->setCreationTime(date('c'));
@ -183,7 +185,6 @@ final class PostDaoTest extends AbstractDatabaseTestCase
$this->tagDao->save($tag2); $this->tagDao->save($tag2);
$testTags = ['tag1' => $tag1, 'tag2' => $tag2]; $testTags = ['tag1' => $tag1, 'tag2' => $tag2];
$postDao = $this->getPostDao();
$post = self::getTestPost(); $post = self::getTestPost();
$post->setTags($testTags); $post->setTags($testTags);
$postDao->save($post); $postDao->save($post);
@ -195,17 +196,17 @@ final class PostDaoTest extends AbstractDatabaseTestCase
$this->assertEquals(2, $post->getTagCount()); $this->assertEquals(2, $post->getTagCount());
$this->assertEquals(2, $savedPost->getTagCount()); $this->assertEquals(2, $savedPost->getTagCount());
$tagDao = $this->getTagDao();
$this->assertEquals(2, count($tagDao->findAll())); $this->assertEquals(2, count($tagDao->findAll()));
} }
public function testSavingUnsavedRelations() public function testSavingUnsavedRelations()
{ {
$postDao = Injector::get(PostDao::class);
$post1 = self::getTestPost(); $post1 = self::getTestPost();
$post2 = self::getTestPost(); $post2 = self::getTestPost();
$testPosts = [$post1, $post2]; $testPosts = [$post1, $post2];
$postDao = $this->getPostDao();
$post = self::getTestPost(); $post = self::getTestPost();
$post->setRelatedPosts($testPosts); $post->setRelatedPosts($testPosts);
$this->setExpectedException(\Exception::class, 'Unsaved entities found'); $this->setExpectedException(\Exception::class, 'Unsaved entities found');
@ -214,11 +215,12 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testSavingRelations() public function testSavingRelations()
{ {
$postDao = Injector::get(PostDao::class);
$post1 = self::getTestPost(); $post1 = self::getTestPost();
$post2 = self::getTestPost(); $post2 = self::getTestPost();
$testPosts = [$post1, $post2]; $testPosts = [$post1, $post2];
$postDao = $this->getPostDao();
$postDao->save($post1); $postDao->save($post1);
$postDao->save($post2); $postDao->save($post2);
$post = self::getTestPost(); $post = self::getTestPost();
@ -232,35 +234,45 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testSavingUser() public function testSavingUser()
{ {
$testUser = new User(5); $userDao = Injector::get(UserDao::class);
$testUser->setName('it\'s me'); $postDao = Injector::get(PostDao::class);
$postDao = $this->getPostDao();
$user = self::getTestUser('uploader');
$userDao->save($user);
$post = self::getTestPost(); $post = self::getTestPost();
$post->setUser($testUser); $post->setUser($user);
$postDao->save($post); $postDao->save($post);
$savedPost = $postDao->findById($post->getId()); $savedPost = $postDao->findById($post->getId());
$this->assertEntitiesEqual($testUser, $post->getUser()); $this->assertNotNull($post->getUserId());
$this->assertEquals(5, $post->getUserId()); $this->assertEntitiesEqual($user, $post->getUser());
} }
public function testNotLoadingContentForNewPosts() public function testNotLoadingContentForNewPosts()
{ {
$postDao = $this->getPostDao(); $postDao = Injector::get(PostDao::class);
$newlyCreatedPost = self::getTestPost(); $newlyCreatedPost = self::getTestPost();
$this->assertNull($newlyCreatedPost->getContent()); $this->assertNull($newlyCreatedPost->getContent());
} }
public function testLoadingContentPostsForExistingPosts() public function testLoadingContentPostsForExistingPosts()
{ {
$postDao = $this->getPostDao(); $thumbnailServiceMock = $this->mock(ThumbnailService::class);
$fileDaoMock = $this->mock(PublicFileDao::class);
$tagDao = Injector::get(TagDao::class);
$userDao = Injector::get(UserDao::class);
$postDao = new PostDao(
$this->databaseConnection,
$tagDao, $userDao, $fileDaoMock, $thumbnailServiceMock);
$post = self::getTestPost(); $post = self::getTestPost();
$postDao->save($post); $postDao->save($post);
$post = $postDao->findById($post->getId()); $post = $postDao->findById($post->getId());
$this->fileDaoMock $fileDaoMock
->expects($this->once()) ->expects($this->once())
->method('load') ->method('load')
->with($post->getContentPath()) ->with($post->getContentPath())
@ -271,18 +283,26 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testSavingContent() public function testSavingContent()
{ {
$postDao = $this->getPostDao(); $thumbnailServiceMock = $this->mock(ThumbnailService::class);
$fileDaoMock = $this->mock(PublicFileDao::class);
$tagDao = Injector::get(TagDao::class);
$userDao = Injector::get(UserDao::class);
$postDao = new PostDao(
$this->databaseConnection,
$tagDao, $userDao, $fileDaoMock, $thumbnailServiceMock);
$post = self::getTestPost(); $post = self::getTestPost();
$post->setContent('whatever'); $post->setContent('whatever');
$this->thumbnailServiceMock $thumbnailServiceMock
->expects($this->exactly(2)) ->expects($this->exactly(2))
->method('deleteUsedThumbnails') ->method('deleteUsedThumbnails')
->withConsecutive( ->withConsecutive(
[$post->getContentPath()], [$post->getContentPath()],
[$post->getThumbnailSourceContentPath()]); [$post->getThumbnailSourceContentPath()]);
$this->fileDaoMock $fileDaoMock
->expects($this->exactly(1)) ->expects($this->exactly(1))
->method('save') ->method('save')
->withConsecutive( ->withConsecutive(
@ -293,19 +313,27 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function testSavingContentAndThumbnail() public function testSavingContentAndThumbnail()
{ {
$postDao = $this->getPostDao(); $thumbnailServiceMock = $this->mock(ThumbnailService::class);
$fileDaoMock = $this->mock(PublicFileDao::class);
$tagDao = Injector::get(TagDao::class);
$userDao = Injector::get(UserDao::class);
$postDao = new PostDao(
$this->databaseConnection,
$tagDao, $userDao, $fileDaoMock, $thumbnailServiceMock);
$post = self::getTestPost(); $post = self::getTestPost();
$post->setContent('whatever'); $post->setContent('whatever');
$post->setThumbnailSourceContent('an image of sharks'); $post->setThumbnailSourceContent('an image of sharks');
$this->thumbnailServiceMock $thumbnailServiceMock
->expects($this->exactly(2)) ->expects($this->exactly(2))
->method('deleteUsedThumbnails') ->method('deleteUsedThumbnails')
->withConsecutive( ->withConsecutive(
[$post->getContentPath()], [$post->getContentPath()],
[$post->getThumbnailSourceContentPath()]); [$post->getThumbnailSourceContentPath()]);
$this->fileDaoMock $fileDaoMock
->expects($this->exactly(2)) ->expects($this->exactly(2))
->method('save') ->method('save')
->withConsecutive( ->withConsecutive(
@ -314,20 +342,4 @@ final class PostDaoTest extends AbstractDatabaseTestCase
$postDao->save($post); $postDao->save($post);
} }
private function getPostDao()
{
return new PostDao(
$this->databaseConnection,
$this->tagDao,
$this->userDao,
$this->fileDaoMock,
$this->thumbnailServiceMock);
}
private function getTagDao()
{
return $this->tagDao;
}
} }

View file

@ -3,42 +3,32 @@ namespace Szurubooru\Tests\Dao;
use Szurubooru\Dao\PostDao; use Szurubooru\Dao\PostDao;
use Szurubooru\Dao\PostNoteDao; use Szurubooru\Dao\PostNoteDao;
use Szurubooru\Entities\PostNote; use Szurubooru\Entities\PostNote;
use Szurubooru\Injector;
use Szurubooru\Tests\AbstractDatabaseTestCase; use Szurubooru\Tests\AbstractDatabaseTestCase;
final class PostNoteDaoTest extends AbstractDatabaseTestCase final class PostNoteDaoTest extends AbstractDatabaseTestCase
{ {
private $postDaoMock;
public function setUp()
{
parent::setUp();
$this->postDaoMock = $this->mock(PostDao::class);
}
public function testSettingValues() public function testSettingValues()
{ {
$postDao = Injector::get(PostDao::class);
$postNoteDao = Injector::get(PostNoteDao::class);
$post = self::getTestPost();
$postDao->save($post);
$expected = new PostNote(); $expected = new PostNote();
$expected->setPostId(5); $expected->setPost($post);
$expected->setLeft(5); $expected->setLeft(5);
$expected->setTop(10); $expected->setTop(10);
$expected->setWidth(50); $expected->setWidth(50);
$expected->setHeight(50); $expected->setHeight(50);
$expected->setText('text'); $expected->setText('text');
$postNoteDao = $this->getPostNoteDao();
$postNoteDao->save($expected); $postNoteDao->save($expected);
$actual = $postNoteDao->findById($expected->getId()); $actual = $postNoteDao->findById($expected->getId());
$this->assertEntitiesEqual($actual, $expected); $this->assertEntitiesEqual($actual, $expected);
$this->assertNotNull($actual->getPostId());
$this->postDaoMock->expects($this->once())->method('findById')->with(5)->willReturn('lazy post'); $this->assertEntitiesEqual($post, $actual->getPost());
$this->assertEquals('lazy post', $actual->getPost());
}
private function getPostNoteDao()
{
return new PostNoteDao(
$this->databaseConnection,
$this->postDaoMock);
} }
} }

View file

@ -1,41 +1,38 @@
<?php <?php
namespace Szurubooru\Tests\Dao; namespace Szurubooru\Tests\Dao;
use Szurubooru\Dao\PostDao;
use Szurubooru\Dao\ScoreDao; use Szurubooru\Dao\ScoreDao;
use Szurubooru\Dao\UserDao;
use Szurubooru\Entities\Post; use Szurubooru\Entities\Post;
use Szurubooru\Entities\Score; use Szurubooru\Entities\Score;
use Szurubooru\Entities\User; use Szurubooru\Entities\User;
use Szurubooru\Services\TimeService; use Szurubooru\Injector;
use Szurubooru\Tests\AbstractDatabaseTestCase; use Szurubooru\Tests\AbstractDatabaseTestCase;
final class ScoreDaoTest extends AbstractDatabaseTestCase final class ScoreDaoTest extends AbstractDatabaseTestCase
{ {
private $timeServiceMock;
public function setUp()
{
parent::setUp();
$this->timeServiceMock = $this->mock(TimeService::class);
}
public function testSaving() public function testSaving()
{ {
$user = new User(1); $userDao = Injector::get(UserDao::class);
$user->setName('olivia'); $postDao = Injector::get(PostDao::class);
$scoreDao = Injector::get(ScoreDao::class);
$post = new Post(2); $user = self::getTestUser('olivia');
$post->setName('sword'); $userDao->save($user);
$post = self::getTestPost();
$postDao->save($post);
$score = new Score(); $score = new Score();
$score->setUserId($user->getId()); $score->setUserId($user->getId());
$score->setPostId($post->getId()); $score->setPostId($post->getId());
$score->setTime(date('c')); $score->setTime(date('c'));
$score->setScore(1); $score->setScore(1);
$scoreDao = $this->getScoreDao();
$scoreDao->save($score); $scoreDao->save($score);
$savedScore = $scoreDao->findById($score->getId()); $savedScore = $scoreDao->findById($score->getId());
$this->assertEquals(1, $savedScore->getUserId()); $this->assertNotNull($savedScore->getUserId());
$this->assertEquals(2, $savedScore->getPostId()); $this->assertNotNull($savedScore->getPostId());
$this->assertEquals($score->getTime(), $savedScore->getTime()); $this->assertEquals($score->getTime(), $savedScore->getTime());
$this->assertEquals($user->getId(), $savedScore->getUserId()); $this->assertEquals($user->getId(), $savedScore->getUserId());
$this->assertEquals($post->getId(), $savedScore->getPostId()); $this->assertEquals($post->getId(), $savedScore->getPostId());
@ -43,14 +40,23 @@ final class ScoreDaoTest extends AbstractDatabaseTestCase
public function testFindingByUserAndPost() public function testFindingByUserAndPost()
{ {
$post1 = new Post(1); $userDao = Injector::get(UserDao::class);
$post2 = new Post(2); $postDao = Injector::get(PostDao::class);
$user1 = new User(3); $scoreDao = Injector::get(ScoreDao::class);
$user2 = new User(4);
$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);
$score1 = new Score(); $score1 = new Score();
$score1->setUserId($user1->getId()); $score1->setUserId($user1->getId());
$score1->setPostId($post1->getId()); $score1->setPostId($post1->geTId());
$score1->setTime(date('c', mktime(1))); $score1->setTime(date('c', mktime(1)));
$score1->setScore(1); $score1->setScore(1);
@ -66,7 +72,6 @@ final class ScoreDaoTest extends AbstractDatabaseTestCase
$score3->setTime(date('c', mktime(3))); $score3->setTime(date('c', mktime(3)));
$score3->setScore(-1); $score3->setScore(-1);
$scoreDao = $this->getScoreDao();
$scoreDao->save($score1); $scoreDao->save($score1);
$scoreDao->save($score2); $scoreDao->save($score2);
$scoreDao->save($score3); $scoreDao->save($score3);
@ -76,16 +81,4 @@ final class ScoreDaoTest extends AbstractDatabaseTestCase
$this->assertEntitiesEqual($score3, $scoreDao->getUserScore($user1, $post2)); $this->assertEntitiesEqual($score3, $scoreDao->getUserScore($user1, $post2));
$this->assertNull($scoreDao->getUserScore($user2, $post1)); $this->assertNull($scoreDao->getUserScore($user2, $post1));
} }
public function findByPost(Post $post)
{
return $this->findOneBy('postId', $post->getId());
}
private function getScoreDao()
{
return new ScoreDao(
$this->databaseConnection,
$this->timeServiceMock);
}
} }

View file

@ -4,39 +4,37 @@ use Szurubooru\Dao\SnapshotDao;
use Szurubooru\Dao\UserDao; use Szurubooru\Dao\UserDao;
use Szurubooru\Entities\Snapshot; use Szurubooru\Entities\Snapshot;
use Szurubooru\Entities\User; use Szurubooru\Entities\User;
use Szurubooru\Injector;
use Szurubooru\Tests\AbstractDatabaseTestCase; use Szurubooru\Tests\AbstractDatabaseTestCase;
final class SnapshotDaoTest extends AbstractDatabaseTestCase final class SnapshotDaoTest extends AbstractDatabaseTestCase
{ {
public function setUp()
{
parent::setUp();
$this->userDaoMock = $this->mock(UserDao::class);
}
public function testSaving() public function testSaving()
{ {
$snapshotDao = Injector::get(SnapshotDao::class);
$snapshot = $this->getTestSnapshot(); $snapshot = $this->getTestSnapshot();
$snapshotDao = $this->getSnapshotDao();
$snapshotDao->save($snapshot); $snapshotDao->save($snapshot);
$this->assertNotNull($snapshot->getId()); $this->assertNotNull($snapshot->getId());
$this->assertEntitiesEqual($snapshot, $snapshotDao->findById($snapshot->getId())); $this->assertEntitiesEqual($snapshot, $snapshotDao->findById($snapshot->getId()));
} }
public function testUserLazyLoader() public function testUserLazyLoader()
{ {
$snapshot = $this->getTestSnapshot(); $userDao = Injector::get(UserDao::class);
$snapshot->setUser(new User(5)); $snapshotDao = Injector::get(SnapshotDao::class);
$this->assertEquals(5, $snapshot->getUserId());
$snapshotDao = $this->getSnapshotDao();
$snapshotDao->save($snapshot);
$savedSnapshot = $snapshotDao->findById($snapshot->getId());
$this->assertEquals(5, $savedSnapshot->getUserId());
$this->userDaoMock $user = self::getTestUser('victoria');
->expects($this->once()) $userDao->save($user);
->method('findById');
$savedSnapshot->getUser(); $snapshot = $this->getTestSnapshot();
$snapshot->setUser($user);
$snapshotDao->save($snapshot);
$savedSnapshot = $snapshotDao->findById($snapshot->getId());
$this->assertNotNull($savedSnapshot->getUserId());
$this->assertEntitiesEqual($user, $savedSnapshot->getUser());
} }
private function getTestSnapshot() private function getTestSnapshot()
@ -50,11 +48,4 @@ final class SnapshotDaoTest extends AbstractDatabaseTestCase
$snapshot->setOperation(Snapshot::OPERATION_CHANGE); $snapshot->setOperation(Snapshot::OPERATION_CHANGE);
return $snapshot; return $snapshot;
} }
private function getSnapshotDao()
{
return new SnapshotDao(
$this->databaseConnection,
$this->userDaoMock);
}
} }

View file

@ -1,24 +1,22 @@
<?php <?php
namespace Szurubooru\Tests\Dao; namespace Szurubooru\Tests\Dao;
use Szurubooru\Dao\PostDao;
use Szurubooru\Dao\TagDao; use Szurubooru\Dao\TagDao;
use Szurubooru\Entities\Tag; use Szurubooru\Entities\Tag;
use Szurubooru\Injector;
use Szurubooru\Tests\AbstractDatabaseTestCase; use Szurubooru\Tests\AbstractDatabaseTestCase;
final class TagDaoTest extends AbstractDatabaseTestCase final class TagDaoTest extends AbstractDatabaseTestCase
{ {
public function setUp()
{
parent::setUp();
}
public function testSaving() public function testSaving()
{ {
$tagDao = Injector::get(TagDao::class);
$tag = self::getTestTag('test'); $tag = self::getTestTag('test');
$tag->setCreationTime(date('c', mktime(0, 0, 0, 10, 1, 2014))); $tag->setCreationTime(date('c', mktime(0, 0, 0, 10, 1, 2014)));
$this->assertFalse($tag->isBanned()); $this->assertFalse($tag->isBanned());
$tag->setBanned(true); $tag->setBanned(true);
$tagDao = $this->getTagDao();
$tagDao->save($tag); $tagDao->save($tag);
$actualTag = $tagDao->findById($tag->getId()); $actualTag = $tagDao->findById($tag->getId());
$this->assertEntitiesEqual($tag, $actualTag); $this->assertEntitiesEqual($tag, $actualTag);
@ -26,11 +24,12 @@ final class TagDaoTest extends AbstractDatabaseTestCase
public function testSavingRelations() public function testSavingRelations()
{ {
$tagDao = Injector::get(TagDao::class);
$tag1 = self::getTestTag('test 1'); $tag1 = self::getTestTag('test 1');
$tag2 = self::getTestTag('test 2'); $tag2 = self::getTestTag('test 2');
$tag3 = self::getTestTag('test 3'); $tag3 = self::getTestTag('test 3');
$tag4 = self::getTestTag('test 4'); $tag4 = self::getTestTag('test 4');
$tagDao = $this->getTagDao();
$tagDao->save($tag1); $tagDao->save($tag1);
$tagDao->save($tag2); $tagDao->save($tag2);
$tagDao->save($tag3); $tagDao->save($tag3);
@ -55,33 +54,30 @@ final class TagDaoTest extends AbstractDatabaseTestCase
public function testFindByPostIds() public function testFindByPostIds()
{ {
$postDao = Injector::get(PostDao::class);
$tagDao = Injector::get(TagDao::class);
$post1 = self::getTestPost();
$post2 = self::getTestPost();
$postDao->save($post1);
$postDao->save($post2);
$tag1 = self::getTestTag('test1');
$tag2 = self::getTestTag('test2');
$tagDao->save($tag1);
$tagDao->save($tag2);
$pdo = $this->databaseConnection->getPDO(); $pdo = $this->databaseConnection->getPDO();
$pdo->exec(sprintf('INSERT INTO postTags(postId, tagId) VALUES (%d, %d)', $post1->getId(), $tag1->getId()));
$pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (1, \'test1\', \'2014-10-01 00:00:00\')'); $pdo->exec(sprintf('INSERT INTO postTags(postId, tagId) VALUES (%d, %d)', $post2->getId(), $tag1->getId()));
$pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (2, \'test2\', \'2014-10-01 00:00:00\')'); $pdo->exec(sprintf('INSERT INTO postTags(postId, tagId) VALUES (%d, %d)', $post1->getId(), $tag2->getId()));
$pdo->exec('INSERT INTO postTags(postId, tagId) VALUES (5, 1)'); $pdo->exec(sprintf('INSERT INTO postTags(postId, tagId) VALUES (%d, %d)', $post2->getId(), $tag2->getId()));
$pdo->exec('INSERT INTO postTags(postId, tagId) VALUES (6, 1)');
$pdo->exec('INSERT INTO postTags(postId, tagId) VALUES (5, 2)');
$pdo->exec('INSERT INTO postTags(postId, tagId) VALUES (6, 2)');
$tag1 = new Tag(1);
$tag1->setName('test1');
$tag1->setCreationTime(date('c', mktime(0, 0, 0, 10, 1, 2014)));
$tag2 = new Tag(2);
$tag2->setName('test2');
$tag2->setCreationTime(date('c', mktime(0, 0, 0, 10, 1, 2014)));
$expected = [ $expected = [
$tag1->getId() => $tag1, $tag1->getId() => $tag1,
$tag2->getId() => $tag2, $tag2->getId() => $tag2,
]; ];
$tagDao = $this->getTagDao(); $actual = $tagDao->findByPostId($post1->getId());
$actual = $tagDao->findByPostId(5);
$this->assertEntitiesEqual($expected, $actual); $this->assertEntitiesEqual($expected, $actual);
} }
private function getTagDao()
{
return new TagDao($this->databaseConnection);
}
} }

View file

@ -101,7 +101,7 @@ final class UserDaoFilterTest extends AbstractDatabaseTestCase
{ {
list ($user1, $user2) = $this->prepareUsers(); list ($user1, $user2) = $this->prepareUsers();
$this->doTestSorting( $this->doTestSorting(
UserFilter::ORDER_REGISTRATION_TIME, UserFilter::ORDER_CREATION_TIME,
UserFilter::ORDER_ASC, UserFilter::ORDER_ASC,
[$user2, $user1]); [$user2, $user1]);
} }
@ -110,7 +110,7 @@ final class UserDaoFilterTest extends AbstractDatabaseTestCase
{ {
list ($user1, $user2) = $this->prepareUsers(); list ($user1, $user2) = $this->prepareUsers();
$this->doTestSorting( $this->doTestSorting(
UserFilter::ORDER_REGISTRATION_TIME, UserFilter::ORDER_CREATION_TIME,
UserFilter::ORDER_DESC, UserFilter::ORDER_DESC,
[$user1, $user2]); [$user1, $user2]);
} }
@ -119,8 +119,8 @@ final class UserDaoFilterTest extends AbstractDatabaseTestCase
{ {
$user1 = self::getTestUser('beartato'); $user1 = self::getTestUser('beartato');
$user2 = self::getTestUser('reginald'); $user2 = self::getTestUser('reginald');
$user1->setRegistrationTime(date('c', mktime(3, 2, 1))); $user1->setCreationTime(date('c', mktime(3, 2, 1)));
$user2->setRegistrationTime(date('c', mktime(1, 2, 3))); $user2->setCreationTime(date('c', mktime(1, 2, 3)));
$userDao = $this->getUserDao(); $userDao = $this->getUserDao();
$userDao->save($user1); $userDao->save($user1);

View file

@ -2,24 +2,25 @@
namespace Szurubooru\Tests\SearchService; namespace Szurubooru\Tests\SearchService;
use \Szurubooru\Helpers\InputReader; use \Szurubooru\Helpers\InputReader;
use \Szurubooru\Search\Filters\UserFilter; use \Szurubooru\Search\Filters\UserFilter;
use \Szurubooru\Search\Parsers\UserSearchParser; use \Szurubooru\Search\ParserConfigs\UserSearchParserConfig;
use \Szurubooru\Search\SearchParser;
use \Szurubooru\Tests\AbstractTestCase; use \Szurubooru\Tests\AbstractTestCase;
final class UserSearchParserTest extends AbstractTestCase final class UserSearchParserTest extends AbstractTestCase
{ {
private $inputReader; private $inputReader;
private $userSearchParser; private $searchParser;
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->inputReader = new InputReader; $this->inputReader = new InputReader;
$this->userSearchParser = new UserSearchParser(); $this->searchParser = new SearchParser(new UserSearchParserConfig);
} }
public function testDefaultOrder() public function testDefaultOrder()
{ {
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
$this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_ASC], $filter->getOrder()); $this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_ASC], $filter->getOrder());
} }
@ -27,36 +28,36 @@ final class UserSearchParserTest extends AbstractTestCase
{ {
$this->inputReader->order = 'invalid,desc'; $this->inputReader->order = 'invalid,desc';
$this->setExpectedException(\Exception::class); $this->setExpectedException(\Exception::class);
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
} }
public function testInvalidOrderDirection() public function testInvalidOrderDirection()
{ {
$this->inputReader->order = 'name,invalid'; $this->inputReader->order = 'name,invalid';
$this->setExpectedException(\Exception::class); $this->setExpectedException(\Exception::class);
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
} }
public function testParamOrder() public function testParamOrder()
{ {
$this->inputReader->order = 'name,desc'; $this->inputReader->order = 'name,desc';
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
$this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder()); $this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder());
} }
public function testTokenOverwriteDefaultOrder() public function testTokenOverwriteDefaultOrder()
{ {
$this->inputReader->query = 'order:name,desc'; $this->inputReader->query = 'order:name,desc';
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
$this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder()); $this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder());
} }
public function testTokenOrder() public function testTokenOrder()
{ {
$this->inputReader->query = 'order:registration_time,desc'; $this->inputReader->query = 'order:creation_time,desc';
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
$this->assertOrderEquals([ $this->assertOrderEquals([
UserFilter::ORDER_REGISTRATION_TIME => UserFilter::ORDER_DESC, UserFilter::ORDER_CREATION_TIME => UserFilter::ORDER_DESC,
UserFilter::ORDER_NAME => UserFilter::ORDER_ASC], UserFilter::ORDER_NAME => UserFilter::ORDER_ASC],
$filter->getOrder()); $filter->getOrder());
} }
@ -64,10 +65,10 @@ final class UserSearchParserTest extends AbstractTestCase
public function testParamAndTokenOrder() public function testParamAndTokenOrder()
{ {
$this->inputReader->order = 'name,desc'; $this->inputReader->order = 'name,desc';
$this->inputReader->query = 'order:registration_time,desc'; $this->inputReader->query = 'order:creation_time,desc';
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->searchParser->createFilterFromInputReader($this->inputReader);
$this->assertOrderEquals([ $this->assertOrderEquals([
UserFilter::ORDER_REGISTRATION_TIME => UserFilter::ORDER_DESC, UserFilter::ORDER_CREATION_TIME => UserFilter::ORDER_DESC,
UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], UserFilter::ORDER_NAME => UserFilter::ORDER_DESC],
$filter->getOrder()); $filter->getOrder());
} }

View file

@ -6,6 +6,7 @@ use Szurubooru\Services\ImageManipulation\IImageManipulator;
use Szurubooru\Services\ImageManipulation\ImageManipulator; use Szurubooru\Services\ImageManipulation\ImageManipulator;
use Szurubooru\Services\ImageManipulation\ImagickImageManipulator; use Szurubooru\Services\ImageManipulation\ImagickImageManipulator;
use Szurubooru\Tests\AbstractTestCase; use Szurubooru\Tests\AbstractTestCase;
use Szurubooru\Tests\ConfigMock;
final class ImageManipulatorTest extends AbstractTestCase final class ImageManipulatorTest extends AbstractTestCase
{ {
@ -47,10 +48,11 @@ final class ImageManipulatorTest extends AbstractTestCase
/** /**
* @dataProvider imageManipulatorProvider * @dataProvider imageManipulatorProvider
* @expectedException Exception
*/ */
public function testNonImage($imageManipulator) public function testNonImage($imageManipulator)
{ {
$this->assertNull($imageManipulator->loadFromBuffer($this->getTestFile('flash.swf'))); $imageManipulator->loadFromBuffer($this->getTestFile('flash.swf'));
} }
/** /**
@ -138,12 +140,18 @@ final class ImageManipulatorTest extends AbstractTestCase
private static function getAutoImageManipulator() private static function getAutoImageManipulator()
{ {
if (extension_loaded('gd') && extension_loaded('imagick')) $configMock = new ConfigMock(null, null);
{
if (extension_loaded('imagick'))
$configMock->set('misc/imageExtension', 'imagick');
elseif (extension_loaded('gd'))
$configMock->set('misc/imageExtension', 'gd');
else
return null;
return new ImageManipulator( return new ImageManipulator(
self::getImagickImageManipulator(), self::getImagickImageManipulator(),
self::getGdImageManipulator()); self::getGdImageManipulator(),
} $configMock);
return null;
} }
} }