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

HealTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
  protected CombatLogger $logger;
22
23
  use \Testbench\TCompiledContainer;
24
25
  public function setUp() {
26
    $this->logger = $this->getService(CombatLogger::class); // @phpstan-ignore assign.propertyType
27
  }
28
29
  protected function generateCharacter(int $id): Character {
30
    $stats = [
31
      "id" => $id, "name" => "Player $id", "level" => 1, "initiativeFormula" => "1d2+DEX/4", "strength" => 10,
32
      "dexterity" => 10, "constitution" => 10, "intelligence" => 10, "charisma" => 10
33
    ];
34
    return new Character($stats);
35
  }
36
37
  public function testShouldUse(): void {
38
    $character1 = $this->generateCharacter(1);
39
    $character2 = $this->generateCharacter(2);
40
    $combat = new CombatBase(clone $this->logger, new StaticSuccessCalculator());
41
    $combat->setDuelParticipants($character1, $character2);
42
    $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

42
    $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...
introduced by
The method parameter $team2 is never used
Loading history...
43
      return Team::fromArray($team1->toArray(), "healers");
44
    };
45
    $action = new Heal();
46
    Assert::false($action->shouldUse($combat, $character1));
47
    $character1->harm(30);
48
    Assert::true($action->shouldUse($combat, $character1));
49
    $character1->harm(20);
50
    Assert::false($action->shouldUse($combat, $character1));
51
  }
52
53
  public function testDo(): void {
54
    $character1 = $this->generateCharacter(1);
55
    $character2 = $this->generateCharacter(2);
56
    $combat = new CombatBase(clone $this->logger, new StaticSuccessCalculator());
57
    $combat->setDuelParticipants($character1, $character2);
58
    $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

58
    $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...
introduced by
The method parameter $team2 is never used
Loading history...
59
      return Team::fromArray($team1->toArray(), "healers");
60
    };
61
    $combat->onCombatStart($combat);
62
    $combat->onRoundStart($combat);
63
    $action = new Heal();
64
    $character1->harm(30);
65
    Assert::same(20, $character1->hitpoints);
66
    $action->do($combat, $character1);
67
    Assert::same(25, $character1->hitpoints);
68
    Assert::count(1, $combat->log);
69
    Assert::count(1, $combat->log->getIterator()[1]);
70
    /** @var CombatLogEntry $record */
71
    $record = $combat->log->getIterator()[1][0];
72
    Assert::type(CombatLogEntry::class, $record);
73
    Assert::same(Heal::ACTION_NAME, $record->action);
74
    Assert::same("", $record->name);
75
    Assert::true($record->result);
76
    Assert::same(5, $record->amount);
77
    Assert::same($character1->name, $record->character1->name);
78
    Assert::same($character1->name, $record->character2->name);
79
  }
80
}
81
82
$test = new HealTest();
83
$test->run();
84
?>