Skip to content

Commit 7d19423

Browse files
Add tests for zend_test_compile_to_ast()
1 parent 512f5f2 commit 7d19423

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
AST can be recreated (basic example)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
/**
9+
* My documentation comment
10+
*/
11+
#[MyAttrib]
12+
function doSomething( int $intParam, string $stringParam ): object {
13+
return (object)[ $intParam, $stringParam ];
14+
}
15+
16+
// Inline comment
17+
18+
const MY_CONSTANT = true;
19+
20+
class Example {
21+
private int $value;
22+
23+
public function __construct( int $v ) {
24+
$this->value = $v;
25+
}
26+
27+
public function getValue(): int {
28+
return $this->value;
29+
}
30+
}
31+
32+
echo zend_test_compile_to_ast( file_get_contents( __FILE__ ) );
33+
34+
?>
35+
--EXPECT--
36+
#[MyAttrib]
37+
function doSomething(int $intParam, string $stringParam): object {
38+
return (object)[$intParam, $stringParam];
39+
}
40+
41+
const MY_CONSTANT = true;
42+
class Example {
43+
private int $value;
44+
public function __construct(int $v) {
45+
$this->value = $v;
46+
}
47+
48+
public function getValue(): int {
49+
return $this->value;
50+
}
51+
52+
}
53+
54+
echo zend_test_compile_to_ast(file_get_contents(__FILE__));
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
--TEST--
2+
AST can be recreated (complicated class example)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
interface MyInterface1 {}
9+
interface MyInterface2 extends MyInterface1 {}
10+
11+
abstract class MyBaseClass implements MyInterface2 {
12+
final public function get5(): int {
13+
return 5;
14+
}
15+
16+
abstract public function setValue( string $value ): void;
17+
}
18+
19+
class MySubClass extends MyBaseClass {
20+
public private(set) string $alwaysLowerCase = "STARTS UPPER" {
21+
get => $this->alwaysLowerCase;
22+
set => strtolower( $value );
23+
}
24+
25+
public readonly bool $negated;
26+
27+
public const CONST_PUBLIC = 1;
28+
protected const CONST_PROTECTED = "abc";
29+
private const CONST_PRIVATE = [ 1, 2 ];
30+
31+
public static ?object $cache = null;
32+
33+
public function __construct( public bool $boolVal ) {
34+
$this->negated = !$boolVal;
35+
}
36+
37+
public function setValue( string $value ): void {
38+
$this->alwaysLowerCase = $value;
39+
}
40+
41+
public static function getPrivateConst(): array {
42+
return self::CONST_PRIVATE;
43+
}
44+
}
45+
46+
echo zend_test_compile_to_ast( file_get_contents( __FILE__ ) );
47+
48+
?>
49+
--EXPECT--
50+
interface MyInterface1 {
51+
}
52+
53+
interface MyInterface2 implements MyInterface1 {
54+
}
55+
56+
abstract class MyBaseClass implements MyInterface2 {
57+
public final function get5(): int {
58+
return 5;
59+
}
60+
61+
public abstract function setValue(string $value): void;
62+
63+
}
64+
65+
class MySubClass extends MyBaseClass {
66+
public private(set) string $alwaysLowerCase = 'STARTS UPPER' {
67+
get => $this->alwaysLowerCase;
68+
set => strtolower($value);
69+
}
70+
public readonly bool $negated;
71+
public const CONST_PUBLIC = 1;
72+
protected const CONST_PROTECTED = 'abc';
73+
private const CONST_PRIVATE = [1, 2];
74+
public static ?object $cache = null;
75+
public function __construct(bool $boolVal) {
76+
$this->negated = !$boolVal;
77+
}
78+
79+
public function setValue(string $value): void {
80+
$this->alwaysLowerCase = $value;
81+
}
82+
83+
public static function getPrivateConst(): array {
84+
return self::CONST_PRIVATE;
85+
}
86+
87+
}
88+
89+
echo zend_test_compile_to_ast(file_get_contents(__FILE__));
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
AST can be recreated (enums)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
enum Suit
9+
{
10+
case Hearts;
11+
case Diamonds;
12+
case Clubs;
13+
case Spades;
14+
15+
public const Clubz = self::Clubs;
16+
}
17+
18+
enum MyBoolean: int {
19+
case MyFalse = 0;
20+
case MyTrue = 1;
21+
22+
public function toBool(): bool {
23+
return match($this) {
24+
MyBoolean::MyFalse => false,
25+
MyBoolean::MyTrue => true,
26+
};
27+
}
28+
}
29+
30+
echo zend_test_compile_to_ast( file_get_contents( __FILE__ ) );
31+
32+
?>
33+
--EXPECT--
34+
enum Suit {
35+
case Hearts;
36+
case Diamonds;
37+
case Clubs;
38+
case Spades;
39+
public const Clubz = self::Clubs;
40+
}
41+
42+
enum MyBoolean: int {
43+
case MyFalse = 0;
44+
case MyTrue = 1;
45+
public function toBool(): bool {
46+
return match ($this) {
47+
MyBoolean::MyFalse => false,
48+
MyBoolean::MyTrue => true,
49+
};
50+
}
51+
52+
}
53+
54+
echo zend_test_compile_to_ast(file_get_contents(__FILE__));

0 commit comments

Comments
 (0)