Spaces:
No application file
No application file
File size: 6,225 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
<?php
declare(strict_types=1);
namespace Mautic\EmailBundle\Tests\Helper;
use Mautic\CoreBundle\Helper\Dsn\Dsn;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
class DsnTest extends TestCase
{
public function testGettersAndSetters(): void
{
$dsn = new Dsn('scheme', 'localhost', 'user', 'password', 3300, 'path', ['ttl' => '200']);
Assert::assertSame('scheme://user:password@localhost:3300/path?ttl=200', (string) $dsn);
$newDsn = $dsn->setScheme('mysql');
Assert::assertNotSame($newDsn, $dsn);
Assert::assertSame('scheme://user:password@localhost:3300/path?ttl=200', (string) $dsn);
Assert::assertSame('mysql://user:password@localhost:3300/path?ttl=200', (string) $newDsn);
Assert::assertSame('mysql', $newDsn->getScheme());
$newDsn = $dsn->setHost('db');
Assert::assertNotSame($newDsn, $dsn);
Assert::assertSame('scheme://user:password@localhost:3300/path?ttl=200', (string) $dsn);
Assert::assertSame('scheme://user:password@db:3300/path?ttl=200', (string) $newDsn);
Assert::assertSame('db', $newDsn->getHost());
$newDsn = $dsn->setUser('john');
Assert::assertNotSame($newDsn, $dsn);
Assert::assertSame('scheme://user:password@localhost:3300/path?ttl=200', (string) $dsn);
Assert::assertSame('scheme://john:password@localhost:3300/path?ttl=200', (string) $newDsn);
Assert::assertSame('john', $newDsn->getUser());
$newDsn = $dsn->setPassword('secret');
Assert::assertNotSame($newDsn, $dsn);
Assert::assertSame('scheme://user:password@localhost:3300/path?ttl=200', (string) $dsn);
Assert::assertSame('scheme://user:secret@localhost:3300/path?ttl=200', (string) $newDsn);
Assert::assertSame('secret', $newDsn->getPassword());
$newDsn = $dsn->setPort(3301);
Assert::assertNotSame($newDsn, $dsn);
Assert::assertSame('scheme://user:password@localhost:3300/path?ttl=200', (string) $dsn);
Assert::assertSame('scheme://user:password@localhost:3301/path?ttl=200', (string) $newDsn);
Assert::assertSame(3301, $newDsn->getPort());
$newDsn = $dsn->setPath('folder');
Assert::assertNotSame($newDsn, $dsn);
Assert::assertSame('scheme://user:password@localhost:3300/path?ttl=200', (string) $dsn);
Assert::assertSame('scheme://user:password@localhost:3300/folder?ttl=200', (string) $newDsn);
Assert::assertSame('folder', $newDsn->getPath());
$newDsn = $dsn->setOptions(['ttl' => '300', 'timeout' => '10']);
Assert::assertNotSame($newDsn, $dsn);
Assert::assertSame('scheme://user:password@localhost:3300/path?ttl=200', (string) $dsn);
Assert::assertSame('scheme://user:password@localhost:3300/path?ttl=300&timeout=10', (string) $newDsn);
Assert::assertSame(['ttl' => '300', 'timeout' => '10'], $newDsn->getOptions());
Assert::assertSame('300', $newDsn->getOption('ttl'));
Assert::assertSame('10', $newDsn->getOption('timeout'));
}
/**
* @dataProvider dataInvalidFromString
*/
public function testInvalidFromString(string $dsn, string $exceptionMessage): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($exceptionMessage);
Dsn::fromString($dsn);
}
/**
* @return iterable<string, array<string|string>>
*/
public function dataInvalidFromString(): iterable
{
yield 'DSN is invalid.' => [
':', 'The ":" DSN is invalid.',
];
yield 'DSN must contain a scheme.' => [
'://host', 'The "://host" DSN must contain a scheme.',
];
yield 'DSN must contain a host.' => [
'scheme:', 'The "scheme:" DSN must contain a host (use "default" by default).',
];
}
public function testFromStringAllowedDns(): void
{
Assert::assertSame('sync://', (string) Dsn::fromString('sync://'));
}
public function testFromString(): void
{
Assert::assertSame('scheme://user:password@localhost:3300/path?ttl=300&timeout=10', (string) Dsn::fromString('scheme://user:password@localhost:3300/path?ttl=300&timeout=10'));
}
/**
* @dataProvider dataToString
*/
public function testToString(Dsn $dsn, string $dsnString): void
{
Assert::assertSame($dsnString, (string) $dsn);
}
/**
* @return iterable<string, array<Dsn|string>>
*/
public function dataToString(): iterable
{
yield 'With host.' => [
new Dsn('smtp', 'host'), 'smtp://host',
];
yield 'With host and user.' => [
new Dsn('smtp', 'host', 'user'), 'smtp://user@host',
];
yield 'With host, user, password.' => [
new Dsn('smtp', 'host', 'user', 'password'), 'smtp://user:password@host',
];
yield 'With host, port, user, password.' => [
new Dsn('smtp', 'host', 'user', 'password', 25), 'smtp://user:password@host:25',
];
yield 'With host, port, path and query.' => [
new Dsn('smtp', 'host', 'user', 'password', 25, 'test-path', ['encryption' => 'tls', 'auth_mode'=>'login']), 'smtp://user:password@host:25/test-path?encryption=tls&auth_mode=login',
];
}
public function testToStringUrlEncodesProperly(): void
{
$dsn = new Dsn('scheme', 'local+@$#/:*!host', 'us+@$#/:*!er', 'pass+@$#/:*!word', 3300, 'pa+@$#/:*!th', ['type' => 'ty+@$#/:*!pe']);
Assert::assertSame('scheme://'.urlencode('us+@$#/:*!er').':'.urlencode('pass+@$#/:*!word').'@'.urlencode('local+@$#/:*!host').':3300/'.urlencode('pa+@$#/:*!th').'?type='.urlencode('ty+@$#/:*!pe'), (string) $dsn);
$dsnFromString = Dsn::fromString((string) $dsn);
Assert::assertSame('local+@$#/:*!host', $dsnFromString->getHost());
Assert::assertSame('us+@$#/:*!er', $dsnFromString->getUser());
Assert::assertSame('pass+@$#/:*!word', $dsnFromString->getPassword());
Assert::assertSame('pa+@$#/:*!th', $dsnFromString->getPath());
Assert::assertSame('ty+@$#/:*!pe', $dsnFromString->getOption('type'));
}
}
|