HealTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 43
c 1
b 0
f 0
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testDo() 0 27 1
A generateCharacter() 0 7 1
A testShouldUse() 0 15 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat\CombatActions;
5
6
use Tester\Assert;
7
use HeroesofAbenez\Combat\Character;
8
use HeroesofAbenez\Combat\Team;
9
use HeroesofAbenez\Combat\CombatBase;
10
use HeroesofAbenez\Combat\CombatLogger;
11
use HeroesofAbenez\Combat\StaticSuccessCalculator;
12
use HeroesofAbenez\Combat\CombatLogEntry;
13
14
require __DIR__ . "/../../../bootstrap.php";
15
16
/**
17
 * @author Jakub Konečný
18
 * @testCase
19
 */
20
final class HealTest extends \Tester\TestCase
21
{
22
    use \Testbench\TCompiledContainer;
23
24
    private CombatLogger $logger;
25
26
    public function setUp(): void
27
    {
28
        $this->logger = $this->getService(CombatLogger::class); // @phpstan-ignore assign.propertyType
29
    }
30
31
    private function generateCharacter(int $id): Character
32
    {
33
        $stats = [
34
            "id" => $id, "name" => "Player $id", "level" => 1, "initiativeFormula" => "1d2+DEX/4", "strength" => 10,
35
            "dexterity" => 10, "constitution" => 10, "intelligence" => 10, "charisma" => 10
36
        ];
37
        return new Character($stats);
38
    }
39
40
    public function testShouldUse(): void
41
    {
42
        $character1 = $this->generateCharacter(1);
43
        $character2 = $this->generateCharacter(2);
44
        $combat = new CombatBase(clone $this->logger, new StaticSuccessCalculator());
45
        $combat->setDuelParticipants($character1, $character2);
46
        $combat->healers = function (Team $team1, Team $team2) {
0 ignored issues
show
Unused Code introduced by
The parameter $team2 is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

46
        $combat->healers = function (Team $team1, /** @scrutinizer ignore-unused */ Team $team2) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
            return Team::fromArray($team1->toArray(), "healers");
48
        };
49
        $action = new Heal();
50
        Assert::false($action->shouldUse($combat, $character1));
51
        $character1->harm(30);
52
        Assert::true($action->shouldUse($combat, $character1));
53
        $character1->harm(20);
54
        Assert::false($action->shouldUse($combat, $character1));
55
    }
56
57
    public function testDo(): void
58
    {
59
        $character1 = $this->generateCharacter(1);
60
        $character2 = $this->generateCharacter(2);
61
        $combat = new CombatBase(clone $this->logger, new StaticSuccessCalculator());
62
        $combat->setDuelParticipants($character1, $character2);
63
        $combat->healers = function (Team $team1, Team $team2) {
0 ignored issues
show
Unused Code introduced by
The parameter $team2 is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
        $combat->healers = function (Team $team1, /** @scrutinizer ignore-unused */ Team $team2) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
            return Team::fromArray($team1->toArray(), "healers");
65
        };
66
        $combat->onCombatStart($combat);
67
        $combat->onRoundStart($combat);
68
        $action = new Heal();
69
        $character1->harm(30);
70
        Assert::same(20, $character1->hitpoints);
71
        $action->do($combat, $character1);
72
        Assert::same(25, $character1->hitpoints);
73
        Assert::count(1, $combat->log);
74
        Assert::count(1, $combat->log->getIterator()[1]);
75
        /** @var CombatLogEntry $record */
76
        $record = $combat->log->getIterator()[1][0];
77
        Assert::type(CombatLogEntry::class, $record);
78
        Assert::same(Heal::ACTION_NAME, $record->action);
79
        Assert::same("", $record->name);
80
        Assert::true($record->result);
81
        Assert::same(5, $record->amount);
82
        Assert::same($character1->name, $record->character1->name);
83
        Assert::same($character1->name, $record->character2->name);
84
    }
85
}
86
87
$test = new HealTest();
88
$test->run();
89