|
11 | 11 |
|
12 | 12 | namespace League\Uri; |
13 | 13 |
|
| 14 | +use DOMException; |
14 | 15 | use GuzzleHttp\Psr7\Utils; |
15 | 16 | use League\Uri\Components\DataPath; |
16 | 17 | use League\Uri\Components\FragmentDirectives; |
@@ -1115,4 +1116,101 @@ public static function resolveProvider(): array |
1115 | 1116 | 'not same origin' => [self::BASE_URI, 'ftp://a/b/c/d', 'ftp://a/b/c/d'], |
1116 | 1117 | ]; |
1117 | 1118 | } |
| 1119 | + |
| 1120 | + #[Test] |
| 1121 | + #[DataProvider('providesUriToMarkdown')] |
| 1122 | + public function it_will_generate_the_markdown_code_for_the_instance(string $uri, ?string $content, string $expected): void |
| 1123 | + { |
| 1124 | + self::assertSame($expected, Modifier::wrap($uri)->toMarkdownAnchor($content)); |
| 1125 | + } |
| 1126 | + |
| 1127 | + public static function providesUriToMarkdown(): iterable |
| 1128 | + { |
| 1129 | + yield 'empty string' => [ |
| 1130 | + 'uri' => '', |
| 1131 | + 'content' => '', |
| 1132 | + 'expected' => '[]()', |
| 1133 | + ]; |
| 1134 | + |
| 1135 | + yield 'URI with a specific content' => [ |
| 1136 | + 'uri' => 'http://example.com/foo/bar', |
| 1137 | + 'content' => 'this is a link', |
| 1138 | + 'expected' => '[this is a link](http://example.com/foo/bar)', |
| 1139 | + ]; |
| 1140 | + |
| 1141 | + yield 'URI without content' => [ |
| 1142 | + 'uri' => 'http://Bébé.be', |
| 1143 | + 'content' => null, |
| 1144 | + 'expected' => '[http://bébé.be](http://xn--bb-bjab.be)', |
| 1145 | + ]; |
| 1146 | + } |
| 1147 | + |
| 1148 | + #[Test] |
| 1149 | + #[DataProvider('providesUriToAnchorTagHTML')] |
| 1150 | + public function it_will_generate_the_html_anchor_tag_code_for_the_instance(string $uri, ?string $content, array $parameters, string $expected): void |
| 1151 | + { |
| 1152 | + self::assertSame($expected, Modifier::wrap($uri)->toHtmlAnchor($content, $parameters)); |
| 1153 | + } |
| 1154 | + |
| 1155 | + public static function providesUriToAnchorTagHTML(): iterable |
| 1156 | + { |
| 1157 | + yield 'empty string' => [ |
| 1158 | + 'uri' => '', |
| 1159 | + 'content' => '', |
| 1160 | + 'parameters' => [], |
| 1161 | + 'expected' => '<a href=""></a>', |
| 1162 | + ]; |
| 1163 | + |
| 1164 | + yield 'URI with a specific content' => [ |
| 1165 | + 'uri' => 'http://example.com/foo/bar', |
| 1166 | + 'content' => 'this is a link', |
| 1167 | + 'parameters' => [], |
| 1168 | + 'expected' => '<a href="http://example.com/foo/bar">this is a link</a>', |
| 1169 | + ]; |
| 1170 | + |
| 1171 | + yield 'URI without content' => [ |
| 1172 | + 'uri' => 'http://Bébé.be', |
| 1173 | + 'content' => null, |
| 1174 | + 'parameters' => [], |
| 1175 | + 'expected' => '<a href="http://xn--bb-bjab.be">http://bébé.be</a>', |
| 1176 | + ]; |
| 1177 | + |
| 1178 | + yield 'URI without content and with class' => [ |
| 1179 | + 'uri' => 'http://Bébé.be', |
| 1180 | + 'content' => null, |
| 1181 | + 'parameters' => [ |
| 1182 | + 'class' => ['foo', 'bar'], |
| 1183 | + 'target' => null, |
| 1184 | + ], |
| 1185 | + 'expected' => '<a href="http://xn--bb-bjab.be" class="foo bar">http://bébé.be</a>', |
| 1186 | + ]; |
| 1187 | + |
| 1188 | + yield 'URI without content and with target' => [ |
| 1189 | + 'uri' => 'http://Bébé.be', |
| 1190 | + 'content' => null, |
| 1191 | + 'parameters' => [ |
| 1192 | + 'class' => null, |
| 1193 | + 'target' => '_blank', |
| 1194 | + ], |
| 1195 | + 'expected' => '<a href="http://xn--bb-bjab.be" target="_blank">http://bébé.be</a>', |
| 1196 | + ]; |
| 1197 | + |
| 1198 | + yield 'URI without content, with target and class' => [ |
| 1199 | + 'uri' => 'http://Bébé.be', |
| 1200 | + 'content' => null, |
| 1201 | + 'parameters' => [ |
| 1202 | + 'class' => 'foo bar', |
| 1203 | + 'target' => '_blank', |
| 1204 | + ], |
| 1205 | + 'expected' => '<a href="http://xn--bb-bjab.be" class="foo bar" target="_blank">http://bébé.be</a>', |
| 1206 | + ]; |
| 1207 | + } |
| 1208 | + |
| 1209 | + #[Test] |
| 1210 | + public function it_will_fail_to_generate_an_anchor_tag_html_for_the_instance(): void |
| 1211 | + { |
| 1212 | + $this->expectException(DOMException::class); |
| 1213 | + |
| 1214 | + Modifier::wrap('https://example.com')->toHtmlAnchor(attributes: ["bébé\r\n" => 'yes']); |
| 1215 | + } |
1118 | 1216 | } |
0 commit comments