|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the TelegramBot package. |
| 4 | + * |
| 5 | + * (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Tests\Unit; |
| 12 | + |
| 13 | +use Tests\TestHelpers; |
| 14 | +use Longman\TelegramBot\Conversation; |
| 15 | +use Longman\TelegramBot\Telegram; |
| 16 | + |
| 17 | +/** |
| 18 | + * @package TelegramTest |
| 19 | + * @author Avtandil Kikabidze <[email protected]> |
| 20 | + * @copyright Avtandil Kikabidze <[email protected]> |
| 21 | + * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) |
| 22 | + * @link http://www.github.com/akalongman/php-telegram-bot |
| 23 | + */ |
| 24 | +class ConversationTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var \Longman\TelegramBot\Telegram |
| 28 | + */ |
| 29 | + private $telegram; |
| 30 | + |
| 31 | + /** |
| 32 | + * setUp |
| 33 | + */ |
| 34 | + protected function setUp() |
| 35 | + { |
| 36 | + $credentials = [ |
| 37 | + 'host' => '127.0.0.1', |
| 38 | + 'user' => 'travis', |
| 39 | + 'password' => '', |
| 40 | + 'database' => 'telegrambot', |
| 41 | + ]; |
| 42 | + |
| 43 | + $this->telegram = new Telegram('testapikey', 'testbotname'); |
| 44 | + $this->telegram->enableMySQL($credentials); |
| 45 | + |
| 46 | + //Make sure we start with an empty DB for each test. |
| 47 | + TestHelpers::emptyDB($credentials); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @test |
| 52 | + */ |
| 53 | + public function conversationThatDoesntExistPropertiesSetCorrectly() |
| 54 | + { |
| 55 | + $conversation = new Conversation(123, 456); |
| 56 | + $this->assertAttributeEquals(123, 'user_id', $conversation); |
| 57 | + $this->assertAttributeEquals(456, 'chat_id', $conversation); |
| 58 | + $this->assertAttributeEquals(null, 'command', $conversation); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @test |
| 63 | + */ |
| 64 | + public function conversationThatExistsPropertiesSetCorrectly() |
| 65 | + { |
| 66 | + $info = TestHelpers::startFakeConversation('command'); |
| 67 | + $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); |
| 68 | + $this->assertAttributeEquals($info['user_id'], 'user_id', $conversation); |
| 69 | + $this->assertAttributeEquals($info['chat_id'], 'chat_id', $conversation); |
| 70 | + $this->assertAttributeEquals('command', 'command', $conversation); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @test |
| 75 | + */ |
| 76 | + public function conversationThatDoesntExistWithoutCommand() |
| 77 | + { |
| 78 | + $conversation = new Conversation(1, 1); |
| 79 | + $this->assertFalse($conversation->exists()); |
| 80 | + $this->assertNull($conversation->getCommand()); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @test |
| 85 | + * @expectedException \Longman\TelegramBot\Exception\TelegramException |
| 86 | + */ |
| 87 | + public function conversationThatDoesntExistWithCommand() |
| 88 | + { |
| 89 | + new Conversation(1, 1, 'command'); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @test |
| 94 | + */ |
| 95 | + public function newConversationThatWontExistWithoutCommand() |
| 96 | + { |
| 97 | + TestHelpers::startFakeConversation(null); |
| 98 | + $conversation = new Conversation(0, 0); |
| 99 | + $this->assertFalse($conversation->exists()); |
| 100 | + $this->assertNull($conversation->getCommand()); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @test |
| 105 | + */ |
| 106 | + public function newConversationThatWillExistWithCommand() |
| 107 | + { |
| 108 | + $info = TestHelpers::startFakeConversation('command'); |
| 109 | + $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); |
| 110 | + $this->assertTrue($conversation->exists()); |
| 111 | + $this->assertEquals('command', $conversation->getCommand()); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @test |
| 116 | + */ |
| 117 | + public function stopConversation() |
| 118 | + { |
| 119 | + $info = TestHelpers::startFakeConversation('command'); |
| 120 | + $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); |
| 121 | + $this->assertTrue($conversation->exists()); |
| 122 | + $conversation->stop(); |
| 123 | + |
| 124 | + $conversation2 = new Conversation($info['user_id'], $info['chat_id']); |
| 125 | + $this->assertFalse($conversation2->exists()); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @test |
| 130 | + */ |
| 131 | + public function cancelConversation() |
| 132 | + { |
| 133 | + $info = TestHelpers::startFakeConversation('command'); |
| 134 | + $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); |
| 135 | + $this->assertTrue($conversation->exists()); |
| 136 | + $conversation->cancel(); |
| 137 | + |
| 138 | + $conversation2 = new Conversation($info['user_id'], $info['chat_id']); |
| 139 | + $this->assertFalse($conversation2->exists()); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @test |
| 144 | + */ |
| 145 | + public function updateConversationNotes() |
| 146 | + { |
| 147 | + $info = TestHelpers::startFakeConversation('command'); |
| 148 | + $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); |
| 149 | + $conversation->notes = 'newnote'; |
| 150 | + $conversation->update(); |
| 151 | + |
| 152 | + $conversation2 = new Conversation($info['user_id'], $info['chat_id'], 'command'); |
| 153 | + $this->assertSame('newnote', $conversation2->notes); |
| 154 | + |
| 155 | + $conversation3 = new Conversation($info['user_id'], $info['chat_id']); |
| 156 | + $this->assertSame('newnote', $conversation3->notes); |
| 157 | + } |
| 158 | +} |
0 commit comments