SkillAttackTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 35
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 2 1
A generateCharacter() 0 12 1
A testDo() 0 25 2
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
use HeroesofAbenez\Combat\SkillAttack as Skill;
13
use HeroesofAbenez\Combat\CharacterAttackSkill as CharacterSkill;
14
15
require __DIR__ . "/../../../bootstrap.php";
16
17
/**
18
 * @author Jakub Konečný
19
 * @testCase
20
 */
21
final class SkillAttackTest extends \Tester\TestCase {
22
  protected CombatLogger $logger;
23
24
  use \Testbench\TCompiledContainer;
25
26
  public function setUp() {
27
    $this->logger = $this->getService(CombatLogger::class); // @phpstan-ignore assign.propertyType
28
  }
29
30
  protected function generateCharacter(int $id): Character {
31
    $stats = [
32
      "id" => $id, "name" => "Player $id", "level" => 1, "initiativeFormula" => "1d2+DEX/4", "strength" => 10,
33
      "dexterity" => 10, "constitution" => 10, "intelligence" => 10, "charisma" => 10
34
    ];
35
    $skillData = [
36
      "id" => 1, "name" => "Skill Attack", "baseDamage" => "60%", "damageGrowth" => "20%", "levels" => 5,
37
      "target" => Skill::TARGET_SINGLE, "strikes" => 2, "hitRate" => "100%",
38
    ];
39
    $skill = new Skill($skillData);
40
    $characterSkill = new CharacterSkill($skill, 2);
41
    return new Character($stats, [], [], [$characterSkill]);
42
  }
43
44
  /*public function testShouldUse(): void {
0 ignored issues
show
introduced by
This comment is 62% valid code; is this commented out code?
Loading history...
45
    $character1 = $this->generateCharacter(1);
46
    $character2 = $this->generateCharacter(2);
47
    $combat = new CombatBase(clone $this->logger, new StaticSuccessCalculator());
48
    $combat->setDuelParticipants($character1, $character2);
49
    $action = new SkillAttack();
50
    Assert::false($action->shouldUse($combat, $character1));
51
    for($i = 1; $i <= $character1->skills[0]->skill->cooldown; $i++) {
52
      $character1->skills[0]->decreaseCooldown();
53
    }
54
    Assert::true($action->shouldUse($combat, $character1));
55
  }*/
56
57
  public function testDo(): void {
58
    $character1 = $this->generateCharacter(1);
59
    $character2 = $this->generateCharacter(2);
60
    $combat = new CombatBase(clone $this->logger, new StaticSuccessCalculator());
61
    $combat->setDuelParticipants($character1, $character2);
62
    $combat->onCombatStart($combat);
63
    $combat->onRoundStart($combat);
64
    for($i = 1; $i <= $character1->skills[0]->skill->cooldown; $i++) {
65
      $character1->skills[0]->decreaseCooldown();
66
    }
67
    $action = new SkillAttack();
68
    $action->do($combat, $character1);
69
    Assert::same(42, $character2->hitpoints);
70
    Assert::same(8, $combat->team1Damage);
71
    Assert::count(1, $combat->log);
72
    Assert::count(2, $combat->log->getIterator()[1]);
73
    /** @var CombatLogEntry $record */
74
    $record = $combat->log->getIterator()[1][0];
75
    Assert::type(CombatLogEntry::class, $record);
76
    Assert::same(SkillAttack::ACTION_NAME, $record->action);
77
    Assert::same("Skill Attack", $record->name);
78
    Assert::true($record->result);
79
    Assert::same(4, $record->amount);
80
    Assert::same($character1->name, $record->character1->name);
81
    Assert::same($character2->name, $record->character2->name);
82
  }
83
}
84
85
$test = new SkillAttackTest();
86
$test->run();
87
?>