Add extra keyboard tests and fix keyboard row and button parsing.

parent 1b664751
......@@ -36,6 +36,9 @@ class Keyboard extends Entity
{
$data = call_user_func_array([$this, 'createFromParams'], func_get_args());
parent::__construct($data);
// Remove any empty buttons.
$this->{$this->getKeyboardType()} = array_filter($this->{$this->getKeyboardType()});
}
/**
......@@ -75,7 +78,6 @@ class Keyboard extends Entity
*/
protected function createFromParams()
{
$button_class = $this->getKeyboardButtonClass();
$keyboard_type = $this->getKeyboardType();
$args = func_get_args();
......@@ -88,27 +90,25 @@ class Keyboard extends Entity
$data = reset($args);
if (!array_key_exists($keyboard_type, (array)$data)) {
$new_keyboard = [];
foreach ($args as $row) {
$new_row = [];
if ($button_class::couldBe($row)) {
$new_row[] = new $button_class($row);
} else {
foreach ($row as $button) {
if ($button instanceof $button_class) {
$new_row[] = $button;
} elseif (!$this->isInlineKeyboard() || $button_class::couldBe($button)) {
$new_row[] = new $button_class($button);
}
}
}
$new_keyboard[] = $new_row;
if ($from_data = array_key_exists($keyboard_type, (array)$data)) {
$args = $data[$keyboard_type];
// Make sure we're working with a proper row.
if (!is_array($args)) {
$args = [];
}
}
if (!empty($new_keyboard)) {
$data = [$keyboard_type => $new_keyboard];
$new_keyboard = [];
foreach ($args as $row) {
$new_keyboard[] = $this->parseRow($row);
}
if (!empty($new_keyboard)) {
if (!$from_data) {
$data = [];
}
$data[$keyboard_type] = $new_keyboard;
}
return $data;
......@@ -121,12 +121,58 @@ class Keyboard extends Entity
*/
public function addRow()
{
$keyboard_type = $this->getKeyboardType();
$this->{$keyboard_type}[] = func_get_args();
if (($new_row = $this->parseRow(func_get_args())) !== null) {
$this->{$this->getKeyboardType()}[] = $new_row;
}
return $this;
}
/**
* Parse a given row to the correct array format.
*
* @param array $row
*
* @return array
*/
protected function parseRow($row)
{
if (!is_array($row)) {
return null;
}
$new_row = [];
foreach ($row as $button) {
if (($new_button = $this->parseButton($button)) !== null) {
$new_row[] = $new_button;
}
}
return $new_row;
}
/**
* Parse a given button to the correct KeyboardButton object type.
*
* @param array|string|\Longman\TelegramBot\Entities\KeyboardButton $button
*
* @return \Longman\TelegramBot\Entities\KeyboardButton|null
*/
protected function parseButton($button)
{
$button_class = $this->getKeyboardButtonClass();
if ($button instanceof $button_class) {
return $button;
}
if (!$this->isInlineKeyboard() || $button_class::couldBe($button)) {
return new $button_class($button);
}
return null;
}
/**
* {@inheritdoc}
*/
......@@ -159,7 +205,7 @@ class Keyboard extends Entity
*/
public static function hide(array $data = [])
{
return new static(array_merge(['keyboard' => null, 'hide_keyboard' => true, 'selective' => false], $data));
return new static(array_merge(['keyboard' => [], 'hide_keyboard' => true, 'selective' => false], $data));
}
/**
......@@ -174,6 +220,6 @@ class Keyboard extends Entity
*/
public static function forceReply(array $data = [])
{
return new static(array_merge(['keyboard' => null, 'force_reply' => true, 'selective' => false], $data));
return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data));
}
}
......@@ -47,11 +47,7 @@ class KeyboardButton extends Entity
*/
public static function couldBe($data)
{
return is_array($data) &&
array_key_exists('text', $data) && (
array_key_exists('request_contact', $data) ||
array_key_exists('request_location', $data)
);
return is_array($data) && array_key_exists('text', $data);
}
/**
......
......@@ -45,7 +45,31 @@ class InlineKeyboardButtonTest extends TestCase
*/
public function testInlineKeyboardButtonTooManyParametersFail()
{
new InlineKeyboardButton(['text' => 'message', 'url' => 'url_value', 'callback_data' => 'callback_data_value']);
$test_funcs = [
function () {
new InlineKeyboardButton([
'text' => 'message',
'url' => 'url_value',
'callback_data' => 'callback_data_value',
]);
},
function () {
new InlineKeyboardButton([
'text' => 'message',
'url' => 'url_value',
'switch_inline_query' => 'switch_inline_query_value',
]);
},
function () {
new InlineKeyboardButton([
'text' => 'message',
'callback_data' => 'callback_data_value',
'switch_inline_query' => 'switch_inline_query_value',
]);
},
];
$test_funcs[array_rand($test_funcs)]();
}
public function testInlineKeyboardButtonSuccess()
......@@ -54,4 +78,50 @@ class InlineKeyboardButtonTest extends TestCase
new InlineKeyboardButton(['text' => 'message', 'callback_data' => 'callback_data_value']);
new InlineKeyboardButton(['text' => 'message', 'switch_inline_query' => 'switch_inline_query_value']);
}
public function testInlineKeyboardButtonCouldBe()
{
self::assertTrue(InlineKeyboardButton::couldBe(
['text' => 'message', 'url' => 'url_value']
));
self::assertTrue(InlineKeyboardButton::couldBe(
['text' => 'message', 'callback_data' => 'callback_data_value']
));
self::assertTrue(InlineKeyboardButton::couldBe(
['text' => 'message', 'switch_inline_query' => 'switch_inline_query_value']
));
self::assertFalse(InlineKeyboardButton::couldBe(['no_text' => 'message']));
self::assertFalse(InlineKeyboardButton::couldBe(['text' => 'message']));
self::assertFalse(InlineKeyboardButton::couldBe(['url' => 'url_value']));
self::assertFalse(InlineKeyboardButton::couldBe(
['callback_data' => 'callback_data_value']
));
self::assertFalse(InlineKeyboardButton::couldBe(
['switch_inline_query' => 'switch_inline_query_value']
));
self::assertFalse(InlineKeyboardButton::couldBe([
'url' => 'url_value',
'callback_data' => 'callback_data_value',
'switch_inline_query' => 'switch_inline_query_value',
]));
}
public function testInlineKeyboardButtonParameterSetting()
{
$button = new InlineKeyboardButton(['text' => 'message', 'url' => 'url_value']);
self::assertSame('url_value', $button->getUrl());
self::assertEmpty($button->getCallbackData());
self::assertEmpty($button->getSwitchInlineQuery());
$button->setCallbackData('callback_data_value');
self::assertEmpty($button->getUrl());
self::assertSame('callback_data_value', $button->getCallbackData());
self::assertEmpty($button->getSwitchInlineQuery());
$button->setSwitchInlineQuery('switch_inline_query_value');
self::assertEmpty($button->getUrl());
self::assertEmpty($button->getCallbackData());
self::assertSame('switch_inline_query_value', $button->getSwitchInlineQuery());
}
}
......@@ -34,11 +34,25 @@ class InlineKeyboardTest extends TestCase
return new InlineKeyboardButton($data);
}
public function testNothing(){
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage Inline Keyboard field is not an array!
*/
public function testInlineKeyboardDataMalformedField()
{
new InlineKeyboard(['inline_keyboard' => 'wrong']);
}
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage Inline Keyboard subfield is not an array!
*/
public function testInlineKeyboardDataMalformedSubfield()
{
new InlineKeyboard(['inline_keyboard' => ['wrong']]);
}
public function testInlineKeyboardSingleButtonSinleRow()
public function testInlineKeyboardSingleButtonSingleRow()
{
$inline_keyboard = (new InlineKeyboard(
$this->getRandomButton('Button Text 1')
......@@ -100,4 +114,25 @@ class InlineKeyboardTest extends TestCase
self::assertSame('Button Text 3', $keyboard[1][0]->getText());
self::assertSame('Button Text 4', $keyboard[1][1]->getText());
}
public function testInlineKeyboardAddRows()
{
$keyboard_obj = new InlineKeyboard([]);
$keyboard_obj->addRow($this->getRandomButton('Button Text 1'));
$keyboard = $keyboard_obj->getProperty('inline_keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
$keyboard_obj->addRow(
$this->getRandomButton('Button Text 2'),
$this->getRandomButton('Button Text 3')
);
$keyboard = $keyboard_obj->getProperty('inline_keyboard');
self::assertSame('Button Text 2', $keyboard[1][0]->getText());
self::assertSame('Button Text 3', $keyboard[1][1]->getText());
$keyboard_obj->addRow($this->getRandomButton('Button Text 4'));
$keyboard = $keyboard_obj->getProperty('inline_keyboard');
self::assertSame('Button Text 4', $keyboard[2][0]->getText());
}
}
......@@ -45,4 +45,25 @@ class KeyboardButtonTest extends TestCase
new KeyboardButton(['text' => 'message', 'request_contact' => true]);
new KeyboardButton(['text' => 'message', 'request_location' => true]);
}
public function testInlineKeyboardButtonCouldBe()
{
self::assertTrue(KeyboardButton::couldBe(['text' => 'message']));
self::assertFalse(KeyboardButton::couldBe(['no_text' => 'message']));
}
public function testKeyboardButtonParameterSetting()
{
$button = new KeyboardButton('message');
self::assertEmpty($button->getRequestContact());
self::assertEmpty($button->getRequestLocation());
$button->setRequestContact(true);
self::assertTrue($button->getRequestContact());
self::assertEmpty($button->getRequestLocation());
$button->setRequestLocation(true);
self::assertEmpty($button->getRequestContact());
self::assertTrue($button->getRequestLocation());
}
}
......@@ -12,6 +12,7 @@ namespace Longman\TelegramBot\Tests\Unit;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\KeyboardButton;
use Longman\TelegramBot\Exception\TelegramException;
/**
* @package TelegramTest
......@@ -22,6 +23,24 @@ use Longman\TelegramBot\Entities\KeyboardButton;
*/
class KeyboardTest extends TestCase
{
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage Keyboard field is not an array!
*/
public function testKeyboardDataMalformedField()
{
new Keyboard(['keyboard' => 'wrong']);
}
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage Keyboard subfield is not an array!
*/
public function testKeyboardDataMalformedSubfield()
{
new Keyboard(['keyboard' => ['wrong']]);
}
public function testKeyboardSingleButtonSingleRow()
{
$keyboard = (new Keyboard('Button Text 1'))->getProperty('keyboard');
......@@ -94,4 +113,76 @@ class KeyboardTest extends TestCase
self::assertSame('Button Text 5', $keyboard[1][0]->getText());
self::assertSame('Button Text 6', $keyboard[1][1]->getText());
}
public function testKeyboardWithDataArray()
{
$resize_keyboard = (bool)mt_rand(0, 1);
$one_time_keyboard = (bool)mt_rand(0, 1);
$selective = (bool)mt_rand(0, 1);
$keyboard_obj = new Keyboard([
'resize_keyboard' => $resize_keyboard,
'one_time_keyboard' => $one_time_keyboard,
'selective' => $selective,
'keyboard' => [['Button Text 1']],
]);
$keyboard = $keyboard_obj->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
self::assertSame($resize_keyboard, $keyboard_obj->getResizeKeyboard());
self::assertSame($one_time_keyboard, $keyboard_obj->getOneTimeKeyboard());
self::assertSame($selective, $keyboard_obj->getSelective());
}
public function testPredefinedKeyboards()
{
$keyboard_hide = Keyboard::hide();
self::assertTrue($keyboard_hide->getProperty('hide_keyboard'));
$keyboard_force_reply = Keyboard::forceReply();
self::assertTrue($keyboard_force_reply->getProperty('force_reply'));
}
public function testKeyboardMethods()
{
$keyboard_obj = new Keyboard([]);
self::assertEmpty($keyboard_obj->getOneTimeKeyboard());
self::assertEmpty($keyboard_obj->getResizeKeyboard());
self::assertEmpty($keyboard_obj->getSelective());
$keyboard_obj->setOneTimeKeyboard(true);
self::assertTrue($keyboard_obj->getOneTimeKeyboard());
$keyboard_obj->setOneTimeKeyboard(false);
self::assertFalse($keyboard_obj->getOneTimeKeyboard());
$keyboard_obj->setResizeKeyboard(true);
self::assertTrue($keyboard_obj->getResizeKeyboard());
$keyboard_obj->setResizeKeyboard(false);
self::assertFalse($keyboard_obj->getResizeKeyboard());
$keyboard_obj->setSelective(true);
self::assertTrue($keyboard_obj->getSelective());
$keyboard_obj->setSelective(false);
self::assertFalse($keyboard_obj->getSelective());
}
public function testKeyboardAddRows()
{
$keyboard_obj = new Keyboard([]);
$keyboard_obj->addRow('Button Text 1');
$keyboard = $keyboard_obj->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
$keyboard_obj->addRow('Button Text 2', 'Button Text 3');
$keyboard = $keyboard_obj->getProperty('keyboard');
self::assertSame('Button Text 2', $keyboard[1][0]->getText());
self::assertSame('Button Text 3', $keyboard[1][1]->getText());
$keyboard_obj->addRow(['text' => 'Button Text 4']);
$keyboard = $keyboard_obj->getProperty('keyboard');
self::assertSame('Button Text 4', $keyboard[2][0]->getText());
}
}
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