TestHelpers.php 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?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 Tests;

13 14 15
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\Message;
16
use Longman\TelegramBot\Entities\Update;
17
use Longman\TelegramBot\Entities\User;
18 19 20 21 22 23 24 25

/**
 * @package         TelegramTest
 * @author          Avtandil Kikabidze <akalongman@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
 */
26
class TestHelpers
27
{
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    /**
     * Data template of a user.
     *
     * @var array
     */
    protected static $user_template = [
        'id' => 1,
        'first_name' => 'first',
        'last_name' => 'last',
        'username' => 'user',
    ];

    /**
     * Data template of a chat.
     *
     * @var array
     */
    protected static $chat_template = [
        'id' => 1,
        'first_name' => 'first',
        'last_name' => 'last',
        'username' => 'name',
        'type' => 'private',
    ];

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    /**
     * Set the value of a private/protected property of an object
     *
     * @param object $object   Object that contains the private property
     * @param string $property Name of the property who's value we want to set
     * @param mixed  $value    The value to set to the property
     */
    public static function setObjectProperty($object, $property, $value)
    {
        $ref_object   = new \ReflectionObject($object);
        $ref_property = $ref_object->getProperty($property);
        $ref_property->setAccessible(true);
        $ref_property->setValue($object, $value);
    }

    /**
     * Return a simple fake Update object
     *
71 72
     * @param array $data Pass custom data array if needed
     *
73 74
     * @return Entities\Update
     */
75
    public static function getFakeUpdateObject($data = null)
76
    {
77
        $data = $data ?: [
78 79 80 81
            'update_id' => 1,
            'message'   => [
                'message_id' => 1,
                'chat' => [
82
                    'id' => 1,
83 84 85 86 87 88
                ],
                'date' => 1,
            ]
        ];
        return new Update($data, 'botname');
    }
89 90 91 92 93 94 95 96 97 98 99 100 101 102

    /**
     * Return a fake command object for the passed command text
     *
     * @param string $command_text
     *
     * @return Entities\Update
     */
    public static function getFakeUpdateCommandObject($command_text)
    {
        $data = [
            'update_id' => 1,
            'message' => [
                'message_id' => 1,
103 104 105 106
                'from'       => self::$user_template,
                'chat'       => self::$chat_template,
                'date'       => 1,
                'text'       => $command_text,
107 108 109 110
            ],
        ];
        return self::getFakeUpdateObject($data);
    }
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

    /**
     * Return a fake user object.
     *
     * @return Entities\User
     */
    public static function getFakeUserObject()
    {
        return new User(self::$user_template);
    }

    /**
     * Return a fake chat object.
     *
     * @return Entities\Chat
     */
    public static function getFakeChatObject()
    {
        return new Chat(self::$chat_template);
    }

    /**
     * Return a fake message object using the passed ids.
     *
     * @param integer $message_id
     * @param integer $user_id
     * @param integer $chat_id
     *
     * @return Entities\Message
     */
    public static function getFakeMessageObject($message_id = 1, $user_id = 1, $chat_id = 1)
    {
        return new Message([
            'message_id' => $message_id,
            'from'       => ['id' => $user_id] + self::$user_template,
            'chat'       => ['id' => $chat_id] + self::$chat_template,
            'date'       => 1,
        ], 'botname');
    }

    /**
     * Start a fake conversation for the passed command and return the randomly generated ids.
     *
     * @param string $command
     * @return array
     */
    public static function startFakeConversation($command)
    {
        if (!DB::isDbConnected()) {
            return false;
        }

        //Just get some random values.
        $message_id = rand();
        $user_id = rand();
        $chat_id = rand();

        //Make sure we have a valid user and chat available.
        $message = self::getFakeMessageObject($message_id, $user_id, $chat_id);
        DB::insertMessageRequest($message);
        DB::insertUser($message->getFrom(), null, $message->getChat());

        return compact('message_id', 'user_id', 'chat_id');
    }

    /**
     * Empty all tables for the passed database
     *
     * @param  array $credentials
     */
    public static function emptyDB(array $credentials)
    {
        $dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
        $options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
        try {
            $pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
            $pdo->prepare('
                DELETE FROM `conversation`;
189 190 191
                DELETE FROM `telegram_update`;
                DELETE FROM `chosen_inline_query`;
                DELETE FROM `inline_query`;
192 193 194
                DELETE FROM `message`;
                DELETE FROM `user_chat`;
                DELETE FROM `chat`;
195
                DELETE FROM `user`;
196 197
            ')->execute();
        } catch (\Exception $e) {
198
            throw new TelegramException($e->getMessage());
199 200
        }
    }
201
}