More unit tests

This commit is contained in:
Marcin Kurczewski 2014-05-09 21:08:13 +02:00
parent 26e27e3339
commit ad7cdcb7fe
4 changed files with 182 additions and 15 deletions

View file

@ -12,11 +12,13 @@ class AddCommentJobTest extends AbstractTest
{ {
return $this->runApi($text); return $this->runApi($text);
}); });
$post = $comment->getPost();
$this->assert->areEqual(1, CommentModel::getCount()); $this->assert->areEqual(1, CommentModel::getCount());
$this->assert->areEqual($text, $comment->getText()); $this->assert->areEqual($text, $comment->getText());
$this->assert->areEqual(Auth::getCurrentUser()->getId(), $comment->getCommenter()->getId()); $this->assert->areEqual(Auth::getCurrentUser()->getId(), $comment->getCommenter()->getId());
$this->assert->areEqual(1, $comment->getPost()->getId()); $this->assert->areEqual(1, $post->getId());
$this->assert->areEqual(1, $post->getCommentCount());
$this->assert->isNotNull($comment->getCreationTime()); $this->assert->isNotNull($comment->getCreationTime());
$this->assert->doesNotThrow(function() use ($comment) $this->assert->doesNotThrow(function() use ($comment)
{ {

View file

@ -6,11 +6,22 @@ class DeleteCommentJobTest extends AbstractTest
$this->prepare(); $this->prepare();
$this->grantAccess('deleteComment'); $this->grantAccess('deleteComment');
$this->assert->doesNotThrow(function() $comment = $this->mockComment(Auth::getCurrentUser());
$post = $comment->getPost();
$this->assert->areEqual(1, $post->getCommentCount());
$this->assert->doesNotThrow(function() use ($comment)
{ {
$this->runApi(); Api::run(
new DeleteCommentJob(),
[
DeleteCommentJob::COMMENT_ID => $comment->getId(),
]);
}); });
//post needs to be retrieved again from db to refresh cache
$post = PostModel::getById($post->getId());
$this->assert->areEqual(0, $post->getCommentCount());
$this->assert->areEqual(0, CommentModel::getCount()); $this->assert->areEqual(0, CommentModel::getCount());
} }
@ -28,18 +39,6 @@ class DeleteCommentJobTest extends AbstractTest
} }
protected function runApi($comment = null)
{
if ($comment === null)
$comment = $this->mockComment(Auth::getCurrentUser());
return Api::run(
new DeleteCommentJob(),
[
DeleteCommentJob::COMMENT_ID => $comment->getId(),
]);
}
protected function prepare() protected function prepare()
{ {
$this->login($this->mockUser()); $this->login($this->mockUser());

View file

@ -0,0 +1,109 @@
<?php
class TogglePostFavoriteJobTest extends AbstractTest
{
public function testFaving()
{
$this->grantAccess('favoritePost');
$user = $this->mockUser();
$this->login($user);
$post = $this->mockPost($user);
$this->assert->areEqual(0, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount());
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostFavoriteJob(),
[
ScorePostJob::POST_ID => $post->getId(),
ScorePostJob::STATE => 1,
]);
});
$this->assert->areEqual(1, $post->getScore());
$this->assert->areEqual(1, $post->getFavoriteCount());
$this->assert->areEqual(1, $user->getFavoriteCount());
$this->assert->areEqual(1, $user->getScore($post));
$this->assert->areEqual(true, $user->hasFavorited($post));
}
public function testDefaving()
{
$this->grantAccess('favoritePost');
$user = $this->mockUser();
$this->login($user);
$post = $this->mockPost($user);
$this->assert->areEqual(0, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount());
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostFavoriteJob(),
[
ScorePostJob::POST_ID => $post->getId(),
ScorePostJob::STATE => 1,
]);
});
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostFavoriteJob(),
[
ScorePostJob::POST_ID => $post->getId(),
ScorePostJob::STATE => 0,
]);
});
$this->assert->areEqual(1, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount());
$this->assert->areEqual(0, $user->getFavoriteCount());
$this->assert->areEqual(1, $user->getScore($post));
$this->assert->areEqual(false, $user->hasFavorited($post));
}
public function testFavingTwoPeople()
{
$this->grantAccess('favoritePost');
$user1 = $this->mockUser();
$user2 = $this->mockUser();
$post = $this->mockPost($user1);
$this->assert->areEqual(0, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount());
$this->login($user1);
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostFavoriteJob(),
[
ScorePostJob::POST_ID => $post->getId(),
ScorePostJob::STATE => 1,
]);
});
$this->login($user2);
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostFavoriteJob(),
[
ScorePostJob::POST_ID => $post->getId(),
ScorePostJob::STATE => 1,
]);
});
$this->assert->areEqual(2, $post->getScore());
$this->assert->areEqual(2, $post->getFavoriteCount());
$this->assert->areEqual(1, $user1->getFavoriteCount());
$this->assert->areEqual(1, $user1->getScore($post));
$this->assert->areEqual(1, $user2->getFavoriteCount());
$this->assert->areEqual(1, $user2->getScore($post));
$this->assert->areEqual(true, $user1->hasFavorited($post));
$this->assert->areEqual(true, $user2->hasFavorited($post));
}
}

View file

@ -0,0 +1,57 @@
<?php
class TogglePostVisibilityJobTest extends AbstractTest
{
public function testHiding()
{
$this->grantAccess('hidePost');
$this->login($this->mockUser());
$post = $this->mockPost(Auth::getCurrentUser());
$this->assert->isFalse($post->isHidden());
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostVisibilityJob(),
[
TogglePostVisibilityJob::POST_ID => $post->getId(),
TogglePostVisibilityJob::STATE => 0,
]);
});
$this->assert->isTrue($post->isHidden());
}
public function testShowing()
{
$this->grantAccess('hidePost');
$this->login($this->mockUser());
$post = $this->mockPost(Auth::getCurrentUser());
$this->assert->isFalse($post->isHidden());
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostVisibilityJob(),
[
TogglePostVisibilityJob::POST_ID => $post->getId(),
TogglePostVisibilityJob::STATE => 0,
]);
});
$this->assert->isTrue($post->isHidden());
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostVisibilityJob(),
[
TogglePostVisibilityJob::POST_ID => $post->getId(),
TogglePostVisibilityJob::STATE => 1,
]);
});
$this->assert->isFalse($post->isHidden());
}
}