3 class UriTest
extends MediaWikiTestCase
{
6 AutoLoader
::loadClass( 'Uri' );
12 'http://example.com/',
18 'host' => 'example.com',
26 '//mediawiki.org/wiki/Main_Page',
32 'host' => 'mediawiki.org',
34 'path' => '/wiki/Main_Page',
40 'http://user:pass@example.com/',
46 'host' => 'example.com',
63 'query' => 'asdf=asdf',
77 'query' => 'asdf=asdf',
85 * Ensure that get* methods properly match the appropriate getComponent( key ) value
86 * @dataProvider dataUris
88 function testGetters( $uri ) {
89 $uri = new Uri( $uri );
91 'getProtocol' => 'scheme',
93 'getPassword' => 'pass',
97 'getQueryString' => 'query',
98 'getFragment' => 'fragment',
100 foreach ( $getterMap as $fn => $c ) {
101 $this->assertSame( $uri->{$fn}(), $uri->getComponent( $c ), "\$uri->{$fn}(); matches \$uri->getComponent( '$c' );" );
106 * Ensure that Uri has the proper components for our example uris
107 * @dataProvider dataUris
109 function testComponents( $uri, $components ) {
110 $uri = new Uri( $uri );
112 $this->assertSame( $components['scheme'], $uri->getProtocol(), 'Correct scheme' );
113 $this->assertSame( $components['delimiter'], $uri->getDelimiter(), 'Correct delimiter' );
114 $this->assertSame( $components['user'], $uri->getUser(), 'Correct user' );
115 $this->assertSame( $components['pass'], $uri->getPassword(), 'Correct pass' );
116 $this->assertSame( $components['host'], $uri->getHost(), 'Correct host' );
117 $this->assertSame( $components['port'], $uri->getPort(), 'Correct port' );
118 $this->assertSame( $components['path'], $uri->getPath(), 'Correct path' );
119 $this->assertSame( $components['query'], $uri->getQueryString(), 'Correct query' );
120 $this->assertSame( $components['fragment'], $uri->getFragment(), 'Correct fragment' );
124 * Ensure that the aliases work for various components.
126 function testAliases() {
127 $url = "//myuser@test.com";
128 $uri = new Uri( $url );
131 $uri->setComponent( 'protocol', 'https' );
132 $uri->setComponent( 'password', 'mypass' );
134 // Now try getting them.
135 $this->assertSame( 'https', $uri->getComponent( 'protocol' ), 'Correct protocol (alias for scheme)' );
136 $this->assertSame( 'mypass', $uri->getComponent( 'password' ), 'Correct password (alias for pass)' );
138 // Finally check their actual names.
139 $this->assertSame( 'https', $uri->getProtocol(), 'Alias for scheme works' );
140 $this->assertSame( 'mypass', $uri->getPassword(), 'Alias for pass works' );
144 * Ensure that Uri's helper methods return the correct data
146 function testHelpers() {
147 $uri = new Uri( 'http://a:b@example.com:8080/path?query=value' );
149 $this->assertSame( 'a:b', $uri->getUserInfo(), 'Correct getUserInfo' );
150 $this->assertSame( 'example.com:8080', $uri->getHostPort(), 'Correct getHostPort' );
151 $this->assertSame( 'a:b@example.com:8080', $uri->getAuthority(), 'Correct getAuthority' );
152 $this->assertSame( '/path?query=value', $uri->getRelativePath(), 'Correct getRelativePath' );
153 $this->assertSame( 'http://a:b@example.com:8080/path?query=value', $uri->toString(), 'Correct toString' );
157 * Ensure that Uri's extend method properly overrides keys
159 function testExtend() {
160 $uri = new Uri( 'http://example.org/?a=b&hello=world' );
161 $uri->extendQuery( 'a=c&foo=bar' );
162 $this->assertSame( 'a=c&hello=world&foo=bar', $uri->getQueryString() );