00001 <?php
00010 require_once("safely-php/assert.php");
00011 require_once("models/soc1c.php");
00012 require_once("models/api1c.php");
00013
00014 $expected_no = 0;
00015 $completed_no = 0;
00016
00020 function testSOC_Cache2 () {
00021 global $assert;
00022 global $expected_no;
00023 global $completed_no;
00024
00025 $expected_no += 1;
00026 print("\tstarting, testSOC_Cache2() $completed_no/$expected_no\n");
00027 $conf = new SOC_Configuration("SOC Cache Refresh", "soc1c.conf");
00028 $cache = new SOC_Cache2($conf);
00029 $assert->equal($cache->get("db_type"), "mysql", "Should have db_type = mysql");
00030 $assert->ok($cache->get("db_name"), "Should have db_name set");
00031 $assert->equal($cache->get("db_host"), "web-mysql.usc.edu", "Should have db_host set to web-mysql.usc.edu");
00032 $assert->ok($cache->get("db_password"), "Should have a db_password set.");
00033 $assert->ok($cache->open(), "Should be able to open cache");
00034 $json = $cache->getCache("/terms");
00035 $assert->ok($json, "Should get some terms data as JSON back" . $cache->message());
00036 $terms = json_decode($json, true);
00037 $assert->ok($terms["term"], "Should have some term data defined.");
00038 $assert->ok($cache->close(), "Should be able to close the cache");
00039 $completed_no += 1;
00040 print("\tcompleted, testSOC_Cache2() $completed_no/$expected_no\n");
00041 }
00042
00046 function testPrototypeRefresh () {
00047 global $assert;
00048 global $expected_no;
00049 global $completed_no;
00050
00051 $expected_no += 1;
00052 print("\tstarting, testPrototypeRefresh() $completed_no/$expected_no\n");
00053 $conf = new SOC_Configuration("SOC Prototype Refresh", "soc1c.conf");
00054 $assert->isTrue($conf->get("soc_namespace") !== false, "soc_namespace should be set and a string in soc1c.conf");
00055
00056 $api1c = new SOC_API1C($conf);
00057 $assert->ok($api1c->open(), "Should be able to open the internal cache");
00058 $assert->ok($api1c->cache->db, "Should have a DB connection for cache");
00059
00060 $now = Date("Y-m-d h:i:s");
00061 $result = $api1c->getTerms();
00062 $assert->isTrue($result, "getTerms should return true.");
00063 $json = $api1c->cache->getCache("/terms");
00064 $result = json_decode($json, true);
00065 $assert->isTrue(isset($result['term']), "api1c->cache->getCache('/terms') should return a term array " . print_r($result, true));
00066 $terms = $result['term'];
00067 $data = array();
00068 $data['cache_table'] = $api1c->get("soc_cache");
00069 $assert->ok($data['cache_table'], "Should have a cache_table saved. " . print_r($data, true));
00070 $data['now'] = $now;
00071
00072 foreach ($terms as $term) {
00073 print("\t\tprocessing $term ");
00074 $assert->isTrue($api1c->getDepartments($term), "getDepartments should return true for $term.");
00075 $sql = "SELECT uri FROM {cache_table} WHERE uri LIKE '/departments/{term}' AND modified < '{now}'";
00076 $data['term'] = $term;
00077 $assert->isTrue($api1c->cache->mapExecute($sql, $data),"mapExecute($sql) should return true" . $api1c->cache->message());
00078 $depts = array();
00079 while ($row = $api1c->cache->getRow()) {
00080 $parts = explode("/",$row['uri']);
00081 $depts[] = $parts[2];
00082 print(".");
00083 }
00084 foreach ($depts as $dept) {
00085 print(" " . $dept);
00086 $assert->isTrue($api1c->getClasses($dept, $term), "getClasses($dept, $term) should return true.");
00087 }
00088 $sql = "SELECT uri FROM {cache_table} WHERE uri LIKE '/session/%{term}' AND modified < '{now}'";
00089 $assert->isTrue($api1c->cache->mapExecute($sql,$data),"mapExecute($sql) should return true");
00090 $sessions = array();
00091 while ($row = $api1c->cache->getRow()) {
00092 $parts = explode("/",$row['uri']);
00093 $sessions[] = $parts[2];
00094 print(".");
00095 }
00096 foreach ($sessions as $session) {
00097 print(" " . $session);
00098 $assert->isTrue($api1c->getSession($session, $term),"getSession($session, $term) should return true.");
00099 }
00100 print("\n");
00101
00102 $sql = "SELECT uri FROM {cache_table} WHERE uri LIKE '%term' AND modified < '{now}'";
00103 $assert->isTrue($api1c->cache->mapExecute($sql, $data), "mapExecute($sql ...) should return true for term $term.");
00104 $row = $api1c->cache->getRow();
00105 $assert->isFalse($row, "row should have been false with everything modied for term $term.");
00106 $assert->isTrue($api1c->cache->get("SQLRowCount") === 0, "Row count should have been zero for term $term.");
00107 }
00108 $completed_no += 1;
00109 print("\ttestPrototypeRefresh() done $completed_no/$expected_no\n");
00110 $api1c->close();
00111 }
00112
00113
00114 if (php_sapi_name() !== 'cli') {
00115 echo 'This should be run from the command line.';
00116 exit(1);
00117 }
00118
00119 echo 'Starting [soc_api1c-tests.php] ...' . PHP_EOL;
00120 testSOC_Cache2();
00121 testPrototypeRefresh();
00122 echo "Success!! completed $completed_no/$expected_no" . PHP_EOL;
00123
00124 ?>