1--TEST--
2Test array_rand() function : usage variations - unexpected values for 'input' parameter
3--FILE--
4<?php
5/* Prototype  : mixed array_rand(array input [, int num_req])
6 * Description: Return key/keys for random entry/entries in the array
7 * Source code: ext/standard/array.c
8*/
9
10/*
11* Test array_rand() with different types of values other than arrays passed to the 'input' parameter
12* to see that function works with unexpeced data and generates warning message as required.
13*/
14
15echo "*** Testing array_rand() : unexpected values for 'input' parameter ***\n";
16
17// Initialise function arguments
18$num_req = 10;
19
20//get an unset variable
21$unset_var = 10;
22unset ($unset_var);
23
24//get a resource variable
25$fp = fopen(__FILE__, "r");
26
27//define a class
28class test
29{
30  var $t = 10;
31  function __toString()
32  {
33    return "object";
34  }
35}
36
37//array of different values for 'input' parameter
38$values = array(
39
40        // int data
41/*1*/   0,
42        1,
43        12345,
44        -2345,
45
46        // float data
47/*5*/   10.5,
48        -10.5,
49        12.3456789000e10,
50        12.3456789000E-10,
51        .5,
52
53        // null data
54/*10*/  NULL,
55        null,
56
57        // boolean data
58/*12*/  true,
59        false,
60        TRUE,
61        FALSE,
62
63        // empty data
64/*16*/  "",
65        '',
66
67        // string data
68/*18*/  "string",
69        'string',
70
71        // object data
72/*20*/  new test(),
73
74        // resource data
75/*21*/  $fp,
76
77        // undefined data
78/*22*/  @$undefined_var,
79
80        // unset data
81/*23*/  @$unset_var,
82);
83
84/* loop through each element of the array to test array_rand() function
85 * for different values for 'input' argument
86*/
87$count = 1;
88foreach($values as $value) {
89  echo "\n-- Iteration $count --\n";
90  var_dump( array_rand($value,$num_req) );
91  $count++;
92};
93
94// closing the resource
95fclose($fp);
96
97echo "Done";
98?>
99--EXPECTF--
100*** Testing array_rand() : unexpected values for 'input' parameter ***
101
102-- Iteration 1 --
103
104Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d
105NULL
106
107-- Iteration 2 --
108
109Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d
110NULL
111
112-- Iteration 3 --
113
114Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d
115NULL
116
117-- Iteration 4 --
118
119Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d
120NULL
121
122-- Iteration 5 --
123
124Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d
125NULL
126
127-- Iteration 6 --
128
129Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d
130NULL
131
132-- Iteration 7 --
133
134Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d
135NULL
136
137-- Iteration 8 --
138
139Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d
140NULL
141
142-- Iteration 9 --
143
144Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d
145NULL
146
147-- Iteration 10 --
148
149Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d
150NULL
151
152-- Iteration 11 --
153
154Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d
155NULL
156
157-- Iteration 12 --
158
159Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d
160NULL
161
162-- Iteration 13 --
163
164Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d
165NULL
166
167-- Iteration 14 --
168
169Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d
170NULL
171
172-- Iteration 15 --
173
174Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d
175NULL
176
177-- Iteration 16 --
178
179Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d
180NULL
181
182-- Iteration 17 --
183
184Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d
185NULL
186
187-- Iteration 18 --
188
189Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d
190NULL
191
192-- Iteration 19 --
193
194Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d
195NULL
196
197-- Iteration 20 --
198
199Warning: array_rand() expects parameter 1 to be array, object given in %s on line %d
200NULL
201
202-- Iteration 21 --
203
204Warning: array_rand() expects parameter 1 to be array, resource given in %s on line %d
205NULL
206
207-- Iteration 22 --
208
209Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d
210NULL
211
212-- Iteration 23 --
213
214Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d
215NULL
216Done
217
218