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
|
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Uri\Components;
use League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Http;
use League\Uri\Uri;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
#[CoversClass(Port::class)]
#[Group('port')]
final class PortTest extends TestCase
{
public function testPortSetter(): void
{
self::assertSame('443', Port::new(443)->toString());
}
#[DataProvider('getToIntProvider')]
public function testToInt(
Stringable|int|string|null $input,
?int $expected,
?string $string_expected,
string $uri_expected
): void {
$port = Port::new($input);
self::assertSame($expected, $port->toInt());
self::assertSame($string_expected, $port->value());
self::assertSame($uri_expected, $port->getUriComponent());
}
public static function getToIntProvider(): array
{
return [
[null, null, null, ''],
[23, 23, '23', ':23'],
['23', 23, '23', ':23'],
[new class () {
public function __toString(): string
{
return '23';
}
}, 23, '23', ':23'],
[Port::new(23), 23, '23', ':23'],
];
}
public function testFailedPortException(): void
{
$this->expectException(SyntaxError::class);
Port::new(-1);
}
#[DataProvider('getURIProvider')]
public function testCreateFromUri(UriInterface|Psr7UriInterface $uri, ?string $expected): void
{
$port = Port::fromUri($uri);
self::assertSame($expected, $port->value());
}
public static function getURIProvider(): iterable
{
return [
'PSR-7 URI object' => [
'uri' => Http::new('http://example.com:443'),
'expected' => '443',
],
'PSR-7 URI object with no fragment' => [
'uri' => Http::new('toto://example.com'),
'expected' => null,
],
'League URI object' => [
'uri' => Uri::new('http://example.com:443'),
'expected' => '443',
],
'League URI object with no fragment' => [
'uri' => Uri::new('toto://example.com'),
'expected' => null,
],
];
}
public function testCreateFromAuthority(): void
{
$uri = Uri::new('http://example.com:443');
$auth = Authority::fromUri($uri);
self::assertEquals(Port::fromUri($uri), Port::fromAuthority($auth));
}
public function testCreateFromIntSucceeds(): void
{
self::assertEquals(0, Port::new(0)->value());
}
public function testCreateFromIntFails(): void
{
$this->expectException(SyntaxError::class);
Port::new(-1);
}
}
|