4ba83e6834
Restored JobArgs approach. Previous introduction of hierarchic argument definitions has backfired: it was confusing what class to take arguments from, the concept of sharing arguments between different jobs was unintelligible and one never knew where given argument was actually defined. This appraoch makes it easier to maintain the arguments list and simplifies the code a lot.
109 lines
3 KiB
PHP
109 lines
3 KiB
PHP
<?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(),
|
|
[
|
|
JobArgs::ARG_POST_ID => $post->getId(),
|
|
JobArgs::ARG_NEW_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(),
|
|
[
|
|
JobArgs::ARG_POST_ID => $post->getId(),
|
|
JobArgs::ARG_NEW_STATE => 1,
|
|
]);
|
|
});
|
|
|
|
$post = $this->assert->doesNotThrow(function() use ($post)
|
|
{
|
|
return Api::run(
|
|
new TogglePostFavoriteJob(),
|
|
[
|
|
JobArgs::ARG_POST_ID => $post->getId(),
|
|
JobArgs::ARG_NEW_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(),
|
|
[
|
|
JobArgs::ARG_POST_ID => $post->getId(),
|
|
JobArgs::ARG_NEW_STATE => 1,
|
|
]);
|
|
});
|
|
|
|
$this->login($user2);
|
|
$post = $this->assert->doesNotThrow(function() use ($post)
|
|
{
|
|
return Api::run(
|
|
new TogglePostFavoriteJob(),
|
|
[
|
|
JobArgs::ARG_POST_ID => $post->getId(),
|
|
JobArgs::ARG_NEW_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));
|
|
}
|
|
}
|