Longman\TelegramBot\Request::generateGeneralFakeServerResponse PHP Method

generateGeneralFakeServerResponse() public static method

Generate general fake server response
public static generateGeneralFakeServerResponse ( array $data = [] ) : array
$data array Data to add to fake response
return array Fake response data
    public static function generateGeneralFakeServerResponse(array $data = [])
    {
        //PARAM BINDED IN PHPUNIT TEST FOR TestServerResponse.php
        //Maybe this is not the best possible implementation
        //No value set in $data ie testing setWebhook
        //Provided $data['chat_id'] ie testing sendMessage
        $fake_response = ['ok' => true];
        // :)
        if ($data === []) {
            $fake_response['result'] = true;
        }
        //some data to let iniatilize the class method SendMessage
        if (isset($data['chat_id'])) {
            $data['message_id'] = '1234';
            $data['date'] = '1441378360';
            $data['from'] = ['id' => 123456789, 'first_name' => 'botname', 'username' => 'namebot'];
            $data['chat'] = ['id' => $data['chat_id']];
            $fake_response['result'] = $data;
        }
        return $fake_response;
    }

Usage Example

 /**
  * @test
  */
 public function testSetGeneralTestFakeResponse()
 {
     //setWebhook ok
     $fake_response = Request::generateGeneralFakeServerResponse();
     $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
     $fake_response = Request::generateGeneralFakeServerResponse(['chat_id' => 123456789, 'text' => 'hello']);
     $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...
 }