Skip to content

Commit 47058ef

Browse files
authored
Merge pull request #430 from lichtscheu/master
add functionality to allow hiding commands in /help command
2 parents b0833a8 + 1528fe5 commit 47058ef

File tree

7 files changed

+154
-1
lines changed

7 files changed

+154
-1
lines changed

src/Commands/Command.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ abstract class Command
5959
*/
6060
protected $usage = 'Command usage';
6161

62+
/**
63+
* Show in Help
64+
*
65+
* @var bool
66+
*/
67+
protected $show_in_help = true;
68+
6269
/**
6370
* Version
6471
*
@@ -252,6 +259,16 @@ public function getName()
252259
return $this->name;
253260
}
254261

262+
/**
263+
* Get Show in Help
264+
*
265+
* @return bool
266+
*/
267+
public function showInHelp()
268+
{
269+
return $this->show_in_help;
270+
}
271+
255272
/**
256273
* Check if command is enabled
257274
*

src/Commands/UserCommands/HelpCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public function execute()
6969
);
7070

7171
foreach ($command_objs as $command) {
72+
if (!$command->showInHelp()) {
73+
continue;
74+
}
75+
7276
$text .= sprintf(
7377
'/%s - %s' . PHP_EOL,
7478
$command->getName(),

tests/unit/Commands/CommandTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ public function testDefaultCommandIsEnabled()
127127
$this->assertTrue($this->command_stub->isEnabled());
128128
}
129129

130+
public function testDefaultCommandShownInHelp()
131+
{
132+
$this->assertAttributeEquals(true, 'show_in_help', $this->command_stub);
133+
$this->assertTrue($this->command_stub->showInHelp());
134+
}
135+
130136
public function testDefaultCommandNeedsMysql()
131137
{
132138
$this->assertAttributeEquals(false, 'need_mysql', $this->command_stub);

tests/unit/Commands/CommandTestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public function setUp()
3939
{
4040
$this->telegram = new Telegram('apikey', 'testbot');
4141
$this->telegram->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands');
42+
43+
// Add custom commands dedicated to do some tests.
44+
$this->telegram->addCommandsPath(__DIR__ . '/CustomTestCommands');
4245
$this->telegram->getCommandsList();
4346
}
4447

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Commands\UserCommands;
12+
13+
use Longman\TelegramBot\Commands\UserCommand;
14+
use Longman\TelegramBot\Request;
15+
16+
/**
17+
* Test "/hidden" command to test $show_in_help
18+
*/
19+
class HiddenCommand extends UserCommand
20+
{
21+
/**
22+
* @var string
23+
*/
24+
protected $name = 'hidden';
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $description = 'This command is hidden in help';
30+
31+
/**
32+
* @var string
33+
*/
34+
protected $usage = '/hidden';
35+
36+
/**
37+
* @var string
38+
*/
39+
protected $version = '1.0.0';
40+
41+
/**
42+
* @var bool
43+
*/
44+
protected $show_in_help = false;
45+
46+
/**
47+
* Command execute method
48+
*
49+
* @return mixed
50+
* @throws \Longman\TelegramBot\Exception\TelegramException
51+
*/
52+
public function execute()
53+
{
54+
return Request::emptyResponse();
55+
}
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Commands\UserCommands;
12+
13+
use Longman\TelegramBot\Commands\UserCommand;
14+
use Longman\TelegramBot\Request;
15+
16+
/**
17+
* Test "/visible" command to test $show_in_help
18+
*/
19+
class VisibleCommand extends UserCommand
20+
{
21+
/**
22+
* @var string
23+
*/
24+
protected $name = 'visible';
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $description = 'This command is visible in help';
30+
31+
/**
32+
* @var string
33+
*/
34+
protected $usage = '/visible';
35+
36+
/**
37+
* @var string
38+
*/
39+
protected $version = '1.0.0';
40+
41+
/**
42+
* @var bool
43+
*/
44+
protected $show_in_help = true;
45+
46+
/**
47+
* Command execute method
48+
*
49+
* @return mixed
50+
* @throws \Longman\TelegramBot\Exception\TelegramException
51+
*/
52+
public function execute()
53+
{
54+
return Request::emptyResponse();
55+
}
56+
}

tests/unit/Commands/UserCommands/HelpCommandTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
namespace Longman\TelegramBot\Tests\Unit\Commands\UserCommands;
1212

13+
use Longman\TelegramBot\Commands\UserCommands\HelpCommand;
1314
use Longman\TelegramBot\Tests\Unit\Commands\CommandTestCase;
1415
use Longman\TelegramBot\Tests\Unit\TestHelpers;
15-
use Longman\TelegramBot\Commands\UserCommands\HelpCommand;
1616

1717
/**
1818
* @package TelegramTest
@@ -71,4 +71,15 @@ public function testHelpCommandExecuteWithParameterValidCommand()
7171
->getText();
7272
$this->assertContains("Description: Show text\nUsage: /echo <text>", $text);
7373
}
74+
75+
public function testHelpCommandWithHiddenCommand()
76+
{
77+
$text = $this->command
78+
->setUpdate(TestHelpers::getFakeUpdateCommandObject('/help'))
79+
->execute()
80+
->getResult()
81+
->getText();
82+
$this->assertContains('/visible', $text);
83+
$this->assertNotContains('/hidden', $text);
84+
}
7485
}

0 commit comments

Comments
 (0)