AttackTest::generateCharacter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 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\CombatBase;
9
use HeroesofAbenez\Combat\CombatLogger;
10
use HeroesofAbenez\Combat\StaticSuccessCalculator;
11
use HeroesofAbenez\Combat\CombatLogEntry;
12
13
require __DIR__ . "/../../../bootstrap.php";
14
15
/**
16
 * @author Jakub Konečný
17
 * @testCase
18
 */
19
final class AttackTest extends \Tester\TestCase
20
{
21
    use \Testbench\TCompiledContainer;
22
23
    private CombatLogger $logger;
24
25
    public function setUp(): void
26
    {
27
        $this->logger = $this->getService(CombatLogger::class); // @phpstan-ignore assign.propertyType
28
    }
29
30
    private function generateCharacter(int $id): Character
31
    {
32
        $stats = [
33
            "id" => $id, "name" => "Player $id", "level" => 1, "initiativeFormula" => "1d2+DEX/4", "strength" => 10,
34
            "dexterity" => 10, "constitution" => 10, "intelligence" => 10, "charisma" => 10
35
        ];
36
        return new Character($stats);
37
    }
38
39
    public function testShouldUse(): void
40
    {
41
        $character1 = $this->generateCharacter(1);
42
        $character2 = $this->generateCharacter(2);
43
        $combat = new CombatBase(clone $this->logger, new StaticSuccessCalculator());
44
        $combat->setDuelParticipants($character1, $character2);
45
        $action = new Attack();
46
        Assert::true($action->shouldUse($combat, $character1));
47
        Assert::true($action->shouldUse($combat, $character2));
48
    }
49
50
    public function testDo(): void
51
    {
52
        $character1 = $this->generateCharacter(1);
53
        $character2 = $this->generateCharacter(2);
54
        $combat = new CombatBase(clone $this->logger, new StaticSuccessCalculator());
55
        $combat->setDuelParticipants($character1, $character2);
56
        $combat->onCombatStart($combat);
57
        $combat->onRoundStart($combat);
58
        $action = new Attack();
59
        $action->do($combat, $character1);
60
        Assert::same(45, $character2->hitpoints);
61
        Assert::same(5, $combat->team1Damage);
62
        Assert::count(1, $combat->log);
63
        Assert::count(1, $combat->log->getIterator()[1]);
64
        /** @var CombatLogEntry $record */
65
        $record = $combat->log->getIterator()[1][0];
66
        Assert::type(CombatLogEntry::class, $record);
67
        Assert::same(Attack::ACTION_NAME, $record->action);
68
        Assert::same("", $record->name);
69
        Assert::true($record->result);
70
        Assert::same(5, $record->amount);
71
        Assert::same($character1->name, $record->character1->name);
72
        Assert::same($character2->name, $record->character2->name);
73
    }
74
}
75
76
$test = new AttackTest();
77
$test->run();
78