forked from phpDocumentor/guides
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelativeUrlGeneratorTest.php
More file actions
142 lines (132 loc) · 5.47 KB
/
Copy pathRelativeUrlGeneratorTest.php
File metadata and controls
142 lines (132 loc) · 5.47 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/
namespace phpDocumentor\Guides\Renderer\UrlGenerator;
use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface;
use phpDocumentor\Guides\RenderContext;
use phpDocumentor\Guides\Renderer\UrlGenerator\Exception\InvalidUrlException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
final class RelativeUrlGeneratorTest extends TestCase
{
#[DataProvider('generateRelativeInternalUrlProvider')]
public function testGenerateRelativeInternalUrl(string $expected, string $canonicalUrl, string $outputFilePath): void
{
$urlGenerator = new RelativeUrlGenerator(self::createStub(DocumentNameResolverInterface::class));
$renderContext = $this->createMock(RenderContext::class);
$renderContext->method('getOutputFilePath')->willReturn($outputFilePath);
self::assertSame($expected, $urlGenerator->generateInternalUrl($renderContext, $canonicalUrl));
}
/** @return array<string, array<string, string|null>> */
public static function generateRelativeInternalUrlProvider(): array
{
return [
'Same File' => [
'expected' => '#',
'canonicalUrl' => 'directory/file.html',
'outputFilePath' => 'directory/file.html',
],
'Same File with anchor' => [
'expected' => '#anchor',
'canonicalUrl' => 'directory/file.html#anchor',
'outputFilePath' => 'directory/file.html',
],
'File in same directory' => [
'expected' => 'file.html',
'canonicalUrl' => 'directory/file.html',
'outputFilePath' => 'directory/anotherFile.html',
],
'File in subdirectory' => [
'expected' => 'subdirectory/file.html',
'canonicalUrl' => 'directory/subdirectory/file.html',
'outputFilePath' => 'directory/anotherFile.html',
],
'File in directory above' => [
'expected' => '../file.html',
'canonicalUrl' => 'file.html',
'outputFilePath' => 'directory/anotherFile.html',
],
'File in two directory above' => [
'expected' => '../../file.html',
'canonicalUrl' => 'file.html',
'outputFilePath' => 'directory/subdirectory/anotherFile',
],
'File in other subdirectory above' => [
'expected' => '../subdirectory/file.html',
'canonicalUrl' => 'directory/subdirectory/file.html',
'outputFilePath' => 'directory/othersubdirectory/anotherFile.html',
],
];
}
#[DataProvider('fileUrlProvider')]
public function testCreateFileUrl(string $expected, string $filename, string $outputFormat = 'html', string|null $anchor = null): void
{
$urlGenerator = new RelativeUrlGenerator(self::createStub(DocumentNameResolverInterface::class));
$renderContext = $this->createMock(RenderContext::class);
$renderContext->method('getOutputFormat')->willReturn($outputFormat);
self::assertSame($expected, $urlGenerator->createFileUrl($renderContext, $filename, $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',
],
'Empty File with null anchor' => [
'expected' => '#',
'filename' => '',
'outputFormat' => 'html',
'anchor' => null,
],
'Empty File with empty string anchor' => [
'expected' => '#',
'filename' => '',
'outputFormat' => 'html',
'anchor' => '',
],
'File with empty string anchor' => [
'expected' => 'file.html',
'filename' => 'file',
'outputFormat' => 'html',
'anchor' => '',
],
];
}
public function testGenerateInternalUrlThrowsOnAbsoluteUrl(): void
{
$urlGenerator = new RelativeUrlGenerator(self::createStub(DocumentNameResolverInterface::class));
$renderContext = $this->createMock(RenderContext::class);
$this->expectException(InvalidUrlException::class);
$urlGenerator->generateInternalUrl($renderContext, 'https://example.com/page.html');
}
}