|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Testing\Unit\Foundation; |
|
4
|
|
|
|
|
5
|
|
|
use Hyde\Framework\Exceptions\BaseUrlNotSetException; |
|
6
|
|
|
use Hyde\Framework\Foundation\Hyperlinks; |
|
7
|
|
|
use Hyde\Framework\HydeKernel; |
|
8
|
|
|
use Hyde\Testing\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers \Hyde\Framework\Foundation\Hyperlinks::hasSiteUrl |
|
12
|
|
|
* @covers \Hyde\Framework\Foundation\Hyperlinks::url |
|
13
|
|
|
* @covers \Hyde\Framework\Exceptions\BaseUrlNotSetException |
|
14
|
|
|
*/ |
|
15
|
|
|
class HyperlinksUrlPathHelpersTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
protected Hyperlinks $class; |
|
18
|
|
|
|
|
19
|
|
|
protected function setUp(): void |
|
20
|
|
|
{ |
|
21
|
|
|
parent::setUp(); |
|
22
|
|
|
|
|
23
|
|
|
$this->class = new Hyperlinks(HydeKernel::getInstance()); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function test_has_site_url_returns_false_when_no_site_url_is_set() |
|
27
|
|
|
{ |
|
28
|
|
|
config(['site.url' => null]); |
|
29
|
|
|
$this->assertFalse($this->class->hasSiteUrl()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function test_has_site_url_returns_true_when_site_url_is_set() |
|
33
|
|
|
{ |
|
34
|
|
|
config(['site.url' => 'https://example.com']); |
|
35
|
|
|
$this->assertTrue($this->class->hasSiteUrl()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// test that url returns the site url when no path is given |
|
39
|
|
|
public function test_qualified_url_returns_site_url_when_no_path_is_given() |
|
40
|
|
|
{ |
|
41
|
|
|
config(['site.url' => 'https://example.com']); |
|
42
|
|
|
$this->assertEquals('https://example.com', $this->class->url()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// test that url returns the site url plus the given path |
|
46
|
|
|
public function test_qualified_url_returns_site_url_plus_given_path() |
|
47
|
|
|
{ |
|
48
|
|
|
config(['site.url' => 'https://example.com']); |
|
49
|
|
|
$this->assertEquals('https://example.com/path', $this->class->url('path')); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// test that url returns the site url plus the given path with extension |
|
53
|
|
|
public function test_qualified_url_returns_site_url_plus_given_path_with_extension() |
|
54
|
|
|
{ |
|
55
|
|
|
config(['site.url' => 'https://example.com']); |
|
56
|
|
|
$this->assertEquals('https://example.com/path.html', $this->class->url('path.html')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// test that url returns the site url plus the given path with extension and query string |
|
60
|
|
|
public function test_qualified_url_returns_site_url_plus_given_path_with_extension_and_query_string() |
|
61
|
|
|
{ |
|
62
|
|
|
config(['site.url' => 'https://example.com']); |
|
63
|
|
|
$this->assertEquals('https://example.com/path.html?query=string', $this->class->url('path.html?query=string')); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// test that url trims trailing slashes |
|
67
|
|
|
public function test_qualified_url_trims_trailing_slashes() |
|
68
|
|
|
{ |
|
69
|
|
|
config(['site.url' => 'https://example.com/']); |
|
70
|
|
|
$this->assertEquals('https://example.com', $this->class->url()); |
|
71
|
|
|
$this->assertEquals('https://example.com', $this->class->url('/')); |
|
72
|
|
|
$this->assertEquals('https://example.com/foo', $this->class->url('/foo/')); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// test that url accepts multiple schemes |
|
76
|
|
|
public function test_qualified_url_accepts_multiple_schemes() |
|
77
|
|
|
{ |
|
78
|
|
|
config(['site.url' => 'http://example.com']); |
|
79
|
|
|
$this->assertEquals('http://example.com', $this->class->url()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// test that url throws an exception when no site url is set |
|
83
|
|
|
public function test_qualified_url_throws_exception_when_no_site_url_is_set() |
|
84
|
|
|
{ |
|
85
|
|
|
config(['site.url' => null]); |
|
86
|
|
|
$this->expectException(BaseUrlNotSetException::class); |
|
87
|
|
|
$this->expectExceptionMessage('No site URL has been set in config (or .env).'); |
|
88
|
|
|
$this->class->url(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// test that url uses default parameter when supplied and no site url is set |
|
92
|
|
|
public function test_qualified_url_uses_default_parameter_when_no_site_url_is_set() |
|
93
|
|
|
{ |
|
94
|
|
|
config(['site.url' => null]); |
|
95
|
|
|
$this->assertEquals('bar/foo', $this->class->url('foo', 'bar')); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// test that url does not use default parameter when supplied and a site url is set |
|
99
|
|
|
public function test_qualified_url_does_not_use_default_parameter_when_site_url_is_set() |
|
100
|
|
|
{ |
|
101
|
|
|
config(['site.url' => 'https://example.com']); |
|
102
|
|
|
$this->assertEquals('https://example.com/foo', $this->class->url('foo', 'bar')); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function test_helper_returns_expected_string_when_site_url_is_set() |
|
106
|
|
|
{ |
|
107
|
|
|
config(['site.url' => 'https://example.com']); |
|
108
|
|
|
$this->assertEquals('https://example.com/foo/bar.html', $this->class->url('foo/bar.html')); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// test returned url uses pretty urls when enabled |
|
112
|
|
|
public function test_helper_returns_expected_string_when_pretty_urls_are_enabled() |
|
113
|
|
|
{ |
|
114
|
|
|
config(['site.url' => 'https://example.com', 'site.pretty_urls' => true]); |
|
115
|
|
|
$this->assertEquals('https://example.com', $this->class->url('index.html')); |
|
116
|
|
|
$this->assertEquals('https://example.com/foo', $this->class->url('foo.html')); |
|
117
|
|
|
$this->assertEquals('https://example.com/docs', $this->class->url('docs/index.html')); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|