Commit 47058ef4 authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #430 from lichtscheu/master

add functionality to allow hiding commands in /help command
parents b0833a89 1528fe52
......@@ -59,6 +59,13 @@ abstract class Command
*/
protected $usage = 'Command usage';
/**
* Show in Help
*
* @var bool
*/
protected $show_in_help = true;
/**
* Version
*
......@@ -252,6 +259,16 @@ abstract class Command
return $this->name;
}
/**
* Get Show in Help
*
* @return bool
*/
public function showInHelp()
{
return $this->show_in_help;
}
/**
* Check if command is enabled
*
......
......@@ -69,6 +69,10 @@ class HelpCommand extends UserCommand
);
foreach ($command_objs as $command) {
if (!$command->showInHelp()) {
continue;
}
$text .= sprintf(
'/%s - %s' . PHP_EOL,
$command->getName(),
......
......@@ -127,6 +127,12 @@ class CommandTest extends TestCase
$this->assertTrue($this->command_stub->isEnabled());
}
public function testDefaultCommandShownInHelp()
{
$this->assertAttributeEquals(true, 'show_in_help', $this->command_stub);
$this->assertTrue($this->command_stub->showInHelp());
}
public function testDefaultCommandNeedsMysql()
{
$this->assertAttributeEquals(false, 'need_mysql', $this->command_stub);
......
......@@ -39,6 +39,9 @@ class CommandTestCase extends TestCase
{
$this->telegram = new Telegram('apikey', 'testbot');
$this->telegram->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands');
// Add custom commands dedicated to do some tests.
$this->telegram->addCommandsPath(__DIR__ . '/CustomTestCommands');
$this->telegram->getCommandsList();
}
......
<?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\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
/**
* Test "/hidden" command to test $show_in_help
*/
class HiddenCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'hidden';
/**
* @var string
*/
protected $description = 'This command is hidden in help';
/**
* @var string
*/
protected $usage = '/hidden';
/**
* @var string
*/
protected $version = '1.0.0';
/**
* @var bool
*/
protected $show_in_help = false;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
{
return Request::emptyResponse();
}
}
<?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\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
/**
* Test "/visible" command to test $show_in_help
*/
class VisibleCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'visible';
/**
* @var string
*/
protected $description = 'This command is visible in help';
/**
* @var string
*/
protected $usage = '/visible';
/**
* @var string
*/
protected $version = '1.0.0';
/**
* @var bool
*/
protected $show_in_help = true;
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
{
return Request::emptyResponse();
}
}
......@@ -10,9 +10,9 @@
namespace Longman\TelegramBot\Tests\Unit\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommands\HelpCommand;
use Longman\TelegramBot\Tests\Unit\Commands\CommandTestCase;
use Longman\TelegramBot\Tests\Unit\TestHelpers;
use Longman\TelegramBot\Commands\UserCommands\HelpCommand;
/**
* @package TelegramTest
......@@ -71,4 +71,15 @@ class HelpCommandTest extends CommandTestCase
->getText();
$this->assertContains("Description: Show text\nUsage: /echo <text>", $text);
}
public function testHelpCommandWithHiddenCommand()
{
$text = $this->command
->setUpdate(TestHelpers::getFakeUpdateCommandObject('/help'))
->execute()
->getResult()
->getText();
$this->assertContains('/visible', $text);
$this->assertNotContains('/hidden', $text);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment