Add tests for nullsafe operator on delayed oplines

This commit is contained in:
Ilija Tovilo 2020-08-15 17:43:34 +02:00 committed by Nikita Popov
parent 4163923e83
commit 70300f6c9d
5 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,28 @@
--TEST--
Test nullsafe operator on delayed dim
--FILE--
<?php
$arr = [
'foo' => null,
'bar' => [
'baz' => null,
],
];
var_dump($arr['foo']?->something);
var_dump($arr['invalid']?->something);
var_dump($arr['bar']['baz']?->something);
var_dump($arr['bar']['invalid']?->something);
?>
--EXPECTF--
NULL
Warning: Undefined array key "invalid" in %s.php on line 11
NULL
NULL
Warning: Undefined array key "invalid" in %s.php on line 14
NULL

View File

@ -0,0 +1,27 @@
--TEST--
Test nullsafe operator on delayed var
--FILE--
<?php
class Foo {
public ?Bar $bar;
}
class Bar {
public string $baz;
}
$foo = new Foo();
$foo->bar = null;
var_dump($foo->bar?->baz);
$bar = new Bar();
$bar->baz = 'baz';
$foo->bar = $bar;
var_dump($foo->bar?->baz);
?>
--EXPECT--
NULL
string(3) "baz"

View File

@ -0,0 +1,28 @@
--TEST--
Test nullsafe method call on delayed var
--FILE--
<?php
class Foo {
public ?Bar $bar;
}
class Bar {
public function baz() {
return 'baz';
}
}
$foo = new Foo();
$foo->bar = null;
var_dump($foo->bar?->baz());
$bar = new Bar();
$foo->bar = $bar;
var_dump($foo->bar?->baz());
?>
--EXPECT--
NULL
string(3) "baz"

View File

@ -0,0 +1,15 @@
--TEST--
Test nullsafe operator in nested delayed dims
--FILE--
<?php
$foo = new stdClass();
$foo->bar = 'bar';
$array = ['foo' => ['bar' => 'baz']];
var_dump($array['foo'][$foo?->bar]);
?>
--EXPECT--
string(3) "baz"

View File

@ -0,0 +1,12 @@
--TEST--
Test nullsafe operator in nested delayed dims 2
--FILE--
<?php
$foo = (object) ['bar' => 0];
$array = [[null]];
var_dump($array[0][$foo->bar]?->baz);
?>
--EXPECT--
NULL