Skip to content

Commit b24bcfc

Browse files
committed
Add first few command tests for "help" and "echo" command.
Rename test Helpers class to TestHelpers.
1 parent 1408ba1 commit b24bcfc

File tree

5 files changed

+243
-6
lines changed

5 files changed

+243
-6
lines changed

tests/Helpers.php renamed to tests/TestHelpers.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
2020
* @link http://www.github.com/akalongman/php-telegram-bot
2121
*/
22-
class Helpers
22+
class TestHelpers
2323
{
2424
/**
2525
* Set the value of a private/protected property of an object
@@ -39,11 +39,13 @@ public static function setObjectProperty($object, $property, $value)
3939
/**
4040
* Return a simple fake Update object
4141
*
42+
* @param array $data Pass custom data array if needed
43+
*
4244
* @return Entities\Update
4345
*/
44-
public static function getFakeUpdateObject()
46+
public static function getFakeUpdateObject($data = null)
4547
{
46-
$data = [
48+
$data = $data ?: [
4749
'update_id' => 1,
4850
'message' => [
4951
'message_id' => 1,
@@ -55,4 +57,37 @@ public static function getFakeUpdateObject()
5557
];
5658
return new Update($data, 'botname');
5759
}
60+
61+
/**
62+
* Return a fake command object for the passed command text
63+
*
64+
* @param string $command_text
65+
*
66+
* @return Entities\Update
67+
*/
68+
public static function getFakeUpdateCommandObject($command_text)
69+
{
70+
$data = [
71+
'update_id' => 1,
72+
'message' => [
73+
'message_id' => 1,
74+
'from' => [
75+
'id' => 1,
76+
'first_name' => 'first',
77+
'last_name' => 'last',
78+
'username' => 'user',
79+
],
80+
'chat' => [
81+
'id' => 1,
82+
'first_name' => 'first',
83+
'last_name' => 'last',
84+
'username' => 'name',
85+
'type' => 'private',
86+
],
87+
'date' => 1,
88+
'text' => $command_text,
89+
],
90+
];
91+
return self::getFakeUpdateObject($data);
92+
}
5893
}

tests/Unit/Commands/CommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace Tests\Unit\Commands;
1212

1313
use Tests\Unit\TestCase;
14-
use Tests\Helpers;
14+
use Tests\TestHelpers;
1515
use Longman\TelegramBot\Telegram;
1616

1717
/**
@@ -44,7 +44,7 @@ public function setUp()
4444
->disableOriginalConstructor()
4545
->getMockForAbstractClass();
4646
//Set a name for the object property so that the constructor can set the config correctly
47-
Helpers::setObjectProperty($this->command_stub_with_config, 'name', 'command_name');
47+
TestHelpers::setObjectProperty($this->command_stub_with_config, 'name', 'command_name');
4848
$this->command_stub_with_config->__construct($this->telegram_with_config);
4949
}
5050

@@ -165,7 +165,7 @@ public function testCommandSetUpdateAndMessage()
165165
$this->assertEquals(null, $stub->getUpdate());
166166
$this->assertEquals(null, $stub->getMessage());
167167

168-
$update = Helpers::getFakeUpdateObject();
168+
$update = TestHelpers::getFakeUpdateObject();
169169
$message = $update->getMessage();
170170
$stub->setUpdate($update);
171171
$this->assertAttributeEquals($update, 'update', $stub);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Tests\Unit\Commands;
12+
13+
use Tests\Unit\TestCase;
14+
use Longman\TelegramBot\Telegram;
15+
16+
/**
17+
* @package TelegramTest
18+
* @author Avtandil Kikabidze <[email protected]>
19+
* @copyright Avtandil Kikabidze <[email protected]>
20+
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
21+
* @link http://www.github.com/akalongman/php-telegram-bot
22+
*/
23+
class CommandTestCase extends TestCase
24+
{
25+
protected $telegram;
26+
protected $command;
27+
28+
public function setUp()
29+
{
30+
$this->telegram = new Telegram('apikey', 'botname');
31+
$this->telegram->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands');
32+
$this->telegram->getCommandsList();
33+
}
34+
35+
/**
36+
* Make sure the version number is in the format x.x.x, x.x or x
37+
*
38+
* @test
39+
*/
40+
public function testVersionNumberFormat()
41+
{
42+
$this->assertRegExp('/^(\d+\\.)?(\d+\\.)?(\d+)$/', $this->command->getVersion());
43+
}
44+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Tests\Unit\Commands\UserCommands;
12+
13+
use Tests\Unit\Commands\CommandTestCase;
14+
use Tests\TestHelpers;
15+
use Longman\TelegramBot\Telegram;
16+
use Longman\TelegramBot\Commands\UserCommands\EchoCommand;
17+
18+
/**
19+
* @package TelegramTest
20+
* @author Avtandil Kikabidze <[email protected]>
21+
* @copyright Avtandil Kikabidze <[email protected]>
22+
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
23+
* @link http://www.github.com/akalongman/php-telegram-bot
24+
*/
25+
class EchoCommandTest extends CommandTestCase
26+
{
27+
public function setUp()
28+
{
29+
parent::setUp();
30+
$this->command = new EchoCommand($this->telegram);
31+
}
32+
33+
/**
34+
* @test
35+
*/
36+
public function testEchoCommandProperties()
37+
{
38+
$this->assertAttributeEquals('echo', 'name', $this->command);
39+
$this->assertAttributeEquals('Show text', 'description', $this->command);
40+
$this->assertAttributeEquals('/echo <text>', 'usage', $this->command);
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function testEchoCommandExecuteWithoutParameter()
47+
{
48+
$text = $this->command
49+
->setUpdate(TestHelpers::getFakeUpdateCommandObject('/echo'))
50+
->execute()
51+
->getResult()
52+
->getText();
53+
$this->assertEquals('Command usage: /echo <text>', $text);
54+
55+
$text = $this->command
56+
->setUpdate(TestHelpers::getFakeUpdateCommandObject('/echo '))
57+
->execute()
58+
->getResult()
59+
->getText();
60+
$this->assertEquals('Command usage: /echo <text>', $text);
61+
}
62+
63+
/**
64+
* @test
65+
*/
66+
public function testEchoCommandExecuteWithParameter()
67+
{
68+
$text = $this->command
69+
->setUpdate(TestHelpers::getFakeUpdateCommandObject('/echo Message!'))
70+
->execute()
71+
->getResult()
72+
->getText();
73+
$this->assertEquals('Message!', $text);
74+
}
75+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Tests\Unit\Commands\UserCommands;
12+
13+
use Tests\Unit\Commands\CommandTestCase;
14+
use Tests\TestHelpers;
15+
use Longman\TelegramBot\Commands\UserCommands\HelpCommand;
16+
17+
/**
18+
* @package TelegramTest
19+
* @author Avtandil Kikabidze <[email protected]>
20+
* @copyright Avtandil Kikabidze <[email protected]>
21+
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
22+
* @link http://www.github.com/akalongman/php-telegram-bot
23+
*/
24+
class HelpCommandTest extends CommandTestCase
25+
{
26+
public function setUp()
27+
{
28+
parent::setUp();
29+
$this->command = new HelpCommand($this->telegram);
30+
}
31+
32+
/**
33+
* @test
34+
*/
35+
public function testHelpCommandProperties()
36+
{
37+
$this->assertAttributeEquals('help', 'name', $this->command);
38+
$this->assertAttributeEquals('Show bot commands help', 'description', $this->command);
39+
$this->assertAttributeEquals('/help or /help <command>', 'usage', $this->command);
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function testHelpCommandExecuteWithoutParameter()
46+
{
47+
$text = $this->command
48+
->setUpdate(TestHelpers::getFakeUpdateCommandObject('/help'))
49+
->execute()
50+
->getResult()
51+
->getText();
52+
$this->assertContains(
53+
"botname v. " . $this->telegram->getVersion() . "\n\nCommands List:",
54+
$text
55+
);
56+
}
57+
58+
/**
59+
* @test
60+
*/
61+
public function testHelpCommandExecuteWithParameterInvalidCommand()
62+
{
63+
$text = $this->command
64+
->setUpdate(TestHelpers::getFakeUpdateCommandObject('/help invalidcommand'))
65+
->execute()
66+
->getResult()
67+
->getText();
68+
$this->assertEquals('No help available: Command /invalidcommand not found', $text);
69+
}
70+
71+
/**
72+
* @test
73+
*/
74+
public function testHelpCommandExecuteWithParameterValidCommand()
75+
{
76+
$text = $this->command
77+
->setUpdate(TestHelpers::getFakeUpdateCommandObject('/help echo'))
78+
->execute()
79+
->getResult()
80+
->getText();
81+
$this->assertContains("Description: Show text\nUsage: /echo <text>", $text);
82+
}
83+
}

0 commit comments

Comments
 (0)