Add some tests, and service refactor

This commit is contained in:
Javier Hernández Gil 2014-03-11 00:17:33 +01:00
parent a1c701b81d
commit f01d7194fe
4 changed files with 132 additions and 20 deletions

View File

@ -1,6 +1,10 @@
parameters: parameters:
services: services:
jhg_nexmo.sms_message:
class: Nexmo\NexmoMessage
arguments: ["%jhg_nexmo.api_key%","%jhg_nexmo.api_secret%"]
jhg_nexmo.sms.sender: jhg_nexmo.sms.sender:
class: Jhg\NexmoBundle\Sender\SmsSender class: Jhg\NexmoBundle\Sender\SmsSender
arguments: [@service_container] arguments: [@service_container,@jhg_nexmo.sms_message]

View File

@ -1,31 +1,44 @@
<?php <?php
namespace Jhg\NexmoBundle\Sender; namespace Jhg\NexmoBundle\Sender;
use Nexmo\NexmoMessage; use Nexmo\NexmoMessage;
use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\Container;
/**
* Class SmsSender
* @package Jhg\NexmoBundle\Sender
*
* @author Javi Hernández
*/
class SmsSender class SmsSender
{ {
/** /**
* @var Container * @var \Nexmo\NexmoMessage
*/
protected $nexmoMessage;
/**
* @var \Symfony\Component\DependencyInjection\Container
*/ */
protected $container; protected $container;
/** /**
* @var NexmoMessage * @param Container $container
* @param NexmoMessage $nexmoMessage
*/ */
protected $nexmoMessage; public function __construct(Container $container,NexmoMessage $nexmoMessage) {
public function __construct( Container $container) {
$this->container = $container; $this->container = $container;
$this->nexmoMessage = $nexmoMessage;
$api_key = $this->container->getParameter('jhg_nexmo.api_key');
$api_secret = $this->container->getParameter('jhg_nexmo.api_secret');
$this->nexmoMessage = new NexmoMessage($api_key, $api_secret);
} }
/**
* @param $number
* @param null $fromName
* @param $message
* @param null $unicode
* @param int $status_report_req
* @return array|bool|\Nexmo\stdClass
*/
public function send($number,$fromName=null,$message,$unicode=null, $status_report_req=0) { public function send($number,$fromName=null,$message,$unicode=null, $status_report_req=0) {
if($fromName===null) if($fromName===null)

View File

@ -0,0 +1,46 @@
<?php
namespace Jhg\NexmoBundle\Tests\Command;
use Jhg\NexmoBundle\Command\SmsSendCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Serializer\Exception\RuntimeException;
/**
* Class SmsSendCommandTest
* @package Jhg\NexmoBundle\Tests\Command
*
* @author Javi Hernández
*/
class SmsSendCommandTest extends \PHPUnit_Framework_TestCase {
public function testExecute()
{
$application = new Application();
$application->add(new SmsSendCommand());
$command = $application->find('nexmo:sms:send');
$commandTester = new CommandTester($command);
try {
$commandTester->execute(array());
$this->assertTrue(false);
} catch(\RuntimeException $e) {
$this->assertEquals('Not enough arguments.',$e->getMessage());
}
$arguments = array(
'command' => 'nexmo:sms:send',
'number' => 'demo:greet',
'fromName' => 'Fabien',
'message' => '',
);
$input = new ArrayInput($arguments);
$returnCode = $command->run($input, $output);
$commandTester->execute(array("+34666555444","MyApp","Hello World!!"));
// $this->assertRegExp('/.../', $commandTester->getDisplay());
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace Jhg\NexmoBundle\Tests\Sender;
use Jhg\NexmoBundle\Sender\SmsSender;
/**
* Class SmsSenderTest
* @package Jhg\NexmoBundle\Tests\Sender
*
* @author Javi Hernández
*/
class SmsSenderTest extends \PHPUnit_Framework_TestCase
{
public function getSendData() {
return array(
array(
array(
'jhg_nexmo.api_key'=>'asdfghjkl',
'jhg_nexmo.api_secret'=>'1234567890',
'jhg_nexmo.from_name'=>'From Name',
),
'+34666555444',
'From Name',
'Message',
null,
0,
array(),
),
);
}
/**
* @dataProvider getSendData
*/
public function testSend($containerParameters,$number,$fromName,$message,$unicode,$status_report_req,$sendRequestReturnData) {
$containerMock = $this->getMock('Symfony\Component\DependencyInjection\Container');
$nexmoMessageMock = $this->getMock('Nexmo\NexmoMessage',array(),array($containerMock,$containerParameters['jhg_nexmo.api_key'],$containerParameters['jhg_nexmo.api_secret']));
$nexmoMessageMock->expects($this->any())
->method('sendRequest')
->will($this->returnValue($sendRequestReturnData));
$sender = new SmsSender($containerMock,$nexmoMessageMock);
$result = $sender->send($number,$fromName,$message,$unicode,$status_report_req);
$this->assertEquals(null,$result);
}
}