Skip to content

Commit

Permalink
Merge pull request #58 from antoniomadonna/unit-tests
Browse files Browse the repository at this point in the history
Some PHPUnit tests #5
  • Loading branch information
irazasyed committed Nov 25, 2015
2 parents 57f6ced + b504674 commit 59ab4df
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 1 deletion.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"autoload": {
"psr-4": {
"Telegram\\Bot\\": "src/"
"Telegram\\Bot\\": "src/",
"test\\TelegramBotSDK\\": "tests/"
}
},
"suggest": {
Expand All @@ -29,5 +30,9 @@
"branch-alias": {
"dev-master": "2.0-dev"
}
},
"require-dev": {
"phpunit/phpunit": "^5.0",
"phpspec/prophecy": "^1.5"
}
}
7 changes: 7 additions & 0 deletions phpunit.xml
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>
126 changes: 126 additions & 0 deletions tests/ApiTest.php
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;
}

}
18 changes: 18 additions & 0 deletions tests/Mocks/MockCommand.php
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) {

}

}
18 changes: 18 additions & 0 deletions tests/Mocks/MockCommandTwo.php
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) {

}

}

0 comments on commit 59ab4df

Please sign in to comment.