]> granicus.if.org Git - php/commitdiff
- committing tests for array_key_exists() function
authorJosie Messa <jmessa@php.net>
Wed, 13 Feb 2008 13:16:21 +0000 (13:16 +0000)
committerJosie Messa <jmessa@php.net>
Wed, 13 Feb 2008 13:16:21 +0000 (13:16 +0000)
12 files changed:
ext/standard/tests/array/array_key_exists_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_error.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_object1.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_object2.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_variation1.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_variation2.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_variation3.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_variation4.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_variation5.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_variation6.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_variation7.phpt [new file with mode: 0644]
ext/standard/tests/array/array_key_exists_variation8.phpt [new file with mode: 0644]

diff --git a/ext/standard/tests/array/array_key_exists_basic.phpt b/ext/standard/tests/array/array_key_exists_basic.phpt
new file mode 100644 (file)
index 0000000..6fbd415
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--
+Test array_key_exists() function : basic functionality 
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Test basic functionality of array_key_exists()
+ */
+
+echo "*** Testing array_key_exists() : basic functionality ***\n";
+
+$key1 = 'key';
+$key2 = 'val';
+$search = array('one', 'key' => 'value', 'val');
+var_dump(array_key_exists($key1, $search));
+var_dump(array_key_exists($key2, $search));
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing array_key_exists() : basic functionality ***
+bool(true)
+bool(false)
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_error.phpt b/ext/standard/tests/array/array_key_exists_error.phpt
new file mode 100644 (file)
index 0000000..8b189c8
--- /dev/null
@@ -0,0 +1,44 @@
+--TEST--
+Test array_key_exists() function : error conditions - Pass incorrect number of args
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Pass incorrect number of arguments to array_key_exists() to test behaviour
+ */
+
+echo "*** Testing array_key_exists() : error conditions ***\n";
+
+//Test array_key_exists with one more than the expected number of arguments
+echo "\n-- Testing array_key_exists() function with more than expected no. of arguments --\n";
+$key = 1;
+$search = array(1, 2);
+$extra_arg = 10;
+var_dump( array_key_exists($key, $search, $extra_arg) );
+
+// Testing array_key_exists with one less than the expected number of arguments
+echo "\n-- Testing array_key_exists() function with less than expected no. of arguments --\n";
+$key = 1;
+var_dump( array_key_exists($key) );
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : error conditions ***
+
+-- Testing array_key_exists() function with more than expected no. of arguments --
+
+Warning: array_key_exists() expects exactly 2 parameters, 3 given in %s on line %d
+NULL
+
+-- Testing array_key_exists() function with less than expected no. of arguments --
+
+Warning: array_key_exists() expects exactly 2 parameters, 1 given in %s on line %d
+NULL
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_object1.phpt b/ext/standard/tests/array/array_key_exists_object1.phpt
new file mode 100644 (file)
index 0000000..6c85f53
--- /dev/null
@@ -0,0 +1,80 @@
+--TEST--
+Test array_key_exists() function : object functionality
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Test basic functionality of array_key_exists() with objects
+ */
+
+echo "*** Testing array_key_exists() : object functionality ***\n";
+
+class myClass {
+       var $var1;
+       var $var2;
+       var $var3;
+       
+       function __construct($a, $b, $c = null) {
+               $this->var1 = $a;
+               $this->var2 = $b;
+               if (!is_null($c)) {
+                       $this->var3 = $c;
+               }
+       }
+}
+
+echo "\n-- Do not assign a value to \$class1->var3 --\n";
+$class1 = new myClass ('a', 'b');
+echo "\$key = var1:\n";
+var_dump(array_key_exists('var1', $class1));
+echo "\$key = var3:\n";
+var_dump(array_key_exists('var3', $class1));
+echo "\$class1:\n";
+var_dump($class1);
+
+echo "\n-- Assign a value to \$class2->var3 --\n";
+$class2 = new myClass('x', 'y', 'z');
+echo "\$key = var3:\n";
+var_dump(array_key_exists('var3', $class2));
+echo "\$class2:\n";
+var_dump($class2);
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : object functionality ***
+
+-- Do not assign a value to $class1->var3 --
+$key = var1:
+bool(true)
+$key = var3:
+bool(true)
+$class1:
+object(myClass)#%d (3) {
+  ["var1"]=>
+  string(1) "a"
+  ["var2"]=>
+  string(1) "b"
+  ["var3"]=>
+  NULL
+}
+
+-- Assign a value to $class2->var3 --
+$key = var3:
+bool(true)
+$class2:
+object(myClass)#%d (3) {
+  ["var1"]=>
+  string(1) "x"
+  ["var2"]=>
+  string(1) "y"
+  ["var3"]=>
+  string(1) "z"
+}
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_object2.phpt b/ext/standard/tests/array/array_key_exists_object2.phpt
new file mode 100644 (file)
index 0000000..1d52a4e
--- /dev/null
@@ -0,0 +1,84 @@
+--TEST--
+Test array_key_exists() function : object functionality - different visibilities
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Pass array_key_exists() an object with private and protected properties
+ */
+
+echo "*** Testing array_key_exists() : object functionality ***\n";
+
+class myClass {
+       public $var1;
+       protected $var2;
+       private $var3;
+       
+       function __construct($a, $b, $c = null) {
+               $this->var1 = $a;
+               $this->var2 = $b;
+               if (!is_null($c)) {
+                       $this->var3 = $c;
+               }
+       }
+}
+
+echo "\n-- Do not assign a value to \$class1->var3 --\n";
+$class1 = new myClass ('a', 'b');
+echo "\$key = var1:\n";
+var_dump(array_key_exists('var1', $class1));
+echo "\$key = var2:\n";
+var_dump(array_key_exists('var2', $class1));
+echo "\$key = var3:\n";
+var_dump(array_key_exists('var3', $class1));
+echo "\$class1:\n";
+var_dump($class1);
+
+echo "\n-- Assign a value to \$class2->var3 --\n";
+$class2 = new myClass('x', 'y', 'z');
+echo "\$key = var3:\n";
+var_dump(array_key_exists('var3', $class2));
+echo "\$class2:\n";
+var_dump($class2);
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : object functionality ***
+
+-- Do not assign a value to $class1->var3 --
+$key = var1:
+bool(true)
+$key = var2:
+bool(false)
+$key = var3:
+bool(false)
+$class1:
+object(myClass)#%d (3) {
+  ["var1"]=>
+  string(1) "a"
+  ["var2":protected]=>
+  string(1) "b"
+  ["var3":"myClass":private]=>
+  NULL
+}
+
+-- Assign a value to $class2->var3 --
+$key = var3:
+bool(false)
+$class2:
+object(myClass)#%d (3) {
+  ["var1"]=>
+  string(1) "x"
+  ["var2":protected]=>
+  string(1) "y"
+  ["var3":"myClass":private]=>
+  string(1) "z"
+}
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_variation1.phpt b/ext/standard/tests/array/array_key_exists_variation1.phpt
new file mode 100644 (file)
index 0000000..2f50d3e
--- /dev/null
@@ -0,0 +1,203 @@
+--TEST--
+Test array_key_exists() function : usage variations - Pass different data types as $key arg
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Pass different data types as $key argument to array_key_exists() to test behaviour
+ */
+
+echo "*** Testing array_key_exists() : usage variations ***\n";
+
+// Initialise function arguments not being substituted
+$search = array ('zero', 'key' => 'val', 'two');
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+// get a class
+class classA
+{
+  public function __toString() {
+    return "key";
+  }
+}
+
+// heredoc string
+$heredoc = <<<EOT
+key
+EOT;
+
+// get a resource variable
+$fp = fopen(__FILE__, "r");
+
+// unexpected values to be passed to $key argument
+$inputs = array(
+
+       // int data
+/*1*/  0,
+       1,
+       12345,
+       -2345,
+
+       // float data
+/*5*/  10.5,
+       -10.5,
+       12.3456789000e10,
+       12.3456789000E-10,
+       .5,
+
+       // null data
+/*10*/ NULL,
+       null,
+
+       // boolean data
+/*12*/ true,
+       false,
+       TRUE,
+       FALSE,
+       
+       // empty data
+/*16*/ "",
+       '',
+       array(),
+
+       // string data
+/*19*/ "key",
+       'key',
+       $heredoc,
+       
+       // object data
+/*22*/ new classA(),
+
+       // undefined data
+/*23*/ @$undefined_var,
+
+       // unset data
+/*24*/ @$unset_var,
+
+       // resource variable
+/*25*/ $fp
+);
+
+// loop through each element of $inputs to check the behavior of array_key_exists()
+$iterator = 1;
+foreach($inputs as $input) {
+  echo "\n-- Iteration $iterator --\n";
+  var_dump( array_key_exists($input, $search) );
+  $iterator++;
+};
+
+fclose($fp);
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : usage variations ***
+
+-- Iteration 1 --
+bool(true)
+
+-- Iteration 2 --
+bool(true)
+
+-- Iteration 3 --
+bool(false)
+
+-- Iteration 4 --
+bool(false)
+
+-- Iteration 5 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 6 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 7 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 8 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 9 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 10 --
+bool(false)
+
+-- Iteration 11 --
+bool(false)
+
+-- Iteration 12 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 13 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 14 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 15 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 16 --
+bool(false)
+
+-- Iteration 17 --
+bool(false)
+
+-- Iteration 18 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 19 --
+bool(true)
+
+-- Iteration 20 --
+bool(true)
+
+-- Iteration 21 --
+bool(true)
+
+-- Iteration 22 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+
+-- Iteration 23 --
+bool(false)
+
+-- Iteration 24 --
+bool(false)
+
+-- Iteration 25 --
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_variation2.phpt b/ext/standard/tests/array/array_key_exists_variation2.phpt
new file mode 100644 (file)
index 0000000..0804006
--- /dev/null
@@ -0,0 +1,225 @@
+--TEST--
+Test array_key_exists() function : usage variations - Pass differnt data types to $search arg
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Pass different data types as $search argument to array_key_exists() to test behaviour
+ */
+
+echo "*** Testing array_key_exists() : usage variations ***\n";
+
+// Initialise function arguments not being substituted
+$key = 'val';
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+// get a class
+class classA
+{
+  public function __toString() {
+    return "Class A object";
+  }
+}
+
+// heredoc string
+$heredoc = <<<EOT
+hello world
+EOT;
+
+// get a resource variable
+$fp = fopen(__FILE__, "r");
+
+// unexpected values to be passed to $search argument
+$inputs = array(
+
+       // int data
+/*1*/  0,
+       1,
+       12345,
+       -2345,
+
+       // float data
+/*5*/  10.5,
+       -10.5,
+       12.3456789000e10,
+       12.3456789000E-10,
+       .5,
+
+       // null data
+/*10*/ NULL,
+       null,
+
+       // boolean data
+/*12*/ true,
+       false,
+       TRUE,
+       FALSE,
+       
+       // empty data
+/*16*/ "",
+       '',
+       array(),
+
+       // string data
+/*19*/ "string",
+       'string',
+       $heredoc,
+       
+       // object data
+/*22*/ new classA(),
+
+       // undefined data
+/*23*/ @$undefined_var,
+
+       // unset data
+/*24*/ @$unset_var,
+
+       // resource variable
+/*25*/ $fp
+);
+
+// loop through each element of $inputs to check the behavior of array_key_exists()
+$iterator = 1;
+foreach($inputs as $input) {
+  echo "\n-- Iteration $iterator --\n";
+  var_dump( array_key_exists($key, $input) );
+  $iterator++;
+};
+
+fclose($fp);
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : usage variations ***
+
+-- Iteration 1 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 2 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 3 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 4 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 5 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 6 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 7 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 8 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 9 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 10 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 11 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 12 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 13 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 14 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 15 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 16 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 17 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 18 --
+bool(false)
+
+-- Iteration 19 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 20 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 21 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 22 --
+bool(false)
+
+-- Iteration 23 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 24 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+
+-- Iteration 25 --
+
+Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d
+bool(false)
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_variation3.phpt b/ext/standard/tests/array/array_key_exists_variation3.phpt
new file mode 100644 (file)
index 0000000..e8a52a7
--- /dev/null
@@ -0,0 +1,60 @@
+--TEST--
+Test array_key_exists() function : usage variations - floats and casting to ints
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Pass floats as $key argument, then cast float values
+ * to integers and pass as $key argument
+ */
+
+echo "*** Testing array_key_exists() : usage variations ***\n";
+
+$keys = array(1.2345678900E-10, 1.00000000000001, 1.99999999999999);
+
+$search = array ('zero', 'one', 'two');
+
+$iterator = 1;
+foreach($keys as $key) {
+       echo "\n-- Iteration $iterator --\n";
+       echo "Pass float as \$key:\n";
+       var_dump(array_key_exists($key, $search));
+       echo "Cast float to int:\n";
+       var_dump(array_key_exists((int)$key, $search));
+}
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : usage variations ***
+
+-- Iteration 1 --
+Pass float as $key:
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+Cast float to int:
+bool(true)
+
+-- Iteration 1 --
+Pass float as $key:
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+Cast float to int:
+bool(true)
+
+-- Iteration 1 --
+Pass float as $key:
+
+Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
+bool(false)
+Cast float to int:
+bool(true)
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_variation4.phpt b/ext/standard/tests/array/array_key_exists_variation4.phpt
new file mode 100644 (file)
index 0000000..349396b
--- /dev/null
@@ -0,0 +1,44 @@
+--TEST--
+Test array_key_exists() function : usage variations - referenced variables
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Pass referenced variables as arguments to array_key_exists() to test behaviour
+ */
+
+echo "*** Testing array_key_exists() : usage variations ***\n";
+
+$array = array('one' => 1, 'two' => 2, 'three' => 3);
+
+echo "\n-- \$search is a reference to \$array --\n";
+$search = &$array;
+var_dump(array_key_exists('one', $search));
+
+echo "\n-- \$key is a referenced variable --\n";
+$key = 'two';
+var_dump(array_key_exists(&$key, $array));
+
+echo "\n-- Both arguments are referenced variables --\n";
+var_dump(array_key_exists(&$key, &$array));
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : usage variations ***
+
+-- $search is a reference to $array --
+bool(true)
+
+-- $key is a referenced variable --
+bool(true)
+
+-- Both arguments are referenced variables --
+bool(true)
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_variation5.phpt b/ext/standard/tests/array/array_key_exists_variation5.phpt
new file mode 100644 (file)
index 0000000..9c15759
--- /dev/null
@@ -0,0 +1,39 @@
+--TEST--
+Test array_key_exists() function : usage variations - multidimensional arrays
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Test how array_key_exists() behaves with multi-dimensional arrays
+ */
+
+echo "*** Testing array_key_exists() : usage variations ***\n";
+
+$multi_array = array ('zero' => 'val1', 
+                      'one' => 'val2', 
+                      'sub1' => array (1, 2, 3));
+
+echo "\n-- Attempt to match key in sub-array --\n";
+// this key is in the sub-array
+var_dump(array_key_exists(0, $multi_array));
+
+echo "\n-- \$search arg points to sub-array --\n";
+var_dump(array_key_exists(0, $multi_array['sub1']));
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : usage variations ***
+
+-- Attempt to match key in sub-array --
+bool(false)
+
+-- $search arg points to sub-array --
+bool(true)
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_variation6.phpt b/ext/standard/tests/array/array_key_exists_variation6.phpt
new file mode 100644 (file)
index 0000000..d19e58e
--- /dev/null
@@ -0,0 +1,96 @@
+--TEST--
+Test array_key_exists() function : usage variations -  equality test for certain data types
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Pass certain data types that can be taken as a key in an array 
+ * and test whether array_key_exists(() thinks they are equal and therefore
+ * returns true when searching for them
+ */
+
+echo "*** Testing array_key_exists() : usage variations ***\n";
+
+$unset = 10;
+unset($unset);
+$array = array ('null' => null, 
+                'NULL' => NULL, 
+                'empty single quoted string' => '', 
+                "empty double quoted string" => "", 
+                'undefined variable' => @$undefined,
+                'unset variable' => @$unset);
+
+//iterate through original array
+foreach($array as $name => $input) {
+       $iterator = 1;
+       echo "\n-- Key in \$search array is : $name --\n";
+       $search[$input] = 'test';
+       
+       //iterate through array again to see which values are considered equal
+       foreach($array as $key) {
+               echo "Iteration $iterator:  ";
+               var_dump(array_key_exists($key, $search));
+               $iterator++;
+       }
+       $search = null;
+}
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : usage variations ***
+
+-- Key in $search array is : null --
+Iteration 1:  bool(true)
+Iteration 2:  bool(true)
+Iteration 3:  bool(true)
+Iteration 4:  bool(true)
+Iteration 5:  bool(true)
+Iteration 6:  bool(true)
+
+-- Key in $search array is : NULL --
+Iteration 1:  bool(true)
+Iteration 2:  bool(true)
+Iteration 3:  bool(true)
+Iteration 4:  bool(true)
+Iteration 5:  bool(true)
+Iteration 6:  bool(true)
+
+-- Key in $search array is : empty single quoted string --
+Iteration 1:  bool(true)
+Iteration 2:  bool(true)
+Iteration 3:  bool(true)
+Iteration 4:  bool(true)
+Iteration 5:  bool(true)
+Iteration 6:  bool(true)
+
+-- Key in $search array is : empty double quoted string --
+Iteration 1:  bool(true)
+Iteration 2:  bool(true)
+Iteration 3:  bool(true)
+Iteration 4:  bool(true)
+Iteration 5:  bool(true)
+Iteration 6:  bool(true)
+
+-- Key in $search array is : undefined variable --
+Iteration 1:  bool(true)
+Iteration 2:  bool(true)
+Iteration 3:  bool(true)
+Iteration 4:  bool(true)
+Iteration 5:  bool(true)
+Iteration 6:  bool(true)
+
+-- Key in $search array is : unset variable --
+Iteration 1:  bool(true)
+Iteration 2:  bool(true)
+Iteration 3:  bool(true)
+Iteration 4:  bool(true)
+Iteration 5:  bool(true)
+Iteration 6:  bool(true)
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_variation7.phpt b/ext/standard/tests/array/array_key_exists_variation7.phpt
new file mode 100644 (file)
index 0000000..845c1e5
--- /dev/null
@@ -0,0 +1,36 @@
+--TEST--
+Test array_key_exists() function : usage variations - position of internal array pointer
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Check the position of the internal array pointer after calling array_key_exists()
+ */
+
+echo "*** Testing array_key_exists() : usage variations ***\n";
+
+$input = array ('one' => 'un', 'two' => 'deux', 'three' => 'trois');
+
+echo "\n-- Call array_key_exists() --\n";
+var_dump($result = array_key_exists('one', $input));
+
+echo "\n-- Position of Internal Pointer in Original Array: --\n";
+echo key($input) . " => " . current ($input) . "\n";
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : usage variations ***
+
+-- Call array_key_exists() --
+bool(true)
+
+-- Position of Internal Pointer in Original Array: --
+one => un
+Done
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_key_exists_variation8.phpt b/ext/standard/tests/array/array_key_exists_variation8.phpt
new file mode 100644 (file)
index 0000000..d5bad62
--- /dev/null
@@ -0,0 +1,547 @@
+--TEST--
+Test array_key_exists() function : usage variations - array keys are different data types
+--FILE--
+<?php
+/* Prototype  : bool array_key_exists(mixed $key, array $search)
+ * Description: Checks if the given key or index exists in the array 
+ * Source code: ext/standard/array.c
+ * Alias to functions: key_exists
+ */
+
+/*
+ * Pass an array where the keys are different data types as the $search argument
+ * then pass many different data types as $key argument to test where array_key_exist()
+ * returns true.
+ */
+
+echo "*** Testing array_key_exists() : usage variations ***\n";
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+// heredoc string
+$heredoc = <<<EOT
+string
+EOT;
+
+// different data types to be iterated over
+$inputs = array(
+
+       // int data
+/*1*/  'int' => array(
+       0 => 'zero',
+       1 => 'one',
+       12345 => 'positive',
+       -2345 => 'negative',
+       ),
+
+       // float data
+/*2*/  'float' => array(
+       10.5 => 'positive',
+       -10.5 => 'negative',
+       .5 => 'half',
+       ),
+       
+       'extreme floats' => array(
+       12.3456789000e10 => 'large',
+       12.3456789000E-10 => 'small',
+       ),
+
+       // null data
+/*3*/ 'null uppercase' => array(
+       NULL => 'null 1',
+       ), 
+       'null lowercase' => array(
+       null => 'null 2',
+       ),
+
+       // boolean data
+/*4*/ 'bool lowercase' => array(
+       true => 'lowert',
+       false => 'lowerf',
+       ),
+       'bool uppercase' => array(
+       TRUE => 'uppert',
+       FALSE => 'upperf',
+       ),
+       
+       // empty data
+/*5*/ 'empty double quotes' => array(
+       "" => 'emptyd',
+       ),
+       'empty single quotes' => array(
+       '' => 'emptys',
+       ),
+
+       // string data
+/*6*/ 'string' => array(
+       "stringd" => 'stringd',
+       'strings' => 'strings',
+       $heredoc => 'stringh',
+       ),
+
+       // undefined data
+/*8*/ 'undefined' => array(
+       @$undefined_var => 'undefined',
+       ),
+
+       // unset data
+/*9*/ 'unset' => array(
+       @$unset_var => 'unset',
+       ),
+);
+
+// loop through each element of $inputs to check the behavior of array_key_exists()
+$iterator = 1;
+foreach($inputs as $type => $input) {
+       echo "\n-- Iteration $iterator: $type data --\n";
+
+       //iterate over again to get all different key values
+       foreach ($inputs as $new_type => $new_input) {
+               echo "-- \$key arguments are $new_type data:\n";
+               foreach ($new_input as $key => $search) {
+                       var_dump(array_key_exists($key, $input));
+               }
+       }
+       $iterator++;
+};
+
+echo "Done";
+?>
+
+--EXPECTF--
+*** Testing array_key_exists() : usage variations ***
+
+-- Iteration 1: int data --
+-- $key arguments are int data:
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(true)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(true)
+-- $key arguments are null uppercase data:
+bool(false)
+-- $key arguments are null lowercase data:
+bool(false)
+-- $key arguments are bool lowercase data:
+bool(true)
+bool(true)
+-- $key arguments are bool uppercase data:
+bool(true)
+bool(true)
+-- $key arguments are empty double quotes data:
+bool(false)
+-- $key arguments are empty single quotes data:
+bool(false)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(false)
+-- $key arguments are unset data:
+bool(false)
+
+-- Iteration 2: float data --
+-- $key arguments are int data:
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(true)
+bool(true)
+bool(true)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(true)
+-- $key arguments are null uppercase data:
+bool(false)
+-- $key arguments are null lowercase data:
+bool(false)
+-- $key arguments are bool lowercase data:
+bool(false)
+bool(true)
+-- $key arguments are bool uppercase data:
+bool(false)
+bool(true)
+-- $key arguments are empty double quotes data:
+bool(false)
+-- $key arguments are empty single quotes data:
+bool(false)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(false)
+-- $key arguments are unset data:
+bool(false)
+
+-- Iteration 3: extreme floats data --
+-- $key arguments are int data:
+bool(true)
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(true)
+-- $key arguments are extreme floats data:
+bool(true)
+bool(true)
+-- $key arguments are null uppercase data:
+bool(false)
+-- $key arguments are null lowercase data:
+bool(false)
+-- $key arguments are bool lowercase data:
+bool(false)
+bool(true)
+-- $key arguments are bool uppercase data:
+bool(false)
+bool(true)
+-- $key arguments are empty double quotes data:
+bool(false)
+-- $key arguments are empty single quotes data:
+bool(false)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(false)
+-- $key arguments are unset data:
+bool(false)
+
+-- Iteration 4: null uppercase data --
+-- $key arguments are int data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(false)
+-- $key arguments are null uppercase data:
+bool(true)
+-- $key arguments are null lowercase data:
+bool(true)
+-- $key arguments are bool lowercase data:
+bool(false)
+bool(false)
+-- $key arguments are bool uppercase data:
+bool(false)
+bool(false)
+-- $key arguments are empty double quotes data:
+bool(true)
+-- $key arguments are empty single quotes data:
+bool(true)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(true)
+-- $key arguments are unset data:
+bool(true)
+
+-- Iteration 5: null lowercase data --
+-- $key arguments are int data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(false)
+-- $key arguments are null uppercase data:
+bool(true)
+-- $key arguments are null lowercase data:
+bool(true)
+-- $key arguments are bool lowercase data:
+bool(false)
+bool(false)
+-- $key arguments are bool uppercase data:
+bool(false)
+bool(false)
+-- $key arguments are empty double quotes data:
+bool(true)
+-- $key arguments are empty single quotes data:
+bool(true)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(true)
+-- $key arguments are unset data:
+bool(true)
+
+-- Iteration 6: bool lowercase data --
+-- $key arguments are int data:
+bool(true)
+bool(true)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(true)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(true)
+-- $key arguments are null uppercase data:
+bool(false)
+-- $key arguments are null lowercase data:
+bool(false)
+-- $key arguments are bool lowercase data:
+bool(true)
+bool(true)
+-- $key arguments are bool uppercase data:
+bool(true)
+bool(true)
+-- $key arguments are empty double quotes data:
+bool(false)
+-- $key arguments are empty single quotes data:
+bool(false)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(false)
+-- $key arguments are unset data:
+bool(false)
+
+-- Iteration 7: bool uppercase data --
+-- $key arguments are int data:
+bool(true)
+bool(true)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(true)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(true)
+-- $key arguments are null uppercase data:
+bool(false)
+-- $key arguments are null lowercase data:
+bool(false)
+-- $key arguments are bool lowercase data:
+bool(true)
+bool(true)
+-- $key arguments are bool uppercase data:
+bool(true)
+bool(true)
+-- $key arguments are empty double quotes data:
+bool(false)
+-- $key arguments are empty single quotes data:
+bool(false)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(false)
+-- $key arguments are unset data:
+bool(false)
+
+-- Iteration 8: empty double quotes data --
+-- $key arguments are int data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(false)
+-- $key arguments are null uppercase data:
+bool(true)
+-- $key arguments are null lowercase data:
+bool(true)
+-- $key arguments are bool lowercase data:
+bool(false)
+bool(false)
+-- $key arguments are bool uppercase data:
+bool(false)
+bool(false)
+-- $key arguments are empty double quotes data:
+bool(true)
+-- $key arguments are empty single quotes data:
+bool(true)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(true)
+-- $key arguments are unset data:
+bool(true)
+
+-- Iteration 9: empty single quotes data --
+-- $key arguments are int data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(false)
+-- $key arguments are null uppercase data:
+bool(true)
+-- $key arguments are null lowercase data:
+bool(true)
+-- $key arguments are bool lowercase data:
+bool(false)
+bool(false)
+-- $key arguments are bool uppercase data:
+bool(false)
+bool(false)
+-- $key arguments are empty double quotes data:
+bool(true)
+-- $key arguments are empty single quotes data:
+bool(true)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(true)
+-- $key arguments are unset data:
+bool(true)
+
+-- Iteration 10: string data --
+-- $key arguments are int data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(false)
+-- $key arguments are null uppercase data:
+bool(false)
+-- $key arguments are null lowercase data:
+bool(false)
+-- $key arguments are bool lowercase data:
+bool(false)
+bool(false)
+-- $key arguments are bool uppercase data:
+bool(false)
+bool(false)
+-- $key arguments are empty double quotes data:
+bool(false)
+-- $key arguments are empty single quotes data:
+bool(false)
+-- $key arguments are string data:
+bool(true)
+bool(true)
+bool(true)
+-- $key arguments are undefined data:
+bool(false)
+-- $key arguments are unset data:
+bool(false)
+
+-- Iteration 11: undefined data --
+-- $key arguments are int data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(false)
+-- $key arguments are null uppercase data:
+bool(true)
+-- $key arguments are null lowercase data:
+bool(true)
+-- $key arguments are bool lowercase data:
+bool(false)
+bool(false)
+-- $key arguments are bool uppercase data:
+bool(false)
+bool(false)
+-- $key arguments are empty double quotes data:
+bool(true)
+-- $key arguments are empty single quotes data:
+bool(true)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(true)
+-- $key arguments are unset data:
+bool(true)
+
+-- Iteration 12: unset data --
+-- $key arguments are int data:
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are float data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are extreme floats data:
+bool(false)
+bool(false)
+-- $key arguments are null uppercase data:
+bool(true)
+-- $key arguments are null lowercase data:
+bool(true)
+-- $key arguments are bool lowercase data:
+bool(false)
+bool(false)
+-- $key arguments are bool uppercase data:
+bool(false)
+bool(false)
+-- $key arguments are empty double quotes data:
+bool(true)
+-- $key arguments are empty single quotes data:
+bool(true)
+-- $key arguments are string data:
+bool(false)
+bool(false)
+bool(false)
+-- $key arguments are undefined data:
+bool(true)
+-- $key arguments are unset data:
+bool(true)
+Done
\ No newline at end of file