Skip to content

Commit 00e54ce

Browse files
Tests for property hooks
1 parent 3f3e275 commit 00e54ce

13 files changed

+409
-190
lines changed

Zend/tests/attributes/delayed_target_validation/errors_from_validator.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,28 @@ readonly class DemoReadonly {}
1919
#[AllowDynamicProperties]
2020
enum DemoEnum {}
2121

22+
class DemoClass {
23+
#[DelayedTargetValidation]
24+
#[NoDiscard] // Does nothing here
25+
public $val;
26+
27+
public string $hooked {
28+
#[DelayedTargetValidation]
29+
// #[NoDiscard] // Does nothing here
30+
get => $this->hooked;
31+
#[DelayedTargetValidation]
32+
// #[NoDiscard] // Does nothing here
33+
set => $value;
34+
}
35+
}
36+
2237
$cases = [
2338
new ReflectionClass('DemoTrait'),
2439
new ReflectionClass('DemoInterface'),
2540
new ReflectionClass('DemoReadonly'),
2641
new ReflectionClass('DemoEnum'),
42+
// new ReflectionProperty('DemoClass', 'hooked')->getHook(PropertyHookType::Get),
43+
// new ReflectionProperty('DemoClass', 'hooked')->getHook(PropertyHookType::Set),
2744
];
2845
foreach ($cases as $r) {
2946
echo str_repeat("*", 20) . "\n";

Zend/tests/attributes/delayed_target_validation/has_runtime_errors.phpt

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@ class Demo {
1313

1414
#[DelayedTargetValidation]
1515
#[Attribute]
16-
public string $v;
16+
public string $v1;
17+
18+
public string $v2 {
19+
#[DelayedTargetValidation]
20+
#[Attribute]
21+
get => $this->v2;
22+
#[DelayedTargetValidation]
23+
#[Attribute]
24+
set => $value;
25+
}
1726

1827
#[DelayedTargetValidation]
1928
#[Attribute]
2029
public function __construct(
2130
#[DelayedTargetValidation]
2231
#[Attribute]
23-
public string $v2
32+
public string $v3
2433
) {
25-
$this->v = $v2;
34+
$this->v1 = $v3;
2635
echo __METHOD__ . "\n";
2736
}
2837
}
@@ -40,10 +49,12 @@ const EXAMPLE = true;
4049
$cases = [
4150
new ReflectionClass('Demo'),
4251
new ReflectionClassConstant('Demo', 'FOO'),
43-
new ReflectionProperty('Demo', 'v'),
52+
new ReflectionProperty('Demo', 'v1'),
53+
new ReflectionProperty('Demo', 'v2')->getHook(PropertyHookType::Get),
54+
new ReflectionProperty('Demo', 'v2')->getHook(PropertyHookType::Set),
4455
new ReflectionMethod('Demo', '__construct'),
45-
new ReflectionParameter([ 'Demo', '__construct' ], 'v2'),
46-
new ReflectionProperty('Demo', 'v2'),
56+
new ReflectionParameter([ 'Demo', '__construct' ], 'v3'),
57+
new ReflectionProperty('Demo', 'v3'),
4758
new ReflectionFunction('demoFn'),
4859
new ReflectionConstant('EXAMPLE'),
4960
];
@@ -62,7 +73,7 @@ foreach ($cases as $r) {
6273
?>
6374
--EXPECTF--
6475
********************
65-
Class [ <user> class Demo ] {
76+
Class [ <user> <iterateable> class Demo ] {
6677
@@ %s %d-%d
6778

6879
- Constants [1] {
@@ -75,17 +86,18 @@ Class [ <user> class Demo ] {
7586
- Static methods [0] {
7687
}
7788

78-
- Properties [2] {
79-
Property [ public string $v ]
89+
- Properties [3] {
90+
Property [ public string $v1 ]
8091
Property [ public string $v2 ]
92+
Property [ public string $v3 ]
8193
}
8294

8395
- Methods [1] {
8496
Method [ <user, ctor> public method __construct ] {
8597
@@ %s %d - %d
8698

8799
- Parameters [1] {
88-
Parameter #0 [ <required> string $v2 ]
100+
Parameter #0 [ <required> string $v3 ]
89101
}
90102
}
91103
}
@@ -121,7 +133,7 @@ array(2) {
121133
}
122134
Error: Attribute "Attribute" cannot target class constant (allowed targets: class)
123135
********************
124-
Property [ public string $v ]
136+
Property [ public string $v1 ]
125137

126138
array(2) {
127139
[0]=>
@@ -137,11 +149,56 @@ array(2) {
137149
}
138150
Error: Attribute "Attribute" cannot target property (allowed targets: class)
139151
********************
152+
Method [ <user> public method $v2::get ] {
153+
@@ %s %d - %d
154+
155+
- Parameters [0] {
156+
}
157+
- Return [ string ]
158+
}
159+
160+
array(2) {
161+
[0]=>
162+
object(ReflectionAttribute)#%d (1) {
163+
["name"]=>
164+
string(23) "DelayedTargetValidation"
165+
}
166+
[1]=>
167+
object(ReflectionAttribute)#%d (1) {
168+
["name"]=>
169+
string(9) "Attribute"
170+
}
171+
}
172+
Error: Attribute "Attribute" cannot target method (allowed targets: class)
173+
********************
174+
Method [ <user> public method $v2::set ] {
175+
@@ %s %d - %d
176+
177+
- Parameters [1] {
178+
Parameter #0 [ <required> string $value ]
179+
}
180+
- Return [ void ]
181+
}
182+
183+
array(2) {
184+
[0]=>
185+
object(ReflectionAttribute)#%d (1) {
186+
["name"]=>
187+
string(23) "DelayedTargetValidation"
188+
}
189+
[1]=>
190+
object(ReflectionAttribute)#%d (1) {
191+
["name"]=>
192+
string(9) "Attribute"
193+
}
194+
}
195+
Error: Attribute "Attribute" cannot target method (allowed targets: class)
196+
********************
140197
Method [ <user, ctor> public method __construct ] {
141198
@@ %s %d - %d
142199

143200
- Parameters [1] {
144-
Parameter #0 [ <required> string $v2 ]
201+
Parameter #0 [ <required> string $v3 ]
145202
}
146203
}
147204

@@ -159,7 +216,7 @@ array(2) {
159216
}
160217
Error: Attribute "Attribute" cannot target method (allowed targets: class)
161218
********************
162-
Parameter #0 [ <required> string $v2 ]
219+
Parameter #0 [ <required> string $v3 ]
163220
array(2) {
164221
[0]=>
165222
object(ReflectionAttribute)#%d (1) {
@@ -174,7 +231,7 @@ array(2) {
174231
}
175232
Error: Attribute "Attribute" cannot target parameter (allowed targets: class)
176233
********************
177-
Property [ public string $v2 ]
234+
Property [ public string $v3 ]
178235

179236
array(2) {
180237
[0]=>

Zend/tests/attributes/delayed_target_validation/no_compile_errors.phpt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@ class Demo {
1313

1414
#[DelayedTargetValidation]
1515
#[Attribute]
16-
public string $v;
16+
public string $v1;
17+
18+
public string $v2 {
19+
#[DelayedTargetValidation]
20+
#[Attribute]
21+
get => $this->v2;
22+
#[DelayedTargetValidation]
23+
#[Attribute]
24+
set => $value;
25+
}
1726

1827
#[DelayedTargetValidation]
1928
#[Attribute]
2029
public function __construct(
2130
#[DelayedTargetValidation]
2231
#[Attribute]
23-
public string $v2
32+
public string $v3
2433
) {
25-
$this->v = $v2;
34+
$this->v1 = $v3;
2635
echo __METHOD__ . "\n";
2736
}
2837
}

Zend/tests/attributes/delayed_target_validation/with_AllowDynamicProperties.phpt

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,45 @@
66
#[DelayedTargetValidation]
77
#[AllowDynamicProperties] // Does something here
88
class DemoClass {
9-
#[DelayedTargetValidation]
10-
#[AllowDynamicProperties] // Does nothing here
11-
public $val;
9+
#[DelayedTargetValidation]
10+
#[AllowDynamicProperties] // Does nothing here
11+
public $val;
1212

13-
#[DelayedTargetValidation]
14-
#[AllowDynamicProperties] // Does nothing here
15-
public const CLASS_CONST = 'FOO';
13+
public string $hooked {
14+
#[DelayedTargetValidation]
15+
#[AllowDynamicProperties] // Does nothing here
16+
get => $this->hooked;
17+
#[DelayedTargetValidation]
18+
#[AllowDynamicProperties] // Does nothing here
19+
set => $value;
20+
}
1621

17-
public function __construct(
18-
#[DelayedTargetValidation]
19-
#[AllowDynamicProperties] // Does nothing here
20-
$str
21-
) {
22-
echo "Got: $str\n";
23-
$this->val = $str;
24-
}
22+
#[DelayedTargetValidation]
23+
#[AllowDynamicProperties] // Does nothing here
24+
public const CLASS_CONST = 'FOO';
2525

26-
#[DelayedTargetValidation]
27-
#[AllowDynamicProperties] // Does nothing here
28-
public function printVal() {
29-
echo 'Value is: ' . $this->val . "\n";
30-
}
26+
public function __construct(
27+
#[DelayedTargetValidation]
28+
#[AllowDynamicProperties] // Does nothing here
29+
$str
30+
) {
31+
echo "Got: $str\n";
32+
$this->val = $str;
33+
}
34+
35+
#[DelayedTargetValidation]
36+
#[AllowDynamicProperties] // Does nothing here
37+
public function printVal() {
38+
echo 'Value is: ' . $this->val . "\n";
39+
}
3140

3241
}
3342

3443
#[DelayedTargetValidation]
3544
#[AllowDynamicProperties] // Does nothing here
3645
function demoFn() {
37-
echo __FUNCTION__ . "\n";
38-
return 456;
46+
echo __FUNCTION__ . "\n";
47+
return 456;
3948
}
4049

4150
#[DelayedTargetValidation]
@@ -45,6 +54,8 @@ const GLOBAL_CONST = 'BAR';
4554
$d = new DemoClass('example');
4655
$d->printVal();
4756
var_dump($d->val);
57+
$d->hooked = "foo";
58+
var_dump($d->hooked);
4859
var_dump(DemoClass::CLASS_CONST);
4960
demoFn();
5061
var_dump(GLOBAL_CONST);
@@ -56,12 +67,15 @@ var_dump($d);
5667
Got: example
5768
Value is: example
5869
string(7) "example"
70+
string(3) "foo"
5971
string(3) "FOO"
6072
demoFn
6173
string(3) "BAR"
62-
object(DemoClass)#%d (2) {
74+
object(DemoClass)#%d (3) {
6375
["val"]=>
6476
string(7) "example"
77+
["hooked"]=>
78+
string(3) "foo"
6579
["missingProp"]=>
6680
string(3) "foo"
6781
}

0 commit comments

Comments
 (0)