Unverified Commit 9ef97301 authored by Armando Lüscher's avatar Armando Lüscher

Merge remote-tracking branch 'upstream/develop' into refactor_entities

* upstream/develop:
  implement getWebhookInfo method
  Fix duplicate test case
  Add test for unit testing class Entitiel/User
  Move AudioTest.php from tests/Unit* to tests/unit*
  Fix error importing class TestHelpers
  Correcting grammatical errors
  Add unit test for Audio class
  Add helper method generate fake audio recorded data
  Add dependency for ext-mbstring, as suggested by @IronLady in #92 [skip ci]
parents d1b4f1ce 3eb15014
......@@ -21,6 +21,7 @@
"php": ">=5.5.0",
"ext-pdo": "*",
"ext-curl": "*",
"ext-mbstring": "*",
"monolog/monolog": "^1.19",
"guzzlehttp/guzzle": "~6.0"
},
......
......@@ -69,6 +69,9 @@ class ServerResponse extends Entity
} elseif (isset($data['result']['user'])) {
//Response from getChatMember
$this->result = new ChatMember($data['result']);
} elseif (isset($data['result']['has_custom_certificate'])) {
//Response from getWebhookInfo
$this->result = new WebhookInfo($data['result']);
} else {
//Response from sendMessage
$this->result = new Message($data['result'], $bot_name);
......
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException;
class WebhookInfo extends Entity
{
protected $url; // String Webhook URL, may be empty if webhook is not set up
protected $has_custom_certificate; // Boolean True, if a custom certificate was provided for webhook certificate checks
protected $pending_update_count; // Integer Number of updates awaiting delivery
protected $last_error_date; // Integer Optional. Unix time for the most recent error that happened when trying to deliver an update via webhook
protected $last_error_message; // String Optional. Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook
public function __construct(array $data)
{
$this->url = isset($data['url']) ? $data['url'] : null;
$this->has_custom_certificate = isset($data['has_custom_certificate']) ? $data['has_custom_certificate'] : null;
$this->pending_update_count = isset($data['pending_update_count']) ? $data['pending_update_count'] : null;
$this->last_error_date = isset($data['last_error_date']) ? $data['last_error_date'] : null;
$this->last_error_message = isset($data['last_error_message']) ? $data['last_error_message'] : null;
}
/**
* Webhook URL, may be empty if webhook is not set up.
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* True, if a custom certificate was provided for webhook certificate checks.
*
* @return bool
*/
public function getHasCustomCertificate()
{
return $this->has_custom_certificate;
}
/**
* Number of updates awaiting delivery.
*
* @return int
*/
public function getPendingUpdateCount()
{
return $this->pending_update_count;
}
/**
* Optional.
* Unix time for the most recent error that happened when trying to deliver an update via webhook.
*
* @return int
*/
public function getLastErrorDate()
{
return $this->last_error_date;
}
/**
* Optional.
* Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook.
*
* @return string
*/
public function getLastErrorMessage()
{
return $this->last_error_message;
}
}
......@@ -81,6 +81,7 @@ class Request
'editMessageText',
'editMessageCaption',
'editMessageReplyMarkup',
'getWebhookInfo',
];
/**
......@@ -842,4 +843,14 @@ class Request
return $results;
}
/**
* Use this method to get current webhook status.
*
* @return Entities\ServerResponse
*/
public static function getWebhookInfo()
{
return self::send('getWebhookInfo', []);
}
}
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Tests\Unit;
use Longman\TelegramBot\Entities\Audio;
/**
* @package TelegramTest
* @author Baev Nikolay <gametester3d@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class AudioTest extends TestCase
{
/**
* @var array
*/
private $record;
/**
* Set Up
*/
public function setUp()
{
$this->record = TestHelpers::getFakeRecordedAudio();
}
public function testInstance()
{
$audio = new Audio($this->record);
self::assertInstanceOf('Longman\TelegramBot\Entities\Audio', $audio);
}
public function testGetProperties()
{
$audio = new Audio($this->record);
self::assertEquals($this->record['file_id'], $audio->getFileId());
self::assertEquals($this->record['duration'], $audio->getDuration());
self::assertEquals($this->record['performer'], $audio->getPerformer());
self::assertEquals($this->record['title'], $audio->getTitle());
self::assertEquals($this->record['mime_type'], $audio->getMimeType());
self::assertEquals($this->record['file_size'], $audio->getFileSize());
}
}
......@@ -10,7 +10,7 @@
namespace Longman\TelegramBot\Tests\Unit;
use \Longman\TelegramBot\Entities\User;
use Longman\TelegramBot\Entities\User;
/**
* @package TelegramTest
......@@ -21,53 +21,67 @@ use \Longman\TelegramBot\Entities\User;
*/
class UserTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Entities\User
*/
private $user;
public function testUsername()
public function testInstance()
{
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'jtaylor']);
$this->assertEquals('@jtaylor', $this->user->tryMention());
$user = new User(['id' => 1]);
self::assertInstanceOf('Longman\TelegramBot\Entities\User', $user);
}
public function testFirstName()
public function testGetId()
{
$this->user = new User(['id' => 1, 'first_name' => 'John']);
$this->assertEquals('John', $this->user->tryMention());
$user = new User(['id' => 123]);
self::assertEquals(123, $user->getId());
}
public function testLastName()
public function testTryMention()
{
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']);
$this->assertEquals('John Taylor', $this->user->tryMention());
// Username
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'jtaylor']);
self::assertEquals('@jtaylor', $user->tryMention());
// First name.
$user = new User(['id' => 1, 'first_name' => 'John']);
self::assertEquals('John', $user->tryMention());
// First and Last name.
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']);
self::assertEquals('John Taylor', $user->tryMention());
}
public function testStripMarkDown()
{
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']);
$this->assertEquals('\`\[\*\_', $this->user->stripMarkDown('`[*_'));
}
// Plain stripMarkDown functionality.
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']);
self::assertEquals('\`\[\*\_', $user->stripMarkDown('`[*_'));
public function testUsernameMarkdown()
{
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']);
$this->assertEquals('@j_taylor', $this->user->tryMention());
$this->assertEquals('@j\_taylor', $this->user->tryMention(true));
}
// Username.
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']);
self::assertEquals('@j_taylor', $user->tryMention());
self::assertEquals('@j\_taylor', $user->tryMention(true));
public function testFirstNameMarkdown()
{
$this->user = new User(['id' => 1, 'first_name' => 'John[']);
$this->assertEquals('John[', $this->user->tryMention());
$this->assertEquals('John\[', $this->user->tryMention(true));
// First name.
$user = new User(['id' => 1, 'first_name' => 'John[']);
self::assertEquals('John[', $user->tryMention());
self::assertEquals('John\[', $user->tryMention(true));
// First and Last name.
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => '`Taylor`']);
self::assertEquals('John `Taylor`', $user->tryMention());
self::assertEquals('John \`Taylor\`', $user->tryMention(true));
}
public function testLastNameMarkdown()
public function testGetProperties()
{
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => '`Taylor`']);
$this->assertEquals('John `Taylor`', $this->user->tryMention());
$this->assertEquals('John \`Taylor\`', $this->user->tryMention(true));
// Username.
$user = new User(['id' => 1, 'username' => 'name_phpunit']);
self::assertEquals('name_phpunit', $user->getUsername());
// First name.
$user = new User(['id' => 1, 'first_name' => 'name_phpunit']);
self::assertEquals('name_phpunit', $user->getFirstName());
// Last name.
$user = new User(['id' => 1, 'last_name' => 'name_phpunit']);
self::assertEquals('name_phpunit', $user->getLastName());
}
}
......@@ -151,6 +151,26 @@ class TestHelpers
return new Chat($data + self::$chat_template);
}
/**
* Get fake recorded audio track
*
* @return array
*/
public static function getFakeRecordedAudio()
{
$mime_type = ['audio/ogg', 'audio/mpeg', 'audio/vnd.wave', 'audio/x-ms-wma', 'audio/basic'];
$data = [
'file_id' => mt_rand(1, 999),
'duration' => (string)mt_rand(1, 99) . ':' . mt_rand(1, 60),
'performer' => 'phpunit',
'title' => 'track from phpunit',
'mime_type' => $mime_type[array_rand($mime_type, 1)],
'file_size' => mt_rand(1, 99999)
];
return $data;
}
/**
* Return a fake message object using the passed ids.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment