Clean up tests and check coding standards for tests folder too.

parent 03abf2f4
......@@ -4,6 +4,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
## [Unreleased]
### Added
- Travis CI webhook for Support Bot.
- Ensure coding standards for `tests` folder with `phpcs`.
### Changed
- All links point to new organisation repo.
- Drop PHP 5.5, add PHP 7.1 support.
......
......@@ -41,7 +41,7 @@
},
"scripts": {
"check-code": [
"./vendor/bin/phpcs --standard=phpcs.xml -snp --encoding=utf-8 src/ --report-width=150"
"./vendor/bin/phpcs --standard=phpcs.xml -snp --encoding=utf-8 src/ tests/ --report-width=150"
],
"test": [
"./vendor/bin/phpunit"
......
......@@ -21,16 +21,15 @@ date_default_timezone_set('UTC');
$autoloader = __DIR__ . '/../vendor/autoload.php';
/*
* Check that --dev composer installation was done
* Check that composer installation was done.
*/
if (!file_exists($autoloader)) {
throw new Exception(
'Please run "php composer.phar install --dev" in root directory '
. 'to setup unit test dependencies before running the tests'
'Please run "composer install" in root directory to setup unit test dependencies before running the tests'
);
}
//Include the Composer autoloader
// Include the Composer autoloader.
require_once $autoloader;
/*
......
......@@ -10,9 +10,9 @@
namespace Longman\TelegramBot\Tests\Unit\Commands;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Tests\Unit\TestCase;
use Longman\TelegramBot\Tests\Unit\TestHelpers;
use Longman\TelegramBot\Telegram;
/**
* @package TelegramTest
......@@ -51,15 +51,15 @@ class CommandTest extends TestCase
public function setUp()
{
//Default command object
$this->telegram = new Telegram(self::$dummy_api_key, 'testbot');
$this->telegram = new Telegram(self::$dummy_api_key, 'testbot');
$this->command_stub = $this->getMockForAbstractClass($this->command_namespace, [$this->telegram]);
//Create separate command object that contain a command config
$this->telegram_with_config = new Telegram(self::$dummy_api_key, 'testbot');
$this->telegram_with_config->setCommandConfig('command_name', ['config_key' => 'config_value']);
$this->command_stub_with_config = $this->getMockBuilder($this->command_namespace)
->disableOriginalConstructor()
->getMockForAbstractClass();
->disableOriginalConstructor()
->getMockForAbstractClass();
//Set a name for the object property so that the constructor can set the config correctly
TestHelpers::setObjectProperty($this->command_stub_with_config, 'name', 'command_name');
$this->command_stub_with_config->__construct($this->telegram_with_config);
......@@ -69,7 +69,7 @@ class CommandTest extends TestCase
public function testCommandConstructorNeedsTelegramObject()
{
$exception_count = 0;
$params_to_test = [
$params_to_test = [
[],
[null],
[12345],
......@@ -160,7 +160,7 @@ class CommandTest extends TestCase
$this->assertEquals(null, $stub->getUpdate());
$this->assertEquals(null, $stub->getMessage());
$update = TestHelpers::getFakeUpdateObject();
$update = TestHelpers::getFakeUpdateObject();
$message = $update->getMessage();
$stub->setUpdate($update);
$this->assertAttributeEquals($update, 'update', $stub);
......
......@@ -10,8 +10,8 @@
namespace Longman\TelegramBot\Tests\Unit\Commands;
use Longman\TelegramBot\Tests\Unit\TestCase;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Tests\Unit\TestCase;
/**
* @package TelegramTest
......
......@@ -27,9 +27,6 @@ class ConversationTest extends TestCase
*/
private $telegram;
/**
* setUp
*/
protected function setUp()
{
$credentials = [
......@@ -43,7 +40,7 @@ class ConversationTest extends TestCase
$this->telegram->enableMySql($credentials);
//Make sure we start with an empty DB for each test.
TestHelpers::emptyDB($credentials);
TestHelpers::emptyDb($credentials);
}
public function testConversationThatDoesntExistPropertiesSetCorrectly()
......
......@@ -26,9 +26,6 @@ class AudioTest extends TestCase
*/
private $record;
/**
* Set Up
*/
public function setUp()
{
$this->record = TestHelpers::getFakeRecordedAudio();
......
......@@ -21,68 +21,34 @@ use Longman\TelegramBot\Entities\File;
*/
class FileTest extends TestCase
{
/**
* lat, long data
*
* @var array
*
*/
public $data;
/**
* @var array
*/
private $data;
/**
*
* Set Up
*
*/
public function setUp()
{
$this->data = [
'file_id' => (int)mt_rand(1, 99),
'file_size' => (int)mt_rand(100, 99999),
'file_path' => 'home' . DIRECTORY_SEPARATOR . 'phpunit'
'file_id' => (int) mt_rand(1, 99),
'file_size' => (int) mt_rand(100, 99999),
'file_path' => 'home' . DIRECTORY_SEPARATOR . 'phpunit',
];
}
/**
*
* TearDown
*
*/
public function tearDown()
{
//pass
}
/**
*
* Testing base stage with data object creating
*
*/
public function testBaseStageLocation()
{
$file = new File($this->data);
$this->assertInstanceOf('Longman\TelegramBot\Entities\File', $file);
}
/**
*
* Testing getFileId
*
*/
public function testGetFileId()
{
$file = new File($this->data);
$id = $file->getFileId();
$id = $file->getFileId();
$this->assertInternalType('int', $id);
$this->assertEquals($this->data['file_id'], $id);
}
/**
*
* Testing getFileSize
*
*/
public function testGetFileSize()
{
$file = new File($this->data);
......@@ -91,11 +57,6 @@ class FileTest extends TestCase
$this->assertEquals($this->data['file_size'], $size);
}
/**
*
* Testing getFilePath
*
*/
public function testGetFilePath()
{
$file = new File($this->data);
......@@ -103,24 +64,14 @@ class FileTest extends TestCase
$this->assertEquals($this->data['file_path'], $path);
}
/**
*
* Testing getFileSize without data
*
*/
public function testGetFileSizeWithoutData()
{
unset($this->data['file_size']);
$file = new File($this->data);
$id = $file->getFileSize();
$id = $file->getFileSize();
$this->assertNull($id);
}
/**
*
* Testing getFilePath without data
*
*/
public function testGetFilePathWithoutData()
{
unset($this->data['file_path']);
......
......@@ -115,9 +115,9 @@ class KeyboardTest extends TestCase
public function testKeyboardWithDataArray()
{
$resize_keyboard = (bool)mt_rand(0, 1);
$one_time_keyboard = (bool)mt_rand(0, 1);
$selective = (bool)mt_rand(0, 1);
$resize_keyboard = (bool) mt_rand(0, 1);
$one_time_keyboard = (bool) mt_rand(0, 1);
$selective = (bool) mt_rand(0, 1);
$keyboard_obj = new Keyboard([
'resize_keyboard' => $resize_keyboard,
......
......@@ -21,71 +21,34 @@ use Longman\TelegramBot\Entities\Location;
*/
class LocationTest extends TestCase
{
/**
* lat, long data
*
* @var array
*
*/
public $coordinates;
private $coordinates;
/**
*
* Set Up
*
*/
public function setUp()
{
$this->coordinates = [
'longitude' => (float)mt_rand(10, 69),
'latitude' => (float)mt_rand(10, 48)
'longitude' => (float) mt_rand(10, 69),
'latitude' => (float) mt_rand(10, 48),
];
}
/**
*
* TearDown
*
*/
public function tearDown()
{
//pass
}
/**
*
* Testing base stage with data object creating
*
*/
public function testBaseStageLocation()
{
$location = new Location($this->coordinates);
$this->assertInstanceOf('Longman\TelegramBot\Entities\Location', $location);
}
/**
*
* Testing getLongitude
*
*/
public function testGetLongitude()
{
$location = new Location($this->coordinates);
$long = $location->getLongitude();
$long = $location->getLongitude();
$this->assertInternalType('float', $long);
$this->assertEquals($this->coordinates['longitude'], $long);
}
/**
*
* Testing getLatitude
*
*/
public function testGetLatitude()
{
$location = new Location($this->coordinates);
$lat = $location->getLatitude();
$lat = $location->getLatitude();
$this->assertInternalType('float', $lat);
$this->assertEquals($this->coordinates['latitude'], $lat);
}
......
......@@ -23,10 +23,23 @@ class ReplyToMessageTest extends TestCase
{
public function testChatType()
{
$json = '
{"update_id":137809335,
"message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"MJohn"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"mybot"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092984,"text":"type some text"},"text":"some text"}}
';
$json = '{
"update_id":137809335,
"message":{
"message_id":4479,
"from":{"id":123,"first_name":"John","username":"MJohn"},
"chat":{"id":-123,"title":"MyChat","type":"group"},
"date":1449092987,
"reply_to_message":{
"message_id":11,
"from":{"id":121,"first_name":"Myname","username":"mybot"},
"chat":{"id":-123,"title":"MyChat","type":"group"},
"date":1449092984,
"text":"type some text"
},
"text":"some text"
}
}';
$update = new Update(json_decode($json, true), 'mybot');
$reply_to_message = $update->getMessage()->getReplyToMessage();
......
......@@ -60,8 +60,8 @@ class ServerResponseTest extends TestCase
self::assertEquals('Mjohn', $server_result->getChat()->getUsername());
self::assertEquals('1441378360', $server_result->getDate());
self::assertEquals('hello', $server_result->getText());
//... they are not finished...
//... they are not finished...
}
public function sendMessageFail()
......
......@@ -23,10 +23,24 @@ class UpdateTest extends TestCase
{
public function testUpdateCast()
{
$json = '
{"update_id":137809336,
"message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"MJohn"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"mybot"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092984,"text":"type some text"},"text":"some text"}}
';
$json = '{
"update_id":137809336,
"message":{
"message_id":4479,
"from":{"id":123,"first_name":"John","username":"MJohn"},
"chat":{"id":-123,"title":"MyChat","type":"group"},
"date":1449092987,
"reply_to_message":{
"message_id":11,
"from":{"id":121,"first_name":"Myname","username":"mybot"},
"chat":{"id":-123,"title":"MyChat","type":"group"},
"date":1449092984,
"text":"type some text"
},
"text":"some text"
}
}';
$struct = json_decode($json, true);
$update = new Update($struct, 'mybot');
......
......@@ -33,18 +33,12 @@ class TelegramLogTest extends TestCase
'external' => '/tmp/php-telegram-bot-externallog.log',
];
/**
* setUp
*/
protected function setUp()
{
// Make sure no monolog instance is set before each test.
TestHelpers::setStaticProperty('Longman\TelegramBot\TelegramLog', 'monolog', null);
}
/**
* tearDown
*/
protected function tearDown()
{
// Make sure no logfiles exist.
......
......@@ -22,8 +22,8 @@ use Longman\TelegramBot\Telegram;
class TelegramTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
*/
* @var \Longman\TelegramBot\Telegram
*/
private $telegram;
/**
......@@ -35,9 +35,6 @@ class TelegramTest extends TestCase
'/tmp/php-telegram-bot-custom-commands-3',
];
/**
* setUp
*/
protected function setUp()
{
$this->telegram = new Telegram(self::$dummy_api_key, 'testbot');
......@@ -48,9 +45,6 @@ class TelegramTest extends TestCase
}
}
/**
* tearDown
*/
protected function tearDown()
{
// Clean up the custom commands paths.
......
......@@ -31,10 +31,10 @@ class TestHelpers
* @var array
*/
protected static $user_template = [
'id' => 1,
'id' => 1,
'first_name' => 'first',
'last_name' => 'last',
'username' => 'user',
'last_name' => 'last',
'username' => 'user',
];
/**
......@@ -43,11 +43,11 @@ class TestHelpers
* @var array
*/
protected static $chat_template = [
'id' => 1,
'first_name' => 'first',
'last_name' => 'last',
'username' => 'name',
'type' => 'private',
'id' => 1,
'first_name' => 'first',
'last_name' => 'last',
'username' => 'name',
'type' => 'private',
'all_members_are_administrators' => false,
];
......@@ -93,11 +93,11 @@ class TestHelpers
'update_id' => mt_rand(),
'message' => [
'message_id' => mt_rand(),
'chat' => [
'chat' => [
'id' => mt_rand(),
],
'date' => time(),
]
'date' => time(),
],
];
return new Update($data, 'testbot');
}
......@@ -113,7 +113,7 @@ class TestHelpers
{
$data = [
'update_id' => mt_rand(),
'message' => [
'message' => [
'message_id' => mt_rand(),
'from' => self::$user_template,
'chat' => self::$chat_template,
......@@ -160,13 +160,13 @@ class TestHelpers
public static function getFakeRecordedAudio()
{
$mime_type = ['audio/ogg', 'audio/mpeg', 'audio/vnd.wave', 'audio/x-ms-wma', 'audio/basic'];
$data = [
$data = [
'file_id' => mt_rand(1, 999),
'duration' => (string)mt_rand(1, 99) . ':' . mt_rand(1, 60),
'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)
'file_size' => mt_rand(1, 99999),
];
return $data;
......@@ -209,8 +209,8 @@ class TestHelpers
//Just get some random values.
$message_id = mt_rand();
$user_id = mt_rand();
$chat_id = mt_rand();
$user_id = mt_rand();
$chat_id = mt_rand();
//Make sure we have a valid user and chat available.
$message = self::getFakeMessageObject(['message_id' => $message_id], ['id' => $user_id], ['id' => $chat_id]);
......@@ -225,9 +225,9 @@ class TestHelpers
*
* @param array $credentials
*/
public static function emptyDB(array $credentials)
public static function emptyDb(array $credentials)
{
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
$pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
......
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