Completed
Push — master ( ce3c79...bfc6cf )
by Michael
03:47
created

StvElectionTest::testCandidatesFetchingByState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
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
36
    public function testElectionRunner()
37
    {
38
        $election = $this->getSampleElection();
39
        $logger = $this->createMock(Logger::class);
40
41
        $runner = new VoteHandler($election, $logger);
0 ignored issues
show
Unused Code introduced by
$runner is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
42
    }
43
44
    public function testCandidatesStateSetting()
45
    {
46
        $election = $this->getSampleElection();
47
48
        $election->getCandidates()[5]->setState(Candidate::ELECTED);
49
        $election->getCandidates()[6]->setState(Candidate::ELECTED);
50
51
        $election->getCandidates()[10]->setState(Candidate::DEFEATED);
52
        $election->getCandidates()[11]->setState(Candidate::DEFEATED);
53
54
        $this->assertEquals(Candidate::ELECTED, $election->getCandidates()[5]->getState());
55
        $this->assertEquals(Candidate::ELECTED, $election->getCandidates()[6]->getState());
56
57
        $this->assertEquals(Candidate::DEFEATED, $election->getCandidates()[10]->getState());
58
        $this->assertEquals(Candidate::DEFEATED, $election->getCandidates()[11]->getState());
59
60
        $this->assertEquals(Candidate::RUNNING, $election->getCandidates()[1]->getState());
61
        $this->assertEquals(Candidate::RUNNING, $election->getCandidates()[2]->getState());
62
    }
63
64
    public function testCandidatesFetchingByState()
65
    {
66
        $election = $this->getSampleElection();
67
68
        $election->getCandidates()[5]->setState(Candidate::ELECTED);
69
        $election->getCandidates()[6]->setState(Candidate::ELECTED);
70
71
        $election->getCandidates()[10]->setState(Candidate::DEFEATED);
72
73
        $active = $election->getActiveCandidates();
74
        $defeated = $election->getDefeatedCandidates();
75
        $elected = $election->getElectedCandidates();
76
77
        $this->assertContains($election->getCandidates()[5], $elected);
78
        $this->assertContains($election->getCandidates()[6], $elected);
79
        $this->assertContains($election->getCandidates()[10], $defeated);
80
        $this->assertContains($election->getCandidates()[1], $active);
81
82
        $this->assertCount(2, $elected);
83
        $this->assertCount(1, $defeated);
84
        $this->assertCount(($election->getCandidateCount() - 3), $active);
85
    }
86
87
    protected function getSampleElection()
88
    {
89
        $candidates = $ballots = [];
90
91
        for ($i = 1; $i <= 20; ++$i) {
92
            $candidates[$i] = new Candidate($i);
93
        }
94
95
        for ($i = 0; $i <= 40; ++$i) {
96
            $ballots[] = new Ballot([random_int(1, 20), random_int(1, 20), random_int(1, 20)]);
97
        }
98
99
        $election = new Election(12, $candidates, $ballots);
100
101
        return $election;
102
    }
103
}
104