Skip to content

Commit c56f6e3

Browse files
Import Zend Engine tests for list()
1 parent 3afd58a commit c56f6e3

23 files changed

+841
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
"Nested" list()
3+
--FILE--
4+
<?php
5+
6+
list($a, list($b)) = array(new stdclass, array(new stdclass));
7+
var_dump($a, $b);
8+
9+
?>
10+
--EXPECT--
11+
object(stdClass)#1 (0) {
12+
}
13+
object(stdClass)#2 (0) {
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Testing full-reference on list()
3+
--FILE--
4+
<?php
5+
6+
error_reporting(E_ALL);
7+
8+
$a = new stdclass;
9+
$b =& $a;
10+
11+
list($a, list($b)) = array($a, array($b));
12+
var_dump($a, $b, $a === $b);
13+
14+
?>
15+
--EXPECT--
16+
object(stdClass)#1 (0) {
17+
}
18+
object(stdClass)#1 (0) {
19+
}
20+
bool(true)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
list() with non-array
3+
--FILE--
4+
<?php
5+
6+
list($a) = NULL;
7+
8+
list($b) = 1;
9+
10+
list($c) = 1.;
11+
12+
list($d) = 'foo';
13+
14+
list($e) = print '';
15+
16+
var_dump($a, $b, $c, $d, $e);
17+
18+
?>
19+
--EXPECT--
20+
NULL
21+
NULL
22+
NULL
23+
NULL
24+
NULL
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
list() with array reference
3+
--FILE--
4+
<?php
5+
6+
$arr = array(2, 1);
7+
$b =& $arr;
8+
9+
list(,$a) = $b;
10+
11+
var_dump($a, $b);
12+
13+
?>
14+
--EXPECT--
15+
int(1)
16+
array(2) {
17+
[0]=>
18+
int(2)
19+
[1]=>
20+
int(1)
21+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
Testing list() with several variables
3+
--FILE--
4+
<?php
5+
6+
$str = "foo";
7+
8+
list($a, $b, $c) = $str;
9+
10+
var_dump($a, $b, $c);
11+
12+
print "----\n";
13+
14+
$int = 1;
15+
16+
list($a, $b, $c) = $int;
17+
18+
var_dump($a, $b, $c);
19+
20+
print "----\n";
21+
22+
$obj = new stdClass;
23+
24+
list($a, $b, $c) = $obj;
25+
26+
var_dump($a, $b, $c);
27+
28+
print "----\n";
29+
30+
$arr = array(1, 2, 3);
31+
32+
list($a, $b, $c) = $arr;
33+
34+
var_dump($a, $b, $c);
35+
36+
?>
37+
--EXPECTF--
38+
NULL
39+
NULL
40+
NULL
41+
----
42+
NULL
43+
NULL
44+
NULL
45+
----
46+
47+
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in %s:%d
48+
Stack trace:
49+
#0 {main}
50+
thrown in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Testing nested list() with empty array
3+
--FILE--
4+
<?php
5+
6+
list($a, list($b, list(list($d)))) = array();
7+
8+
?>
9+
--EXPECTF--
10+
Notice: Undefined offset: 0 in %s on line %d
11+
12+
Notice: Undefined offset: 1 in %s on line %d
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Using lambda with list()
3+
--FILE--
4+
<?php
5+
6+
list($x, $y) = function() { };
7+
8+
var_dump($x, $y);
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Uncaught Error: Cannot use object of type Closure as array in %slist_007.php:3
13+
Stack trace:
14+
#0 {main}
15+
thrown in %slist_007.php on line 3
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
list() can be used to destructure to string offsets, __set and ArrayAccess::offsetSet
3+
--FILE--
4+
<?php
5+
6+
class Obj {
7+
public $values = [];
8+
public function __set($name, $value) {
9+
$this->values[$name] = $value;
10+
}
11+
}
12+
13+
class Arr implements ArrayAccess {
14+
public $values = [];
15+
public function offsetSet($name, $value) {
16+
$this->values[$name] = $value;
17+
}
18+
public function offsetGet($name) {}
19+
public function offsetExists($name) {}
20+
public function offsetUnset($name) {}
21+
}
22+
23+
$str = 'ab';
24+
list($str[0], $str[1]) = ['x', 'y'];
25+
var_dump($str);
26+
27+
$obj = new Obj;
28+
list($obj->foo, $obj->bar) = ['foo', 'bar'];
29+
var_dump($obj->values);
30+
31+
$arr = new Arr;
32+
list($arr['foo'], $arr['bar']) = ['foo', 'bar'];
33+
var_dump($arr->values);
34+
35+
?>
36+
--EXPECT--
37+
string(2) "xy"
38+
array(2) {
39+
["foo"]=>
40+
string(3) "foo"
41+
["bar"]=>
42+
string(3) "bar"
43+
}
44+
array(2) {
45+
["foo"]=>
46+
string(3) "foo"
47+
["bar"]=>
48+
string(3) "bar"
49+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Empty list() assignments are not allowed
3+
--FILE--
4+
<?php
5+
6+
list(,,,,,,,,,,) = [];
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use empty list in %s on line %d
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--TEST--
2+
list() with keys
3+
--FILE--
4+
<?php
5+
6+
$antonyms = [
7+
"good" => "bad",
8+
"happy" => "sad",
9+
];
10+
11+
list("good" => $good_antonym, "happy" => $happy_antonym) = $antonyms;
12+
var_dump($good_antonym, $happy_antonym);
13+
14+
echo PHP_EOL;
15+
16+
$powersOfTwo = [
17+
1 => 2,
18+
2 => 4,
19+
3 => 8
20+
];
21+
22+
list(1 => $two_1, 2 => $two_2, 3 => $two_3) = $powersOfTwo;
23+
var_dump($two_1, $two_2, $two_3);
24+
25+
echo PHP_EOL;
26+
27+
$contrivedMixedKeyTypesExample = [
28+
7 => "the best PHP version",
29+
"elePHPant" => "the cutest mascot"
30+
];
31+
32+
list(7 => $seven, "elePHPant" => $elePHPant) = $contrivedMixedKeyTypesExample;
33+
var_dump($seven, $elePHPant);
34+
35+
echo PHP_EOL;
36+
37+
$allTogetherNow = [
38+
"antonyms" => $antonyms,
39+
"powersOfTwo" => $powersOfTwo,
40+
"contrivedMixedKeyTypesExample" => $contrivedMixedKeyTypesExample
41+
];
42+
43+
list(
44+
"antonyms" => list("good" => $good_antonym, "happy" => $happy_antonym),
45+
"powersOfTwo" => list(1 => $two_1, 2 => $two_2, 3 => $two_3),
46+
"contrivedMixedKeyTypesExample" => list(7 => $seven, "elePHPant" => $elePHPant)
47+
) = $allTogetherNow;
48+
49+
var_dump($good_antonym, $happy_antonym);
50+
var_dump($two_1, $two_2, $two_3);
51+
var_dump($seven, $elePHPant);
52+
53+
?>
54+
--EXPECT--
55+
string(3) "bad"
56+
string(3) "sad"
57+
58+
int(2)
59+
int(4)
60+
int(8)
61+
62+
string(20) "the best PHP version"
63+
string(17) "the cutest mascot"
64+
65+
string(3) "bad"
66+
string(3) "sad"
67+
int(2)
68+
int(4)
69+
int(8)
70+
string(20) "the best PHP version"
71+
string(17) "the cutest mascot"

0 commit comments

Comments
 (0)