Skip to content

Commit

Permalink
Merge pull request #605 from phpDocumentor/task/cleanup-URL-Generator
Browse files Browse the repository at this point in the history
!!![TASK] Cleanup UrlGeneratorInterface
  • Loading branch information
jaapio authored Oct 1, 2023
2 parents 3eca0a5 + 87c803b commit 667fbd3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
15 changes: 0 additions & 15 deletions packages/guides/src/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace phpDocumentor\Guides;

use InvalidArgumentException;
use League\Uri\UriInfo;

use function array_pop;
Expand All @@ -24,20 +23,6 @@

final class UrlGenerator implements UrlGeneratorInterface
{
public function generateUrl(string $path): string
{
try {
$uri = UriFactory::createUri($path);
if (UriInfo::isAbsolutePath($uri)) {
return $path;
}

return $this->relativeUrl($path);
} catch (InvalidArgumentException) {
return $path;
}
}

/**
* Returns the absolute path, including prefixing '/'.
*
Expand Down
2 changes: 0 additions & 2 deletions packages/guides/src/UrlGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

interface UrlGeneratorInterface
{
public function generateUrl(string $path): string;

/**
* Returns the absolute path, including prefixing '/'.
*
Expand Down
66 changes: 55 additions & 11 deletions packages/guides/tests/unit/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,73 @@

declare(strict_types=1);

namespace unit;
namespace phpDocumentor\Guides;

use phpDocumentor\Guides\UrlGenerator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class UrlGeneratorTest extends TestCase
{
#[DataProvider('cannicalUrlProvider')]
public function testCannicalUrl(string $basePath, string $url, string $result): void
#[DataProvider('fileUrlProvider')]
public function testCreateFileUrl(string $expected, string $filename, string $outputFormat = 'html', string|null $anchor = null, string $skip = ''): void
{
if ($skip !== '') {
self::markTestSkipped($skip);
}

$urlGenerator = new UrlGenerator();
self::assertSame($expected, $urlGenerator->createFileUrl($filename, $outputFormat, $anchor));
}

/** @return array<string, array<string, string|null>> */
public static function fileUrlProvider(): array
{
return [
'Simple Filename' => [
'expected' => 'file.html',
'filename' => 'file',
],
'Complex Filename' => [
'expected' => 'file-something.html',
'filename' => 'file-something',
],
'Output Format' => [
'expected' => 'texfile.tex',
'filename' => 'texfile',
'outputFormat' => 'tex',
],
'File with anchor' => [
'expected' => 'file.html#anchor',
'filename' => 'file',
'outputFormat' => 'html',
'anchor' => 'anchor',
],
'Empty File with anchor' => [
'expected' => '#anchor',
'filename' => '',
'outputFormat' => 'html',
'anchor' => 'anchor',
'skip' => 'Empty filenames are not supported',
],
'Empty File with empty anchor' => [
'expected' => '#',
'filename' => '',
'outputFormat' => 'html',
'anchor' => null,
'skip' => 'Empty filenames are not supported',
],
];
}

#[DataProvider('canonicalUrlProvider')]
public function testCanonicalUrl(string $basePath, string $url, string $result): void
{
$urlGenerator = new UrlGenerator();
self::assertSame($result, $urlGenerator->canonicalUrl($basePath, $url));
}

/** @return string[][] */
public static function cannicalUrlProvider(): array
public static function canonicalUrlProvider(): array
{
return [
[
Expand Down Expand Up @@ -87,10 +137,4 @@ public static function abstractUrlProvider(): array
],
];
}

public function testUrlGenerationOfInvalidUrlReturnsInput(): void
{
$urlGenerator = new UrlGenerator();
self::assertSame('tcp://hostname:port', $urlGenerator->generateUrl('tcp://hostname:port'));
}
}

0 comments on commit 667fbd3

Please sign in to comment.