8000 [Notifier] Use abstract test cases in 5.2 by OskarStark · Pull Request #39736 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Notifier] Use abstract test cases in 5.2 #39736

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
Jan 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,74 +11,43 @@

namespace Symfony\Component\Notifier\Bridge\Discord\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;

final class DiscordTransportFactoryTest extends TestCase
final class DiscordTransportFactoryTest extends TransportFactoryTestCase
{
public function testCreateWithDsn()
/**
* @return DiscordTransportFactory
*/
public function createFactory(): TransportFactoryInterface
{
$factory = $this->createFactory();

$transport = $factory->create(Dsn::fromString('discord://token@host.test?webhook_id=testWebhookId'));

$this->assertSame('discord://host.test?webhook_id=testWebhookId', (string) $transport);
}

public function testCreateWithMissingOptionWebhookIdThrowsIncompleteDsnException()
{
$factory = $this->createFactory();

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

$factory->create(Dsn::fromString('discord://token@host'));
}

public function testCreateWithNoTokenThrowsIncompleteDsnException()
{
$factory = $this->createFactory();

$this->expectException(IncompleteDsnException::class);
$factory->create(Dsn::fromString('discord://host.test?webhook_id=testWebhookId'));
}

public function testSupportsReturnsTrueWithSupportedScheme()
{
$factory = $this->createFactory();

$this->assertTrue($factory->supports(Dsn::fromString('discord://host?webhook_id=testWebhookId')));
return new DiscordTransportFactory();
}

public function testSupportsReturnsFalseWithUnsupportedScheme()
public function createProvider(): iterable
{
$factory = $this->createFactory();

$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://host?webhook_id=testWebhookId')));
yield [
'discord://host.test?webhook_id=testWebhookId',
'discord://token@host.test?webhook_id=testWebhookId',
];
}

public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
public function supportsProvider(): iterable
{
$factory = $this->createFactory();

$this->expectException(UnsupportedSchemeException::class);
$factory->create(Dsn::fromString('somethingElse://token@host?webhook_id=testWebhookId'));
yield [true, 'discord://host?webhook_id=testWebhookId'];
yield [false, 'somethingElse://host?webhook_id=testWebhookId'];
}

public function testUnsupportedSchemeThrowsUnsupportedSchemeExceptionEvenIfRequiredOptionIsMissing()
public function incompleteDsnProvider(): iterable
{
$factory = $this->createFactory();

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

// unsupported scheme and missing "webhook_id" option
$factory->create(Dsn::fromString('somethingElse://token@host'));
yield 'missing token' => ['discord://host.test?webhook_id=testWebhookId'];
yield 'missing option: webhook_id' => ['discord://token@host'];
}

private function createFactory(): DiscordTransportFactory
public function unsupportedSchemeProvider(): iterable
{
return new DiscordTransportFactory();
yield ['somethingElse://token@host?webhook_id=testWebhookId'];
yield ['somethingElse://token@host']; // missing "webhook_id" option
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,42 @@

namespace Symfony\Component\Notifier\Bridge\Discord\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransport;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Tests\TransportTestCase;
use Symfony\Component\Notifier\Transport\TransportInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

final class DiscordTransportTest extends TestCase
final class DiscordTransportTest extends TransportTestCase
{
public function testToStringContainsProperties()
/**
* @return DiscordTransport
*/
public function createTransport(?HttpClientInterface $client = null): TransportInterface
{
$transport = $this->createTransport();

$this->assertSame('discord://host.test?webhook_id=testWebhookId', (string) $transport);
return (new DiscordTransport('testToken', 'testWebhookId', $client ?: $this->createMock(HttpClientInterface::class)))->setHost('host.test');
}

public function testSupportsChatMessage()
public function toStringProvider(): iterable
{
$transport = $this->createTransport();

$this->assertTrue($transport->supports(new ChatMessage('testChatMessage')));
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
yield ['discord://host.test?webhook_id=testWebhookId', $this->createTransport()];
}

public function testSendNonChatMessageThrowsLogicException()
public function supportedMessagesProvider(): iterable
{
$transport = $this->createTransport();

$this->expectException(LogicException::class);
yield [new ChatMessage('Hello!')];
}

$transport->send($this->createMock(MessageInterface::class));
public function unsupportedMessagesProvider(): iterable
{
yield [new SmsMessage('0611223344', 'Hello!')];
yield [$this->createMock(MessageInterface::class)];
}

public function testSendChatMessageWithMoreThan2000CharsThrowsLogicException()
Expand Down Expand Up @@ -78,9 +80,4 @@ public function testSendWithErrorResponseThrows()

$transport->send(new ChatMessage('testMessage'));
}

private function createTransport(?HttpClientInterface $client = null): DiscordTransport
{
return (new DiscordTransport('testToken', 'testWebhookId', $client ?? $this->createMock(HttpClientInterface::class)))->setHost('host.test');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "~5.2.0",
"symfony/notifier": "~5.2.2",
"symfony/polyfill-mbstring": "^1.0"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(string $token, string $accountReference, string $fro

public function __toString(): string
{
return sprintf('esendex://%s', $this->getEndpoint());
return sprintf('esendex://%s?accountreference=%s&from=%s', $this->getEndpoint(), $this->accountReference, $this->from);
}

public function supports(MessageInterface $message): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,75 +11,43 @@

namespace Symfony\Component\Notifier\Bridge\Esendex\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;

final class EsendexTransportFactoryTest extends TestCase
final class EsendexTransportFactoryTest extends TransportFactoryTestCase
{
public function testCreateWithDsn()
/**
* @return EsendexTransportFactory
*/
public function createFactory(): TransportFactoryInterface
{
$factory = $this->createFactory();

$transport = $factory->create(Dsn::fromString('esendex://email:password@host.test?accountreference=testAccountreference&from=testFrom'));

$this->assertSame('esendex://host.test', (string) $transport);
}

public function testCreateWithMissingOptionAccountreferenceThrowsIncompleteDsnException()
{
$factory = $this->createFactory();

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

$factory->create(Dsn::fromString('esendex://email:password@host?from=FROM'));
}

public function testCreateWithMissingOptionFromThrowsIncompleteDsnException()
{
$factory = $this->createFactory();

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

$factory->create(Dsn::fromString('esendex://email:password@host?accountreference=ACCOUNTREFERENCE'));
return new EsendexTransportFactory();
}

public function testSupportsReturnsTrueWithSupportedScheme()
public function createProvider(): iterable
{
$factory = $this->createFactory();

$this->assertTrue($factory->supports(Dsn::fromString('esendex://email:password@host?accountreference=ACCOUNTREFERENCE&from=FROM')));
yield [
'esendex://host.test?accountreference=ACCOUNTREFERENCE&from=FROM',
'esendex://email:password@host.test?accountreference=ACCOUNTREFERENCE&from=FROM',
];
}

public function testSupportsReturnsFalseWithUnsupportedScheme()
public function supportsProvider(): iterable
{
$factory = $this->createFactory();

$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://email:password@host?accountreference=ACCOUNTREFERENCE&from=FROM')));
yield [true, 'esendex://email:password@host?accountreference=ACCOUNTREFERENCE&from=FROM'];
yield [false, 'somethingElse://email:password@default'];
}

public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
public function incompleteDsnProvider(): iterable
{
$factory = $this->createFactory();

$this->expectException(UnsupportedSchemeException::class);
$factory->create(Dsn::fromString('somethingElse://email:password@host?accountreference=REFERENCE&from=FROM'));
yield 'missing option: from' => ['esendex://email:password@host?accountreference=ACCOUNTREFERENCE'];
yield 'missing option: accountreference' => ['esendex://email:password@host?from=FROM'];
}

public function testUnsupportedSchemeThrowsUnsupportedSchemeExceptionEvenIfRequiredOptionIsMissing()
public function unsupportedSchemeProvider(): iterable
{
$factory = $this->createFactory();

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

// unsupported scheme and missing "from" option
$factory->create(Dsn::fromString('somethingElse://email:password@host?accountreference=REFERENCE'));
}

private function createFactory(): EsendexTransportFactory
{
return new EsendexTransportFactory();
yield ['somethingElse://email:password@default?accountreference=ACCOUNTREFERENCE&from=FROM'];
yield ['somethingElse://email:password@host?accountreference=ACCOUNTREFERENCE']; // missing "from" option
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,44 @@

namespace Symfony\Component\Notifier\Bridge\Esendex\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransport;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Tests\TransportTestCase;
use Symfony\Component\Notifier\Transport\TransportInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

final class EsendexTransportTest extends TestCase
final class EsendexTransportTest extends TransportTestCase
{
public function testToString()
/**
* @return EsendexTransport
*/
public function createTransport(?HttpClientInterface $client = null): TransportInterface
{
$transport = $this->createTransport();

$this->assertSame('esendex://host.test', (string) $transport);
return (new EsendexTransport('testToken', 'testAccountReference', 'testFrom', $client ?: $this->createMock(HttpClientInterface::class)))->setHost('host.test');
}

public function testSupportsSmsMessage()
public function toStringProvider(): iterable
{
$transport = $this->createTransport();

$this->assertTrue($transport->supports(new SmsMessage('phone', 'testSmsMessage')));
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
yield ['esendex://host.test?accountreference=testAccountReference&from=testFrom', $this->createTransport()];
}

public function testSendNonSmsMessageThrowsLogicException()
public function supportedMessagesProvider(): iterable
{
$transport = $this->createTransport();
yield [new SmsMessage('0611223344', 'Hello!')];
}

$this->expectException(LogicException::class);
$transport->send($this->createMock(MessageInterface::class));
public function unsupportedMessagesProvider(): iterable
{
yield [new ChatMessage('Hello!')];
yield [$this->createMock(MessageInterface::class)];
}

public function testSendWithErrorResponseThrows()
public function testSendWithErrorResponseThrowsTransportException()
{
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->exactly(2))
Expand All @@ -65,7 +67,7 @@ public function testSendWithErrorResponseThrows()
$transport->send(new SmsMessage('phone', 'testMessage'));
}

public function testSendWithErrorResponseContainingDetailsThrows()
public function testSendWithErrorResponseContainingDetailsThrowsTransportException()
{
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->exactly(2))
Expand All @@ -86,9 +88,4 @@ public function testSendWithErrorResponseContainingDetailsThrows()

$transport->send(new SmsMessage('phone', 'testMessage'));
}

private function createTransport(?HttpClientInterface $client = null): EsendexTransport
{
return (new EsendexTransport('testToken', 'testAccountReference', 'testFrom', $client ?: $this->createMock(HttpClientInterface::class)))->setHost('host.test');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.4|^5.0",
"symfony/notifier": "~5.2.0"
"symfony/notifier": "~5.2.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Esendex\\": "" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where:
- `ACCESS_KEY` is your Google Chat access key
- `ACCESS_TOKEN` is your Google Chat access token
- `SPACE` is the Google Chat space
- `THREAD_KEY` is the the Google Chat message thread to group messages into a single thread (optional)
- `THREAD_KEY` is the Google Chat message thread to group messages into a single thread (optional)

Resources
---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatOptions;

class GoogleChatOptionsTest extends TestCase
final class GoogleChatOptionsTest extends TestCase
{
public function testToArray()
{
Expand Down
Loading
0