|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Notifier\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Notifier\Exception\LogicException; |
| 16 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 17 | +use Symfony\Component\Notifier\Transport\TransportInterface; |
| 18 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * A test case to ease testing a Notifier transport. |
| 22 | + * |
| 23 | + * @author Oskar Stark <[email protected]> |
| 24 | + */ |
| 25 | +abstract class TransportTestCase extends TestCase |
| 26 | +{ |
| 27 | + protected const CUSTOM_HOST = 'host.test'; |
| 28 | + protected const CUSTOM_PORT = 42; |
| 29 | + |
| 30 | + abstract public function createTransport(?HttpClientInterface $client = null): TransportInterface; |
| 31 | + |
| 32 | + /** |
| 33 | + * @return iterable<array{0: string, 1: TransportInterface}> |
| 34 | + */ |
| 35 | + abstract public function toStringProvider(): iterable; |
| 36 | + |
| 37 | + /** |
| 38 | + * @return iterable<array{0: MessageInterface, 1: TransportInterface}> |
| 39 | + */ |
| 40 | + abstract public function supportedMessagesProvider(): iterable; |
| 41 | + |
| 42 | + /** |
| 43 | + * @return iterable<array{0: MessageInterface, 1: TransportInterface}> |
| 44 | + */ |
| 45 | + abstract public function unsupportedMessagesProvider(): iterable; |
| 46 | + |
| 47 | + /** |
| 48 | + * @dataProvider toStringProvider |
| 49 | + */ |
| 50 | + public function testToString(string $expected, TransportInterface $transport): void |
| 51 | + { |
| 52 | + $this->assertSame($expected, (string) $transport); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @dataProvider supportedMessagesProvider |
| 57 | + */ |
| 58 | + public function testSupportedMessages(MessageInterface $message, ?TransportInterface $transport = null): void |
| 59 | + { |
| 60 | + if (null === $transport) { |
| 61 | + $transport = $this->createTransport(); |
| 62 | + } |
| 63 | + |
| 64 | + $this->assertTrue($transport->supports($message)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @dataProvider unsupportedMessagesProvider |
| 69 | + */ |
| 70 | + public function testUnsupportedMessages(MessageInterface $message, ?TransportInterface $transport = null): void |
| 71 | + { |
| 72 | + if (null === $transport) { |
| 73 | + $transport = $this->createTransport(); |
| 74 | + } |
| 75 | + |
| 76 | + $this->assertFalse($transport->supports($message)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @dataProvider unsupportedMessagesProvider |
| 81 | + */ |
| 82 | + public function testUnsupportedMessagesTrowLogicExceptionWhenSend(MessageInterface $message, ?TransportInterface $transport = null): void |
| 83 | + { |
| 84 | + if (null === $transport) { |
| 85 | + $transport = $this->createTransport(); |
| 86 | + } |
| 87 | + |
| 88 | + $this->expectException(LogicException::class); |
| 89 | + |
| 90 | + $transport->send($message); |
| 91 | + } |
| 92 | + |
| 93 | + public function testCanSetCustomHost(): void |
| 94 | + { |
| 95 | + $transport = $this->createTransport(); |
| 96 | + |
| 97 | + $transport->setHost($customHost = self::CUSTOM_HOST); |
| 98 | + |
| 99 | + $this->assertStringContainsString(sprintf('://%s', $customHost), (string) $transport); |
| 100 | + } |
| 101 | + |
| 102 | + public function testCanSetCustomPort(): void |
| 103 | + { |
| 104 | + $transport = $this->createTransport(); |
| 105 | + |
| 106 | + $transport->setPort($customPort = self::CUSTOM_PORT); |
| 107 | + |
| 108 | + /* |
| 109 | + * @see https://regex101.com/r/0xQKuY/2 |
| 110 | + */ |
| 111 | + $this->assertMatchesRegularExpression(sprintf('/^.*\/\/.*\:%s.*$/', $customPort), (string) $transport); |
| 112 | + } |
| 113 | + |
| 114 | + public function testCanSetCustomHostAndPort(): void |
| 115 | + { |
| 116 | + $transport = $this->createTransport(); |
| 117 | + |
| 118 | + $transport->setHost($customHost = self::CUSTOM_HOST); |
| 119 | + $transport->setPort($customPort = self::CUSTOM_PORT); |
| 120 | + |
| 121 | + $this->assertStringContainsString(sprintf('://%s:%s', $customHost, $customPort), (string) $transport); |
| 122 | + } |
| 123 | +} |
0 commit comments