Skip to content

Commit

Permalink
Some unit tests and some bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
unreal4u committed Jun 19, 2017
1 parent f32550e commit aafcb02
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 2 deletions.
28 changes: 28 additions & 0 deletions examples/send-invoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types = 1);

include 'basics.php';

use unreal4u\TelegramAPI\Telegram\Methods\SendInvoice;
use unreal4u\TelegramAPI\Telegram\Types\LabeledPrice;
use unreal4u\TelegramAPI\Telegram\Types\Message;
use unreal4u\TelegramAPI\TgLog;

$sendInvoice = new SendInvoice();
$sendInvoice->chat_id = A_USER_CHAT_ID;
$sendInvoice->title = 'My Special Invoice';
$sendInvoice->description = 'This is the description of the invoice';
$sendInvoice->payload = 'specialItem-001';
$sendInvoice->provider_token = PAYMENT_TOKEN;
$sendInvoice->start_parameter = 'u4u-invoice-0001';
$sendInvoice->currency = 'EUR';
$sendInvoice->prices = [ new LabeledPrice(['amount' => 975, 'label' => 'That special item']) ];

var_dump($sendInvoice);

$tgLog = new TgLog(BOT_TOKEN);
/** @var Message $result */
$result = $tgLog->performApiRequest($sendInvoice);

var_dump($result);
3 changes: 1 addition & 2 deletions src/Abstracts/TelegramTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ protected function mapSubObjects(string $key, array $data): TelegramTypes
'The key "%s" does not exist in the class! Maybe a recent Telegram Bot API update? In any way, please '.
'submit an issue (bug report) at %s with this complete log line',
$key,
'https://core.telegram.org/bots/api-changelog',
'https://github.com/unreal4u/telegram-api/issues'
), [
'object' => get_class($this),
'data' => $data
'data' => $data,
]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Telegram/Types/PreCheckoutQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ protected function mapSubObjects(string $key, array $data): TelegramTypes
switch ($key) {
case 'order_info':
return new OrderInfo($data, $this->logger);
case 'from':
return new User($data, $this->logger);
}

return parent::mapSubObjects($key, $data);
Expand Down
1 change: 1 addition & 0 deletions tests/Mock/MockData/GetUpdates-preCheckoutQuery.json
Original file line number Diff line number Diff line change
@@ -0,0 +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"}}]}
1 change: 1 addition & 0 deletions tests/Mock/MockData/SendInvoice.json
Original file line number Diff line number Diff line change
@@ -0,0 +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}}}
19 changes: 19 additions & 0 deletions tests/Telegram/Methods/GetUpdatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use unreal4u\TelegramAPI\Telegram\Types\Custom\UpdatesArray;
use unreal4u\TelegramAPI\Telegram\Types\PreCheckoutQuery;
use unreal4u\TelegramAPI\Telegram\Types\Update;
use unreal4u\TelegramAPI\Telegram\Types\Message;
use unreal4u\TelegramAPI\Telegram\Types\User;
Expand Down Expand Up @@ -133,4 +134,22 @@ public function testNewChatMemberInUpdate()
$this->assertInstanceOf(User::class, $update->message->new_chat_member);
}
}

public function testPreCheckoutQuery()
{
$this->tgLog->specificTest = 'preCheckoutQuery';

$getUpdates = new GetUpdates();

$updatesArray = $this->tgLog->performApiRequest($getUpdates);
/** @var Update $update */
foreach ($updatesArray->traverseObject() as $update) {
$this->assertInstanceOf(Update::class, $update);
$this->assertInstanceOf(PreCheckoutQuery::class, $update->pre_checkout_query);
$this->assertInstanceOf(User::class, $update->pre_checkout_query->from);
$this->assertNull($update->pre_checkout_query->order_info);
$this->assertSame(975, $update->pre_checkout_query->total_amount);
$this->assertSame('EUR', $update->pre_checkout_query->currency);
}
}
}
57 changes: 57 additions & 0 deletions tests/Telegram/Methods/SendInvoiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace unreal4u\TelegramAPI\tests\Telegram\Methods;

use PHPUnit\Framework\TestCase;
use unreal4u\TelegramAPI\Telegram\Methods\SendInvoice;
use unreal4u\TelegramAPI\Telegram\Types\Invoice;
use unreal4u\TelegramAPI\Telegram\Types\LabeledPrice;
use unreal4u\TelegramAPI\tests\Mock\MockTgLog;
use unreal4u\TelegramAPI\Telegram\Types\Message;

class SendInvoiceTest extends TestCase
{
/**
* @var MockTgLog
*/
private $tgLog;

/**
* Prepares the environment before running a test.
*/
protected function setUp()
{
parent::setUp();
$this->tgLog = new MockTgLog('TEST-TEST');
}

/**
* Cleans up the environment after running a test.
*/
protected function tearDown()
{
$this->tgLog = null;
parent::tearDown();
}

public function testSendInvoice()
{
$sendInvoice = new SendInvoice();
$sendInvoice->chat_id = 12341234;
$sendInvoice->title = 'My Special Invoice';
$sendInvoice->description = 'This is the description of the invoice';
$sendInvoice->payload = 'specialItem-001';
$sendInvoice->provider_token = 'TEST-TOKEN-TEST';
$sendInvoice->start_parameter = 'u4u-invoice-0001';
$sendInvoice->currency = 'EUR';
$sendInvoice->prices = [ new LabeledPrice(['amount' => 975, 'label' => 'That special item']) ];

/** @var Message $result */
$result = $this->tgLog->performApiRequest($sendInvoice);
$this->assertInstanceOf(Message::class, $result);
$this->assertInstanceOf(Invoice::class, $result->invoice);
$this->assertSame(975, $result->invoice->total_amount);
$this->assertSame('EUR', $result->invoice->currency);
$this->assertSame('u4u-invoice-0001', $result->invoice->start_parameter);
}
}

0 comments on commit aafcb02

Please sign in to comment.