Skip to content

Commit 667fbd3

Browse files
authored
Merge pull request #605 from phpDocumentor/task/cleanup-URL-Generator
!!![TASK] Cleanup UrlGeneratorInterface
2 parents 3eca0a5 + 87c803b commit 667fbd3

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

packages/guides/src/UrlGenerator.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace phpDocumentor\Guides;
1515

16-
use InvalidArgumentException;
1716
use League\Uri\UriInfo;
1817

1918
use function array_pop;
@@ -24,20 +23,6 @@
2423

2524
final class UrlGenerator implements UrlGeneratorInterface
2625
{
27-
public function generateUrl(string $path): string
28-
{
29-
try {
30-
$uri = UriFactory::createUri($path);
31-
if (UriInfo::isAbsolutePath($uri)) {
32-
return $path;
33-
}
34-
35-
return $this->relativeUrl($path);
36-
} catch (InvalidArgumentException) {
37-
return $path;
38-
}
39-
}
40-
4126
/**
4227
* Returns the absolute path, including prefixing '/'.
4328
*

packages/guides/src/UrlGeneratorInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
interface UrlGeneratorInterface
88
{
9-
public function generateUrl(string $path): string;
10-
119
/**
1210
* Returns the absolute path, including prefixing '/'.
1311
*

packages/guides/tests/unit/UrlGeneratorTest.php

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,73 @@
22

33
declare(strict_types=1);
44

5-
namespace unit;
5+
namespace phpDocumentor\Guides;
66

7-
use phpDocumentor\Guides\UrlGenerator;
87
use PHPUnit\Framework\Attributes\DataProvider;
98
use PHPUnit\Framework\TestCase;
109

1110
final class UrlGeneratorTest extends TestCase
1211
{
13-
#[DataProvider('cannicalUrlProvider')]
14-
public function testCannicalUrl(string $basePath, string $url, string $result): void
12+
#[DataProvider('fileUrlProvider')]
13+
public function testCreateFileUrl(string $expected, string $filename, string $outputFormat = 'html', string|null $anchor = null, string $skip = ''): void
14+
{
15+
if ($skip !== '') {
16+
self::markTestSkipped($skip);
17+
}
18+
19+
$urlGenerator = new UrlGenerator();
20+
self::assertSame($expected, $urlGenerator->createFileUrl($filename, $outputFormat, $anchor));
21+
}
22+
23+
/** @return array<string, array<string, string|null>> */
24+
public static function fileUrlProvider(): array
25+
{
26+
return [
27+
'Simple Filename' => [
28+
'expected' => 'file.html',
29+
'filename' => 'file',
30+
],
31+
'Complex Filename' => [
32+
'expected' => 'file-something.html',
33+
'filename' => 'file-something',
34+
],
35+
'Output Format' => [
36+
'expected' => 'texfile.tex',
37+
'filename' => 'texfile',
38+
'outputFormat' => 'tex',
39+
],
40+
'File with anchor' => [
41+
'expected' => 'file.html#anchor',
42+
'filename' => 'file',
43+
'outputFormat' => 'html',
44+
'anchor' => 'anchor',
45+
],
46+
'Empty File with anchor' => [
47+
'expected' => '#anchor',
48+
'filename' => '',
49+
'outputFormat' => 'html',
50+
'anchor' => 'anchor',
51+
'skip' => 'Empty filenames are not supported',
52+
],
53+
'Empty File with empty anchor' => [
54+
'expected' => '#',
55+
'filename' => '',
56+
'outputFormat' => 'html',
57+
'anchor' => null,
58+
'skip' => 'Empty filenames are not supported',
59+
],
60+
];
61+
}
62+
63+
#[DataProvider('canonicalUrlProvider')]
64+
public function testCanonicalUrl(string $basePath, string $url, string $result): void
1565
{
1666
$urlGenerator = new UrlGenerator();
1767
self::assertSame($result, $urlGenerator->canonicalUrl($basePath, $url));
1868
}
1969

2070
/** @return string[][] */
21-
public static function cannicalUrlProvider(): array
71+
public static function canonicalUrlProvider(): array
2272
{
2373
return [
2474
[
@@ -87,10 +137,4 @@ public static function abstractUrlProvider(): array
87137
],
88138
];
89139
}
90-
91-
public function testUrlGenerationOfInvalidUrlReturnsInput(): void
92-
{
93-
$urlGenerator = new UrlGenerator();
94-
self::assertSame('tcp://hostname:port', $urlGenerator->generateUrl('tcp://hostname:port'));
95-
}
96140
}

0 commit comments

Comments
 (0)