10000 [Notifier] [Twilio] Add tests by OskarStark · Pull Request #39409 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Notifier] [Twilio] Add tests #39409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments. 8000
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Notifier][Twilio] Add tests
  • Loading branch information
OskarStark committed Dec 9, 2020
commit 568523bf7a8c8021dc982f559803591ca2a87bae
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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\Twilio\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\Dsn;

final class TwilioTransportFactoryTest extends TestCase
{
public function testCreateWithDsn(): void
{
$factory = $this->createFactory();

$dsn = 'twilio://accountSid:authToken@default?from=0611223344';
$transport = $factory->create(Dsn::fromString($dsn));
$transport->setHost('host.test');

$this->assertSame('twilio://host.test?from=0611223344', (string) $transport);
}

public function testCreateWithNoFromThrowsMalformed(): void
{
$factory = $this->createFactory();

$this->expectException(IncompleteDsnException::class);

$dsnIncomplete = 'twilio://accountSid:authToken@default';
$factory->create(Dsn::fromString($dsnIncomplete));
}

public function testSupportsTwilioScheme(): void
{
$factory = $this->createFactory();

$dsn = 'twilio://accountSid:authToken@default?from=0611223344';
$dsnUnsupported = 'twilioooo://accountSid:authToken@default?from=0611223344';

$this->assertTrue($factory->supports(Dsn::fromString($dsn)));
$this->assertFalse($factory->supports(Dsn::fromString($dsnUnsupported)));
}

public function testNonTwilioSchemeThrows(): void
{
$factory = $this->createFactory();

$this->expectException(UnsupportedSchemeException::class);

$dsnUnsupported = 'twilioooo://accountSid:authToken@default?from=0611223344';
$factory->create(Dsn::fromString($dsnUnsupported));
}

private function createFactory(): TwilioTransportFactory
{
return new TwilioTransportFactory();
}
}
Original file line number Diff line number Diff line change
@@ -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\Twilio\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransport;
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 TwilioTransportTest extends TestCase
{
public function testToStringContainsProperties(): void
{
$transport = $this->createTransport();

$this->assertSame('twilio://host.test?from=sender', (string) $transport);
}

public function testSupportsMessageInterface(): void
{
$transport = $this->createTransport();

$this->assertTrue($transport->supports(new SmsMessage('0611223344', 'Hello!')));
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
}

public function testSendNonSmsMessageThrowsException(): void
{
$transport = $this->createTransport();

$this->expectException(LogicException::class);

$transport->send($this->createMock(MessageInterface::class));
}

private function createTransport(): TwilioTransport
{
return (new TwilioTransport('accountSid', 'authToken', 'sender', $this->createMock(HttpClientInterface::class)))->setHost('host.test');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Notifier\Bridge\Twilio;

use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
Expand All @@ -32,6 +33,11 @@ public function create(Dsn $dsn): TransportInterface
$accountSid = $this->getUser($dsn);
$authToken = $this->getPassword($dsn);
$from = $dsn->getOption('from');

if (!$from) {
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn());
}

Comment on lines +36 to +40
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be considered a bugfix

$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Twilio/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -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 Twilio 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>
0