ChatCommandsProcessorTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 45
c 0
b 0
f 0
dl 0
loc 83
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A testGetCommand() 0 5 1
A testAddCommand() 0 7 1
A getTexts() 0 3 1
A testParse() 0 7 1
A testHasCommand() 0 3 1
A testExtractParametersNothing() 0 4 1
A testExtractCommand() 0 4 1
A testAddAlias() 0 10 1
A testExtractParameters() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Chat;
5
6
use Tester\Assert;
7
8
require __DIR__ . "/../../bootstrap.php";
9
10
/**
11
 * @author Jakub Konečný
12
 * @testCase
13
 */
14
final class ChatCommandsProcessorTest extends \Tester\TestCase {
15
  const COMMAND_NAME = "test1";
16
  const TEXT = "/" . self::COMMAND_NAME;
17
18
  protected ChatCommandsProcessor $model;
19
  
20
  use \Testbench\TCompiledContainer;
21
  
22
  public function __construct() {
23
    $this->model = $this->getService(ChatCommandsProcessor::class); // @phpstan-ignore assign.propertyType
24
    $this->model->addCommand(new TestCommand());
25
  }
26
  
27
  public function testAddCommand(): void {
28
    $model = clone $this->model;
29
    $model->addCommand(new Test2Command());
30
    Assert::same("test", $model->parse("/" . Test2Command::NAME));
31
    Assert::exception(function() use($model) {
32
      $model->addCommand(new Test2Command());
33
    }, CommandNameAlreadyUsedException::class);
34
  }
35
  
36
  public function testAddAlias(): void {
37
    $model = clone $this->model;
38
    $model->addAlias(self::COMMAND_NAME, "test");
39
    Assert::same("passed", $model->parse("/test"));
40
    Assert::exception(function() use($model) {
41
      $model->addAlias("abc", "test");
42
    }, CommandNotFoundException::class);
43
    Assert::exception(function() use($model) {
44
      $model->addAlias(self::COMMAND_NAME, "test");
45
    }, CommandNameAlreadyUsedException::class);
46
  }
47
  
48
  public function testExtractCommand(): void {
49
    Assert::same("", $this->model->extractCommand("anagfdffd"));
50
    Assert::same("", $this->model->extractCommand("/anagfdffd"));
51
    Assert::same(self::COMMAND_NAME, $this->model->extractCommand(self::TEXT));
52
  }
53
  
54
  /**
55
   * @return array<int, string[]>
56
   */
57
  public function getTexts(): array {
58
    return [
59
      ["anagfdffd", "/anagfdffd", ]
60
    ];
61
  }
62
  
63
  /**
64
   * @dataProvider getTexts
65
   */
66
  public function testExtractParametersNothing(string $text): void {
67
    $result = $this->model->extractParameters($text);
68
    Assert::type("array", $result);
69
    Assert::count(0, $result);
70
  }
71
  
72
  public function testExtractParameters(): void {
73
    $result = $this->model->extractParameters("/test abc 123");
74
    Assert::type("array", $result);
75
    Assert::count(2, $result);
76
  }
77
  
78
  public function testHasCommand(): void {
79
    Assert::false($this->model->hasCommand("anagfdffd"));
80
    Assert::true($this->model->hasCommand(self::COMMAND_NAME));
81
  }
82
  
83
  public function testGetCommand(): void {
84
    Assert::type(IChatCommand::class, $this->model->getCommand(self::COMMAND_NAME));
85
    Assert::exception(function() {
86
      $this->model->getCommand("abc");
87
    }, CommandNotFoundException::class);
88
  }
89
  
90
  public function testParse(): void {
91
    $model = clone $this->model;
92
    $model->addCommand(new Test2Command());
93
    Assert::same("passed", $this->model->parse(self::TEXT));
94
    Assert::null($model->parse("anagfdffd"));
95
    Assert::null($model->parse("/anagfdffd"));
96
    Assert::same("test12", $model->parse("/test2 1 2"));
97
  }
98
}
99
100
$test = new ChatCommandsProcessorTest();
101
$test->run();
102
?>