ChatCommandsProcessorTest::testParse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
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
{
16
    use \Testbench\TCompiledContainer;
17
18
    private const COMMAND_NAME = "test1";
19
    private const TEXT = "/" . self::COMMAND_NAME;
20
21
    protected ChatCommandsProcessor $model;
22
23
    public function __construct()
24
    {
25
        $this->model = $this->getService(ChatCommandsProcessor::class); // @phpstan-ignore assign.propertyType
26
        $this->model->addCommand(new TestCommand());
27
    }
28
29
    public function testAddCommand(): void
30
    {
31
        $model = clone $this->model;
32
        $model->addCommand(new Test2Command());
33
        Assert::same("test", $model->parse("/" . Test2Command::NAME));
34
        Assert::exception(function () use ($model) {
35
            $model->addCommand(new Test2Command());
36
        }, CommandNameAlreadyUsedException::class);
37
    }
38
39
    public function testAddAlias(): void
40
    {
41
        $model = clone $this->model;
42
        $model->addAlias(self::COMMAND_NAME, "test");
43
        Assert::same("passed", $model->parse("/test"));
44
        Assert::exception(function () use ($model) {
45
            $model->addAlias("abc", "test");
46
        }, CommandNotFoundException::class);
47
        Assert::exception(function () use ($model) {
48
            $model->addAlias(self::COMMAND_NAME, "test");
49
        }, CommandNameAlreadyUsedException::class);
50
    }
51
52
    public function testExtractCommand(): void
53
    {
54
        Assert::same("", $this->model->extractCommand("anagfdffd"));
55
        Assert::same("", $this->model->extractCommand("/anagfdffd"));
56
        Assert::same(self::COMMAND_NAME, $this->model->extractCommand(self::TEXT));
57
    }
58
59
    /**
60
     * @return array<int, string[]>
61
     */
62
    public function getTexts(): array
63
    {
64
        return [
65
            ["anagfdffd", "/anagfdffd",]
66
        ];
67
    }
68
69
    /**
70
     * @dataProvider getTexts
71
     */
72
    public function testExtractParametersNothing(string $text): void
73
    {
74
        $result = $this->model->extractParameters($text);
75
        Assert::type("array", $result);
76
        Assert::count(0, $result);
77
    }
78
79
    public function testExtractParameters(): void
80
    {
81
        $result = $this->model->extractParameters("/test abc 123");
82
        Assert::type("array", $result);
83
        Assert::count(2, $result);
84
    }
85
86
    public function testHasCommand(): void
87
    {
88
        Assert::false($this->model->hasCommand("anagfdffd"));
89
        Assert::true($this->model->hasCommand(self::COMMAND_NAME));
90
    }
91
92
    public function testGetCommand(): void
93
    {
94
        Assert::type(IChatCommand::class, $this->model->getCommand(self::COMMAND_NAME));
95
        Assert::exception(function () {
96
            $this->model->getCommand("abc");
97
        }, CommandNotFoundException::class);
98
    }
99
100
    public function testParse(): void
101
    {
102
        $model = clone $this->model;
103
        $model->addCommand(new Test2Command());
104
        Assert::same("passed", $this->model->parse(self::TEXT));
105
        Assert::null($model->parse("anagfdffd"));
106
        Assert::null($model->parse("/anagfdffd"));
107
        Assert::same("test12", $model->parse("/test2 1 2"));
108
    }
109
}
110
111
$test = new ChatCommandsProcessorTest();
112
$test->run();
113