PHP Classes

File: test_phalidate.php

Recommend this page to a friend!
  Classes of Chris Hubbard   phalidate   test_phalidate.php   Download  
File: test_phalidate.php
Role: Unit test script
Content type: text/plain
Description: File used to perform simple unit testing of phalidate
Class: phalidate
Data validation class
Author: By
Last change: Added tests for is_value and is_not_value
Updated tests to test for not required fields
Date: 20 years ago
Size: 18,639 bytes
 

Contents

Class file image Download
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>phalidate test cases</title> <style> .pass { background-color : #CCFF99; } .fail { background-color : #FFCCCC; } </style> </head> <body> <h1>test of phalidate class</h1> <table> <tr> <th>id</th> <th>result</th> <th>data</th> </tr> <?php error_reporting (E_ALL); require "../phalidate.php"; require "../phalidators.php"; require "Timer.php"; $p = new phalidators(); $t = new Timer(); $test = array ( "is_currency" => TRUE, // "is_currency" => FALSE, "is_strong_password" => TRUE, // "is_strong_password" => FALSE, "is_time" => TRUE, // "is_time" => FALSE, "is_url" => TRUE, // "is_url" => FALSE, "is_email" => TRUE, // "is_email" => FALSE, "is_alpha" => TRUE, // "is_alpha" => FALSE, "is_alphanumeric" => TRUE, // "is_alphanumeric" => FALSE, "is_filled_in" => TRUE, // "is_filled_in" => FALSE, "is_numeric" => TRUE, // "is_numeric" => FALSE, "is_loosestring" => TRUE, // "is_loosestring" => FALSE, "is_date" => TRUE, // "is_date" => FALSE, "is_value" => TRUE, // "is_value" => FALSE, "is_not_value" => TRUE // "is_not_value" => FALSE ); if ($test["is_currency"]) { echo "<tr><td colspan=\"3\"><h3>is_currency</h3></td>"; $p->set_required(TRUE); /*** * test of is_currency */ $data = array ( 0 => array ("test"=>"1.50", "expect"=>"true"), 1 => array ("test"=>"21.00", "expect"=>"true"), 2 => array ("test"=>"", "expect"=>"false"), 3 => array ("test"=>".4", "expect"=>"false"), 4 => array ("test"=>NULL, "expect"=>"false"), 5 => array ("test"=>".05", "expect"=>"true"), 6 => array ("test"=>"1000.00", "expect"=>"true"), 7 => array ("test"=>"1,000.00", "expect"=>"false"), 8 => array ("test"=>"2000", "expect"=>"false"), 9 => array ("test"=>"", "expect"=>"true"), 10 => array ("test"=>NULL, "expect"=>"true") ); $t->start(); for ($x=0; $x<count($data); $x++) { if ($x > 8) { $p->set_required(FALSE); } $result = $p->is_currency("",$data[$x]["test"]); $msg = get_message("is_currency",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_strong_password"]) { echo "<tr><td colspan=\"3\"><h3>is_strong_password</h3></td>"; $p->set_required(TRUE); /*** * test of is_strong_password */ $data = array ( 0 => array ("test"=>"password", "expect"=>"false"), 1 => array ("test"=>"PASSWORD", "expect"=>"false"), 2 => array ("test"=>"", "expect"=>"false"), 3 => array ("test"=>"pa55word", "expect"=>"false"), 4 => array ("test"=>NULL, "expect"=>"false"), 5 => array ("test"=>"pa55Word", "expect"=>"false"), 6 => array ("test"=>"pa55W()rd", "expect"=>"true"), 7 => array ("test"=>"pa55WOORrd", "expect"=>"true"), 8 => array ("test"=>"pa55w'Rrd", "expect"=>"false"), 9 => array ("test"=>"pa55w\"Rrd", "expect"=>"false"), 10 => array ("test"=>"pa55w`Rrd", "expect"=>"false"), 11 => array ("test"=>"11111111111", "expect"=>"false") ); $t->start(); for ($x=0; $x<count($data); $x++) { $result = $p->is_strong_password("",$data[$x]["test"]); $msg = get_message("is_strong_password",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_time"]) { echo "<tr><td colspan=\"3\"><h3>is_time</h3></td>"; $p->set_required(TRUE); /*** * test of is_time */ $data = array ( 0 => array ("test"=>"12:00:00", "expect"=>"true", "mask"=>"1"), 1 => array ("test"=>"00:00:00", "expect"=>"true", "mask"=>"1"), 2 => array ("test"=>"21:10:00", "expect"=>"true", "mask"=>"1"), 3 => array ("test"=>"", "expect"=>"false", "mask"=>"1"), 4 => array ("test"=>NULL, "expect"=>"false", "mask"=>"1"), 5 => array ("test"=>"a:33:12", "expect"=>"false", "mask"=>"1"), 6 => array ("test"=>"32:12:00", "expect"=>"false", "mask"=>"1"), 7 => array ("test"=>"7:12", "expect"=>"true", "mask"=>"2"), 8 => array ("test"=>"00:00", "expect"=>"true", "mask"=>"2"), 9 => array ("test"=>"17:12", "expect"=>"true", "mask"=>"2"), 10 => array ("test"=>"", "expect"=>"false", "mask"=>"2"), 11 => array ("test"=>NULL, "expect"=>"false", "mask"=>"2"), 12 => array ("test"=>"7:12:00 am", "expect"=>"true", "mask"=>"3"), 13 => array ("test"=>"00:00:00 am", "expect"=>"true", "mask"=>"3"), 14 => array ("test"=>"17:12 pm", "expect"=>"true", "mask"=>"3"), 15 => array ("test"=>"", "expect"=>"false", "mask"=>"3"), 16 => array ("test"=>NULL, "expect"=>"false", "mask"=>"3"), 17 => array ("test"=>"7:12 am", "expect"=>"true", "mask"=>"4"), 18 => array ("test"=>"00:00 am", "expect"=>"true", "mask"=>"4"), 19 => array ("test"=>"17:12 pm", "expect"=>"true", "mask"=>"4"), 20 => array ("test"=>"", "expect"=>"false", "mask"=>"4"), 21 => array ("test"=>NULL, "expect"=>"false", "mask"=>"4"), 22 => array ("test"=>"a:33:12", "expect"=>"false", "mask"=>"0"), 23 => array ("test"=>"32:12:00", "expect"=>"false", "mask"=>"0"), 24 => array ("test"=>"7:12", "expect"=>"true", "mask"=>"0"), 25 => array ("test"=>"00:00", "expect"=>"true", "mask"=>"0"), 26 => array ("test"=>"", "expect"=>"true", "mask"=>"0"), 27 => array ("test"=>NULL, "expect"=>"true", "mask"=>"0"), 28 => array ("test"=>"", "expect"=>"true", "mask"=>"1"), 29 => array ("test"=>NULL, "expect"=>"true", "mask"=>"1") ); $t->start(); for ($x=0; $x<count($data); $x++) { if ($x > 25) { $p->set_required(FALSE); } $result = $p->is_time("",$data[$x]["test"],$data[$x]["mask"]); $msg = get_message("is_time",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_url"]) { echo "<tr><td colspan=\"3\"><h3>is_url</h3></td>"; $p->set_required(TRUE); /*** * test of is_url */ $data = array ( 0 => array ("test"=>"http://www.example.com", "expect"=>"true"), 1 => array ("test"=>"http://www.example.com/path/to/file.html", "expect"=>"true"), 2 => array ("test"=>"", "expect"=>"false"), 3 => array ("test"=>"www.example.com", "expect"=>"false"), 4 => array ("test"=>NULL, "expect"=>"false"), 5 => array ("test"=>"www.example", "expect"=>"false"), 6 => array ("test"=>"hrrp://www.example.com", "expect"=>"false"), 7 => array ("test"=>"http://example.com", "expect"=>"true"), 8 => array ("test"=>"http://46.48.12.15", "expect"=>"true") ); $t->start(); for ($x=0; $x<count($data); $x++) { if ($x > 7) { $p->set_required(FALSE); } $result = $p->is_url("",$data[$x]["test"]); $msg = get_message("is_url",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_email"]) { echo "<tr><td colspan=\"3\"><h3>is_email</h3></td>"; $p->set_required(TRUE); /*** * test of is_email */ $data = array ( 0 => array ("test"=>"joe@example.com", "expect"=>"true"), 1 => array ("test"=>"joe_user@example.com", "expect"=>"true"), 2 => array ("test"=>"", "expect"=>"false"), 3 => array ("test"=>"joe@.com", "expect"=>"false"), 4 => array ("test"=>NULL, "expect"=>"false"), 5 => array ("test"=>"123", "expect"=>"false"), 6 => array ("test"=>"joe user@example.com", "expect"=>"false"), 7 => array ("test"=>"", "expect"=>"true"), 8 => array ("test"=>NULL, "expect"=>"true") ); $t->start(); for ($x=0; $x<count($data); $x++) { if ($x > 6) { $p->set_required(FALSE); } $result = $p->is_email("",$data[$x]["test"]); $msg = get_message("is_email",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_alpha"]) { echo "<tr><td colspan=\"3\"><h3>is_alpha</h3></td>"; $p->set_required(TRUE); /*** * test of is_alpha */ $data = array ( 0 => array ("test"=>"abcdefg", "expect"=>"true"), 1 => array ("test"=>"abc123", "expect"=>"false"), 2 => array ("test"=>"", "expect"=>"false"), 3 => array ("test"=>"<strong>asbd</strong>", "expect"=>"false"), 4 => array ("test"=>"this is some test text", "expect"=>"false"), 5 => array ("test"=>"", "expect"=>"true"), 6 => array ("test"=>NULL, "expect"=>"true") ); $t->start(); for ($x=0; $x<count($data); $x++) { if ($x > 4) { $p->set_required(FALSE); } $result = $p->is_alpha("",$data[$x]["test"]); $msg = get_message("is_alpha",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $p->set_required(TRUE); $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_alphanumeric"]) { echo "<tr><td colspan=\"3\"><h3>is_alphanumeric</h3></td>"; $p->set_required(TRUE); /*** * test of is_alphanumeric */ $data = array ( 0 => array ("test"=>"abcdefg", "expect"=>"true"), 1 => array ("test"=>"abc123", "expect"=>"true"), 2 => array ("test"=>"", "expect"=>"false"), 3 => array ("test"=>"<strong>asbd</strong>", "expect"=>"false"), 4 => array ("test"=>"this is some test text", "expect"=>"false"), 5 => array ("test"=>"", "expect"=>"true"), 6 => array ("test"=>NULL, "expect"=>"true"), 7 => array ("test"=>0, "expect"=>"true") ); $t->start(); for ($x=0; $x<count($data); $x++) { if ($x > 4) { $p->set_required(FALSE); } $result = $p->is_alphanumeric("",$data[$x]["test"]); $msg = get_message("is_alphanumeric",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_filled_in"]) { echo "<tr><td colspan=\"3\"><h3>is_filled_in</h3></td>"; $p->set_required(TRUE); /*** * test of is_filled_in */ $data = array ( 0 => array ("test"=>"abcdefg", "expect"=>"true"), 1 => array ("test"=>"abc123", "expect"=>"true"), 2 => array ("test"=>"", "expect"=>"false"), 3 => array ("test"=>"<strong>asbd</strong>", "expect"=>"true"), 4 => array ("test"=>NULL, "expect"=>"false") ); $t->start(); for ($x=0; $x<count($data); $x++) { $result = $p->is_filled_in("",$data[$x]["test"]); $msg = get_message("is_filled_in",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_numeric"]) { echo "<tr><td colspan=\"3\"><h3>is_numeric</h3></td>"; $p->set_required(TRUE); /*** * test of is_numeric */ $data = array ( 0 => array ("test"=>"abcdefg", "expect"=>"false"), 1 => array ("test"=>"abc123", "expect"=>"false"), 2 => array ("test"=>"", "expect"=>"false"), 3 => array ("test"=>"<strong>asbd</strong>", "expect"=>"false"), 4 => array ("test"=>NULL, "expect"=>"false"), 5 => array ("test"=>"123", "expect"=>"true"), 6 => array ("test"=>"0", "expect"=>"true"), 7 => array ("test"=>0, "expect"=>"true"), 8 => array ("test"=>"", "expect"=>"true"), 9 => array ("test"=>NULL, "expect"=>"true") ); $t->start(); for ($x=0; $x<count($data); $x++) { if ($x > 7) { $p->set_required(FALSE); } $result = $p->is_numeric("",$data[$x]["test"]); $msg = get_message("is_numeric",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_loosestring"]) { echo "<tr><td colspan=\"3\"><h3>is_loosestring</h3></td>"; $p->set_required(TRUE); /*** * test of is_loosestring */ $data = array ( 0 => array ("test"=>"abcdefg", "expect"=>"true"), 1 => array ("test"=>"abc123", "expect"=>"true"), 2 => array ("test"=>"", "expect"=>"false"), 3 => array ("test"=>"<strong>asbd</strong>", "expect"=>"false"), 4 => array ("test"=>NULL, "expect"=>"false"), 5 => array ("test"=>"123", "expect"=>"true"), 6 => array ("test"=>"This is some test 123", "expect"=>"true"), 7 => array ("test"=>"<i>asbd</i>", "expect"=>"true"), 8 => array ("test"=>"<b>asbd</b>", "expect"=>"true"), 9 => array ("test"=>"", "expect"=>"true"), 10 => array ("test"=>NULL, "expect"=>"true") ); $t->start(); for ($x=0; $x<count($data); $x++) { if ($x > 8) { $p->set_required(FALSE); } $result = $p->is_loosestring("",$data[$x]["test"]); $msg = get_message("is_stringloose",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_date"]) { echo "<tr><td colspan=\"3\"><h3>is_date</h3></td>"; $p->set_required(TRUE); /*** * test of is_date */ $data = array ( 0 => array ("test"=>"03/15/2004", "mask"=>"1", "expect"=>"true"), 1 => array ("test"=>"03/45/2004", "mask"=>"1", "expect"=>"false"), 2 => array ("test"=>"14/15/2004", "mask"=>"1", "expect"=>"false"), 3 => array ("test"=>"0a/15/2004", "mask"=>"1", "expect"=>"false"), 4 => array ("test"=>"03/15/04", "mask"=>"1", "expect"=>"false"), 5 => array ("test"=>"02/29/2005", "mask"=>"1", "expect"=>"false"), 6 => array ("test"=>"2003/03/15", "mask"=>"2", "expect"=>"true"), 7 => array ("test"=>"2003/03/45", "mask"=>"2", "expect"=>"false"), 8 => array ("test"=>"2003/13/15", "mask"=>"2", "expect"=>"false"), 9 => array ("test"=>"2003/a3/15", "mask"=>"2", "expect"=>"false"), 10 => array ("test"=>"03/03/15", "mask"=>"2", "expect"=>"false"), 11 => array ("test"=>"2003/02/29", "mask"=>"2", "expect"=>"false"), 12 => array ("test"=>"03/15/04", "mask"=>"3", "expect"=>"true"), 13 => array ("test"=>"03/45/04", "mask"=>"3", "expect"=>"false"), 14 => array ("test"=>"14/45/04", "mask"=>"3", "expect"=>"false"), 15 => array ("test"=>"0a/45/04", "mask"=>"3", "expect"=>"false"), 16 => array ("test"=>"02/29/05", "mask"=>"3", "expect"=>"false"), 17 => array ("test"=>"03/15/2004", "mask"=>"3", "expect"=>"false"), 18 => array ("test"=>"", "mask"=>"1", "expect"=>"false"), 19 => array ("test"=>NULL, "mask"=>"1", "expect"=>"false"), 20 => array ("test"=>"03/15/04", "mask"=>"4", "expect"=>"true"), 21 => array ("test"=>"3/15/04", "mask"=>"4", "expect"=>"true"), 22 => array ("test"=>"3/15/2004", "mask"=>"4", "expect"=>"true"), 23 => array ("test"=>"03-15-2004", "mask"=>"1", "expect"=>"true"), 24 => array ("test"=>"03/15/04", "mask"=>"3", "expect"=>"true"), 25 => array ("test"=>"03/15/04", "mask"=>"4", "expect"=>"true"), 26 => array ("test"=>"3/15/04", "mask"=>"4", "expect"=>"true"), 27 => array ("test"=>"3/15/2004", "mask"=>"4", "expect"=>"true"), 28 => array ("test"=>"3/15/004", "mask"=>"4", "expect"=>"false"), 28 => array ("test"=>"3/15/4", "mask"=>"4", "expect"=>"false") ); $t->start(); for ($x=0; $x<count($data); $x++) { if ($x > 7) { $p->set_required(FALSE); } $result = $p->is_date("",$data[$x]["test"],$data[$x]["mask"]); $msg = get_message("is_date",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["test"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); } if ($test["is_value"]) { echo "<tr><td colspan=\"3\"><h3>is_value</h3></td>"; $p->set_required(TRUE); /*** * test of is_value */ $data = array ( 0 => array ("data"=>"1", "value"=>"1", "expect"=>"true"), 1 => array ("data"=>"a", "value"=>"a", "expect"=>"true"), 2 => array ("data"=>"1", "value"=>"a", "expect"=>"false"), 3 => array ("data"=>"a", "value"=>"", "expect"=>"false"), ); $t->start(); for ($x=0; $x<count($data); $x++) { $result = $p->is_value("",$data[$x]["data"],$data[$x]["value"]); $msg = get_message("is_value",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["data"]." ".$data[$x]["value"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } if ($test["is_not_value"]) { echo "<tr><td colspan=\"3\"><h3>is_not_value</h3></td>"; $p->set_required(TRUE); /*** * test of is_not_value */ $data = array ( 0 => array ("data"=>"1", "value"=>"a", "expect"=>"true"), 1 => array ("data"=>"a", "value"=>"1", "expect"=>"true"), 2 => array ("data"=>"1", "value"=>"1", "expect"=>"false"), 3 => array ("data"=>"a", "value"=>"", "expect"=>"false"), ); $t->start(); for ($x=0; $x<count($data); $x++) { $result = $p->is_not_value("",$data[$x]["data"],$data[$x]["value"]); $msg = get_message("is_not_value",$result,$data[$x]["expect"]); print_tablerow ($x, $msg, $data[$x]["data"]." ".$data[$x]["value"]); } $t->stop(); $time = $t->elapsed(); $t->reset(); print_tablerow("", "elapsed time", $time); echo "<tr><td colspan=\"3\"><hr></td>"; } echo "</table>"; echo "error count: ". $p->get_errors_count() ."<br>"; $p->set_language("en"); $errors = $p->get_errors_message(); echo "<pre>errors"; print_r($errors); echo "</pre>"; function get_message ($test, $result, $expect) { if ($result) // if $result is true { $tmp_result = "true"; }else{ $tmp_result = "false"; } if ($tmp_result == $expect) { $str = "<span class=\"pass\">result of ". $test ." is ". $tmp_result .", expecting ". $expect ."</span><br>"; }else{ $str = "<span class=\"fail\">result of ". $test ." ". $tmp_result .", expecting ". $expect ."</span><br>"; } return $str; } function print_tablerow ($x,$msg,$data) { echo "<tr>"; echo " <td>". $x ."</td>"; echo " <td>". $msg ."</td>"; echo " <td>". htmlspecialchars($data) ."</td>"; echo "</tr>"; } ?> </body> </html>