Annotation of embedaddon/php/ext/standard/tests/array/array_key_exists.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: Test array_key_exists() function
! 3: --FILE--
! 4: <?php
! 5: /* Prototype:
! 6: * bool array_key_exists ( mixed $key, array $search );
! 7: * Description:
! 8: * Returns TRUE if the given key is set in the array.
! 9: * key can be any value possible for an array index.
! 10: * Also also works on objects.
! 11: */
! 12:
! 13: echo "*** Testing basic functionalities ***\n";
! 14: /* Arrays with regular values */
! 15: $search_arrays = array(
! 16: array(1,2,3,4),
! 17: array('a','b','c'),
! 18: array('abc', 'bcd', 'dcf'),
! 19: array("test", "rest", "enjoy"),
! 20: array("Name" => "Jack", "Loc" => "Mars", "Id" => "MS123"),
! 21: array('Red' => 'Rose', 'I' => 'You'),
! 22: array(0 => 'Zero', 1 => 'One', 2 => 'Two', 3 => "Three" ),
! 23: array(0.1 => 'Zero', 1.1 => 'One', 2.2 => 'Two', 3.3 => "Three" )
! 24: );
! 25: /* keys to search in $search_arrays. $keys[0]
! 26: is the key to be searched in $search_arrays[0] and so on */
! 27: $keys = array( 1, 'a', 2, 4, "Name", "Red", 0, 3 );
! 28:
! 29: $key_counter = 0;
! 30: foreach ($search_arrays as $search_array) {
! 31: $key = $keys[ $key_counter++ ];
! 32: echo "-- Iteration $key_counter --\n";
! 33: var_dump( array_key_exists($key, $search_array) );
! 34: }
! 35:
! 36: echo "\n*** Testing possible variations ***\n";
! 37: // use different keys on each sub array of $search_arrays
! 38: $key_variations = array ("", NULL, null, " ", '', "test", 1);
! 39: $key_counter = 0;
! 40: $key_count = count ( $key_variations );
! 41: echo "\n** Variation loop 1 **\n";
! 42: $out_loop_count = 0;
! 43: foreach ($search_arrays as $search_array) {
! 44: $key_counter = 0;
! 45: $out_loop_count ++; echo "-- Iteration $out_loop_count --\n";
! 46: while ( $key_counter < $key_count ) {
! 47: $key = $key_variations[ $key_counter++ ];
! 48: var_dump( array_key_exists($key, $search_array) );
! 49: }
! 50: }
! 51: // arrays with variation in elements
! 52: $search_arrays_v = array (
! 53: array(),
! 54: array(NULL),
! 55: array(array(), 1, 2),
! 56: array(1,2,3, "" => "value", NULL => "value", true => "value" ),
! 57: array( array(2,4,5), array ("a","b","d") )
! 58: );
! 59: // search for $key_variations in each sub array of $search_arrays_v
! 60: echo "\n** Variation loop 2 **\n";
! 61: $out_loop_count = 0;
! 62: foreach ($search_arrays_v as $search_array) {
! 63: $key_counter = 0;
! 64: $out_loop_count ++; echo "-- Iteration $out_loop_count --\n";
! 65: while ( $key_counter < $key_count ) {
! 66: $key = $key_variations[ $key_counter++ ];
! 67: var_dump( array_key_exists($key, $search_array) );
! 68: }
! 69: }
! 70:
! 71: echo "\n*** Testing error conditions ***\n";
! 72: //Zeor args
! 73: var_dump( array_key_exists() );
! 74: // first args as array
! 75: var_dump( array_key_exists(array(), array()) );
! 76: // second args as string
! 77: var_dump( array_key_exists("", "") );
! 78: // second args a integer
! 79: var_dump( array_key_exists(1, 1) );
! 80: // second args as NULL
! 81: var_dump( array_key_exists(1, NULL) );
! 82: // second args as boolean
! 83: var_dump( array_key_exists(1, true) );
! 84: // first args as boolean
! 85: var_dump( array_key_exists(false, true) );
! 86: // second args as float
! 87: var_dump( array_key_exists(false, 17.5) );
! 88: // args more than expected
! 89: var_dump( array_key_exists(1, array(), array()) );
! 90: // first argument as floating point value
! 91: var_dump( array_key_exists(17.5, array(1,23) ) ) ;
! 92:
! 93: echo "\n*** Testing operation on objects ***\n";
! 94: class key_check
! 95: {
! 96: private $private_var = "Priviate var";
! 97: protected $protected_var = "Protected var";
! 98: public $public_var = "Public var";
! 99: public $arr = array("var" => "value", "1" => "one", ""=>"value");
! 100: public function print_member()
! 101: {
! 102: echo $this->$private_var."\n";
! 103: echo $this->$protected_var."\n";
! 104: echo $this->$public_var."\n";
! 105: }
! 106: }
! 107:
! 108: $key_check_obj = new key_check; //new object
! 109: /* array_key_exists() on an object, it should work on only public member variables */
! 110: var_dump(array_key_exists("private_var", $key_check_obj)); // not found, private member
! 111: var_dump(array_key_exists("protected_var", $key_check_obj)); // not found, private member
! 112: var_dump(array_key_exists("public_var", $key_check_obj)); // found, public member
! 113: var_dump(array_key_exists("print_member", $key_check_obj)); // not found, its a function
! 114: var_dump(array_key_exists("arr", $key_check_obj)); //found, public member
! 115: var_dump(array_key_exists("var", $key_check_obj->arr)); //found, key is in member array
! 116:
! 117: /* error condition, first arguemnt as object */
! 118: var_dump( array_key_exists($key_check_obj, $key_check_obj) );
! 119: echo "Done\n";
! 120: ?>
! 121: --EXPECTF--
! 122: *** Testing basic functionalities ***
! 123: -- Iteration 1 --
! 124: bool(true)
! 125: -- Iteration 2 --
! 126: bool(false)
! 127: -- Iteration 3 --
! 128: bool(true)
! 129: -- Iteration 4 --
! 130: bool(false)
! 131: -- Iteration 5 --
! 132: bool(true)
! 133: -- Iteration 6 --
! 134: bool(true)
! 135: -- Iteration 7 --
! 136: bool(true)
! 137: -- Iteration 8 --
! 138: bool(true)
! 139:
! 140: *** Testing possible variations ***
! 141:
! 142: ** Variation loop 1 **
! 143: -- Iteration 1 --
! 144: bool(false)
! 145: bool(false)
! 146: bool(false)
! 147: bool(false)
! 148: bool(false)
! 149: bool(false)
! 150: bool(true)
! 151: -- Iteration 2 --
! 152: bool(false)
! 153: bool(false)
! 154: bool(false)
! 155: bool(false)
! 156: bool(false)
! 157: bool(false)
! 158: bool(true)
! 159: -- Iteration 3 --
! 160: bool(false)
! 161: bool(false)
! 162: bool(false)
! 163: bool(false)
! 164: bool(false)
! 165: bool(false)
! 166: bool(true)
! 167: -- Iteration 4 --
! 168: bool(false)
! 169: bool(false)
! 170: bool(false)
! 171: bool(false)
! 172: bool(false)
! 173: bool(false)
! 174: bool(true)
! 175: -- Iteration 5 --
! 176: bool(false)
! 177: bool(false)
! 178: bool(false)
! 179: bool(false)
! 180: bool(false)
! 181: bool(false)
! 182: bool(false)
! 183: -- Iteration 6 --
! 184: bool(false)
! 185: bool(false)
! 186: bool(false)
! 187: bool(false)
! 188: bool(false)
! 189: bool(false)
! 190: bool(false)
! 191: -- Iteration 7 --
! 192: bool(false)
! 193: bool(false)
! 194: bool(false)
! 195: bool(false)
! 196: bool(false)
! 197: bool(false)
! 198: bool(true)
! 199: -- Iteration 8 --
! 200: bool(false)
! 201: bool(false)
! 202: bool(false)
! 203: bool(false)
! 204: bool(false)
! 205: bool(false)
! 206: bool(true)
! 207:
! 208: ** Variation loop 2 **
! 209: -- Iteration 1 --
! 210: bool(false)
! 211: bool(false)
! 212: bool(false)
! 213: bool(false)
! 214: bool(false)
! 215: bool(false)
! 216: bool(false)
! 217: -- Iteration 2 --
! 218: bool(false)
! 219: bool(false)
! 220: bool(false)
! 221: bool(false)
! 222: bool(false)
! 223: bool(false)
! 224: bool(false)
! 225: -- Iteration 3 --
! 226: bool(false)
! 227: bool(false)
! 228: bool(false)
! 229: bool(false)
! 230: bool(false)
! 231: bool(false)
! 232: bool(true)
! 233: -- Iteration 4 --
! 234: bool(true)
! 235: bool(true)
! 236: bool(true)
! 237: bool(false)
! 238: bool(true)
! 239: bool(false)
! 240: bool(true)
! 241: -- Iteration 5 --
! 242: bool(false)
! 243: bool(false)
! 244: bool(false)
! 245: bool(false)
! 246: bool(false)
! 247: bool(false)
! 248: bool(true)
! 249:
! 250: *** Testing error conditions ***
! 251:
! 252: Warning: array_key_exists() expects exactly 2 parameters, 0 given in %s on line %d
! 253: NULL
! 254:
! 255: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
! 256: bool(false)
! 257:
! 258: Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
! 259: NULL
! 260:
! 261: Warning: array_key_exists() expects parameter 2 to be array, integer given in %s on line %d
! 262: NULL
! 263:
! 264: Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
! 265: NULL
! 266:
! 267: Warning: array_key_exists() expects parameter 2 to be array, boolean given in %s on line %d
! 268: NULL
! 269:
! 270: Warning: array_key_exists() expects parameter 2 to be array, boolean given in %s on line %d
! 271: NULL
! 272:
! 273: Warning: array_key_exists() expects parameter 2 to be array, double given in %s on line %d
! 274: NULL
! 275:
! 276: Warning: array_key_exists() expects exactly 2 parameters, 3 given in %s on line %d
! 277: NULL
! 278:
! 279: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
! 280: bool(false)
! 281:
! 282: *** Testing operation on objects ***
! 283: bool(false)
! 284: bool(false)
! 285: bool(true)
! 286: bool(false)
! 287: bool(true)
! 288: bool(true)
! 289:
! 290: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
! 291: bool(false)
! 292: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>