Commit 98f13b08 authored by Marco Boretto's avatar Marco Boretto Committed by GitHub

Merge pull request #240 from noplanman/230-flexible-methods

Add flexible methods
parents 7d75d16c 2f242074
...@@ -459,20 +459,34 @@ class Telegram ...@@ -459,20 +459,34 @@ class Telegram
} }
/** /**
* Enable Admin Account * Enable a single Admin account
* *
* @param array $admins_list List of admins * @param integer $admin_id Single admin id
* *
* @return string * @return Telegram
*/ */
public function enableAdmins(array $admins_list) public function enableAdmin($admin_id)
{ {
foreach ($admins_list as $admin) { if (is_int($admin_id) && $admin_id > 0 && !in_array($admin_id, $this->admins_list)) {
if ($admin > 0) { $this->admins_list[] = $admin_id;
$this->admins_list[] = $admin; } else {
} else { TelegramLog::error('Invalid value "' . $admin_id . '" for admin.');
throw new TelegramException('Invalid value "' . $admin . '" for admin!'); }
}
return $this;
}
/**
* Enable a list of Admin Accounts
*
* @param array $admin_ids List of admin ids
*
* @return Telegram
*/
public function enableAdmins(array $admin_ids)
{
foreach ($admin_ids as $admin_id) {
$this->enableAdmin($admin_id);
} }
return $this; return $this;
...@@ -531,25 +545,42 @@ class Telegram ...@@ -531,25 +545,42 @@ class Telegram
} }
/** /**
* Add custom commands path * Add a single custom commands path
* *
* @param string $path Custom commands path * @param string $path Custom commands path to add
* @param bool $before If the path should be prepended or appended to the list * @param bool $before If the path should be prepended or appended to the list
* *
* @return \Longman\TelegramBot\Telegram * @return Telegram
*/ */
public function addCommandsPath($path, $before = true) public function addCommandsPath($path, $before = true)
{ {
if (!is_dir($path)) { if (!is_dir($path)) {
throw new TelegramException('Commands path "' . $path . '" does not exist!'); TelegramLog::error('Commands path "' . $path . '" does not exist.');
} } elseif (!in_array($path, $this->commands_paths)) {
if (!in_array($path, $this->commands_paths)) {
if ($before) { if ($before) {
array_unshift($this->commands_paths, $path); array_unshift($this->commands_paths, $path);
} else { } else {
array_push($this->commands_paths, $path); array_push($this->commands_paths, $path);
} }
} }
return $this;
}
/**
* Add multiple custom commands paths
*
* @param array $paths Custom commands paths to add
* @param bool $before If the paths should be prepended or appended to the list
*
* @return Telegram
*/
public function addCommandsPaths(array $paths, $before = true)
{
foreach ($paths as $path) {
$this->addCommandsPath($path);
}
return $this; return $this;
} }
......
...@@ -26,12 +26,37 @@ class TelegramTest extends TestCase ...@@ -26,12 +26,37 @@ class TelegramTest extends TestCase
*/ */
private $telegram; private $telegram;
/**
* @var array
*/
private $custom_commands_paths = [
'/tmp/php-telegram-bot-custom-commands-1',
'/tmp/php-telegram-bot-custom-commands-2',
'/tmp/php-telegram-bot-custom-commands-3',
];
/** /**
* setUp * setUp
*/ */
protected function setUp() protected function setUp()
{ {
$this->telegram = new Telegram('testapikey', 'testbotname'); $this->telegram = new Telegram('testapikey', 'testbotname');
// Create a few custom commands paths.
foreach ($this->custom_commands_paths as $custom_path) {
mkdir($custom_path);
}
}
/**
* tearDown
*/
protected function tearDown()
{
// Clean up the custom commands paths.
foreach ($this->custom_commands_paths as $custom_path) {
rmdir($custom_path);
}
} }
/** /**
...@@ -68,6 +93,53 @@ class TelegramTest extends TestCase ...@@ -68,6 +93,53 @@ class TelegramTest extends TestCase
$this->assertEquals('testbotname', $this->telegram->getBotName()); $this->assertEquals('testbotname', $this->telegram->getBotName());
} }
/**
* @test
*/
public function enableAdmins()
{
$tg = &$this->telegram;
$this->assertEmpty($tg->getAdminList());
$tg->enableAdmin(1);
$this->assertCount(1, $tg->getAdminList());
$tg->enableAdmins([2, 3]);
$this->assertCount(3, $tg->getAdminList());
$tg->enableAdmin(2);
$this->assertCount(3, $tg->getAdminList());
$tg->enableAdmin('a string?');
$this->assertCount(3, $tg->getAdminList());
}
/**
* @test
*/
public function addCustomCommandsPaths()
{
$tg = &$this->telegram;
$this->assertAttributeCount(1, 'commands_paths', $tg);
$tg->addCommandsPath($this->custom_commands_paths[0]);
$this->assertAttributeCount(2, 'commands_paths', $tg);
$tg->addCommandsPath('/invalid/path');
$this->assertAttributeCount(2, 'commands_paths', $tg);
$tg->addCommandsPaths([
$this->custom_commands_paths[1],
$this->custom_commands_paths[2],
]);
$this->assertAttributeCount(4, 'commands_paths', $tg);
$tg->addCommandsPath($this->custom_commands_paths[0]);
$this->assertAttributeCount(4, 'commands_paths', $tg);
}
/** /**
* @test * @test
*/ */
......
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