Skip to content

Commit a65ba61

Browse files
committed
add more tests
1 parent 179f489 commit a65ba61

File tree

10 files changed

+179
-0
lines changed

10 files changed

+179
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
ensure autoloading works
3+
--FILE--
4+
<?php
5+
spl_autoload_register(static function ($class_name) {
6+
require_once(__DIR__ . '/' . explode('\\', $class_name)[0] . '.inc');
7+
echo 'autoload(' . $class_name . ")\n";
8+
});
9+
10+
$point = new inner_classes\Point(1, 2);
11+
echo $point->x, ' ', $point->y, "\n";
12+
?>
13+
--EXPECT--
14+
autoload(inner_classes\Point)
15+
1 2
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
usage in an enum
3+
--FILE--
4+
<?php
5+
6+
enum Outer {
7+
class Inner {}
8+
}
9+
10+
var_dump(new Outer\Inner());
11+
var_dump(class_exists(Outer\Inner::class));
12+
?>
13+
--EXPECT--
14+
object(Outer\Inner)#1 (0) {
15+
}
16+
bool(true)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
no outer class
3+
--FILE--
4+
<?php
5+
6+
new Outer\Inner();
7+
?>
8+
--EXPECTF--
9+
Fatal error: Uncaught Error: Class "Outer\Inner" not found in %s:%d
10+
Stack trace:
11+
#0 {main}
12+
thrown 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+
inner class not found
3+
--FILE--
4+
<?php
5+
6+
class Outer {
7+
}
8+
9+
new Outer\Inner();
10+
?>
11+
--EXPECTF--
12+
Fatal error: Uncaught Error: Class "Outer\Inner" not found in %s:%d
13+
Stack trace:
14+
#0 {main}
15+
thrown in %s on line %d
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
circular inheritance
3+
--FILE--
4+
<?php
5+
6+
class Outer {
7+
class Middle extends Outer\Other {
8+
class Inner1 extends Inner2 {}
9+
class Inner2 extends Other {}
10+
}
11+
abstract class Other {}
12+
}
13+
?>
14+
--EXPECT--
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
class inner_classes {
4+
public class Point {
5+
public function __construct(public int $x, public int $y) {}
6+
}
7+
8+
private class Line {}
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
usage in an interface
3+
--FILE--
4+
<?php
5+
6+
interface Outer {
7+
class Inner {}
8+
}
9+
10+
var_dump(new Outer\Inner());
11+
12+
class Foo implements Outer {}
13+
14+
var_dump(class_exists(Outer\Inner::class));
15+
var_dump(class_exists(Foo\Inner::class));
16+
?>
17+
--EXPECT--
18+
object(Outer\Inner)#1 (0) {
19+
}
20+
bool(true)
21+
bool(false)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
readonly should work
3+
--FILE--
4+
<?php
5+
6+
class Outer {
7+
public readonly class Inner { public function __construct(public int $t) {} }
8+
}
9+
10+
var_dump($foo = new Outer\Inner(1));
11+
$foo->t = 42;
12+
var_dump($foo);
13+
?>
14+
--EXPECTF--
15+
object(Outer\Inner)#1 (1) {
16+
["t"]=>
17+
int(1)
18+
}
19+
20+
Fatal error: Uncaught Error: Cannot modify readonly property Outer\Inner::$t in %s:%d
21+
Stack trace:
22+
#0 {main}
23+
thrown in %s on line %d
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
::class, statics, and inner classes
3+
--FILE--
4+
<?php
5+
6+
class Outer {
7+
class Middle {
8+
public const FOO = 'foo';
9+
public static int $bar = 42;
10+
class Inner {
11+
public const FOO = 'foo';
12+
public static int $bar = 42;
13+
}
14+
}
15+
}
16+
17+
var_dump(Outer\Middle::class);
18+
var_dump(Outer\Middle::FOO);
19+
var_dump(Outer\Middle::$bar);
20+
var_dump(Outer\Middle\Inner::class);
21+
var_dump(Outer\Middle\Inner::FOO);
22+
var_dump(Outer\Middle\Inner::$bar);
23+
?>
24+
--EXPECT--
25+
string(12) "Outer\Middle"
26+
string(3) "foo"
27+
int(42)
28+
string(18) "Outer\Middle\Inner"
29+
string(3) "foo"
30+
int(42)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
usage inside a trait
3+
--FILE--
4+
<?php
5+
6+
trait Outer {
7+
class Inner {}
8+
}
9+
10+
var_dump(new Outer\Inner());
11+
12+
class Foo {
13+
use Outer;
14+
}
15+
16+
var_dump(class_exists(Outer\Inner::class));
17+
var_dump(class_exists(Foo\Inner::class));
18+
19+
?>
20+
--EXPECT--
21+
object(Outer\Inner)#1 (0) {
22+
}
23+
bool(true)
24+
bool(false)

0 commit comments

Comments
 (0)