Passed
Push — master ( bc69e6...465240 )
by Jakub
12:35
created

AttackTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 2 1
A generateCharacter() 0 6 1
A testShouldUse() 0 8 1
A testDo() 0 22 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
  protected CombatLogger $logger;
21
22
  use \Testbench\TCompiledContainer;
23
24
  public function setUp() {
25
    $this->logger = $this->getService(CombatLogger::class); // @phpstan-ignore assign.propertyType
26
  }
27
28
  protected function generateCharacter(int $id): Character {
29
    $stats = [
30
      "id" => $id, "name" => "Player $id", "level" => 1, "initiativeFormula" => "1d2+DEX/4", "strength" => 10,
31
      "dexterity" => 10, "constitution" => 10, "intelligence" => 10, "charisma" => 10
32
    ];
33
    return new Character($stats);
34
  }
35
36
  public function testShouldUse(): void {
37
    $character1 = $this->generateCharacter(1);
38
    $character2 = $this->generateCharacter(2);
39
    $combat = new CombatBase(clone $this->logger, new StaticSuccessCalculator());
40
    $combat->setDuelParticipants($character1, $character2);
41
    $action = new Attack();
42
    Assert::true($action->shouldUse($combat, $character1));
43
    Assert::true($action->shouldUse($combat, $character2));
44
  }
45
46
  public function testDo(): void {
47
    $character1 = $this->generateCharacter(1);
48
    $character2 = $this->generateCharacter(2);
49
    $combat = new CombatBase(clone $this->logger, new StaticSuccessCalculator());
50
    $combat->setDuelParticipants($character1, $character2);
51
    $combat->onCombatStart($combat);
52
    $combat->onRoundStart($combat);
53
    $action = new Attack();
54
    $action->do($combat, $character1);
55
    Assert::same(45, $character2->hitpoints);
56
    Assert::same(5, $combat->team1Damage);
57
    Assert::count(1, $combat->log);
58
    Assert::count(1, $combat->log->getIterator()[1]);
59
    /** @var CombatLogEntry $record */
60
    $record = $combat->log->getIterator()[1][0];
61
    Assert::type(CombatLogEntry::class, $record);
62
    Assert::same(Attack::ACTION_NAME, $record->action);
63
    Assert::same("", $record->name);
64
    Assert::true($record->result);
65
    Assert::same(5, $record->amount);
66
    Assert::same($character1->name, $record->character1->name);
67
    Assert::same($character2->name, $record->character2->name);
68
  }
69
}
70
71
$test = new AttackTest();
72
$test->run();
73
?>