Skip to content

Commit

Permalink
Add tests for nullsafe operator on delayed oplines
Browse files Browse the repository at this point in the history
  • Loading branch information
iluuu1994 authored and nikic committed Aug 31, 2020
1 parent 4163923 commit 70300f6
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Zend/tests/nullsafe_operator/034.phpt
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions Zend/tests/nullsafe_operator/035.phpt
Original file line number Diff line number Diff line change
@@ -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"
28 changes: 28 additions & 0 deletions Zend/tests/nullsafe_operator/036.phpt
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 15 additions & 0 deletions Zend/tests/nullsafe_operator/037.phpt
Original file line number Diff line number Diff line change
@@ -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"
12 changes: 12 additions & 0 deletions Zend/tests/nullsafe_operator/038.phpt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 70300f6

Please sign in to comment.