Skip to content

Commit 70300f6

Browse files
iluuu1994nikic
authored andcommitted
Add tests for nullsafe operator on delayed oplines
1 parent 4163923 commit 70300f6

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

Zend/tests/nullsafe_operator/034.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test nullsafe operator on delayed dim
3+
--FILE--
4+
<?php
5+
6+
$arr = [
7+
'foo' => null,
8+
'bar' => [
9+
'baz' => null,
10+
],
11+
];
12+
13+
var_dump($arr['foo']?->something);
14+
var_dump($arr['invalid']?->something);
15+
16+
var_dump($arr['bar']['baz']?->something);
17+
var_dump($arr['bar']['invalid']?->something);
18+
19+
?>
20+
--EXPECTF--
21+
NULL
22+
23+
Warning: Undefined array key "invalid" in %s.php on line 11
24+
NULL
25+
NULL
26+
27+
Warning: Undefined array key "invalid" in %s.php on line 14
28+
NULL

Zend/tests/nullsafe_operator/035.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Test nullsafe operator on delayed var
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public ?Bar $bar;
8+
}
9+
10+
class Bar {
11+
public string $baz;
12+
}
13+
14+
$foo = new Foo();
15+
16+
$foo->bar = null;
17+
var_dump($foo->bar?->baz);
18+
19+
$bar = new Bar();
20+
$bar->baz = 'baz';
21+
$foo->bar = $bar;
22+
var_dump($foo->bar?->baz);
23+
24+
?>
25+
--EXPECT--
26+
NULL
27+
string(3) "baz"

Zend/tests/nullsafe_operator/036.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test nullsafe method call on delayed var
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public ?Bar $bar;
8+
}
9+
10+
class Bar {
11+
public function baz() {
12+
return 'baz';
13+
}
14+
}
15+
16+
$foo = new Foo();
17+
18+
$foo->bar = null;
19+
var_dump($foo->bar?->baz());
20+
21+
$bar = new Bar();
22+
$foo->bar = $bar;
23+
var_dump($foo->bar?->baz());
24+
25+
?>
26+
--EXPECT--
27+
NULL
28+
string(3) "baz"

Zend/tests/nullsafe_operator/037.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Test nullsafe operator in nested delayed dims
3+
--FILE--
4+
<?php
5+
6+
$foo = new stdClass();
7+
$foo->bar = 'bar';
8+
9+
$array = ['foo' => ['bar' => 'baz']];
10+
11+
var_dump($array['foo'][$foo?->bar]);
12+
13+
?>
14+
--EXPECT--
15+
string(3) "baz"

Zend/tests/nullsafe_operator/038.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test nullsafe operator in nested delayed dims 2
3+
--FILE--
4+
<?php
5+
6+
$foo = (object) ['bar' => 0];
7+
$array = [[null]];
8+
var_dump($array[0][$foo->bar]?->baz);
9+
10+
?>
11+
--EXPECT--
12+
NULL

0 commit comments

Comments
 (0)