[Notifier] [Nexmo] Add tests

This commit is contained in:
Oskar Stark 2020-12-09 13:58:17 +01:00 committed by Nicolas Grekas
parent b6f3bdba9f
commit 8e566ef84d
5 changed files with 178 additions and 6 deletions

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Notifier\Bridge\Nexmo;
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
@ -29,19 +30,25 @@ final class NexmoTransportFactory extends AbstractTransportFactory
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
if ('nexmo' !== $scheme) {
throw new UnsupportedSchemeException($dsn, 'nexmo', $this->getSupportedSchemes());
}
$apiKey = $this->getUser($dsn);
$apiSecret = $this->getPassword($dsn);
$from = $dsn->getOption('from');
if (!$from) {
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn());
}
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();
if ('nexmo' === $scheme) {
return (new NexmoTransport($apiKey, $apiSecret, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}
throw new UnsupportedSchemeException($dsn, 'nexmo', $this->getSupportedSchemes());
}
protected function getSupportedSchemes(): array
{
return ['nexmo'];

View File

@ -0,0 +1,83 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Notifier\Bridge\Nexmo\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\Dsn;
final class NexmoTransportFactoryTest extends TestCase
{
public function testCreateWithDsn()
{
$factory = $this->createFactory();
$dsn = 'nexmo://apiKey:apiSecret@default?from=0611223344';
$transport = $factory->create(Dsn::fromString($dsn));
$transport->setHost('host.test');
$this->assertSame('nexmo://host.test?from=0611223344', (string) $transport);
}
public function testCreateWithMissingOptionFromThrowsIncompleteDsnException()
{
$factory = $this->createFactory();
$this->expectException(IncompleteDsnException::class);
$dsnIncomplete = 'nexmo://apiKey:apiSecret@default';
$factory->create(Dsn::fromString($dsnIncomplete));
}
public function testSupportsReturnsTrueWithSupportedScheme()
{
$factory = $this->createFactory();
$dsn = 'nexmo://apiKey:apiSecret@default?from=0611223344';
$this->assertTrue($factory->supports(Dsn::fromString($dsn)));
}
public function testSupportsReturnsFalseWithUnsupportedScheme()
{
$factory = $this->createFactory();
$dsnUnsupported = 'nexmoo://apiKey:apiSecret@default?from=0611223344';
$this->assertFalse($factory->supports(Dsn::fromString($dsnUnsupported)));
}
public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
{
$factory = $this->createFactory();
$this->expectException(UnsupportedSchemeException::class);
$dsnUnsupported = 'nexmoo://apiKey:apiSecret@default?from=0611223344';
$factory->create(Dsn::fromString($dsnUnsupported));
}
public function testUnsupportedSchemeThrowsUnsupportedSchemeExceptionEvenIfRequiredOptionIsMissing()
{
$factory = $this->createFactory();
$this->expectException(UnsupportedSchemeException::class);
// unsupported scheme and missing "from" option
$factory->create(Dsn::fromString('nexmoo://apiKey:apiSecret@default'));
}
private function createFactory(): NexmoTransportFactory
{
return new NexmoTransportFactory();
}
}

View File

@ -0,0 +1,51 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Notifier\Bridge\Nexmo\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransport;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Contracts\HttpClient\HttpClientInterface;
final class NexmoTransportTest extends TestCase
{
public function testToStringContainsProperties()
{
$transport = $this->createTransport();
$this->assertSame('nexmo://host.test?from=sender', (string) $transport);
}
public function testSupportsMessageInterface()
{
$transport = $this->createTransport();
$this->assertTrue($transport->supports(new SmsMessage('0611223344', 'Hello!')));
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
}
public function testSendNonSmsMessageThrowsException()
{
$transport = $this->createTransport();
$this->expectException(LogicException::class);
$transport->send($this->createMock(MessageInterface::class));
}
private function createTransport(): NexmoTransport
{
return (new NexmoTransport('apiKey', 'apiSecret', 'sender', $this->createMock(HttpClientInterface::class)))->setHost('host.test');
}
}

View File

@ -18,7 +18,7 @@
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.0,<5.2"
"symfony/notifier": "~5.1.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Nexmo\\": "" },

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>
<testsuites>
<testsuite name="Symfony Nexmo Notifier Bridge Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>