|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TelegramBotTest; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use TelegramBot\Entities\Message; |
|
7
|
|
|
use TelegramBot\Entities\Update; |
|
8
|
|
|
use TelegramBot\Plugin; |
|
9
|
|
|
use TelegramBot\UpdateHandler; |
|
10
|
|
|
|
|
11
|
|
|
class WebhookTest extends \PHPUnit\Framework\TestCase { |
|
12
|
|
|
|
|
13
|
|
|
public function testAnonymousPlugins() { |
|
14
|
|
|
$plugin = new class($this) extends Plugin { |
|
15
|
|
|
|
|
16
|
|
|
private $testCase; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(TestCase $testCase) { |
|
19
|
|
|
$this->testCase = $testCase; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function onMessage(int $update_id, Message $message): \Generator { |
|
23
|
|
|
$this->testCase->assertEquals(1, $update_id); |
|
24
|
|
|
$this->testCase->assertEquals('Hello World!', $message->getText()); |
|
25
|
|
|
yield; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
}; |
|
29
|
|
|
|
|
30
|
|
|
$message = DummyUpdate::message(); |
|
31
|
|
|
$message->set('text', 'Hello World!'); |
|
32
|
|
|
|
|
33
|
|
|
(new UpdateHandler())->addPlugins($plugin)->resolve(new Update([ |
|
34
|
|
|
'update_id' => 1, |
|
35
|
|
|
'message' => $message->getRawData(), |
|
36
|
|
|
])); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testFilterIncomingUpdates() { |
|
40
|
|
|
|
|
41
|
|
|
$plugin = new class($this) extends Plugin { |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(TestCase $testCase) { |
|
44
|
|
|
$this->testCase = $testCase; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function __process(Update $update): void { |
|
48
|
|
|
$this->testCase->fail('This should not be called'); |
|
49
|
|
|
} |
|
50
|
|
|
}; |
|
51
|
|
|
|
|
52
|
|
|
$handler = (new UpdateHandler())->addPlugins($plugin); |
|
53
|
|
|
|
|
54
|
|
|
$handler->filterIncomingUpdates([ |
|
55
|
|
|
Update::TYPE_MESSAGE |
|
56
|
|
|
]); |
|
57
|
|
|
|
|
58
|
|
|
$handler->resolve(new Update([ |
|
59
|
|
|
'update_id' => 1, |
|
60
|
|
|
'message' => DummyUpdate::message()->getRawData(), |
|
61
|
|
|
])); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertTrue(true); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |