6 require_once 'application/Url.php';
9 * Unitary tests for unparse_url()
11 class UnparseUrlTest
extends PHPUnit_Framework_TestCase
14 * Thanks for building nothing
16 public function testUnparseEmptyArray()
18 $this->assertEquals('', unparse_url(array()));
22 * Rebuild a full-featured URL
24 public function testUnparseFull()
26 $ref = 'http://username:password@hostname:9090/path'
27 .'?arg1=value1&arg2=value2#anchor';
28 $this->assertEquals($ref, unparse_url(parse_url($ref)));
33 * Unitary tests for URL utilities
35 class UrlTest
extends PHPUnit_Framework_TestCase
38 protected static $baseUrl = 'http://domain.tld:3000';
43 private function assertUrlIsCleaned($query='', $fragment='')
45 $url = new Url(self
::$baseUrl.$query.$fragment);
47 $this->assertEquals(self
::$baseUrl, $url->__toString());
51 * Instantiate an empty URL
53 public function testEmptyConstruct()
55 $this->assertEquals('', new Url(''));
61 public function testConstruct()
63 $ref = 'http://username:password@hostname:9090/path'
64 .'?arg1=value1&arg2=value2#anchor';
65 $this->assertEquals($ref, new Url($ref));
69 * URL cleanup - nothing to do
71 public function testNoCleanup()
73 // URL with no query nor fragment
74 $this->assertUrlIsCleaned();
76 // URL with no annoying elements
77 $ref = self
::$baseUrl.'?p1=val1&p2=1234#edit';
79 $this->assertEquals($ref, $url->cleanup());
83 * URL cleanup - annoying fragment
85 public function testCleanupFragment()
87 $this->assertUrlIsCleaned('', '#tk.rss_all');
88 $this->assertUrlIsCleaned('', '#xtor=RSS-');
89 $this->assertUrlIsCleaned('', '#xtor=RSS-U3ht0tkc4b');
93 * URL cleanup - single annoying query parameter
95 public function testCleanupSingleQueryParam()
97 $this->assertUrlIsCleaned('?action_object_map=junk');
98 $this->assertUrlIsCleaned('?action_ref_map=Cr4p!');
99 $this->assertUrlIsCleaned('?action_type_map=g4R84g3');
101 $this->assertUrlIsCleaned('?fb_stuff=v41u3');
102 $this->assertUrlIsCleaned('?fb=71m3w4573');
104 $this->assertUrlIsCleaned('?utm_campaign=zomg');
105 $this->assertUrlIsCleaned('?utm_medium=numnum');
106 $this->assertUrlIsCleaned('?utm_source=c0d3');
107 $this->assertUrlIsCleaned('?utm_term=1n4l');
109 $this->assertUrlIsCleaned('?xtor=some-url');
113 * URL cleanup - multiple annoying query parameters
115 public function testCleanupMultipleQueryParams()
117 $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng');
118 $this->assertUrlIsCleaned(
119 '?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3'
124 * URL cleanup - multiple annoying query parameters, annoying fragment
126 public function testCleanupMultipleQueryParamsAndFragment()
128 $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng', '#tk.rss_all');
132 * Nominal case - the URL contains both useful and annoying parameters
134 public function testCleanupMixedContent()
136 // ditch annoying query params and fragment, keep useful params
139 .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
141 $this->assertEquals(self
::$baseUrl.'?my=stuff&is=kept', $url->cleanup());
144 // ditch annoying query params, keep useful params and fragment
147 .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
150 self
::$baseUrl.'?my=stuff&is=kept#again',
156 * Test default http scheme.
158 public function testDefaultScheme() {
159 $url = new Url(self
::$baseUrl);
160 $this->assertEquals('http', $url->getScheme());
161 $url = new Url('domain.tld');
162 $this->assertEquals('http', $url->getScheme());
163 $url = new Url('ssh://domain.tld');
164 $this->assertEquals('ssh', $url->getScheme());
165 $url = new Url('ftp://domain.tld');
166 $this->assertEquals('ftp', $url->getScheme());
167 $url = new Url('git://domain.tld/push?pull=clone#checkout');
168 $this->assertEquals('git', $url->getScheme());