From 67c36ffe4bd35b887c40e348e7d774d9e6a012ae Mon Sep 17 00:00:00 2001 From: Romain d'Alverny Date: Sat, 25 Feb 2012 14:45:20 +0000 Subject: tests for mga_geoip and downloads --- lib/t/00_downloads.t | 40 ++++++ lib/t/01_mga_geoip.t | 28 +++++ lib/testmore.php | 343 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 411 insertions(+) create mode 100644 lib/t/00_downloads.t create mode 100644 lib/t/01_mga_geoip.t create mode 100644 lib/testmore.php (limited to 'lib') diff --git a/lib/t/00_downloads.t b/lib/t/00_downloads.t new file mode 100644 index 000000000..f30f9bf51 --- /dev/null +++ b/lib/t/00_downloads.t @@ -0,0 +1,40 @@ + array('FR', 'EU'), + 'DE' => array('DE', 'EU'), + 'AU' => array('AU', 'OC') +); + +foreach ($countries as $k => $v) { + $one = $wsd->prepare_download(true, $k); + + //is($one['country'], $v[0], "countries match"); + is($one['continent'], $v[1], "continents match"); +} + +diag('Testing get_mirror(). Note, this depends on the actual mirrors list.'); + +$ccs = array( + array('FR', 'EU'), + array('DE', 'EU'), + array('CN', 'AS') +); + +foreach ($ccs as $cc) { + $mir = $wsd->get_mirror($cc[0], $cc[1]); + + //is($mir['country'], $cc[0], 'country match'); + is($mir['continent'], $cc[1], 'continents match'); +} + diff --git a/lib/t/01_mga_geoip.t b/lib/t/01_mga_geoip.t new file mode 100644 index 000000000..a33a708a7 --- /dev/null +++ b/lib/t/01_mga_geoip.t @@ -0,0 +1,28 @@ + array('FR', 'EU'), + + '2a00:1450:8006::93' + => array('IE', 'EU'), + + '173.194.67.99' + => array('US', 'NA'), + + '46.33.75.17' + => array('DE', 'EU') +); + +foreach ($ips as $ip => $cc) { + is($country = mga_geoip_country_by_ip($ip, false), $cc[0], 'country match for ' . $ip); + is(mga_geoip_continent_by_country($country), $cc[1], 'continent match for ' . $ip); +} + diff --git a/lib/testmore.php b/lib/testmore.php new file mode 100644 index 000000000..aa0616188 --- /dev/null +++ b/lib/testmore.php @@ -0,0 +1,343 @@ + 'My reason is...')); + plan(23); + */ + + global $_no_plan; + global $_skip_all; + + switch ($plan) { + case 'no_plan': + $_no_plan = TRUE; + break; + + case 'skip_all': + echo "1..0\n"; + break; + + default: + if (is_array($plan)) { + echo "1..0 # Skip {$plan['skip_all']}\n"; + exit; + } + + echo "1..$plan\n"; + break; + } +} + +function ok($pass, $test_name = '') +{ + global $_test_num; + global $_num_failures; + global $_num_skips; + + $_test_num++; + + if ($_num_skips) { + $_num_skips--; + return TRUE; + } + + if (!empty($test_name) && $test_name[0] != '#') { + $test_name = "- $test_name"; + } + + if ($pass) { + echo "ok $_test_num $test_name\n"; + } else { + echo "not ok $_test_num $test_name\n"; + + $_num_failures++; + $caller = debug_backtrace(); + + if (strstr($caller['0']['file'], $_SERVER['PHP_SELF'])) { + $file = $caller['0']['file']; + $line = $caller['0']['line']; + } else { + $file = $caller['1']['file']; + $line = $caller['1']['line']; + } + + if (isset($_SERVER['SERVER_ROOT'])){ + $file = str_replace($_SERVER['SERVER_ROOT'], 't', $file); + } + + diag(" Failed test ($file at line $line)"); + } + + return $pass; +} + +function is($this, $that, $test_name = '') +{ + $pass = ($this == $that); + + ok($pass, $test_name); + + if (!$pass) { + diag(" got: '$this'"); + diag(" expected: '$that'"); + } + + return $pass; +} + +function isnt($this, $that, $test_name = '') +{ + $pass = ($this != $that); + + ok($pass, $test_name); + + if (!$pass) { + diag(" '$this'"); + diag(' !='); + diag(" '$that'"); + } + + return $pass; +} + +function like($string, $pattern, $test_name = '') +{ + $pass = preg_match($pattern, $string); + + ok($pass, $test_name); + + if (!$pass) { + diag(" '$string'"); + diag(" doesn't match '$pattern'"); + } + + return $pass; +} + +function unlike($string, $pattern, $test_name = '') +{ + $pass = !preg_match($pattern, $string); + + ok($pass, $test_name); + + if (!$pass) { + diag(" '$string'"); + diag(" matches '$pattern'"); + } + + return $pass; +} + +function cmp_ok($this, $operator, $that, $test_name = '') +{ + eval("\$pass = (\$this $operator \$that);"); + + ob_start(); + var_dump($this); + $_this = trim(ob_get_clean()); + + ob_start(); + var_dump($that); + $_that = trim(ob_get_clean()); + + ok($pass, $test_name); + + if (!$pass) { + diag(" got: $_this"); + diag(" expected: $_that"); + } + + return $pass; +} + +function can_ok($object, $methods) +{ + $pass = TRUE; + $errors = array(); + + foreach ($methods as $method) { + if (!method_exists($object, $method)) { + $pass = FALSE; + $errors[] = " method_exists(\$object, $method) failed"; + } + } + + if ($pass) { + ok(TRUE, "method_exists(\$object, ...)"); + } else { + ok(FALSE, "method_exists(\$object, ...)"); + diag($errors); + } + + return $pass; +} + +function isa_ok($object, $expected_class, $object_name = 'The object') +{ + $got_class = get_class($object); + + if (version_compare(phpversion(), '5', '>=')) { + $pass = ($got_class == $expected_class); + } else { + $pass = ($got_class == strtolower($expected_class)); + } + + if ($pass) { + ok(TRUE, "$object_name isa $expected_class"); + } else { + ok(FALSE, "$object_name isn't a '$expected_class' it's a '$got_class'"); + } + + return $pass; +} + +function pass($test_name = '') +{ + return ok(TRUE, $test_name); +} + +function fail($test_name = '') +{ + return ok(FALSE, $test_name); +} + +function diag($message) +{ + if (is_array($message)) { + foreach($message as $current) { + echo "# $current\n"; + } + } else { + echo "# $message\n"; + } +} + +function include_ok($module) +{ + $pass = ((include $module) == 1); + return ok($pass); +} + +function require_ok($module) +{ + $pass = ((require $module) == 1); + return ok($pass); +} + +function skip($message, $num) +{ + global $_num_skips; + + if ($num < 0) { + $num = 0; + } + + for ($i = 0; $i < $num; $i++) { + pass("# SKIP $message"); + } + + $_num_skips = $num; +} + +function is_deeply($got_struct, $expected_struct, $test_name = '') +{ + $got_flat = var_export($got_struct, true); + $expected_flat = var_export($expected_struct, true); + + is($got_flat, $expected_flat, $test_name); +} + + +/* + +TODO: + +function todo() +{ +} + +function todo_skip() +{ +} + +function eq_array() +{ +} + +function eq_hash() +{ +} + +function eq_set() +{ +} + +*/ + +function _test_end() +{ + global $_no_plan; + global $_num_failures; + global $_test_num; + + if ($_no_plan) { + echo "1..$_test_num\n"; + } + + if ($_num_failures) { + diag("Looks like you failed $_num_failures tests of $_test_num."); + } +} + +?> -- cgit v1.2.1