Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
8dba19fe
Unverified
Commit
8dba19fe
authored
Oct 01, 2016
by
Armando Lüscher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add initial keyboard tests and fix a few keyboard issues.
parent
c35a03fd
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
198 additions
and
5 deletions
+198
-5
InlineKeyboardButton.php
src/Entities/InlineKeyboardButton.php
+4
-0
Keyboard.php
src/Entities/Keyboard.php
+10
-4
KeyboardButton.php
src/Entities/KeyboardButton.php
+4
-0
InlineKeyboardButtonTest.php
tests/unit/Entities/InlineKeyboardButtonTest.php
+1
-1
KeyboardButtonTest.php
tests/unit/Entities/KeyboardButtonTest.php
+48
-0
KeyboardTest.php
tests/unit/Entities/KeyboardTest.php
+131
-0
No files found.
src/Entities/InlineKeyboardButton.php
View file @
8dba19fe
...
...
@@ -51,6 +51,10 @@ class InlineKeyboardButton extends KeyboardButton
*/
protected
function
validate
()
{
if
(
$this
->
getProperty
(
'text'
,
''
)
===
''
)
{
throw
new
TelegramException
(
'You must add some text to the button!'
);
}
$num_params
=
0
;
foreach
([
'url'
,
'callback_data'
,
'switch_inline_query'
]
as
$param
)
{
...
...
src/Entities/Keyboard.php
View file @
8dba19fe
...
...
@@ -80,10 +80,16 @@ class Keyboard extends Entity
$keyboard_type
=
$this
->
getKeyboardType
();
// If the inline_keyboard isn't set directly, try to create one from the arguments.
$data
=
func_get_arg
(
0
);
if
(
!
array_key_exists
(
$keyboard_type
,
$data
))
{
$args
=
func_get_args
();
$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
=
[];
foreach
(
func_get_args
()
as
$row
)
{
foreach
(
$args
as
$row
)
{
if
(
is_array
(
$row
))
{
$new_row
=
[];
if
(
$button_class
::
couldBe
(
$row
))
{
...
...
@@ -119,7 +125,7 @@ class Keyboard extends Entity
public
function
addRow
()
{
$keyboard_type
=
$this
->
getKeyboardType
();
$this
->
$keyboard_type
[]
=
func_get_args
();
$this
->
{
$keyboard_type
}
[]
=
func_get_args
();
return
$this
;
}
...
...
src/Entities/KeyboardButton.php
View file @
8dba19fe
...
...
@@ -59,6 +59,10 @@ class KeyboardButton extends Entity
*/
protected
function
validate
()
{
if
(
$this
->
getProperty
(
'text'
,
''
)
===
''
)
{
throw
new
TelegramException
(
'You must add some text to the button!'
);
}
if
(
$this
->
getRequestContact
()
&&
$this
->
getRequestLocation
())
{
throw
new
TelegramException
(
'You must use only one of these fields: request_contact, request_location!'
);
}
...
...
tests/unit/Entities/InlineKeyboardButtonTest.php
View file @
8dba19fe
...
...
@@ -23,7 +23,7 @@ class InlineKeyboardButtonTest extends TestCase
{
/**
* @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
()
{
...
...
tests/unit/Entities/KeyboardButtonTest.php
0 → 100644
View file @
8dba19fe
<?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
]);
}
}
tests/unit/Entities/KeyboardTest.php
0 → 100644
View file @
8dba19fe
<?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]);
}*/
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment