TeamTest::testGetRowToAttack()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 13
rs 9.9
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
require __DIR__ . "/../../bootstrap.php";
7
8
use Tester\Assert;
9
10
/**
11
 * @author Jakub Konečný
12
 * @testCase
13
 */
14
final class TeamTest extends \Tester\TestCase
15
{
16
    private function generateCharacter(int $id): Character
17
    {
18
        $stats = [
19
            "id" => $id, "name" => "Player $id", "level" => 1, "initiativeFormula" => "1d2+DEX/4", "strength" => 10,
20
            "dexterity" => 10, "constitution" => 10, "intelligence" => 10, "charisma" => 10
21
        ];
22
        return new Character($stats);
23
    }
24
25
    public function testGetName(): void
26
    {
27
        $name = "Team 1";
28
        $team = new Team($name);
29
        Assert::same($name, $team->name);
30
    }
31
32
    public function testMaxRowSize(): void
33
    {
34
        $team = new Team("");
35
        $team->maxRowSize = 1;
36
        Assert::same(1, $team->maxRowSize);
37
    }
38
39
    public function testGetAliveMembers(): void
40
    {
41
        $team = new Team("");
42
        Assert::count(0, $team->aliveMembers);
43
        $team[] = $this->generateCharacter(1);
44
        Assert::count(1, $team->aliveMembers);
45
        $team[] = $this->generateCharacter(2);
46
        Assert::count(2, $team->aliveMembers);
47
        $team[0]->harm($team[0]->maxHitpoints);
48
        Assert::count(1, $team->aliveMembers);
49
    }
50
51
    public function testGetUsableMembers(): void
52
    {
53
        $team = new Team("");
54
        Assert::count(0, $team->usableMembers);
55
        $team[] = $this->generateCharacter(1);
56
        Assert::count(1, $team->usableMembers);
57
        $team[] = $this->generateCharacter(2);
58
        Assert::count(2, $team->usableMembers);
59
        $team[0]->harm($team[0]->maxHitpoints);
60
        Assert::count(1, $team->usableMembers);
61
        $stunEffect = [
62
            "id" => "stunEffect", "type" => SkillSpecial::TYPE_STUN, "valueAbsolute" => false,
63
            "value" => 0, "duration" => CharacterEffect::DURATION_FOREVER, "stat" => "",
64
        ];
65
        $team[1]->effects[] = new CharacterEffect($stunEffect);
66
        $team[1]->recalculateStats();
67
        Assert::count(0, $team->usableMembers);
68
    }
69
70
    public function testHasAliveMembers(): void
71
    {
72
        $team = new Team("");
73
        Assert::false($team->hasAliveMembers());
74
        $team[] = $this->generateCharacter(1);
75
        Assert::true($team->hasAliveMembers());
76
        $team[0]->harm($team[0]->maxHitpoints);
77
        Assert::false($team->hasAliveMembers());
78
    }
79
80
    public function testSetCharacterPosition(): void
81
    {
82
        $team = new Team("");
83
        $team[] = $this->generateCharacter(1);
84
        $team->setCharacterPosition(1, 1, 1);
85
        Assert::same(1, $team[0]->positionRow);
86
        Assert::same(1, $team[0]->positionColumn);
87
        Assert::exception(function () use ($team) {
88
            $team->setCharacterPosition(2, 1, 1);
89
        }, \OutOfBoundsException::class);
90
        $team[] = $this->generateCharacter(2);
91
        Assert::exception(function () use ($team) {
92
            $team->setCharacterPosition(2, 1, 1);
93
        }, InvalidCharacterPositionException::class, null, InvalidCharacterPositionException::POSITION_OCCUPIED);
94
        Assert::exception(function () use ($team) {
95
            $team->maxRowSize = 1;
96
            $team->setCharacterPosition(2, 1, 2);
97
        }, InvalidCharacterPositionException::class, null, InvalidCharacterPositionException::ROW_FULL);
98
    }
99
100
    public function testGetRandomCharacter(): void
101
    {
102
        $team = new Team("");
103
        Assert::null($team->getRandomCharacter());
104
        $team[] = $this->generateCharacter(1);
105
        Assert::same($team[0], $team->getRandomCharacter());
106
        $team[] = $this->generateCharacter(2);
107
        Assert::type(Character::class, $team->getRandomCharacter());
108
    }
109
110
    public function testGetLowestHpCharacter(): void
111
    {
112
        $team = new Team("");
113
        Assert::null($team->getLowestHpCharacter());
114
        $team[] = $this->generateCharacter(1);
115
        Assert::null($team->getLowestHpCharacter());
116
        $team[0]->harm(10);
117
        Assert::same($team[0], $team->getLowestHpCharacter(0.8));
118
        Assert::null($team->getLowestHpCharacter());
119
        $team[0]->harm(20);
120
        Assert::same($team[0], $team->getLowestHpCharacter());
121
    }
122
123
    public function testGetRowToAttack(): void
124
    {
125
        $team = new Team("");
126
        Assert::null($team->rowToAttack);
127
        $team[] = $character1 = $this->generateCharacter(1);
128
        $character1->positionRow = 1;
129
        $team[] = $character2 = $this->generateCharacter(2);
130
        $character2->positionRow = 2;
131
        Assert::same(1, $team->rowToAttack);
132
        $character1->harm($character1->maxHitpoints);
133
        Assert::same(2, $team->rowToAttack);
134
        $character2->harm($character2->maxHitpoints);
135
        Assert::null($team->rowToAttack);
136
    }
137
}
138
139
$test = new TeamTest();
140
$test->run();
141