1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TelegramBotTest; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Symfony\Component\Dotenv\Dotenv; |
8
|
|
|
use TelegramBot\Entities\Update; |
9
|
|
|
use TelegramBot\Plugin; |
10
|
|
|
use TelegramBot\Request; |
11
|
|
|
use TelegramBot\Telegram; |
12
|
|
|
use TelegramBot\UpdateHandler; |
13
|
|
|
use TelegramBotTest\EchoBot\Handler; |
14
|
|
|
|
15
|
|
|
class HandlerTest extends \PHPUnit\Framework\TestCase |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
public function test_echo_bot(): void |
19
|
|
|
{ |
20
|
|
|
(new Handler())->resolve(Telegram::processUpdate( |
21
|
|
|
'{"update_id":1,"message":{"message_id":1,"from":{"id":1,"is_bot":false,"first_name":"First","last_name":"Last","username":"username","language_code":"en"},"chat":{"id":1,"first_name":"First","last_name":"Last","username":"username","type":"private"},"date":1546300800,"text":"Hello World!"}}', |
22
|
|
|
'1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' |
23
|
|
|
)); |
24
|
|
|
|
25
|
|
|
$this->assertTrue(true); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function test_single_plugin(): void |
29
|
|
|
{ |
30
|
|
|
$plugin = new class($this) extends Plugin { |
31
|
|
|
|
32
|
|
|
public function __construct(private TestCase $class) |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function onUpdate(Update $update): \Generator |
38
|
|
|
{ |
39
|
|
|
$this->class->assertEquals(1, $update->getUpdateId()); |
40
|
|
|
|
41
|
|
|
yield Request::sendMessage([ |
42
|
|
|
'chat_id' => $update->getMessage()->getChat()->getId(), |
43
|
|
|
'text' => 'Hello World!' |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
(new Dotenv)->load(__DIR__ . '/../.env.example'); |
50
|
|
|
(new UpdateHandler())->addPlugins($plugin)->resolve(Telegram::processUpdate( |
51
|
|
|
'{"update_id":1,"message":{"message_id":1,"from":{"id":1,"is_bot":false,"first_name":"First","last_name":"Last","username":"username","language_code":"en"},"chat":{"id":1,"first_name":"First","last_name":"Last","username":"username","type":"private"},"date":1546300800,"text":"Hello World!"}}', |
52
|
|
|
$_ENV['TELEGRAM_BOT_TOKEN'] |
53
|
|
|
)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
} |