Add InlineKeyboard tests and fix createFromParams method.

parent 9eb50bb0
...@@ -88,7 +88,7 @@ class Keyboard extends Entity ...@@ -88,7 +88,7 @@ class Keyboard extends Entity
$data = reset($args); $data = reset($args);
if (!array_key_exists($keyboard_type, $data)) { if (!array_key_exists($keyboard_type, (array)$data)) {
$new_keyboard = []; $new_keyboard = [];
foreach ($args as $row) { foreach ($args as $row) {
if (is_array($row)) { if (is_array($row)) {
...@@ -105,7 +105,11 @@ class Keyboard extends Entity ...@@ -105,7 +105,11 @@ class Keyboard extends Entity
} }
} }
} else { } else {
$new_row = [new $button_class($row)]; $button = $row;
if (!($button instanceof $button_class)) {
$button = new $button_class($button);
}
$new_row = [$button];
} }
$new_keyboard[] = $new_row; $new_keyboard[] = $new_row;
} }
......
<?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\InlineKeyboard;
use Longman\TelegramBot\Entities\InlineKeyboardButton;
/**
* @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 InlineKeyboardTest extends TestCase
{
private function getRandomButton($text)
{
$random_params = ['url', 'callback_data', 'switch_inline_query'];
$param = $random_params[array_rand($random_params, 1)];
$data = [
'text' => $text,
$param => 'random_param',
];
return new InlineKeyboardButton($data);
}
public function testNothing(){
}
public function testInlineKeyboardSingleButtonSinleRow()
{
$inline_keyboard = (new InlineKeyboard(
$this->getRandomButton('Button Text 1')
))->getProperty('inline_keyboard');
self::assertSame('Button Text 1', $inline_keyboard[0][0]->getText());
$inline_keyboard = (new InlineKeyboard(
[$this->getRandomButton('Button Text 2')]
))->getProperty('inline_keyboard');
self::assertSame('Button Text 2', $inline_keyboard[0][0]->getText());
}
public function testInlineKeyboardSingleButtonMultipleRows()
{
$keyboard = (new InlineKeyboard(
$this->getRandomButton('Button Text 1'),
$this->getRandomButton('Button Text 2'),
$this->getRandomButton('Button Text 3')
))->getProperty('inline_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());
$keyboard = (new InlineKeyboard(
[$this->getRandomButton('Button Text 4')],
[$this->getRandomButton('Button Text 5')],
[$this->getRandomButton('Button Text 6')]
))->getProperty('inline_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[2][0]->getText());
}
public function testInlineKeyboardMultipleButtonsSingleRow()
{
$keyboard = (new InlineKeyboard([
$this->getRandomButton('Button Text 1'),
$this->getRandomButton('Button Text 2')
]))->getProperty('inline_keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
self::assertSame('Button Text 2', $keyboard[0][1]->getText());
}
public function testInlineKeyboardMultipleButtonsMultipleRows()
{
$keyboard = (new InlineKeyboard(
[
$this->getRandomButton('Button Text 1'),
$this->getRandomButton('Button Text 2')
],
[
$this->getRandomButton('Button Text 3'),
$this->getRandomButton('Button Text 4')
]
))->getProperty('inline_keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
self::assertSame('Button Text 2', $keyboard[0][1]->getText());
self::assertSame('Button Text 3', $keyboard[1][0]->getText());
self::assertSame('Button Text 4', $keyboard[1][1]->getText());
}
}
...@@ -43,20 +43,20 @@ class KeyboardTest extends TestCase ...@@ -43,20 +43,20 @@ class KeyboardTest extends TestCase
self::assertSame('Button Text 3', $keyboard[2][0]->getText()); self::assertSame('Button Text 3', $keyboard[2][0]->getText());
$keyboard = (new Keyboard( $keyboard = (new Keyboard(
['Button Text 1'], ['Button Text 4'],
['Button Text 2'], ['Button Text 5'],
['Button Text 3'] ['Button Text 6']
))->getProperty('keyboard'); ))->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText()); self::assertSame('Button Text 4', $keyboard[0][0]->getText());
self::assertSame('Button Text 2', $keyboard[1][0]->getText()); self::assertSame('Button Text 5', $keyboard[1][0]->getText());
self::assertSame('Button Text 3', $keyboard[2][0]->getText()); self::assertSame('Button Text 6', $keyboard[2][0]->getText());
} }
public function testKeyboardMultipleButtonsSingleRow() public function testKeyboardMultipleButtonsSingleRow()
{ {
$keyboard = (new Keyboard(['Button Text 3', 'Button Text 4']))->getProperty('keyboard'); $keyboard = (new Keyboard(['Button Text 1', 'Button Text 2']))->getProperty('keyboard');
self::assertSame('Button Text 3', $keyboard[0][0]->getText()); self::assertSame('Button Text 1', $keyboard[0][0]->getText());
self::assertSame('Button Text 4', $keyboard[0][1]->getText()); self::assertSame('Button Text 2', $keyboard[0][1]->getText());
} }
public function testKeyboardMultipleButtonsMultipleRows() public function testKeyboardMultipleButtonsMultipleRows()
......
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