ae7816abe8a7abe0319c27299a5ffb0535c00b40
[yaffs-website] / vendor / webmozart / path-util / tests / UrlTest.php
1 <?php
2
3 /*
4  * This file is part of the webmozart/path-util package.
5  *
6  * (c) Bernhard Schussek <bschussek@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Webmozart\PathUtil\Tests;
13
14 use Webmozart\PathUtil\Url;
15
16 /**
17  * @since  2.3
18  *
19  * @author Bernhard Schussek <bschussek@gmail.com>
20  * @author Claudio Zizza <claudio@budgegeria.de>
21  */
22 class UrlTest extends \PHPUnit_Framework_TestCase
23 {
24     /**
25      * @dataProvider provideMakeRelativeTests
26      * @covers Webmozart\PathUtil\Url
27      */
28     public function testMakeRelative($absolutePath, $basePath, $relativePath)
29     {
30         $host = 'http://example.com';
31
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);
36     }
37
38     /**
39      * @dataProvider provideMakeRelativeIsAlreadyRelativeTests
40      * @covers Webmozart\PathUtil\Url
41      */
42     public function testMakeRelativeIsAlreadyRelative($absolutePath, $basePath, $relativePath)
43     {
44         $host = 'http://example.com';
45
46         $relative = Url::makeRelative($absolutePath, $host.$basePath);
47         $this->assertSame($relativePath, $relative);
48     }
49
50     /**
51      * @dataProvider provideMakeRelativeTests
52      * @covers Webmozart\PathUtil\Url
53      */
54     public function testMakeRelativeWithFullUrl($absolutePath, $basePath, $relativePath)
55     {
56         $host = 'ftp://user:password@example.com:8080';
57
58         $relative = Url::makeRelative($host.$absolutePath, $host.$basePath);
59         $this->assertSame($relativePath, $relative);
60     }
61
62     /**
63      * @expectedException \InvalidArgumentException
64      * @expectedExceptionMessage The URL must be a string. Got: array
65      * @covers Webmozart\PathUtil\Url
66      */
67     public function testMakeRelativeFailsIfInvalidUrl()
68     {
69         Url::makeRelative(array(), 'http://example.com/webmozart/puli');
70     }
71
72     /**
73      * @expectedException \InvalidArgumentException
74      * @expectedExceptionMessage The base URL must be a string. Got: array
75      * @covers Webmozart\PathUtil\Url
76      */
77     public function testMakeRelativeFailsIfInvalidBaseUrl()
78     {
79         Url::makeRelative('http://example.com/webmozart/puli/css/style.css', array());
80     }
81
82     /**
83      * @expectedException \InvalidArgumentException
84      * @expectedExceptionMessage "webmozart/puli" is not an absolute Url.
85      * @covers Webmozart\PathUtil\Url
86      */
87     public function testMakeRelativeFailsIfBaseUrlNoUrl()
88     {
89         Url::makeRelative('http://example.com/webmozart/puli/css/style.css', 'webmozart/puli');
90     }
91
92     /**
93      * @expectedException \InvalidArgumentException
94      * @expectedExceptionMessage "" is not an absolute Url.
95      * @covers Webmozart\PathUtil\Url
96      */
97     public function testMakeRelativeFailsIfBaseUrlEmpty()
98     {
99         Url::makeRelative('http://example.com/webmozart/puli/css/style.css', '');
100     }
101
102     /**
103      * @expectedException \InvalidArgumentException
104      * @expectedExceptionMessage The base URL must be a string. Got: NULL
105      * @covers Webmozart\PathUtil\Url
106      */
107     public function testMakeRelativeFailsIfBaseUrlNull()
108     {
109         Url::makeRelative('http://example.com/webmozart/puli/css/style.css', null);
110     }
111
112     /**
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
117      */
118     public function testMakeRelativeFailsIfDifferentDomains()
119     {
120         Url::makeRelative('http://example.com/webmozart/puli/css/style.css', 'http://example2.com/webmozart/puli');
121     }
122
123     public function provideMakeRelativeTests()
124     {
125         return array(
126
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'),
132             array('/', '/', ''),
133
134             // relative to root
135             array('/css/style.css', '/', 'css/style.css'),
136
137             // same sub directories in different base directories
138             array('/puli/css/style.css', '/webmozart/css', '../../puli/css/style.css'),
139
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'),
150
151             // first argument shorter than second
152             array('/css', '/webmozart/puli', '../../css'),
153
154             // second argument shorter than first
155             array('/webmozart/puli', '/css', '../webmozart/puli'),
156
157             array('', '', ''),
158         );
159     }
160
161     public function provideMakeRelativeIsAlreadyRelativeTests()
162     {
163         return array(
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'),
177         );
178     }
179 }