Skip to content

Commit 2eeb333

Browse files
committed
Issue #1205
Add tests and update changed code
1 parent fb440a5 commit 2eeb333

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

src/Telegram.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
defined('TB_BASE_COMMANDS_PATH') || define('TB_BASE_COMMANDS_PATH', TB_BASE_PATH . '/Commands');
1616

1717
use Exception;
18+
use InvalidArgumentException;
1819
use Longman\TelegramBot\Commands\AdminCommand;
1920
use Longman\TelegramBot\Commands\Command;
2021
use Longman\TelegramBot\Commands\SystemCommand;
@@ -310,10 +311,11 @@ public function getCommandsList(): array
310311
*
311312
* @return string|null
312313
*/
313-
private function getCommandClassName(string $auth, string $command): ?string
314+
public function getCommandClassName(string $auth, string $command): ?string
314315
{
315316
$command = mb_strtolower($command);
316317
$auth = $this->ucFirstUnicode($auth);
318+
317319
if (!empty($this->commandsClasses[$auth][$command])) {
318320
$className = $this->commandsClasses[$auth][$command];
319321
if (class_exists($className)){
@@ -774,11 +776,15 @@ public function addCommandsClass(string $command, string $className, $auth = 'Us
774776
{
775777
if (!class_exists($className))
776778
{
777-
TelegramLog::error('Command class name: "' . $className . '" does not exist.');
779+
$error = 'Command class name: "' . $className . '" does not exist.';
780+
TelegramLog::error($error);
781+
throw new InvalidArgumentException($error);
778782
}
779-
if (!empty($command))
783+
if (empty($command))
780784
{
781-
TelegramLog::error('Command Name "' . $command . '" not set.');
785+
$error = 'Command Name "' . $command . '" not set.';
786+
TelegramLog::error($error);
787+
throw new InvalidArgumentException($error);
782788
}
783789

784790
if (!is_array($this->commandsClasses))
@@ -844,6 +850,16 @@ public function getCommandsPaths(): array
844850
return $this->commands_paths;
845851
}
846852

853+
/**
854+
* Return the list of commands classes
855+
*
856+
* @return array
857+
*/
858+
public function getCommandsClasses(): array
859+
{
860+
return $this->commandsClasses;
861+
}
862+
847863
/**
848864
* Set custom upload path
849865
*

tests/Unit/TelegramTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Longman\TelegramBot\Tests\Unit;
1313

1414
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
15+
use Exception;
16+
use Longman\TelegramBot\Commands\UserCommands\StartCommand;
1517
use Longman\TelegramBot\Entities\Update;
1618
use Longman\TelegramBot\Exception\TelegramException;
1719
use Longman\TelegramBot\Telegram;
@@ -133,6 +135,33 @@ public function testAddCustomCommandsPaths(): void
133135
self::assertCount(4, $tg->getCommandsPaths());
134136
}
135137

138+
public function testAddCustomCommandsClass(): void
139+
{
140+
$tg = $this->telegram;
141+
$class = StartCommand::class;
142+
143+
self::assertCount(3, $tg->getCommandsClasses());
144+
145+
try {
146+
$tg->addCommandsClass('test', 'not\exist\Class', 'user');
147+
}
148+
catch (\Exception $ex){}
149+
self::assertCount(0, $tg->getCommandsClasses()['User']);
150+
151+
try {
152+
$tg->addCommandsClass('', $class, 'user');
153+
}
154+
catch (\Exception $ex){}
155+
self::assertCount(0, $tg->getCommandsClasses()['User']);
156+
157+
$tg->addCommandsClass('test', $class, 'user');
158+
self::assertCount(1, $tg->getCommandsClasses()['User']);
159+
160+
$tg->addCommandsClass('testadmin', $class, 'admin');
161+
self::assertCount(1, $tg->getCommandsClasses()['Admin']);
162+
163+
}
164+
136165
public function testSettingDownloadUploadPaths(): void
137166
{
138167
self::assertEmpty($this->telegram->getDownloadPath());
@@ -152,6 +181,23 @@ public function testGetCommandsList(): void
152181
self::assertNotCount(0, $commands);
153182
}
154183

184+
public function testGetCommandClass(): void
185+
{
186+
$className = StartCommand::class;
187+
$commands = $this->telegram->getCommandsClasses();
188+
self::assertIsArray($commands);
189+
self::assertCount(3, $commands);
190+
191+
$class = $this->telegram->getCommandClassName('user', 'notexist');
192+
self::assertNull($class);
193+
194+
$this->telegram->addCommandsClass('test', $className, 'user');
195+
$class = $this->telegram->getCommandClassName('user', 'test');
196+
self::assertNotNull($class);
197+
self::assertSame($className, $class);
198+
199+
}
200+
155201
public function testUpdateFilter(): void
156202
{
157203
$rawUpdate = '{

0 commit comments

Comments
 (0)