/unit-test/testcase/test_system.cache.php
PHP | 460 lines | 219 code | 171 blank | 70 comment | 38 complexity | b1b935daeb01db1a935c002805d677c7 MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-2.0, LGPL-2.1
- <?php
- /**
- * BP-MEDIA UNIT TEST SCRIPT - SYSTEM CACHE CLASS
- * Exercises all functions of the class
- *
- * @version 0.1.9
- * @since 0.1.9
- * @package BP-Media
- * @subpackage Unit Test
- * @license GPL v2.0
- * @link http://code.google.com/p/buddypress-media/
- *
- * ========================================================================================================
- */
- class core_system_cache_APC extends BPM_testCase {
- var $cls;
- var $check_array = array();
- var $namespace_max = 10; // These values need to be set high enough to get a range of data types
- var $key_max = 100;
- function setUp() {
-
- parent::setUp();
-
- // Since we're directly instantiating the cache class, we need to
- // set the cache engines to use from the settings stored in the test manager
-
- global $bpm_test_manager;
- $this->cls = new BPM_mCache( $bpm_test_manager->cache_engine );
- // Get APC's status
- $result = $this->cls->apcStatus();
- $apc_status = $result["status"];
- $apc_state = $result["state"];
- unset($result);
- // If APC is not active on the system, skip the entire block of tests
- if(!$apc_status){
- switch($apc_state){
- case "disabled" : {
- $reason = "APC disabled in class file";
- } break;
- case "inop" : {
- $reason = "APC not present on server";
- } break;
- case "fail" : {
- $reason = "APC unknown error";
- } break;
- }
- $this->markTestSkipped($reason);
- }
- }
- function generateData($type=null){
- $valid_types = array("null", "bool", "int", "float", "string", "array", "object");
- if($type === null){ // "0" is a valid type
- $current_type = $valid_types[mt_rand(0,6)];
- }
- else {
- // Using the modulus operator causes the function to cycle
- // through types if $type variable exceeds (int)5.
- $type_idx = $type % 6;
- $current_type = $valid_types[$type_idx];
- }
- switch($current_type){
- case "null" : {
- $val = null;
- } break;
- case "bool" : {
- $val = mt_rand(0,1);
- $val = (bool)$val;
- } break;
- case "int" : {
- $val = mt_rand(-100,100);
- $val = (int)$val;
- } break;
- case "float" : {
- $val = (mt_rand(-100,100) / 17);
- $val = (float)$val;
- } break;
- case "string" : {
- $width = mt_rand(0, 87);
- $val = BPM_sUtil::random_string($width);
- } break;
- case "array" : {
- $named_keys = mt_rand(0,1);
- $val = array();
- if(!$named_keys){
- for($i=0; $i<5; $i++){
- $val[] = mt_rand(-2500, 2500);
- }
- }
- else {
- for($i=0; $i<5; $i++){
- $width = mt_rand(1, 37);
- // PHP does not support arrays with numeric string keys. It silently converts them
- // to int keys. For example: $test_array["1234"] = "foo"; $test_array["bar"] = "baz";
- // var_dump($test_array) produces [ test_array[(int)1234] = "foo", test_array[(string)bar]
- // = "baz"]
- //
- // BPM_sUtil::random_string() can (and does) produce values like (string)7. To avoid
- // debugging problems, prepend all the key names with "k" to ensure PHP treats them as strings.
- $key_name = "k" . BPM_sUtil::random_string($width);
- $val[$key_name] = mt_rand(-2500, 2500);
- }
- }
- } break;
- case "object" : {
- $val = new stdClass();
- for($i=0; $i<3; $i++){
- $width = mt_rand(1, 17);
- // We prepend all the key names with "k" because BPM_sUtil::random_string()
- // can (and does) produce values like (string)7, which is an illegal name for a
- // variable in an object [you can't do $test_object->7 = "foo"; ...because the parser
- // doesn't know if you mean (string)7 or (int)7 ]
- //
- // But, PHP *doesn't catch* this defect when the variable name is created dynamically
- // [ $name = "7"; $test_object->{$name} = "foo" ]. Instead, it converts the key's type
- // from (string)7 to (int)7 and allows the variable to be accessed only if the name is
- // created dynamically [$name = "7"; echo ($test_object->{$name}; ]
- $key_name = "k" . BPM_sUtil::random_string($width);
- $val->{$key_name} = mt_rand(-2500, 2500);
- }
- } break;
- } // End of switch($current_type)
-
- return $val;
-
- }
- function load_table(){
-
- $total_keys = 0;
- for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
- for ($key=1; $key < $this->key_max; $key++) {
- // Cycle through data types for the first 50 keys, then
- // randomly pick data types
- if($total_keys < 50){
- $val = self::generateData($total_keys);
- }
- else {
- $val = self::generateData();
- }
-
- $ns_name = "ns" . $namespace;
- $key_name = "key" . $key;
- // Write generated value to the cache and the check array
- $this->cls->set($ns_name, $key_name, $val);
- $this->check_array[$ns_name][$key_name] = $val;
- $total_keys++;
- } // ENDOF for( $key
- } // ENDOF: for( $namespace
- }
- function check_table(){
- // Load every key stored to the table and compare it against
- // the value stored in the check array, making sure data types were
- // correctly recovered (bool) false doesn't become (int) 0 or "NULL"
- // ====================================================================
- for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
- for ($key=1; $key < $this->key_max; $key++) {
- $ns_name = "ns" . $namespace;
- $key_name = "key" . $key;
- $result = $this->cls->get($ns_name, $key_name);
- $this->assertEquals($this->check_array[$ns_name][$key_name], $result);
- }
- }
- }
- function test_create(){
- // Delete all entries in the cache
- $this->cls->flushAll();
- // Load the database and check array with test data. Note that PHPUnit resets ALL the
- // class variables between tests, so we have to reload the table and reset the cache
- // in each test
- self::load_table();
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_create
- function test_update(){
- // Delete all entries in the cache
- $this->cls->flushAll();
- // Load the database and check array with test data. Note that PHPUnit resets ALL the
- // class variables between tests, so we have to reload the table and reset the cache
- // in each test
- self::load_table();
- $total_keys = 0;
- // Overwrite half of the items in the table with random new data
- for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
- for ($key=1; $key < $this->key_max; $key++) {
- $overwrite = mt_rand(0,1);
- if(true){
- // Cycle through data types for the first 50 keys, then
- // randomly pick data types
- if($total_keys < 50){
- $val = self::generateData($total_keys);
- }
- else {
- $val = self::generateData();
- }
- $ns_name = "ns" . $namespace;
- $key_name = "key" . $key;
- // Write generated value to the cache and the check array
- $this->cls->set($ns_name, $key_name, $val);
- $this->check_array[$ns_name][$key_name] = $val;
- $total_keys++;
- } // ENDOF if($overwrite)
- } // ENDOF for( $key
- } // ENDOF: for( $namespace
-
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_update
- function test_del(){
- // Delete all entries in the cache
- $this->cls->flushAll();
- // Load the database and check array with test data. Note that PHPUnit resets ALL the
- // class variables between tests, so we have to reload the table and reset the cache
- // in each test
- self::load_table();
- // Delete half the keys in the cache
- for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
- for ($key=1; $key < $this->key_max; $key++) {
- $delete = mt_rand(0,1);
- if($delete){
- $ns_name = "ns" . $namespace;
- $key_name = "key" . $key;
- // Remove the key from the check array
- $this->cls->del($ns_name, $key_name);
- unset($this->check_array[$ns_name][$key_name]);
- } // ENDOF if($delete)
- } // ENDOF for( $key
- } // ENDOF: for( $namespace
- for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
- for ($key=1; $key < $this->key_max; $key++) {
- $ns_name = "ns" . $namespace;
- $key_name = "key" . $key;
- $result = $this->cls->get($ns_name, $key_name, &$success);
- $expected = $this->check_array[$ns_name][$key_name];
- if( is_array($this->check_array[$ns_name]) && array_key_exists($key_name, $this->check_array[$ns_name]) ){
- $key_exists = true;
- }
- else {
- $key_exists = false;
- }
- if($key_exists){
-
- $this->assertEquals($this->check_array[$ns_name][$key_name], $result);
- $this->assertEquals(true, $success);
- }
- else {
- $this->assertEquals($this->check_array[$ns_name][$key_name], $result);
- $this->assertEquals(false, $success);
- }
- }
- }
- } // End of test_del
- function test_flushNamespace(){
- // Delete all entries in the cache
- $this->cls->flushAll();
- // Load the database and check array with test data. Note that PHPUnit resets ALL the
- // class variables between tests, so we have to reload the table and reset the cache
- // in each test
- self::load_table();
- // Delete half the namespaces in the cache
- for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
- $delete = mt_rand(0,1);
- if($delete){
- $ns_name = "ns" . $namespace;
- $key_name = "key" . $key;
- // Remove the namespace from the check array
- $this->cls->flushNamespace($ns_name);
- unset($this->check_array[$ns_name]);
- } // ENDOF if($delete)
- } // ENDOF: for( $namespace
- // Check that the cache returns the correct value when loading keys from both
- // active and deleted namespaces
- for( $namespace=1; $namespace < $this->namespace_max; $namespace++){
- for ($key=1; $key < $this->key_max; $key++) {
- $ns_name = "ns" . $namespace;
- $key_name = "key" . $key;
- $result = $this->cls->get($ns_name, $key_name, &$success);
- $expected = $this->check_array[$ns_name][$key_name];
- if( is_array($this->check_array[$ns_name]) && array_key_exists($key_name, $this->check_array[$ns_name]) ){
- $key_exists = true;
- }
- else {
- $key_exists = false;
- }
- if($key_exists){
- $this->assertEquals($this->check_array[$ns_name][$key_name], $result);
- $this->assertEquals(true, $success);
- }
- else {
- $this->assertEquals($this->check_array[$ns_name][$key_name], $result);
- $this->assertEquals(false, $success);
- }
- }
- }
- } // End of test_flushNamespace
- function tearDown() {
- parent::tearDown();
- }
- }
- ?>