Add initial keyboard tests and fix a few keyboard issues.

parent c35a03fd
...@@ -51,6 +51,10 @@ class InlineKeyboardButton extends KeyboardButton ...@@ -51,6 +51,10 @@ class InlineKeyboardButton extends KeyboardButton
*/ */
protected function validate() protected function validate()
{ {
if ($this->getProperty('text', '') === '') {
throw new TelegramException('You must add some text to the button!');
}
$num_params = 0; $num_params = 0;
foreach (['url', 'callback_data', 'switch_inline_query'] as $param) { foreach (['url', 'callback_data', 'switch_inline_query'] as $param) {
......
...@@ -80,10 +80,16 @@ class Keyboard extends Entity ...@@ -80,10 +80,16 @@ class Keyboard extends Entity
$keyboard_type = $this->getKeyboardType(); $keyboard_type = $this->getKeyboardType();
// If the inline_keyboard isn't set directly, try to create one from the arguments. // If the inline_keyboard isn't set directly, try to create one from the arguments.
$data = func_get_arg(0); $args = func_get_args();
if (!array_key_exists($keyboard_type, $data)) { $data = reset($args);
// Force button parameters into a single row.
if (!is_array($data)) {
$args = [$args];
}
if (!array_key_exists($keyboard_type, (array) $data)) {
$new_keyboard = []; $new_keyboard = [];
foreach (func_get_args() as $row) { foreach ($args as $row) {
if (is_array($row)) { if (is_array($row)) {
$new_row = []; $new_row = [];
if ($button_class::couldBe($row)) { if ($button_class::couldBe($row)) {
...@@ -119,7 +125,7 @@ class Keyboard extends Entity ...@@ -119,7 +125,7 @@ class Keyboard extends Entity
public function addRow() public function addRow()
{ {
$keyboard_type = $this->getKeyboardType(); $keyboard_type = $this->getKeyboardType();
$this->$keyboard_type[] = func_get_args(); $this->{$keyboard_type}[] = func_get_args();
return $this; return $this;
} }
......
...@@ -59,6 +59,10 @@ class KeyboardButton extends Entity ...@@ -59,6 +59,10 @@ class KeyboardButton extends Entity
*/ */
protected function validate() protected function validate()
{ {
if ($this->getProperty('text', '') === '') {
throw new TelegramException('You must add some text to the button!');
}
if ($this->getRequestContact() && $this->getRequestLocation()) { if ($this->getRequestContact() && $this->getRequestLocation()) {
throw new TelegramException('You must use only one of these fields: request_contact, request_location!'); throw new TelegramException('You must use only one of these fields: request_contact, request_location!');
} }
......
...@@ -23,7 +23,7 @@ class InlineKeyboardButtonTest extends TestCase ...@@ -23,7 +23,7 @@ class InlineKeyboardButtonTest extends TestCase
{ {
/** /**
* @expectedException \Longman\TelegramBot\Exception\TelegramException * @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query! * @expectedExceptionMessage You must add some text to the button!
*/ */
public function testInlineKeyboardButtonNoTextFail() public function testInlineKeyboardButtonNoTextFail()
{ {
......
<?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;
use Longman\TelegramBot\Entities\KeyboardButton;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class KeyboardButtonTest extends TestCase
{
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage You must add some text to the button!
*/
public function testKeyboardButtonNoTextFail()
{
new KeyboardButton([]);
}
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage You must use only one of these fields: request_contact, request_location!
*/
public function testKeyboardButtonTooManyParametersFail()
{
new KeyboardButton(['text' => 'message', 'request_contact' => true, 'request_location' => true]);
}
public function testKeyboardButtonSuccess()
{
new KeyboardButton(['text' => 'message']);
new KeyboardButton(['text' => 'message', 'request_contact' => true]);
new KeyboardButton(['text' => 'message', 'request_location' => true]);
}
}
<?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;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\KeyboardButton;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class KeyboardTest extends TestCase
{
public function testKeyboardSingleButtonSingleRow()
{
$keyboard = (new Keyboard('Button Text 1'))->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
$keyboard = (new Keyboard(['Button Text 2']))->getProperty('keyboard');
self::assertSame('Button Text 2', $keyboard[0][0]->getText());
}
public function testKeyboardSingleButtonMultipleRows()
{
$keyboard = (new Keyboard(
['Button Text 1'],
['Button Text 2'],
['Button Text 3']
))->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
self::assertSame('Button Text 2', $keyboard[1][0]->getText());
self::assertSame('Button Text 3', $keyboard[2][0]->getText());
}
public function testKeyboardMultipleButtonsSingleRow()
{
/** @var KeyboardButton $keyboard_button_1 */
/** @var KeyboardButton $keyboard_button_2 */
$keyboard = (new Keyboard('Button Text 1', 'Button Text 2'))->getProperty('keyboard');
list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1.
self::assertSame('Button Text 1', $keyboard_button_1->getText());
self::assertSame('Button Text 2', $keyboard_button_2->getText());
$keyboard = (new Keyboard(['Button Text 3', 'Button Text 4']))->getProperty('keyboard');
list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1.
self::assertSame('Button Text 3', $keyboard_button_1->getText());
self::assertSame('Button Text 4', $keyboard_button_2->getText());
}
public function testKeyboardMultipleButtonsMultipleRows()
{
/** @var KeyboardButton $keyboard_button_1 */
/** @var KeyboardButton $keyboard_button_2 */
/** @var KeyboardButton $keyboard_button_3 */
/** @var KeyboardButton $keyboard_button_4 */
$keyboard = (new Keyboard(
['Button Text 1', 'Button Text 2'],
['Button Text 3', 'Button Text 4']
))->getProperty('keyboard');
list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1.
list($keyboard_button_3, $keyboard_button_4) = $keyboard[1]; // Row 2.
self::assertSame('Button Text 1', $keyboard_button_1->getText());
self::assertSame('Button Text 2', $keyboard_button_2->getText());
self::assertSame('Button Text 3', $keyboard_button_3->getText());
self::assertSame('Button Text 4', $keyboard_button_4->getText());
}
public function testKeyboardWithButtonObjects()
{
$keyboard = (new Keyboard(
new KeyboardButton('Button Text 1')
))->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
$keyboard = (new Keyboard(
new KeyboardButton('Button Text 2'),
new KeyboardButton('Button Text 3')
))->getProperty('keyboard');
self::assertSame('Button Text 2', $keyboard[0][0]->getText());
self::assertSame('Button Text 3', $keyboard[0][1]->getText());
$keyboard = (new Keyboard(
[new KeyboardButton('Button Text 4')],
[new KeyboardButton('Button Text 5'), new KeyboardButton('Button Text 6')]
))->getProperty('keyboard');
self::assertSame('Button Text 4', $keyboard[0][0]->getText());
self::assertSame('Button Text 5', $keyboard[1][0]->getText());
self::assertSame('Button Text 6', $keyboard[1][1]->getText());
}
/*public function testKeyboardWithData()
{
$keyboard = (new Keyboard(
['Button Text 1', 'Button Text 2'],
['Button Text 3', 'Button Text 4']
))->getProperty('keyboard');
}*/
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage You must use only one of these fields: request_contact, request_location!
*/
/*public function testKeyboardTooManyParametersFail()
{
new Keyboard(['text' => 'message', 'request_contact' => true, 'request_location' => true]);
}*/
/*public function testKeyboardSuccess()
{
new Keyboard(['text' => 'message']);
new Keyboard(['text' => 'message', 'request_contact' => true]);
new Keyboard(['text' => 'message', 'request_location' => true]);
}*/
}
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