PHP Classes

File: test/test_Url.php

Recommend this page to a friend!
  Classes of Keyvan Minoukadeh   URL class   test/test_Url.php   Download  
File: test/test_Url.php
Role: Unit test script
Content type: text/plain
Description: Test script for Url class
Class: URL class
Class for handling http(s) URLs.
Author: By
Last change: Updated to include test demonstrating equal_to() method
Date: 20 years ago
Size: 3,544 bytes
 

Contents

Class file image Download
<?php
error_reporting
(E_ALL);
echo
'<pre>';

// include class
require_once('../URL.php');

// Test 1 - based on the examples in RFC 2396 Appendix C
$base = 'http://a/b/c/d;p?q';
$rel = array();
$rel[] = array (
   
'g:h',
   
'g',
   
'./g',
   
'g/',
   
'/g',
   
'//g',
   
'?y',
   
'g?y',
   
'#s',
   
'g#s',
   
'g?y#s',
   
';x',
   
'g;x',
   
'g;x?y#s',
   
'.',
   
'./',
   
'..',
   
'../',
   
'../g',
   
'../..',
   
'../../',
   
'../../g'
   
);

$rel[] = array (
   
'',
   
'../../../g',
   
'../../../../g'
   
);

$rel[] = array (
   
'/./g',
   
'/../g',
   
'g.',
   
'.g',
   
'g..',
   
'..g'
   
);

$rel[] = array (
   
'./../g',
   
'./g/.',
   
'g/./h',
   
'g/../h',
   
'g;x=1/./y',
   
'g;x=1/../y'
   
);

$rel[] = array (
   
'g?y/./x',
   
'g?y/../x',
   
'g#s/./x',
   
'g#s/../x'
   
);

$rel[] = array('http:g');

print_section('RFC 2396 - Appendix C - Examples of resolving relative URI references');
echo
'Base: '.$base."\n";
foreach (
$rel as $rel_urls) {
    echo
"\n";
    foreach (
$rel_urls as $rel_url) {
       
$url =& new URL($base);
        echo
$rel_url."\t\t = ";
       
$url->set_relative($rel_url);
        echo
$url->as_string();
        echo
"\n";
    }
}

// Test
print_section('Setting a relative URL');
$absolute =& new URL('http://www.bla.com/seg1/seg2/seg3/bah.php?query');
$relative =& new URL('../../test.php');
echo
'Absolute: '.$absolute->as_string()."\n";
echo
'Relative: '.$relative->as_string()."\n";
$absolute->set_relative($relative->as_string());
echo
'$absolute->set_relative($relative->as_string()); $absolute->as_string() == '.$absolute->as_string()."\n";


// Test
print_section('Setting a relative URL 2');
$absolute =& new URL('http://www.k1m.com');
echo
'Absolute: '.$absolute->as_string()."\n";
$absolute->set_relative('scripts/');
echo
'$absolute->set_relative(\'scripts/\'); $absolute->as_string() == '.$absolute->as_string()."\n";


// Test
print_section('Comparing URLs');
// following should all be equivalent
$url1_string = 'http://abc.com:80/~smith/home.html';
$url2_string = 'http://ABC.com/%7Esmith/home.html';
$url3_string = 'http://ABC.com:/%7esmith/home.html';

$url1 =& new URL($url1_string);
$url2 =& new URL($url2_string);
$url3 =& new URL($url3_string);
// url1 and url2
echo $url1_string;
echo (
$url1->equal_to($url2)) ? "\n is equal to\n" : "\n is NOT equal to\n";
echo
$url2_string,"\n\n";
// url1 and url3
echo $url1_string;
echo (
$url1->equal_to($url3)) ? "\n is equal to\n" : "\n is NOT equal to\n";
echo
$url3_string;


// Test
print_section('Grabbing various URL parts');
$test_url = 'http://user:pass@domain.com/path/to/here.htm?query=bla&show=no#fragment';
echo
'$test_url = \'http://user:pass@domain.com/path/to/here.htm?query=bla&show=no#fragment\';',"\n";
$url =& new URL($test_url);
echo
'$url =& new URL($test_url);',"\n";
echo
'scheme: ',$url->get_scheme(),"\n";
echo
'user: ',$url->get_user(),"\n";
echo
'pass: ',$url->get_pass(),"\n";
echo
'host: ',$url->get_host(),"\n";
echo
'path: ',$url->get_path(),"\n";
echo
'query: ',$url->get_query(),"\n";
echo
'fragment: ',$url->get_fragment(),"\n";

echo
'</pre>';

function
print_section($desc='')
{
    static
$num = 1;
    echo
"\n\n";
    echo
'+---------------------------------------------',"\n";
    echo
'| Test ',$num,' - ',$desc,"\n";
    echo
'+---------------------------------------------',"\n";
    echo
"\n";
   
$num++;
}
?>