Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Zend/tests/return_types/001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Returned nothing, expected array

--FILE--
<?php
function test1() : array {
}

test1();

--EXPECTF--
Catchable fatal error: Return value of test1() must be of the type array, none returned in %s on line %d
13 changes: 13 additions & 0 deletions Zend/tests/return_types/002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Returned null, expected array

--FILE--
<?php
function test1() : array {
return null;
}

test1();

--EXPECTF--
Catchable fatal error: Return value of test1() must be of the type array, null returned in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/return_types/003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Returned 1, expected array

--FILE--
<?php
function test1() : array {
return 1;
}
test1();

--EXPECTF--
Catchable fatal error: Return value of test1() must be of the type array, integer returned in %s on line %d
13 changes: 13 additions & 0 deletions Zend/tests/return_types/004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Returned string, expected array

--FILE--
<?php
function test1() : array {
return "hello";
}

test1();

--EXPECTF--
Catchable fatal error: Return value of test1() must be of the type array, string returned in %s on line %d
18 changes: 18 additions & 0 deletions Zend/tests/return_types/005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Return value fails inheritance check in method

--FILE--
<?php
class foo {}

class qux {
public function foo() : foo {
return $this;
}
}

$qux = new qux();
$qux->foo();

--EXPECTF--
Catchable fatal error: Return value of qux::foo() must be an instance of foo, instance of qux returned in %s on line %d
21 changes: 21 additions & 0 deletions Zend/tests/return_types/006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Return type allowed in child when parent does not have return type

--FILE--
<?php
class Comment {}

class CommentsIterator extends ArrayIterator implements Iterator {
function current() : Comment {
return parent::current();
}
}

$comments = new CommentsIterator([new Comment]);
foreach ($comments as $comment) {
var_dump($comment);
}

--EXPECTF--
object(Comment)#%d (%d) {
}
19 changes: 19 additions & 0 deletions Zend/tests/return_types/007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Return value is subclass of return type

--FILE--
<?php
class foo {}

class qux extends foo {
public function foo() : foo {
return $this;
}
}

$qux = new qux();
var_dump($qux->foo());

--EXPECTF--
object(qux)#%d (%d) {
}
21 changes: 21 additions & 0 deletions Zend/tests/return_types/008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Return type covariance in interface implementation

--FILE--
<?php
interface foo {
public function bar() : foo;
}


class qux implements foo {
public function bar() : qux {
return $this;
}
}

$qux = new qux();
var_dump($qux->bar());

--EXPECTF--
Fatal error: Declaration of qux::bar() must be compatible with foo::bar(): foo in %s008.php on line 7
19 changes: 19 additions & 0 deletions Zend/tests/return_types/009.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Return type covariance error

--FILE--
<?php
interface foo {
public function bar() : foo;
}

interface biz {}

class qux implements foo {
public function bar() : biz {
return $this;
}
}

--EXPECTF--
Fatal error: Declaration of qux::bar() must be compatible with foo::bar(): foo in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/return_types/010.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Returned null, expected array reference

--FILE--
<?php
function &foo(array &$in) : array {
return null;
}

$array = [1, 2, 3];
var_dump(foo($array));

--EXPECTF--
Catchable fatal error: Return value of foo() must be of the type array, null returned in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/return_types/011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Function returned callable, expected callable

--FILE--
<?php
function foo() : callable {
return function() {};
}

var_dump(foo());

--EXPECTF--
object(Closure)#%d (%d) {
}
28 changes: 28 additions & 0 deletions Zend/tests/return_types/012.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Method returned callable, expected callable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong test name, copied from 011.phpt


--FILE--
<?php
class foo {
public function bar() : callable {
$test = "one";
return function() use($test) : array {
return array($test);
};
}
}

$baz = new foo();
var_dump($baz->bar());

--EXPECT--
object(Closure)#2 (2) {
["static"]=>
array(1) {
["test"]=>
string(3) "one"
}
["this"]=>
object(foo)#1 (0) {
}
}
19 changes: 19 additions & 0 deletions Zend/tests/return_types/013.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Closure inside method returned null, expected array

--FILE--
<?php
class foo {
public function bar() : callable {
$test = "one";
return function() use($test) : array {
return null;
};
}
}

$baz = new foo();
var_dump($func=$baz->bar(), $func());

--EXPECTF--
Catchable fatal error: Return value of foo::{closure}() must be of the type array, null returned in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/return_types/014.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Constructors cannot declare a return type

--FILE--
<?php

class Foo {
function __construct() : Foo {}
}

--EXPECTF--
Fatal error: Constructor %s::%s() cannot declare a return type in %s on line %s
24 changes: 24 additions & 0 deletions Zend/tests/return_types/015.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Return types allowed in namespace

--FILE--
<?php

namespace Collections;

interface Collection {
function values(): Collection;
}

class Vector implements Collection {
function values(): Collection {
return $this;
}
}

$v = new Vector;
var_dump($v->values());

--EXPECTF--
object(Collections\Vector)#%d (%d) {
}
20 changes: 20 additions & 0 deletions Zend/tests/return_types/016.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Fully qualified classes are allowed in return types

--FILE--
<?php

namespace Collections;

class Foo {
function foo(\Iterator $i): \Iterator {
return $i;
}
}

$foo = new Foo;
var_dump($foo->foo(new \EmptyIterator()));

--EXPECTF--
object(EmptyIterator)#%d (0) {
}
24 changes: 24 additions & 0 deletions Zend/tests/return_types/017.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Fully qualified classes in trait return types

--FILE--
<?php

namespace FooSpace;

trait Fooable {
function foo(): \Iterator {
return new \EmptyIterator();
}
}

class Foo {
use Fooable;
}

$foo = new Foo;
var_dump($foo->foo([]));

--EXPECTF--
object(EmptyIterator)#%d (%d) {
}
12 changes: 12 additions & 0 deletions Zend/tests/return_types/018.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Destructors cannot declare a return type

--FILE--
<?php

class Foo {
function __destruct() : Foo {}
}

--EXPECTF--
Fatal error: Destructor %s::%s() cannot declare a return type in %s on line %s
12 changes: 12 additions & 0 deletions Zend/tests/return_types/019.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
__clone cannot declare a return type

--FILE--
<?php

class Foo {
function __clone() : Foo {}
}

--EXPECTF--
Fatal error: %s::%s() cannot declare a return type in %s on line %s
17 changes: 17 additions & 0 deletions Zend/tests/return_types/020.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Exception thrown from function with return type

--FILE--
<?php
function test() : array {
throw new Exception();
}

test();

--EXPECTF--
Fatal error: Uncaught exception 'Exception' in %s:%d
Stack trace:
#0 %s(%d): test()
#1 {main}
thrown in %s on line %d
21 changes: 21 additions & 0 deletions Zend/tests/return_types/021.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Return type allows self

--FILE--
<?php
class Foo {
public static function getInstance() : self {
return new static();
}
}

class Bar extends Foo {}

var_dump(Foo::getInstance());
var_dump(Bar::getInstance());

--EXPECTF--
object(Foo)#%d (%d) {
}
object(Bar)#%d (%d) {
}
Loading