-
Notifications
You must be signed in to change notification settings - Fork 675
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from antoniomadonna/unit-tests
Some PHPUnit tests #5
- Loading branch information
Showing
5 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<phpunit bootstrap="vendor/autoload.php"> | ||
<testsuites> | ||
<testsuite name="default"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace test\TelegramBotSDK; | ||
|
||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
use Prophecy\Argument; | ||
use Prophecy\Prophet; | ||
use Telegram\Bot\Api; | ||
use Telegram\Bot\Commands\CommandBus; | ||
use Telegram\Bot\HttpClients\GuzzleHttpClient; | ||
use Telegram\Bot\Objects\Update; | ||
use Telegram\Bot\TelegramClient; | ||
use test\TelegramBotSDK\Mocks\MockCommand; | ||
use test\TelegramBotSDK\Mocks\MockCommandTwo; | ||
|
||
class ApiTest extends \PHPUnit_Framework_TestCase { | ||
|
||
protected $api, $prophet; | ||
|
||
public function setUp() { | ||
$this->api = new Api('token'); | ||
$this->prophet = new Prophet(); | ||
} | ||
|
||
/** | ||
* @expectedException Telegram\Bot\Exceptions\TelegramSDKException | ||
*/ | ||
public function testThrowsExceptionOnNullToken() { | ||
new Api(); | ||
} | ||
|
||
public function testReturnsPassedToken() { | ||
$this->assertEquals('token', $this->api->getAccessToken()); | ||
$this->api->setAccessToken('another'); | ||
$this->assertEquals('another', $this->api->getAccessToken()); | ||
} | ||
|
||
public function testReturnsClientObject() { | ||
$this->assertInstanceOf(TelegramClient::class, $this->api->getClient()); | ||
} | ||
|
||
public function testReturnsCommandBus() { | ||
$this->assertInstanceOf(CommandBus::class, $this->api->getCommandBus()); | ||
} | ||
|
||
public function testAddsAndReturnsInstantiatedCommands() { | ||
$this->api->addCommand(MockCommand::class); | ||
$commands = $this->api->getCommands(); | ||
$this->assertInstanceOf(MockCommand::class, $commands['mycommand']); | ||
} | ||
|
||
public function testAddsMultipleCommands() { | ||
$this->api->addCommands([MockCommand::class, MockCommandTwo::class]); | ||
$commands = $this->api->getCommands(); | ||
$this->assertInstanceOf(MockCommand::class, $commands['mycommand']); | ||
$this->assertInstanceOf(MockCommandTwo::class, $commands['mycommand2']); | ||
} | ||
|
||
public function testCommandsHandlerReturnsUpdates() { | ||
$this->setTelegramTextResponse('/start'); | ||
$updates = $this->api->commandsHandler(); | ||
$this->assertInstanceOf(Update::class, $updates[0]); | ||
} | ||
|
||
public function testHandlesTheRightCommand() { | ||
$this->setTelegramTextResponse('/mycommand'); | ||
$command = $this->addStubCommand('mycommand'); | ||
$command2 = $this->addStubCommand('mycommand2'); | ||
|
||
$this->api->commandsHandler(); | ||
|
||
$command->make(Argument::any(), Argument::any(), Argument::any())->shouldHaveBeenCalled(); | ||
$command2->make(Argument::any(), Argument::any(), Argument::any())->shouldNotHaveBeenCalled(); | ||
} | ||
|
||
/** | ||
* Shortcut to setTelegramResponse() | ||
* | ||
* @param string $message | ||
*/ | ||
private function setTelegramTextResponse($message = '/start') { | ||
$response = ['result' => [ | ||
['message' => ['text' => $message]] | ||
]]; | ||
$this->setTelegramResponse($response); | ||
} | ||
|
||
/** | ||
* Recreates the Api object, using a mock http client, with predefined | ||
* responses containing the provided $body | ||
* | ||
* @param $body | ||
*/ | ||
private function setTelegramResponse($body) { | ||
$body = json_encode($body); | ||
$mock = new MockHandler([ | ||
new Response(200, [], $body), | ||
new Response(200, [], $body) // two times because Api::commandsHandler makes two requests | ||
]); | ||
$handler = HandlerStack::create($mock); | ||
$client = new GuzzleHttpClient(new Client(['handler' => $handler])); | ||
|
||
$this->api = new Api('token', false, $client); | ||
} | ||
|
||
/** | ||
* Creates a stub command that responds to getName() and make() method calls | ||
* | ||
* @param string $name | ||
* @return \Prophecy\Prophecy\ObjectProphecy | ||
*/ | ||
private function addStubCommand($name = 'start') { | ||
$command = $this->prophet->prophesize(MockCommand::class); | ||
$command->getName()->willReturn($name); | ||
$command->make(Argument::any(), Argument::any(), Argument::any())->willReturn(null); | ||
|
||
$this->api->addCommand($command->reveal()); | ||
|
||
return $command; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace test\TelegramBotSDK\Mocks; | ||
|
||
|
||
use Telegram\Bot\Commands\Command; | ||
|
||
class MockCommand extends Command { | ||
|
||
protected $name = 'mycommand'; | ||
|
||
protected $description = 'a mock command'; | ||
|
||
public function handle($args) { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace test\TelegramBotSDK\Mocks; | ||
|
||
|
||
use Telegram\Bot\Commands\Command; | ||
|
||
class MockCommandTwo extends Command { | ||
|
||
protected $name = 'mycommand2'; | ||
|
||
protected $description = 'another mock command'; | ||
|
||
public function handle($args) { | ||
|
||
} | ||
|
||
} |