-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
Copy pathCommandTest.php
171 lines (143 loc) · 5.33 KB
/
CommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Tests\Unit\Commands;
use Longman\TelegramBot\Commands\Command;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Tests\Unit\TestCase;
use Longman\TelegramBot\Tests\Unit\TestHelpers;
/**
* @link https://github.com/php-telegram-bot/core
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @package TelegramTest
*/
class CommandTest extends TestCase
{
/**
* @var string
*/
private $command_namespace = Command::class;
/**
* @var Telegram
*/
private $telegram;
/**
* @var Command
*/
private $command_stub;
/**
* @var Telegram
*/
private $telegram_with_config;
/**
* @var Command
*/
private $command_stub_with_config;
public function setUp(): void
{
//Default command object
$this->telegram = new Telegram(self::$dummy_api_key, 'testbot');
$this->command_stub = $this->getMockForAbstractClass($this->command_namespace, [$this->telegram]);
//Create separate command object that contain a command config
$this->telegram_with_config = new Telegram(self::$dummy_api_key, 'testbot');
$this->telegram_with_config->setCommandConfig('command_name', ['config_key' => 'config_value']);
$this->command_stub_with_config = $this->getMockBuilder($this->command_namespace)
->disableOriginalConstructor()
->getMockForAbstractClass();
//Set a name for the object property so that the constructor can set the config correctly
TestHelpers::setObjectProperty($this->command_stub_with_config, 'name', 'command_name');
$this->command_stub_with_config->__construct($this->telegram_with_config);
}
// Test idea from here: http://stackoverflow.com/a/4371606
public function testCommandConstructorNeedsTelegramObject(): void
{
$exception_count = 0;
$params_to_test = [
[],
[null],
[12345],
['something'],
[new \stdClass()],
[$this->telegram], // only this one is valid
];
foreach ($params_to_test as $param) {
try {
$this->getMockForAbstractClass($this->command_namespace, $param);
} catch (\Throwable $e) {
$exception_count++;
}
}
self::assertEquals(5, $exception_count);
}
public function testCommandHasCorrectTelegramObject(): void
{
self::assertSame($this->telegram, $this->command_stub->getTelegram());
}
public function testDefaultCommandName(): void
{
self::assertEmpty($this->command_stub->getName());
}
public function testDefaultCommandDescription(): void
{
self::assertEquals('Command description', $this->command_stub->getDescription());
}
public function testDefaultCommandUsage(): void
{
self::assertEquals('Command usage', $this->command_stub->getUsage());
}
public function testDefaultCommandVersion(): void
{
self::assertEquals('1.0.0', $this->command_stub->getVersion());
}
public function testDefaultCommandIsEnabled(): void
{
self::assertTrue($this->command_stub->isEnabled());
}
public function testDefaultCommandShownInHelp(): void
{
self::assertTrue($this->command_stub->showInHelp());
}
public function testDefaultCommandNeedsMysql(): void
{
self::markTestSkipped('Think about better test');
}
public function testDefaultCommandEmptyConfig(): void
{
self::assertSame([], $this->command_stub->getConfig());
}
public function testDefaultCommandUpdateNull(): void
{
self::assertNull($this->command_stub->getUpdate());
}
public function testCommandSetUpdateAndMessage(): void
{
$stub = $this->command_stub;
self::assertEquals(null, $stub->getUpdate());
self::assertEquals(null, $stub->getMessage());
$update = TestHelpers::getFakeUpdateObject();
$message = $update->getMessage();
$stub->setUpdate($update);
self::assertEquals($update, $stub->getUpdate());
self::assertEquals($message, $stub->getMessage());
}
public function testCommandWithConfigNotEmptyConfig(): void
{
self::assertNotEmpty($this->command_stub_with_config->getConfig());
}
public function testCommandWithConfigCorrectConfig(): void
{
self::assertEquals(['config_key' => 'config_value'], $this->command_stub_with_config->getConfig());
self::assertEquals(['config_key' => 'config_value'], $this->command_stub_with_config->getConfig(null));
self::assertEquals(['config_key' => 'config_value'], $this->command_stub_with_config->getConfig());
self::assertEquals('config_value', $this->command_stub_with_config->getConfig('config_key'));
self::assertEquals(null, $this->command_stub_with_config->getConfig('not_config_key'));
}
}