irazasyed / telegram-bot-sdk

🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
https://telegram-bot-sdk.com
BSD 3-Clause "New" or "Revised" License
2.98k stars 660 forks source link

Functional testing in Laravel Project #201

Closed ivankovbas closed 7 years ago

ivankovbas commented 8 years ago

Hi! I'm using the SDK in my Laravel project and I want to write functional tests like this:

    ...
    public function testStartCommandWorksAsExpected() {
        $this->post($this->getWebHookUrl(), $this->prepareMessage("/start"));
        $this->seeInDatabase('dialogues', ['user_chat_id' => 0]);
    }
    ...

post method emulates Request object, but not php://input. So the SDK can't populate Update object correctly cause of that: https://github.com/irazasyed/telegram-bot-sdk/blob/master/src/Api.php#L966

So, how can I write my tests?

jonnywilliamson commented 8 years ago

Yeah I've had this issue since the very start.

Some non-optimal solutions

http://stackoverflow.com/questions/3221542/how-do-i-override-php-input-when-doing-unit-tests

http://news-from-the-basement.blogspot.ie/2011/07/mocking-phpinput.html

If you have any better suggestions please pass them on and make a PR too!

jonnywilliamson commented 8 years ago

Still have this problem but don't know of a good way around it. Anyone else any clever solutions?

http://stackoverflow.com/questions/16296899/file-get-contentsphp-input-empty-in-unit-test

jonnywilliamson commented 8 years ago

I've had an idea. Seems to be working!

PR coming!

mobasher92 commented 8 years ago

I have a problem with laravel. the webhook is set but whenever a request is sent to my webhook I just want to log that it's working with "Log::info('webhook is working!');" but I get this error : "Illuminate\Session\TokenMismatchException" I tried to solve it by adding the route exceptions in protected $except in this file: app/Http/Middleware/VerifyCsrfToken.php but I'm still getting the error :( can you help me please?

irazasyed commented 8 years ago

@mobasher92 Create a new issue with your route codes and the $except array, so we can verify and suggest something.

McGo commented 7 years ago

@mobasher92 your route seems to be setup in web middleware group. This include the check for CSRF token which is not sent from telegram. Just setup the route without web middleware and you are done.

Sorry: was reposted as own topix.

mobasher92 commented 7 years ago

I did it in laravel 5.1. the $exception property is not supported in laravel 5 but in larvel 5.1 when I did the installation step in this doc: https://telegram-bot-sdk.readme.io/docs I had a problem in this step: php artisan vendor:publish --provider="Telegram\Bot\Laravel\TelegramServiceProvider" I got the message that nothing to be published and telegram config file was not cpoied to config folder so i did it manually but after that when I wanted to user Telegram::... in my code I got this error that "Telegram class is not found ..."

why? how can i specify in code that where is the Telegram class I am using?

irazasyed commented 7 years ago

Please refer this example starter: https://github.com/irazasyed/telegram-bot-laravel-starter

Closing this issue as it's quite old now. But feel free to create a new one after testing the above starter. Thanks!