00001 <?php 00005 require_once("models/WSCORE2.php"); 00006 require_once("safely-php/assert.php"); 00007 require_once("models/soc1c.php"); 00008 00009 $expected_no = 2; 00010 $completed_no = 0; 00011 00015 function testBasic () { 00016 global $assert; 00017 global $completed_no; 00018 00019 00020 $conf = new SOC_Configuration("SOC Cache2 Test", "test_soc1c.conf"); 00021 $obj = new SOC_Cache2($conf); 00022 $assert->isTrue(is_object($obj), "Constructor worked."); 00023 $methods = array("set", "get", "open", "close", "execute", "mapExecute", "map", "setCache", "getCache", "listCache"); 00024 foreach ($methods as $method_name) { 00025 $assert->isTrue(method_exists($obj, $method_name), "has $method_name method."); 00026 } 00027 $assert->isTrue($obj->open(), "open() SOC Cach2"); 00028 $assert->isTrue($obj->createTables(), "createTables() should return true. " . $obj->message()); 00029 $pages = array(); 00030 for ($i = 0; $i < 10; $i++ ) { 00031 $page =<<<HTML 00032 <html> 00033 <head> 00034 <title>Test Page $i</title> 00035 </head> 00036 <body> 00037 <h1>Test Page $i</h1> 00038 <p> 00039 <a href="http://www.usc.edu">USC Home Page</a> 00040 </p> 00041 </body> 00042 </html> 00043 HTML; 00044 $assert->isTrue($obj->setCache("/test$i", $page, true), "Should be able to save /test$i. " . $obj->message()); 00045 $assert->isTrue($obj->getCache("/test$i", true) === $page, "Cache should return page for /test$i. " . $obj->message()); 00046 $pages[$i] = $page; 00047 } 00048 00049 $cachedPages = $obj->listCache(true); 00050 foreach ($cachedPages as $uri) { 00051 $i = str_replace('/test','',$uri); 00052 $assert->isTrue($obj->getCache($uri) === $pages[$i], "Cached page for $uri ($i). " . $obj->message()); 00053 } 00054 $assert->isTrue($obj->close(), "close() SOC Cache2."); 00055 00056 $completed_no += 1; 00057 } 00058 00062 function testQuoteFix () { 00063 global $assert; 00064 global $completed_no; 00065 00066 $conf = new SOC_Configuration("SOC Cache2 Test", "test_soc1c.conf"); 00067 $obj = new SOC_Cache2($conf); 00068 $obj->open(); 00069 $page =<<<HTML 00070 <html> 00071 <head> 00072 <title>Quote's Test Page</title> 00073 </head> 00074 <body> 00075 <h1>Quote's Test Page</h1> 00076 <p> 00077 <a href="http://www.usc.edu">USC Home Page</a> 00078 </p> 00079 <p>Fred's comment was about quoting</p> 00080 </body> 00081 </html> 00082 HTML; 00083 $expected = "single quote: ' -> " . str_replace("'", "\\'", $page); 00084 $result = $obj->map("single quote: ' -> {page}", array('page' => $page), true); 00085 $assert->isTrue(strpos($result, $expected) === 0, "map() expected\n[" . $expected . "]\n[" . $result . "] " . strpos($result, $expected)); 00086 $assert->isTrue($obj->setCache("/Qtest", $page, true), "Should be able to save /Qtest. " . $obj->message()); 00087 $assert->isTrue($obj->getCache("/Qtest", true) === $page, "Cache should return page for /Qtest. " . $obj->message()); 00088 $obj->close(); 00089 $completed_no += 1; 00090 } 00091 00092 if (php_sapi_name() !== 'cli') { 00093 echo 'This should be run from the command line.'; 00094 exit(1); 00095 } 00096 00097 echo 'Starting [soc_cache2-tests.php] ...' . PHP_EOL; 00098 $assert->ok(file_exists("_conf/test_soc1c.conf"), "Should have a test configuration file."); 00099 echo "\tstarting, testBasic() $completed_no/$expected_no\n"; 00100 testBasic(); 00101 echo "\tstarting, testQuoteFix() $completed_no/$expected_no\n"; 00102 testQuoteFix(); 00103 echo "Success!! completed $completed_no/$expected_no" . PHP_EOL; 00104 ?>