PageRenderTime 24ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/unit-test/bpm-testcase/test_system.cache.php

http://buddypress-media.googlecode.com/
PHP | 455 lines | 218 code | 169 blank | 68 comment | 38 complexity | c2c8cb76d61ee5b53ed846e996cc34c5 MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * BP-MEDIA UNIT TEST SCRIPT - SYSTEM CACHE CLASS
  4. * Exercises all functions of the class
  5. *
  6. * @version 0.1.9
  7. * @since 0.1.9
  8. * @package BP-Media
  9. * @subpackage Unit Test
  10. * @license GPL v2.0
  11. * @link http://code.google.com/p/buddypress-media/
  12. *
  13. * ========================================================================================================
  14. */
  15. class core_system_cache_APC extends BPM_testCase {
  16. var $cls;
  17. var $check_array = array();
  18. var $namespace_max = 10; // These values need to be set high enough to get a range of data types
  19. var $key_max = 100;
  20. function setUp() {
  21. parent::setUp();
  22. $this->cls = new BPM_mCache();
  23. // Get APC's status
  24. $result = $this->cls->apcStatus();
  25. $apc_status = $result["status"];
  26. $apc_state = $result["state"];
  27. unset($result);
  28. // If APC is not active on the system, skip the entire block of tests
  29. if(!$apc_status){
  30. switch($apc_state){
  31. case "disabled" : {
  32. $reason = "APC disabled in class file";
  33. } break;
  34. case "inop" : {
  35. $reason = "APC not present on server";
  36. } break;
  37. case "fail" : {
  38. $reason = "APC unknown error";
  39. } break;
  40. }
  41. $this->markTestSkipped($reason);
  42. }
  43. }
  44. function generateData($type=null){
  45. $valid_types = array("null", "bool", "int", "float", "string", "array", "object");
  46. if($type === null){ // "0" is a valid type
  47. $current_type = $valid_types[mt_rand(0,6)];
  48. }
  49. else {
  50. // Using the modulus operator causes the function to cycle
  51. // through types if $type variable exceeds (int)5.
  52. $type_idx = $type % 6;
  53. $current_type = $valid_types[$type_idx];
  54. }
  55. switch($current_type){
  56. case "null" : {
  57. $val = null;
  58. } break;
  59. case "bool" : {
  60. $val = mt_rand(0,1);
  61. $val = (bool)$val;
  62. } break;
  63. case "int" : {
  64. $val = mt_rand(-100,100);
  65. $val = (int)$val;
  66. } break;
  67. case "float" : {
  68. $val = (mt_rand(-100,100) / 17);
  69. $val = (float)$val;
  70. } break;
  71. case "string" : {
  72. $width = mt_rand(0, 87);
  73. $val = BPM_sUtil::random_string($width);
  74. } break;
  75. case "array" : {
  76. $named_keys = mt_rand(0,1);
  77. $val = array();
  78. if(!$named_keys){
  79. for($i=0; $i<5; $i++){
  80. $val[] = mt_rand(-2500, 2500);
  81. }
  82. }
  83. else {
  84. for($i=0; $i<5; $i++){
  85. $width = mt_rand(1, 37);
  86. // PHP does not support arrays with numeric string keys. It silently converts them
  87. // to int keys. For example: $test_array["1234"] = "foo"; $test_array["bar"] = "baz";
  88. // var_dump($test_array) produces [ test_array[(int)1234] = "foo", test_array[(string)bar]
  89. // = "baz"]
  90. //
  91. // BPM_sUtil::random_string() can (and does) produce values like (string)7. To avoid
  92. // debugging problems, prepend all the key names with "k" to ensure PHP treats them as strings.
  93. $key_name = "k" . BPM_sUtil::random_string($width);
  94. $val[$key_name] = mt_rand(-2500, 2500);
  95. }
  96. }
  97. } break;
  98. case "object" : {
  99. $val = new stdClass();
  100. for($i=0; $i<3; $i++){
  101. $width = mt_rand(1, 17);
  102. // We prepend all the key names with "k" because BPM_sUtil::random_string()
  103. // can (and does) produce values like (string)7, which is an illegal name for a
  104. // variable in an object [you can't do $test_object->7 = "foo"; ...because the parser
  105. // doesn't know if you mean (string)7 or (int)7 ]
  106. //
  107. // But, PHP *doesn't catch* this defect when the variable name is created dynamically
  108. // [ $name = "7"; $test_object->{$name} = "foo" ]. Instead, it converts the key's type
  109. // from (string)7 to (int)7 and allows the variable to be accessed only if the name is
  110. // created dynamically [$name = "7"; echo ($test_object->{$name}; ]
  111. $key_name = "k" . BPM_sUtil::random_string($width);
  112. $val->{$key_name} = mt_rand(-2500, 2500);
  113. }
  114. } break;
  115. } // End of switch($current_type)
  116. return $val;
  117. }
  118. function load_table(){
  119. $total_keys = 0;
  120. for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
  121. for ($key=1; $key < $this->key_max; $key++) {
  122. // Cycle through data types for the first 50 keys, then
  123. // randomly pick data types
  124. if($total_keys < 50){
  125. $val = self::generateData($total_keys);
  126. }
  127. else {
  128. $val = self::generateData();
  129. }
  130. $ns_name = "ns" . $namespace;
  131. $key_name = "key" . $key;
  132. // Write generated value to the cache and the check array
  133. $this->cls->set($ns_name, $key_name, $val);
  134. $this->check_array[$ns_name][$key_name] = $val;
  135. $total_keys++;
  136. } // ENDOF for( $key
  137. } // ENDOF: for( $namespace
  138. }
  139. function check_table(){
  140. // Load every key stored to the table and compare it against
  141. // the value stored in the check array, making sure data types were
  142. // correctly recovered (bool) false doesn't become (int) 0 or "NULL"
  143. // ====================================================================
  144. for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
  145. for ($key=1; $key < $this->key_max; $key++) {
  146. $ns_name = "ns" . $namespace;
  147. $key_name = "key" . $key;
  148. $result = $this->cls->get($ns_name, $key_name);
  149. $this->assertEquals($this->check_array[$ns_name][$key_name], $result);
  150. }
  151. }
  152. }
  153. function test_create(){
  154. // Delete all entries in the cache
  155. $this->cls->flushAll();
  156. // Load the database and check array with test data. Note that PHPUnit resets ALL the
  157. // class variables between tests, so we have to reload the table and reset the cache
  158. // in each test
  159. self::load_table();
  160. // Verify the stored data matches the check array
  161. self::check_table();
  162. } // End of test_create
  163. function test_update(){
  164. // Delete all entries in the cache
  165. $this->cls->flushAll();
  166. // Load the database and check array with test data. Note that PHPUnit resets ALL the
  167. // class variables between tests, so we have to reload the table and reset the cache
  168. // in each test
  169. self::load_table();
  170. $total_keys = 0;
  171. // Overwrite half of the items in the table with random new data
  172. for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
  173. for ($key=1; $key < $this->key_max; $key++) {
  174. $overwrite = mt_rand(0,1);
  175. if(true){
  176. // Cycle through data types for the first 50 keys, then
  177. // randomly pick data types
  178. if($total_keys < 50){
  179. $val = self::generateData($total_keys);
  180. }
  181. else {
  182. $val = self::generateData();
  183. }
  184. $ns_name = "ns" . $namespace;
  185. $key_name = "key" . $key;
  186. // Write generated value to the cache and the check array
  187. $this->cls->set($ns_name, $key_name, $val);
  188. $this->check_array[$ns_name][$key_name] = $val;
  189. $total_keys++;
  190. } // ENDOF if($overwrite)
  191. } // ENDOF for( $key
  192. } // ENDOF: for( $namespace
  193. // Verify the stored data matches the check array
  194. self::check_table();
  195. } // End of test_update
  196. function test_del(){
  197. // Delete all entries in the cache
  198. $this->cls->flushAll();
  199. // Load the database and check array with test data. Note that PHPUnit resets ALL the
  200. // class variables between tests, so we have to reload the table and reset the cache
  201. // in each test
  202. self::load_table();
  203. // Delete half the keys in the cache
  204. for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
  205. for ($key=1; $key < $this->key_max; $key++) {
  206. $delete = mt_rand(0,1);
  207. if($delete){
  208. $ns_name = "ns" . $namespace;
  209. $key_name = "key" . $key;
  210. // Remove the key from the check array
  211. $this->cls->del($ns_name, $key_name);
  212. unset($this->check_array[$ns_name][$key_name]);
  213. } // ENDOF if($delete)
  214. } // ENDOF for( $key
  215. } // ENDOF: for( $namespace
  216. for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
  217. for ($key=1; $key < $this->key_max; $key++) {
  218. $ns_name = "ns" . $namespace;
  219. $key_name = "key" . $key;
  220. $result = $this->cls->get($ns_name, $key_name, &$success);
  221. $expected = $this->check_array[$ns_name][$key_name];
  222. if( is_array($this->check_array[$ns_name]) && array_key_exists($key_name, $this->check_array[$ns_name]) ){
  223. $key_exists = true;
  224. }
  225. else {
  226. $key_exists = false;
  227. }
  228. if($key_exists){
  229. $this->assertEquals($this->check_array[$ns_name][$key_name], $result);
  230. $this->assertEquals(true, $success);
  231. }
  232. else {
  233. $this->assertEquals($this->check_array[$ns_name][$key_name], $result);
  234. $this->assertEquals(false, $success);
  235. }
  236. }
  237. }
  238. } // End of test_del
  239. function test_flushNamespace(){
  240. // Delete all entries in the cache
  241. $this->cls->flushAll();
  242. // Load the database and check array with test data. Note that PHPUnit resets ALL the
  243. // class variables between tests, so we have to reload the table and reset the cache
  244. // in each test
  245. self::load_table();
  246. // Delete half the namespaces in the cache
  247. for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
  248. $delete = mt_rand(0,1);
  249. if($delete){
  250. $ns_name = "ns" . $namespace;
  251. $key_name = "key" . $key;
  252. // Remove the namespace from the check array
  253. $this->cls->flushNamespace($ns_name);
  254. unset($this->check_array[$ns_name]);
  255. } // ENDOF if($delete)
  256. } // ENDOF: for( $namespace
  257. // Check that the cache returns the correct value when loading keys from both
  258. // active and deleted namespaces
  259. for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
  260. for ($key=1; $key < $this->key_max; $key++) {
  261. $ns_name = "ns" . $namespace;
  262. $key_name = "key" . $key;
  263. $result = $this->cls->get($ns_name, $key_name, &$success);
  264. $expected = $this->check_array[$ns_name][$key_name];
  265. if( is_array($this->check_array[$ns_name]) && array_key_exists($key_name, $this->check_array[$ns_name]) ){
  266. $key_exists = true;
  267. }
  268. else {
  269. $key_exists = false;
  270. }
  271. if($key_exists){
  272. $this->assertEquals($this->check_array[$ns_name][$key_name], $result);
  273. $this->assertEquals(true, $success);
  274. }
  275. else {
  276. $this->assertEquals($this->check_array[$ns_name][$key_name], $result);
  277. $this->assertEquals(false, $success);
  278. }
  279. }
  280. }
  281. } // End of test_flushNamespace
  282. function tearDown() {
  283. parent::tearDown();
  284. }
  285. }
  286. ?>