Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit aafcb02

Browse files
committed
Some unit tests and some bug fixes
1 parent f32550e commit aafcb02

File tree

7 files changed

+109
-2
lines changed

7 files changed

+109
-2
lines changed

examples/send-invoice.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
include 'basics.php';
6+
7+
use unreal4u\TelegramAPI\Telegram\Methods\SendInvoice;
8+
use unreal4u\TelegramAPI\Telegram\Types\LabeledPrice;
9+
use unreal4u\TelegramAPI\Telegram\Types\Message;
10+
use unreal4u\TelegramAPI\TgLog;
11+
12+
$sendInvoice = new SendInvoice();
13+
$sendInvoice->chat_id = A_USER_CHAT_ID;
14+
$sendInvoice->title = 'My Special Invoice';
15+
$sendInvoice->description = 'This is the description of the invoice';
16+
$sendInvoice->payload = 'specialItem-001';
17+
$sendInvoice->provider_token = PAYMENT_TOKEN;
18+
$sendInvoice->start_parameter = 'u4u-invoice-0001';
19+
$sendInvoice->currency = 'EUR';
20+
$sendInvoice->prices = [ new LabeledPrice(['amount' => 975, 'label' => 'That special item']) ];
21+
22+
var_dump($sendInvoice);
23+
24+
$tgLog = new TgLog(BOT_TOKEN);
25+
/** @var Message $result */
26+
$result = $tgLog->performApiRequest($sendInvoice);
27+
28+
var_dump($result);

src/Abstracts/TelegramTypes.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ protected function mapSubObjects(string $key, array $data): TelegramTypes
7171
'The key "%s" does not exist in the class! Maybe a recent Telegram Bot API update? In any way, please '.
7272
'submit an issue (bug report) at %s with this complete log line',
7373
$key,
74-
'https://core.telegram.org/bots/api-changelog',
7574
'https://github.com/unreal4u/telegram-api/issues'
7675
), [
7776
'object' => get_class($this),
78-
'data' => $data
77+
'data' => $data,
7978
]);
8079
}
8180

src/Telegram/Types/PreCheckoutQuery.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ protected function mapSubObjects(string $key, array $data): TelegramTypes
6666
switch ($key) {
6767
case 'order_info':
6868
return new OrderInfo($data, $this->logger);
69+
case 'from':
70+
return new User($data, $this->logger);
6971
}
7072

7173
return parent::mapSubObjects($key, $data);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"ok":true,"result":[{"update_id":816722710,"pre_checkout_query":{"id":"47054498177565117","from":{"id":10955729,"first_name":"Camilo","last_name":"Sperberg","username":"unreal4u","language_code":"en-NL"},"currency":"EUR","total_amount":975,"invoice_payload":"specialItem-001"}}]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"ok":true,"result":{"message_id":2796,"from":{"id":167423524,"first_name":"unreal4uBot","username":"unreal4uBot"},"chat":{"id":10955729,"first_name":"Camilo","last_name":"Sperberg","username":"unreal4u","type":"private"},"date":1497903034,"invoice":{"title":"My Special Invoice","description":"This is the description of the invoice","start_parameter":"u4u-invoice-0001","currency":"EUR","total_amount":975}}}

tests/Telegram/Methods/GetUpdatesTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use unreal4u\TelegramAPI\Telegram\Types\Custom\UpdatesArray;
7+
use unreal4u\TelegramAPI\Telegram\Types\PreCheckoutQuery;
78
use unreal4u\TelegramAPI\Telegram\Types\Update;
89
use unreal4u\TelegramAPI\Telegram\Types\Message;
910
use unreal4u\TelegramAPI\Telegram\Types\User;
@@ -133,4 +134,22 @@ public function testNewChatMemberInUpdate()
133134
$this->assertInstanceOf(User::class, $update->message->new_chat_member);
134135
}
135136
}
137+
138+
public function testPreCheckoutQuery()
139+
{
140+
$this->tgLog->specificTest = 'preCheckoutQuery';
141+
142+
$getUpdates = new GetUpdates();
143+
144+
$updatesArray = $this->tgLog->performApiRequest($getUpdates);
145+
/** @var Update $update */
146+
foreach ($updatesArray->traverseObject() as $update) {
147+
$this->assertInstanceOf(Update::class, $update);
148+
$this->assertInstanceOf(PreCheckoutQuery::class, $update->pre_checkout_query);
149+
$this->assertInstanceOf(User::class, $update->pre_checkout_query->from);
150+
$this->assertNull($update->pre_checkout_query->order_info);
151+
$this->assertSame(975, $update->pre_checkout_query->total_amount);
152+
$this->assertSame('EUR', $update->pre_checkout_query->currency);
153+
}
154+
}
136155
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace unreal4u\TelegramAPI\tests\Telegram\Methods;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use unreal4u\TelegramAPI\Telegram\Methods\SendInvoice;
7+
use unreal4u\TelegramAPI\Telegram\Types\Invoice;
8+
use unreal4u\TelegramAPI\Telegram\Types\LabeledPrice;
9+
use unreal4u\TelegramAPI\tests\Mock\MockTgLog;
10+
use unreal4u\TelegramAPI\Telegram\Types\Message;
11+
12+
class SendInvoiceTest extends TestCase
13+
{
14+
/**
15+
* @var MockTgLog
16+
*/
17+
private $tgLog;
18+
19+
/**
20+
* Prepares the environment before running a test.
21+
*/
22+
protected function setUp()
23+
{
24+
parent::setUp();
25+
$this->tgLog = new MockTgLog('TEST-TEST');
26+
}
27+
28+
/**
29+
* Cleans up the environment after running a test.
30+
*/
31+
protected function tearDown()
32+
{
33+
$this->tgLog = null;
34+
parent::tearDown();
35+
}
36+
37+
public function testSendInvoice()
38+
{
39+
$sendInvoice = new SendInvoice();
40+
$sendInvoice->chat_id = 12341234;
41+
$sendInvoice->title = 'My Special Invoice';
42+
$sendInvoice->description = 'This is the description of the invoice';
43+
$sendInvoice->payload = 'specialItem-001';
44+
$sendInvoice->provider_token = 'TEST-TOKEN-TEST';
45+
$sendInvoice->start_parameter = 'u4u-invoice-0001';
46+
$sendInvoice->currency = 'EUR';
47+
$sendInvoice->prices = [ new LabeledPrice(['amount' => 975, 'label' => 'That special item']) ];
48+
49+
/** @var Message $result */
50+
$result = $this->tgLog->performApiRequest($sendInvoice);
51+
$this->assertInstanceOf(Message::class, $result);
52+
$this->assertInstanceOf(Invoice::class, $result->invoice);
53+
$this->assertSame(975, $result->invoice->total_amount);
54+
$this->assertSame('EUR', $result->invoice->currency);
55+
$this->assertSame('u4u-invoice-0001', $result->invoice->start_parameter);
56+
}
57+
}

0 commit comments

Comments
 (0)