4 * This file is part of the webmozart/path-util package.
6 * (c) Bernhard Schussek <bschussek@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Webmozart\PathUtil\Tests;
14 use Webmozart\PathUtil\Url;
19 * @author Bernhard Schussek <bschussek@gmail.com>
20 * @author Claudio Zizza <claudio@budgegeria.de>
22 class UrlTest extends \PHPUnit_Framework_TestCase
25 * @dataProvider provideMakeRelativeTests
26 * @covers Webmozart\PathUtil\Url
28 public function testMakeRelative($absolutePath, $basePath, $relativePath)
30 $host = 'http://example.com';
32 $relative = Url::makeRelative($host.$absolutePath, $host.$basePath);
33 $this->assertSame($relativePath, $relative);
34 $relative = Url::makeRelative($absolutePath, $host.$basePath);
35 $this->assertSame($relativePath, $relative);
39 * @dataProvider provideMakeRelativeIsAlreadyRelativeTests
40 * @covers Webmozart\PathUtil\Url
42 public function testMakeRelativeIsAlreadyRelative($absolutePath, $basePath, $relativePath)
44 $host = 'http://example.com';
46 $relative = Url::makeRelative($absolutePath, $host.$basePath);
47 $this->assertSame($relativePath, $relative);
51 * @dataProvider provideMakeRelativeTests
52 * @covers Webmozart\PathUtil\Url
54 public function testMakeRelativeWithFullUrl($absolutePath, $basePath, $relativePath)
56 $host = 'ftp://user:password@example.com:8080';
58 $relative = Url::makeRelative($host.$absolutePath, $host.$basePath);
59 $this->assertSame($relativePath, $relative);
63 * @expectedException \InvalidArgumentException
64 * @expectedExceptionMessage The URL must be a string. Got: array
65 * @covers Webmozart\PathUtil\Url
67 public function testMakeRelativeFailsIfInvalidUrl()
69 Url::makeRelative(array(), 'http://example.com/webmozart/puli');
73 * @expectedException \InvalidArgumentException
74 * @expectedExceptionMessage The base URL must be a string. Got: array
75 * @covers Webmozart\PathUtil\Url
77 public function testMakeRelativeFailsIfInvalidBaseUrl()
79 Url::makeRelative('http://example.com/webmozart/puli/css/style.css', array());
83 * @expectedException \InvalidArgumentException
84 * @expectedExceptionMessage "webmozart/puli" is not an absolute Url.
85 * @covers Webmozart\PathUtil\Url
87 public function testMakeRelativeFailsIfBaseUrlNoUrl()
89 Url::makeRelative('http://example.com/webmozart/puli/css/style.css', 'webmozart/puli');
93 * @expectedException \InvalidArgumentException
94 * @expectedExceptionMessage "" is not an absolute Url.
95 * @covers Webmozart\PathUtil\Url
97 public function testMakeRelativeFailsIfBaseUrlEmpty()
99 Url::makeRelative('http://example.com/webmozart/puli/css/style.css', '');
103 * @expectedException \InvalidArgumentException
104 * @expectedExceptionMessage The base URL must be a string. Got: NULL
105 * @covers Webmozart\PathUtil\Url
107 public function testMakeRelativeFailsIfBaseUrlNull()
109 Url::makeRelative('http://example.com/webmozart/puli/css/style.css', null);
113 * @expectedException \InvalidArgumentException
114 * @expectedExceptionMessage The URL "http://example.com" cannot be made relative to "http://example2.com" since
115 * their host names are different.
116 * @covers Webmozart\PathUtil\Url
118 public function testMakeRelativeFailsIfDifferentDomains()
120 Url::makeRelative('http://example.com/webmozart/puli/css/style.css', 'http://example2.com/webmozart/puli');
123 public function provideMakeRelativeTests()
127 array('/webmozart/puli/css/style.css', '/webmozart/puli', 'css/style.css'),
128 array('/webmozart/puli/css/style.css?key=value&key2=value', '/webmozart/puli', 'css/style.css?key=value&key2=value'),
129 array('/webmozart/puli/css/style.css?key[]=value&key[]=value', '/webmozart/puli', 'css/style.css?key[]=value&key[]=value'),
130 array('/webmozart/css/style.css', '/webmozart/puli', '../css/style.css'),
131 array('/css/style.css', '/webmozart/puli', '../../css/style.css'),
135 array('/css/style.css', '/', 'css/style.css'),
137 // same sub directories in different base directories
138 array('/puli/css/style.css', '/webmozart/css', '../../puli/css/style.css'),
140 array('/webmozart/puli/./css/style.css', '/webmozart/puli', 'css/style.css'),
141 array('/webmozart/puli/../css/style.css', '/webmozart/puli', '../css/style.css'),
142 array('/webmozart/puli/.././css/style.css', '/webmozart/puli', '../css/style.css'),
143 array('/webmozart/puli/./../css/style.css', '/webmozart/puli', '../css/style.css'),
144 array('/webmozart/puli/../../css/style.css', '/webmozart/puli', '../../css/style.css'),
145 array('/webmozart/puli/css/style.css', '/webmozart/./puli', 'css/style.css'),
146 array('/webmozart/puli/css/style.css', '/webmozart/../puli', '../webmozart/puli/css/style.css'),
147 array('/webmozart/puli/css/style.css', '/webmozart/./../puli', '../webmozart/puli/css/style.css'),
148 array('/webmozart/puli/css/style.css', '/webmozart/.././puli', '../webmozart/puli/css/style.css'),
149 array('/webmozart/puli/css/style.css', '/webmozart/../../puli', '../webmozart/puli/css/style.css'),
151 // first argument shorter than second
152 array('/css', '/webmozart/puli', '../../css'),
154 // second argument shorter than first
155 array('/webmozart/puli', '/css', '../webmozart/puli'),
161 public function provideMakeRelativeIsAlreadyRelativeTests()
164 array('css/style.css', '/webmozart/puli', 'css/style.css'),
165 array('css/style.css', '', 'css/style.css'),
166 array('css/../style.css', '', 'style.css'),
167 array('css/./style.css', '', 'css/style.css'),
168 array('../style.css', '/', 'style.css'),
169 array('./style.css', '/', 'style.css'),
170 array('../../style.css', '/', 'style.css'),
171 array('../../style.css', '', 'style.css'),
172 array('./style.css', '', 'style.css'),
173 array('../style.css', '', 'style.css'),
174 array('./../style.css', '', 'style.css'),
175 array('css/./../style.css', '', 'style.css'),
176 array('css//style.css', '', 'css/style.css'),