Commit f7a36aee authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #304 from monster3d/tests/telegram-bot-api

Unit tests for Audio and User entity.
Thanks a lot @monster3d 
parents ac570091 05816f31
<?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;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Tests\Unit\TestHelpers;
/**
* @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();
}
/**
* Testing base stage with data object creating
*/
public function testBaseStageAudio()
{
$audio = new Audio($this->record);
$this->assertInstanceOf('Longman\TelegramBot\Entities\Audio', $audio);
}
/**
* Test base stage without duration property
*
* @expectedException Longman\TelegramBot\Exception\TelegramException
*/
public function testBaseStageWithoutDuration()
{
$this->record['duration'] = null;
new Audio($this->record);
}
/**
* Test base stage without file_id property
*
* @expectedException Longman\TelegramBot\Exception\TelegramException
*/
public function testBaseStageWithoutFileId()
{
$this->record['file_id'] = null;
new Audio($this->record);
}
/**
* Test get file id
*/
public function testGetFileId()
{
$audio = new Audio($this->record);
$file_id = $audio->getFileId();
$this->assertEquals($this->record['file_id'], $file_id);
}
/**
* Test get duration track
*/
public function testGetDuration()
{
$audio = new Audio($this->record);
$duration = $audio->getDuration();
$this->assertEquals($this->record['duration'], $duration);
}
/**
* Test get performer track
*/
public function testGetPerformer()
{
$audio = new Audio($this->record);
$performer = $audio->getPerformer();
$this->assertEquals($this->record['performer'], $performer);
}
/**
* Test get title track
*/
public function testGetTitle()
{
$audio = new Audio($this->record);
$title = $audio->getTitle();
$this->assertEquals($this->record['title'], $title);
}
/**
* Test get mime type file
*/
public function testGetMimeType()
{
$audio = new Audio($this->record);
$mime_type = $audio->getMimeType();
$this->assertEquals($this->record['mime_type'], $mime_type);
}
/**
* Test get file size
*/
public function testGetFileSize()
{
$audio = new Audio($this->record);
$file_size = $audio->getFileSize();
$this->assertEquals($this->record['file_size'], $file_size);
}
}
<?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\User;
use Longman\TelegramBot\Exception\TelegramException;
/**
* @package UserTest
* @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 UserTest extends TestCase
{
/**
* setUp
*
*/
public function setUp()
{
//void
}
/**
* tearDown
*
*/
public function tearDown()
{
//void
}
/**
* Test base stage
*
*/
public function testStageBase()
{
$user = new User(['id' => mt_rand(1, 99)]);
$this->assertInstanceOf('Longman\TelegramBot\Entities\User', $user);
}
/**
* Test sage without param user id
*
* @expectedException Longman\TelegramBot\Exception\TelegramException
*
*/
public function testStageWithoutId()
{
new User([]);
}
/**
* Test stage get user id
*
*/
public function testGetId()
{
$user = new User(['id' => mt_rand(1, 99)]);
$result = $user->getId();
$this->assertGreaterThan(0, $result);
}
/**
* Test stage get first name
*
*/
public function testGetFirstName()
{
$user = new User(['id' => mt_rand(1, 99), 'first_name' => 'name_phpunit']);
$result = $user->getFirstName();
$this->assertEquals('name_phpunit', $result);
}
/**
* Test stage get last name
*
*/
public function testGetLastName()
{
$user = new User(['id' => mt_rand(1, 99), 'last_name' => 'name_phpunit']);
$result = $user->getLastName();
$this->assertEquals('name_phpunit', $result);
}
/**
* Test stage get username
*
*/
public function testGetUsername()
{
$user = new User(['id' => mt_rand(1, 99), 'username' => 'name_phpunit']);
$result = $user->getUsername();
$this->assertEquals('name_phpunit', $result);
}
/**
* Test stage mention user
*
*/
public function testTryMention()
{
$user = new User(['id' => mt_rand(1, 99), 'username' => 'name_phpunit']);
$result = $user->tryMention();
$this->assertRegExp('/^\@.*/', $result);
}
/**
* Test stage mention user without param username
*
*/
public function testTryMentionWithoutUsernameStageOne()
{
$user = new User(['id' => mt_rand(1, 99), 'first_name' => 'name_phpunit']);
$result = $user->tryMention();
$this->assertEquals('name_phpunit', $result);
}
/**
* Test stage mention user without username but with last and first name
*
*/
public function testTryMentionWithoutUsernameStageTwo()
{
$user = new User(['id' => mt_rand(1, 99), 'first_name' => 'name_phpunit',
'last_name' => 'name_phpunit']);
$result = $user->tryMention();
$this->assertRegExp('/^.*\s{1}.*$/', $result);
}
}
......@@ -174,6 +174,24 @@ class TestHelpers
'text' => 'dummy',
], 'testbot');
}
/**
* 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;
}
/**
* Start a fake conversation for the passed command and return the randomly generated 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