Skip to content

Commit

Permalink
add votesDiff method
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamednagy committed Jan 22, 2018
1 parent 81c2313 commit d6e91dc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Traits/Votable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public function downVotesCount()
{
return $this->votes()->where('value', 0)->count();
}

public function votesDiff()
{
return $this->upVotesCount() - $this->downVotesCount();
}
}
13 changes: 13 additions & 0 deletions tests/VotingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,17 @@ public function it_returns_total_votes_count()

$this->assertTrue($post->votesCount() == 2);
}

/** @test */
public function it_returns_votes_diff()
{
$user = User::create(['name' => 'test']);
$user2 = User::create(['name' => 'test2']);
$post = Post::create(['name' => 'test post']);

$user->upVote($post);
$user2->downVote($post);

$this->assertTrue($post->votesDiff() == 0);
}
}

0 comments on commit d6e91dc

Please sign in to comment.