[Notifier] [Nexmo] Add tests
This commit is contained in:
parent
b6f3bdba9f
commit
8e566ef84d
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Notifier\Bridge\Nexmo;
|
namespace Symfony\Component\Notifier\Bridge\Nexmo;
|
||||||
|
|
||||||
|
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
|
||||||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
|
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
|
||||||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
|
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
|
||||||
use Symfony\Component\Notifier\Transport\Dsn;
|
use Symfony\Component\Notifier\Transport\Dsn;
|
||||||
@ -29,19 +30,25 @@ final class NexmoTransportFactory extends AbstractTransportFactory
|
|||||||
public function create(Dsn $dsn): TransportInterface
|
public function create(Dsn $dsn): TransportInterface
|
||||||
{
|
{
|
||||||
$scheme = $dsn->getScheme();
|
$scheme = $dsn->getScheme();
|
||||||
|
|
||||||
|
if ('nexmo' !== $scheme) {
|
||||||
|
throw new UnsupportedSchemeException($dsn, 'nexmo', $this->getSupportedSchemes());
|
||||||
|
}
|
||||||
|
|
||||||
$apiKey = $this->getUser($dsn);
|
$apiKey = $this->getUser($dsn);
|
||||||
$apiSecret = $this->getPassword($dsn);
|
$apiSecret = $this->getPassword($dsn);
|
||||||
$from = $dsn->getOption('from');
|
$from = $dsn->getOption('from');
|
||||||
|
|
||||||
|
if (!$from) {
|
||||||
|
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn());
|
||||||
|
}
|
||||||
|
|
||||||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
|
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
|
||||||
$port = $dsn->getPort();
|
$port = $dsn->getPort();
|
||||||
|
|
||||||
if ('nexmo' === $scheme) {
|
|
||||||
return (new NexmoTransport($apiKey, $apiSecret, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
|
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
|
protected function getSupportedSchemes(): array
|
||||||
{
|
{
|
||||||
return ['nexmo'];
|
return ['nexmo'];
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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');
|
||||||
|
}
|
||||||
|
}
|
@ -18,7 +18,7 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.2.5",
|
"php": ">=7.2.5",
|
||||||
"symfony/http-client": "^4.3|^5.0",
|
"symfony/http-client": "^4.3|^5.0",
|
||||||
"symfony/notifier": "^5.0,<5.2"
|
"symfony/notifier": "~5.1.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Nexmo\\": "" },
|
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Nexmo\\": "" },
|
||||||
|
31
src/Symfony/Component/Notifier/Bridge/Nexmo/phpunit.xml.dist
Normal file
31
src/Symfony/Component/Notifier/Bridge/Nexmo/phpunit.xml.dist
Normal 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>
|
Reference in New Issue
Block a user