Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Notifier] Use abstract test cases in 5.x #39749

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 9, 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,75 +11,47 @@

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\MissingRequiredOptionException;
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://[email protected]?webhook_id=testWebhookId'));

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

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

$this->expectException(MissingRequiredOptionException::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'));
return new DiscordTransportFactory();
}

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

$this->assertTrue($factory->supports(Dsn::fromString('discord://host?webhook_id=testWebhookId')));
yield [
'discord://host.test?webhook_id=testWebhookId',
'discord://[email protected]?webhook_id=testWebhookId',
];
}

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

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

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

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

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

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

// unsupported scheme and missing "webhook_id" option
$factory->create(Dsn::fromString('somethingElse://token@host'));
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,41 +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\LengthException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
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(UnsupportedMessageTypeException::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 @@ -79,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 @@ -11,94 +11,50 @@

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\MissingRequiredOptionException;
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:[email protected]?accountreference=testAccountreference&from=testFrom'));

$this->assertSame('esendex://host.test?accountreference=testAccountreference&from=testFrom', (string) $transport);
}

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

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

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

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

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

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

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

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

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

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

$this->expectException(MissingRequiredOptionException::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:[email protected]?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 credentials' => ['esendex://host?accountreference=ACCOUNTREFERENCE&from=FROM'];
yield 'missing email' => ['esendex://:password@host?accountreference=ACCOUNTREFERENCE&from=FROM'];
yield 'missing password' => ['esendex://email:@host?accountreference=ACCOUNTREFERENCE&from=FROM'];
}

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

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

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

private function createFactory(): EsendexTransportFactory
public function unsupportedSchemeProvider(): iterable
{
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,43 +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\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
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?accountreference=testAccountReference&from=testFrom', (string) $transport);
return (new EsendexTransport('email', 'password', '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();

$this->expectException(UnsupportedMessageTypeException::class);
yield [new SmsMessage('0611223344', 'Hello!')];
}

$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 @@ -66,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 @@ -87,9 +88,4 @@ public function testSendWithErrorResponseContainingDetailsThrows()

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

private function createTransport(?HttpClientInterface $client = null): EsendexTransport
{
return (new EsendexTransport('testEmail', 'testPassword', 'testAccountReference', 'testFrom', $client ?: $this->createMock(HttpClientInterface::class)))->setHost('host.test');
}
}
Loading