ServerResponseTest.php 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?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.
 * Written by Marco Boretto
 */
namespace Tests\Unit;

use \Longman\TelegramBot\Entities\ServerResponse;
use \Longman\TelegramBot\Entities\Message;
use \Longman\TelegramBot\Request;

/**
18 19 20 21 22
 * @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
23 24 25 26 27 28 29
 */
class ServerResponseTest extends TestCase
{
    /**
    * @var \Longman\TelegramBot\Telegram
    */
    private $server;
30

31 32 33 34 35 36 37 38 39 40 41
    /**
    * setUp
    */
    protected function setUp()
    {
    }

    /**
     * @test
     */

42
    public function sendMessageOk()
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    {
        return '{
            "ok":true,
            "result":{
                "message_id":1234,
                "from":{"id":123456789,"first_name":"botname","username":"namebot"},
                "chat":{"id":123456789,"first_name":"john","username":"Mjohn"},
                "date":1441378360,
                "text":"hello"
                }
            }';
    }

    public function testSendMessageOk() {

58
        $result = $this->sendMessageOk();
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

        $this->server = new ServerResponse(json_decode($result, true), 'testbot');

        $this->assertTrue($this->server->isOk());
        $this->assertInstanceOf('\Longman\TelegramBot\Entities\Message', $this->server->getResult());
        $this->assertNull($this->server->getErrorCode());
        $this->assertNull($this->server->getDescription());

        //Message
        $this->assertEquals('1234', $this->server->getResult()->getMessageId());
        $this->assertEquals('123456789', $this->server->getResult()->getFrom()->getId());
        $this->assertEquals('botname', $this->server->getResult()->getFrom()->getFirstName());
        $this->assertEquals('namebot', $this->server->getResult()->getFrom()->getUserName());
        $this->assertEquals('123456789', $this->server->getResult()->getChat()->getId());
        $this->assertEquals('john', $this->server->getResult()->getChat()->getFirstName());
        $this->assertEquals('Mjohn', $this->server->getResult()->getChat()->getUserName());
        $this->assertEquals('1441378360', $this->server->getResult()->getDate());
        $this->assertEquals('hello', $this->server->getResult()->getText());
        //... they are not finished...

    }

    /**
     * @test
     */

85
    public function sendMessageFail()
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    {
        return '{
                "ok":false,
                "error_code":400,
                "description":"Error: Bad Request: wrong chat id"
            }';
    }

    public function testSendMessageFail() {

        $result = $this->sendMessageFail();

        $this->server = new ServerResponse(json_decode($result, true), 'testbot');

        $this->assertFalse($this->server->isOk());
        $this->assertNull($this->server->getResult());
        $this->assertEquals('400', $this->server->getErrorCode());
        $this->assertEquals('Error: Bad Request: wrong chat id', $this->server->getDescription());
    }

    /**
     * @test
     */

   public function setWebHookOk()
    {
        return '{"ok":true,"result":true,"description":"Webhook was set"}';
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

    public function testSetWebhookOk() {

        $result = $this->setWebhookOk();

        $this->server =  new ServerResponse(json_decode($result, true), 'testbot');

        $this->assertTrue($this->server->isOk());
        $this->assertTrue($this->server->getResult());
        $this->assertNull($this->server->getErrorCode());
        $this->assertEquals('Webhook was set', $this->server->getDescription());
    }

    /**
     * @test
     */

    public function setWebHookFail()
    {
        return '{
            "ok":false,
            "error_code":400,
            "description":"Error: Bad request: htttps:\/\/domain.host.org\/dir\/hook.php"
            }';
    }


    public function testSetWebhookFail() {

        $result = $this->setWebHookFail();

        $this->server =  new ServerResponse(json_decode($result, true), 'testbot');

        $this->assertFalse($this->server->isOk());
        $this->assertNull($this->server->getResult());
        $this->assertEquals(400, $this->server->getErrorCode());
        $this->assertEquals("Error: Bad request: htttps://domain.host.org/dir/hook.php", $this->server->getDescription());
    }


MBoretto's avatar
MBoretto committed
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 189 190 191 192 193 194 195 196 197 198 199 200 201

    /**
     * @test
     */

    public function getUpdatesArray()
    {
        return '{
            "ok":true,
            "result":[
                {"update_id":123,
                    "message":{
                        "message_id":90,
                        "from":{"id":123456789,"first_name":"John","username":"Mjohn"},
                        "chat":{"id":123456789,"first_name":"John","username":"Mjohn"},
                        "date":1441569067,
                        "text":"\/start"}
                },
                {"update_id":124,
                    "message":{
                        "message_id":91,
                        "from":{"id":123456788,"first_name":"Patrizia","username":"Patry"},
                        "chat":{"id":123456788,"first_name":"Patrizia","username":"Patry"},
                        "date":1441569073,
                        "text":"Hello!"}
                    },
                {"update_id":125,
                    "message":{
                        "message_id":92,
                        "from":{"id":123456789,"first_name":"John","username":"MJohn"},
                        "chat":{"id":123456789,"first_name":"John","username":"MJohn"},
                        "date":1441569094,
                        "text":"\/echo hello!"}
                    },
                {"update_id":126,
                "message":{
                    "message_id":93,
                    "from":{"id":123456788,"first_name":"Patrizia","username":"Patry"},
                    "chat":{"id":123456788,"first_name":"Patrizia","username":"Patry"},
                    "date":1441569112,
                    "text":"\/echo the best"
                    }
                }
            ]
        }';
    }


MBoretto's avatar
MBoretto committed
202 203 204 205 206 207 208 209 210
    public function testGetUpdatesArray() {
        $result = $this->getUpdatesArray();
        $this->server = new ServerResponse(json_decode($result, true), 'testbot');

        $this->assertCount(4, $this->server->getResult());

        $this->assertInstanceOf('\Longman\TelegramBot\Entities\Update', $this->server->getResult()[0]);
    }

MBoretto's avatar
MBoretto committed
211 212 213 214 215 216 217 218 219 220
    /**
     * @test
     */

    public function getUpdatesEmpty()
    {
        return '{"ok":true,"result":[]}';
    }


MBoretto's avatar
MBoretto committed
221 222 223
    public function testGetUpdatesEmpty() {
        $result = $this->getUpdatesEmpty();
        $this->server = new ServerResponse(json_decode($result, true), 'testbot');
MBoretto's avatar
MBoretto committed
224
        $this->assertNull($this->server->getResult());
MBoretto's avatar
MBoretto committed
225
    }
MBoretto's avatar
MBoretto committed
226

MBoretto's avatar
MBoretto committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

    /**
     * @test
     */


    public function getUserProfilePhotos()
    {
        return '{
            "ok":true,
            "result":{
                "total_count":3,
                "photos":[
                    [
                        {"file_id":"AgADBG6_vmQaVf3qOGVurBRzHqgg5uEju-8IBAAEC","file_size":7402,"width":160,"height":160},
                        {"file_id":"AgADBG6_vmQaVf3qOGVurBRzHWMuphij6_MIBAAEC","file_size":15882,"width":320,"height":320},
                        {"file_id":"AgADBG6_vmQaVf3qOGVurBRzHNWdpQ9jz_cIBAAEC","file_size":46680,"width":640,"height":640}
                    ],
                    [
                        {"file_id":"AgADBAADr6cxG6_vmH-bksDdiYzAABO8UCGz_JLAAgI","file_size":7324,"width":160,"height":160},
                        {"file_id":"AgADBAADr6cxG6_vmH-bksDdiYzAABAlhB5Q_K0AAgI","file_size":15816,"width":320,"height":320},
                        {"file_id":"AgADBAADr6cxG6_vmH-bksDdiYzAABIIxOSHyayAAgI","file_size":46620,"width":640,"height":640}
                    ],
                    [
                        {"file_id":"AgABxG6_vmQaL2X0CUTAABMhd1n2RLaRSj6cAAgI","file_size":2710,"width":160,"height":160},
                        {"file_id":"AgADcxG6_vmQaL2X0EUTAABPXm1og0O7qwjKcAAgI","file_size":11660,"width":320,"height":320},
                        {"file_id":"AgADxG6_vmQaL2X0CUTAABMOtcfUmoPrcjacAAgI","file_size":37150,"width":640,"height":640}
                    ]
                ]
            }
        }';
    }


    public function testGetUserProfilePhotos()
    {
        $result = $this->getUserProfilePhotos();
        $this->server = new ServerResponse(json_decode($result, true), 'testbot');

        $this->assertCount(3, $this->server->getResult()->getPhotos());
        $this->assertCount(3, $this->server->getResult()->getPhotos()[0]);
        $this->assertInstanceOf('\Longman\TelegramBot\Entities\UserProfilePhotos', $this->server->getResult());

        $this->assertInstanceOf('\Longman\TelegramBot\Entities\PhotoSize', $this->server->getResult()->getPhotos()[0][0]);

    }


    /**
     * @test
     */


    public function getFile()
    {
        return '{
            "ok":true,
            "result":{
                "file_id":"AgADBxG6_vmQaVf3qRzHYTAABD1hNWdpQ9qz_cIBAAEC",
                "file_size":46680,
                "file_path":"photo\/file_1.jpg"
            }
        }';
    }


    public function testGetFile()
    {
        $result = $this->getFile();
        //print_r(json_decode($result, true));
        $this->server = new ServerResponse(json_decode($result, true), 'testbot');
        //var_dump($this->server->getResult()->getPhotos());

        $this->assertInstanceOf('\Longman\TelegramBot\Entities\File', $this->server->getResult());

    }


MBoretto's avatar
MBoretto committed
305 306 307 308
    /**
     * @test
     */

309 310
    public function testSetGeneralTestFakeResponse() {
        //setWebhook ok
JOSUNGIL's avatar
JOSUNGIL committed
311
        $fake_response = Request::generateGeneralFakeServerResponse();
312 313 314 315 316 317 318 319 320 321

        $this->server =  new ServerResponse($fake_response, 'testbot');

        $this->assertTrue($this->server->isOk());
        $this->assertTrue($this->server->getResult());
        $this->assertNull($this->server->getErrorCode());
        $this->assertEquals('', $this->server->getDescription());


        //sendMessage ok
JOSUNGIL's avatar
JOSUNGIL committed
322
        $fake_response = Request::generateGeneralFakeServerResponse(['chat_id' => 123456789, 'text' => 'hello']);
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

        $this->server =  new ServerResponse($fake_response, 'testbot');

        $this->assertTrue($this->server->isOk());
        $this->assertInstanceOf('\Longman\TelegramBot\Entities\Message', $this->server->getResult());
        $this->assertNull($this->server->getErrorCode());
        $this->assertNull($this->server->getDescription());

        //Message
        $this->assertEquals('1234', $this->server->getResult()->getMessageId());
        $this->assertEquals('1441378360', $this->server->getResult()->getDate());
        $this->assertEquals('hello', $this->server->getResult()->getText());
        //Message //User
        $this->assertEquals('123456789', $this->server->getResult()->getFrom()->getId());
        $this->assertEquals('botname', $this->server->getResult()->getFrom()->getFirstName());
        $this->assertEquals('namebot', $this->server->getResult()->getFrom()->getUserName());
        //Message //Chat
        $this->assertEquals('123456789', $this->server->getResult()->getChat()->getId());
        $this->assertEquals('', $this->server->getResult()->getChat()->getFirstName());
        $this->assertEquals('', $this->server->getResult()->getChat()->getUserName());

        //... they are not finished...
    }
}