|
4 | 4 |
|
5 | 5 | namespace Boson\Component\Uri\Factory\Tests; |
6 | 6 |
|
| 7 | +use Boson\Component\Uri\Factory\Component\UriPathFactory; |
| 8 | +use Boson\Component\Uri\Factory\Component\UriQueryFactory; |
| 9 | +use Boson\Component\Uri\Factory\Component\UriSchemeFactory; |
| 10 | +use Boson\Component\Uri\Factory\Exception\InvalidUriException; |
7 | 11 | use Boson\Component\Uri\Factory\UriFactory; |
| 12 | +use Boson\Component\Uri\Uri; |
8 | 13 | use PHPUnit\Framework\Attributes\Before; |
9 | 14 | use PHPUnit\Framework\Attributes\Group; |
| 15 | +use Boson\Component\Uri\Factory\Exception\InvalidUriSchemeComponentException; |
| 16 | +use Boson\Contracts\Uri\Component\SchemeInterface; |
| 17 | +use Boson\Contracts\Uri\Factory\Component\UriSchemeFactoryInterface; |
10 | 18 |
|
11 | 19 | #[Group('boson-php/uri-factory')] |
12 | 20 | final class UriFactoryTest extends TestCase |
@@ -54,4 +62,152 @@ public function testCreateUriFromStringWithInvalidUri(): void |
54 | 62 | self::assertSame('', $uri->query->toString()); |
55 | 63 | self::assertSame('$%^&*()', $uri->fragment); |
56 | 64 | } |
| 65 | + |
| 66 | + public function testCreateUriFromStringWithEmptyScheme(): void |
| 67 | + { |
| 68 | + $uri = $this->factory->createUriFromString('://example.com/path'); |
| 69 | + |
| 70 | + self::assertNull($uri->scheme); |
| 71 | + self::assertNull($uri->authority); |
| 72 | + self::assertSame('%3A/example.com/path', $uri->path->toString()); |
| 73 | + self::assertSame('', $uri->query->toString()); |
| 74 | + self::assertNull($uri->fragment); |
| 75 | + } |
| 76 | + |
| 77 | + public function testCreateUriFromStringWithEmptySchemeStringThrowsException(): void |
| 78 | + { |
| 79 | + $schemeFactory = new UriSchemeFactory(); |
| 80 | + |
| 81 | + $this->expectException(InvalidUriSchemeComponentException::class); |
| 82 | + $this->expectExceptionMessage('URI scheme cannot be empty'); |
| 83 | + |
| 84 | + $schemeFactory->createSchemeFromString(''); |
| 85 | + } |
| 86 | + |
| 87 | + public function testCreateUriFromStringWithStringableObjectThrowingException(): void |
| 88 | + { |
| 89 | + $stringable = new class implements \Stringable { |
| 90 | + public function __toString(): string |
| 91 | + { |
| 92 | + throw new \RuntimeException('String casting failed'); |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + $this->expectException(InvalidUriException::class); |
| 97 | + $this->expectExceptionMessage('An error occurred while converting an URI of type'); |
| 98 | + |
| 99 | + $this->factory->createUriFromString($stringable); |
| 100 | + } |
| 101 | + |
| 102 | + public function testCreateUriFromStringWithEmptyString(): void |
| 103 | + { |
| 104 | + $uri = $this->factory->createUriFromString(''); |
| 105 | + |
| 106 | + self::assertNull($uri->scheme); |
| 107 | + self::assertNull($uri->authority); |
| 108 | + self::assertSame('', $uri->path->toString()); |
| 109 | + self::assertSame('', $uri->query->toString()); |
| 110 | + self::assertNull($uri->fragment); |
| 111 | + } |
| 112 | + |
| 113 | + public function testCreateUriFromStringWithOnlyFragment(): void |
| 114 | + { |
| 115 | + $uri = $this->factory->createUriFromString('#fragment'); |
| 116 | + |
| 117 | + self::assertNull($uri->scheme); |
| 118 | + self::assertNull($uri->authority); |
| 119 | + self::assertSame('', $uri->path->toString()); |
| 120 | + self::assertSame('', $uri->query->toString()); |
| 121 | + self::assertSame('fragment', $uri->fragment); |
| 122 | + } |
| 123 | + |
| 124 | + public function testCreateUriFromStringWithOnlyQuery(): void |
| 125 | + { |
| 126 | + $uri = $this->factory->createUriFromString('?key=value'); |
| 127 | + |
| 128 | + self::assertNull($uri->scheme); |
| 129 | + self::assertNull($uri->authority); |
| 130 | + self::assertSame('', $uri->path->toString()); |
| 131 | + self::assertSame('key=value', $uri->query->toString()); |
| 132 | + self::assertNull($uri->fragment); |
| 133 | + } |
| 134 | + |
| 135 | + public function testCreateUriFromStringWithMalformedAuthority(): void |
| 136 | + { |
| 137 | + $uri = $this->factory->createUriFromString('http://user@:8080/path'); |
| 138 | + |
| 139 | + // parse_url returns false for malformed authority, so all components are reset to defaults |
| 140 | + self::assertNull($uri->scheme); |
| 141 | + self::assertNull($uri->authority); |
| 142 | + self::assertSame('', $uri->path->toString()); |
| 143 | + self::assertSame('', $uri->query->toString()); |
| 144 | + self::assertNull($uri->fragment); |
| 145 | + } |
| 146 | + |
| 147 | + public function testCreateUriFromStringWithInvalidPort(): void |
| 148 | + { |
| 149 | + $uri = $this->factory->createUriFromString('http://example.com:99999/path'); |
| 150 | + |
| 151 | + self::assertNull($uri->scheme); |
| 152 | + self::assertNull($uri->authority); |
| 153 | + self::assertSame('', $uri->path->toString()); |
| 154 | + self::assertSame('', $uri->query->toString()); |
| 155 | + self::assertNull($uri->fragment); |
| 156 | + } |
| 157 | + |
| 158 | + public function testCreateUriFromStringWithUriInterfaceReturnsClone(): void |
| 159 | + { |
| 160 | + $pathFactory = new UriPathFactory(); |
| 161 | + $queryFactory = new UriQueryFactory(); |
| 162 | + $schemeFactory = new UriSchemeFactory(); |
| 163 | + |
| 164 | + $originalUri = new Uri( |
| 165 | + path: $pathFactory->createPathFromString('/test/path'), query: $queryFactory->createQueryFromString( |
| 166 | + 'param=value', |
| 167 | + ), scheme: $schemeFactory->createSchemeFromString('https'), authority: null, fragment: 'test-fragment', |
| 168 | + ); |
| 169 | + |
| 170 | + $clonedUri = $this->factory->createUriFromString($originalUri); |
| 171 | + |
| 172 | + self::assertNotSame($originalUri, $clonedUri); |
| 173 | + self::assertSame('/test/path', $clonedUri->path->toString()); |
| 174 | + self::assertSame('param=value', $clonedUri->query->toString()); |
| 175 | + self::assertSame('https', $clonedUri->scheme?->toString()); |
| 176 | + self::assertSame('test-fragment', $clonedUri->fragment); |
| 177 | + } |
| 178 | + |
| 179 | + public function testCreateUriFromStringWithStringableObject(): void |
| 180 | + { |
| 181 | + $stringable = new class implements \Stringable { |
| 182 | + public function __toString(): string |
| 183 | + { |
| 184 | + return 'https://example.com/path?param=value#fragment'; |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + $uri = $this->factory->createUriFromString($stringable); |
| 189 | + |
| 190 | + self::assertSame('https', $uri->scheme?->toString()); |
| 191 | + self::assertSame('example.com', $uri->authority?->host); |
| 192 | + self::assertSame('/path', $uri->path->toString()); |
| 193 | + self::assertSame('param=value', $uri->query->toString()); |
| 194 | + self::assertSame('fragment', $uri->fragment); |
| 195 | + } |
| 196 | + |
| 197 | + public function testCreateUriFromStringWithComponentExceptionIsWrapped(): void |
| 198 | + { |
| 199 | + $schemeFactory = new class implements UriSchemeFactoryInterface { |
| 200 | + public function createSchemeFromString(\Stringable|string $scheme): SchemeInterface |
| 201 | + { |
| 202 | + throw new InvalidUriSchemeComponentException('Test exception'); |
| 203 | + } |
| 204 | + }; |
| 205 | + |
| 206 | + $factory = new UriFactory(schemes: $schemeFactory); |
| 207 | + |
| 208 | + $this->expectException(InvalidUriException::class); |
| 209 | + $this->expectExceptionMessage('Test exception'); |
| 210 | + |
| 211 | + $factory->createUriFromString('https://example.com'); |
| 212 | + } |
57 | 213 | } |
0 commit comments