[Notifier][Sinch] Add tests

This commit is contained in:
Oskar Stark 2020-12-09 14:18:17 +01:00 committed by Nicolas Grekas
parent 185ba9cc9a
commit 46d5fb1f30
5 changed files with 178 additions and 6 deletions

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Notifier\Bridge\Sinch;
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
@ -26,17 +27,23 @@ final class SinchTransportFactory extends AbstractTransportFactory
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
if ('sinch' !== $scheme) {
throw new UnsupportedSchemeException($dsn, 'sinch', $this->getSupportedSchemes());
}
$accountSid = $this->getUser($dsn);
$authToken = $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 ('sinch' === $scheme) {
return (new SinchTransport($accountSid, $authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}
throw new UnsupportedSchemeException($dsn, 'sinch', $this->getSupportedSchemes());
return (new SinchTransport($accountSid, $authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}
protected function getSupportedSchemes(): array

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\Sinch\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\Dsn;
final class SinchTransportFactoryTest extends TestCase
{
public function testCreateWithDsn()
{
$factory = $this->createFactory();
$dsn = 'sinch://accountSid:authToken@default?from=0611223344';
$transport = $factory->create(Dsn::fromString($dsn));
$transport->setHost('host.test');
$this->assertSame('sinch://host.test?from=0611223344', (string) $transport);
}
public function testCreateWithMissingOptionFromThrowsIncompleteDsnException()
{
$factory = $this->createFactory();
$this->expectException(IncompleteDsnException::class);
$dsnIncomplete = 'sinch://accountSid:authToken@default';
$factory->create(Dsn::fromString($dsnIncomplete));
}
public function testSupportsReturnsTrueWithSupportedScheme()
{
$factory = $this->createFactory();
$dsn = 'sinch://accountSid:authToken@default?from=0611223344';
$this->assertTrue($factory->supports(Dsn::fromString($dsn)));
}
public function testSupportsReturnsFalseWithUnsupportedScheme()
{
$factory = $this->createFactory();
$dsnUnsupported = 'sinnnnch://accountSid:authToken@default?from=0611223344';
$this->assertFalse($factory->supports(Dsn::fromString($dsnUnsupported)));
}
public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
{
$factory = $this->createFactory();
$this->expectException(UnsupportedSchemeException::class);
$dsnUnsupported = 'sinnnnch://accountSid:authToken@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('sinnnnch://accountSid:authToken@default'));
}
private function createFactory(): SinchTransportFactory
{
return new SinchTransportFactory();
}
}

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\Sinch\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransport;
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 SinchTransportTest extends TestCase
{
public function testToStringContainsProperties()
{
$transport = $this->createTransport();
$this->assertSame('sinch://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(): SinchTransport
{
return (new SinchTransport('accountSid', 'authToken', 'sender', $this->createMock(HttpClientInterface::class)))->setHost('host.test');
}
}

View File

@ -19,7 +19,7 @@
"php": ">=7.2.5",
"ext-json": "*",
"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\\Sinch\\": "" },

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 Sinch 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>