Skip to content

Commit

Permalink
InlineQueryResultContact added
Browse files Browse the repository at this point in the history
  • Loading branch information
nexor committed Nov 6, 2016
1 parent 7b1ec34 commit 4a9afdc
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
135 changes: 135 additions & 0 deletions src/InlineQueryResult/InlineQueryResultContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Steelbot\TelegramBotApi\InlineQueryResult;

use Steelbot\TelegramBotApi\Traits\{
InputMessageContentTrait,
JsonAttributesBuilderTrait,
ReplyMarkupTrait,
ThumbUrlOptionalTrait,
ThumbWidthHeightOptionalTrait
};

class InlineQueryResultContact extends AbstractInlineQueryResult
{
use ReplyMarkupTrait;
use InputMessageContentTrait;
use ThumbUrlOptionalTrait;
use ThumbWidthHeightOptionalTrait;

use JsonAttributesBuilderTrait;

/**
* @var string
*/
protected $type = 'contact';

/**
* @var string
*/
protected $phoneNumber;

/**
* @var string
*/
protected $firstName;

/**
* @var string|null
*/
protected $lastName;

/**
* @param array $data
*/
public function __construct($id = null, string $phoneNumber, string $firstName)
{
parent::__construct($id);
$this->phoneNumber = $phoneNumber;
$this->firstName = $firstName;
}

/**
* @return string
*/
public function getPhoneNumber(): string
{
return $this->phoneNumber;
}

/**
* @param string $phoneNumber
*
* @return InlineQueryResultContact
*/
public function setPhoneNumber(string $phoneNumber): self
{
$this->phoneNumber = $phoneNumber;

return $this;
}

/**
* @return string
*/
public function getFirstName(): string
{
return $this->firstName;
}

/**
* @param string $firstName
*
* @return InlineQueryResultContact
*/
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;

return $this;
}

/**
* @return null|string
*/
public function getLastName(): ?string
{
return $this->lastName;
}

/**
* @param null|string $lastName
*
* @return InlineQueryResultContact
*/
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;

return $this;
}

/**
* @return array
*/
public function jsonSerialize()
{
$result = parent::jsonSerialize();
$result = array_merge($result, [
'phone_number' => $this->phoneNumber,
'first_name' => $this->firstName,
]);

$attributes = $this->buildJsonAttributes([
'last_name' => $this->lastName,
'reply_markup' => $this->replyMarkup,
'input_message_content' => $this->inputMessageContent,
'thumb_url' => $this->thumbUrl,
'thumb_width' => $this->thumbWidth,
'thumb_height' => $this->thumbHeight
]);
$result = array_merge($result, $attributes);

return $result;
}
}
75 changes: 75 additions & 0 deletions tests/InlineQueryResult/InlineQueryResultContactTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Steelbot\Tests\TelegramBotApi\InlineQueryResult;

use Steelbot\TelegramBotApi\{
InlineQueryResult\InlineQueryResultContact,
InputMessageContent\InputTextMessageContent,
Type\ReplyKeyboardMarkup
};

class InlineQueryResultContactTest extends \PHPUnit_Framework_TestCase
{
const THUMB_URL = 'http://example.com/img.jpg';

/**
* @var InlineQueryResultContact
*/
protected $inlineQueryResult;

public function setUp()
{
$this->inlineQueryResult = new InlineQueryResultContact(null, '+0123456789', 'Firstname');
}

public function testGetSetPhoneNumber()
{
$this->assertEquals('+0123456789', $this->inlineQueryResult->getPhoneNumber());
$this->inlineQueryResult->setPhoneNumber('+9876543210');
$this->assertEquals('+9876543210', $this->inlineQueryResult->getPhoneNumber());
}

public function testGetSetFirstName()
{
$this->assertEquals('Firstname', $this->inlineQueryResult->getFirstName());
$this->inlineQueryResult->setFirstName('Firstname2');
$this->assertEquals('Firstname2', $this->inlineQueryResult->getFirstName());
}

public function testGetSetLastName()
{
$this->assertNull($this->inlineQueryResult->getLastName());
$this->inlineQueryResult->setLastName('Last Name');
$this->assertEquals('Last Name', $this->inlineQueryResult->getLastName());

$this->inlineQueryResult->setLastName(null);
$this->assertNull($this->inlineQueryResult->getLastName());
}

public function testJsonSerialize()
{
$this->inlineQueryResult->setLastName('Lastname');
$this->inlineQueryResult->setThumbUrl(self::THUMB_URL);
$this->inlineQueryResult->setThumbWidth(126);
$this->inlineQueryResult->setThumbHeight(125);
$this->inlineQueryResult->setReplyMarkup(new ReplyKeyboardMarkup(['1', '2', '3']));

$inlineQueryResult = new InputTextMessageContent('input text');
$this->inlineQueryResult->setInputMessageContent($inlineQueryResult);

$resultArray = $this->inlineQueryResult->jsonSerialize();

$result = json_decode(json_encode($resultArray), true);

$this->assertStringStartsWith('steelbot', $result['id']);
$this->assertEquals('+0123456789', $result['phone_number']);
$this->assertEquals('Firstname', $result['first_name']);
$this->assertEquals('Lastname', $result['last_name']);

$this->assertArrayHasKey('reply_markup', $result);
$this->assertArrayHasKey('input_message_content', $result);
$this->assertEquals(self::THUMB_URL, $result['thumb_url']);
$this->assertEquals(126, $result['thumb_width']);
$this->assertEquals(125, $result['thumb_height']);
}
}

0 comments on commit 4a9afdc

Please sign in to comment.