Skip to content

Commit

Permalink
Add tests for Election:jsonVotes
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed Jan 12, 2018
1 parent 5f55d58 commit 14ff68c
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Tests/lib/ElectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,58 @@ public function testVoteWeight ()

}

public function testJsonVotes ()
{
$election = new Election;

$election->addCandidate('A');
$election->addCandidate('B');
$election->addCandidate('C');

$votes = [];

$votes[]['vote'] = 'B>C>A';
$votes[]['vote'] = new \stdClass(); // Invalid Vote
$votes[]['vote'] = ['C','B','A'];

$election->jsonVotes(json_encode($votes));

self::assertSame(
'B > C > A * 1
C > B > A * 1',
$election->getVotesListAsString()
);

$votes = [];

$votes[0]['vote'] = 'A>B>C';
$votes[0]['multi'] = 5;
$votes[0]['tag'] = 'tag1';

$election->jsonVotes(json_encode($votes));

self::assertSame(
'A > B > C * 5
B > C > A * 1
C > B > A * 1',
$election->getVotesListAsString()
);
self::assertSame(5,$election->countVotes('tag1'));
}

/**
* @expectedException Condorcet\CondorcetException
* @expectedExceptionCode 15
*/
public function testJsonVotesWithInvalidJson ()
{
self::assertFalse($this->election1->jsonVotes("42"));
self::assertFalse($this->election1->jsonVotes(42));
self::assertFalse($this->election1->jsonVotes(false));
self::assertFalse($this->election1->jsonVotes(true));
self::assertFalse($$this->election1->jsonVotes(""));
self::assertFalse($$this->election1->jsonVotes(" "));
self::assertFalse($$this->election1->jsonVotes([]));
self::assertFalse($$this->election1->jsonVotes(json_encode(new \stdClass())));
}
}

0 comments on commit 14ff68c

Please sign in to comment.