1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Michaelc\Voting; |
4
|
|
|
|
5
|
|
|
use Michaelc\Voting\STV\Ballot; |
6
|
|
|
use Michaelc\Voting\STV\Candidate; |
7
|
|
|
use Michaelc\Voting\STV\Election; |
8
|
|
|
use Michaelc\Voting\STV\VoteHandler; |
9
|
|
|
use Psr\Log\LoggerInterface as Logger; |
10
|
|
|
|
11
|
|
|
class StvElectionTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
public function testNewElection() |
14
|
|
|
{ |
15
|
|
|
$winners = 2; |
16
|
|
|
$candidateCount = 6; |
17
|
|
|
|
18
|
|
|
$candidates = $ballots = []; |
19
|
|
|
|
20
|
|
|
for ($i = 1; $i <= $candidateCount; ++$i) { |
21
|
|
|
$candidates[$i] = new Candidate($i); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$ballots[] = new Ballot([4, 5, 6]); |
25
|
|
|
$ballots[] = new Ballot([1, 2, 3]); |
26
|
|
|
|
27
|
|
|
$election = new Election($winners, $candidates, $ballots); |
28
|
|
|
|
29
|
|
|
$this->assertEquals($candidates[3], $election->getCandidate(3)); |
30
|
|
|
$this->assertEquals($candidateCount, $election->getCandidateCount()); |
31
|
|
|
$this->assertEquals($candidateCount, $election->getActiveCandidateCount()); |
32
|
|
|
$this->assertEquals($winners, $election->getWinnersCount()); |
33
|
|
|
$this->assertEquals(2, $election->getNumBallots()); |
34
|
|
|
|
35
|
|
|
$candidateIds = $election->getCandidateIds(); |
36
|
|
|
$this->assertCount($candidateCount, $candidateIds); |
37
|
|
|
|
38
|
|
|
for ($i = 1; $i <= $candidateCount; ++$i) { |
39
|
|
|
$this->assertContains($i, $candidateIds); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testElectionRunner() |
44
|
|
|
{ |
45
|
|
|
$election = $this->getSampleElection(); |
46
|
|
|
$logger = $this->createMock(Logger::class); |
47
|
|
|
|
48
|
|
|
$runner = new VoteHandler($election, $logger); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testCandidatesStateSetting() |
52
|
|
|
{ |
53
|
|
|
$election = $this->getSampleElection(); |
54
|
|
|
|
55
|
|
|
$election->getCandidates()[5]->setState(Candidate::ELECTED); |
56
|
|
|
$election->getCandidates()[6]->setState(Candidate::ELECTED); |
57
|
|
|
|
58
|
|
|
$election->getCandidates()[10]->setState(Candidate::DEFEATED); |
59
|
|
|
$election->getCandidates()[11]->setState(Candidate::DEFEATED); |
60
|
|
|
|
61
|
|
|
$this->assertEquals(Candidate::ELECTED, $election->getCandidates()[5]->getState()); |
62
|
|
|
$this->assertEquals(Candidate::ELECTED, $election->getCandidates()[6]->getState()); |
63
|
|
|
|
64
|
|
|
$this->assertEquals(Candidate::DEFEATED, $election->getCandidates()[10]->getState()); |
65
|
|
|
$this->assertEquals(Candidate::DEFEATED, $election->getCandidates()[11]->getState()); |
66
|
|
|
|
67
|
|
|
$this->assertEquals(Candidate::RUNNING, $election->getCandidates()[1]->getState()); |
68
|
|
|
$this->assertEquals(Candidate::RUNNING, $election->getCandidates()[2]->getState()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testCandidatesFetchingByState() |
72
|
|
|
{ |
73
|
|
|
$election = $this->getSampleElection(); |
74
|
|
|
|
75
|
|
|
$election->getCandidates()[5]->setState(Candidate::ELECTED); |
76
|
|
|
$election->getCandidates()[6]->setState(Candidate::ELECTED); |
77
|
|
|
|
78
|
|
|
$election->getCandidates()[10]->setState(Candidate::DEFEATED); |
79
|
|
|
|
80
|
|
|
$active = $election->getActiveCandidates(); |
81
|
|
|
$defeated = $election->getDefeatedCandidates(); |
82
|
|
|
$elected = $election->getElectedCandidates(); |
83
|
|
|
|
84
|
|
|
$this->assertContains($election->getCandidates()[5], $elected); |
85
|
|
|
$this->assertContains($election->getCandidates()[6], $elected); |
86
|
|
|
$this->assertContains($election->getCandidates()[10], $defeated); |
87
|
|
|
$this->assertContains($election->getCandidates()[1], $active); |
88
|
|
|
|
89
|
|
|
$this->assertCount(2, $elected); |
90
|
|
|
$this->assertCount(1, $defeated); |
91
|
|
|
$this->assertCount(($election->getCandidateCount() - 3), $active); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function getSampleElection() |
95
|
|
|
{ |
96
|
|
|
$candidates = $ballots = []; |
97
|
|
|
|
98
|
|
|
for ($i = 1; $i <= 20; ++$i) { |
99
|
|
|
$candidates[$i] = new Candidate($i); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
for ($i = 0; $i <= 40; ++$i) { |
103
|
|
|
$ballots[] = new Ballot([random_int(1, 20), random_int(1, 20), random_int(1, 20)]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$election = new Election(12, $candidates, $ballots); |
107
|
|
|
|
108
|
|
|
return $election; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.