Skip to content

Commit 277fde3

Browse files
test(preprocessor): cover property-hook placement rules
1 parent 9f26b23 commit 277fde3

9 files changed

Lines changed: 82 additions & 0 deletions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
abstract class Box { abstract public int $x { get => 1; } }
3+
4+
function main() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
abstract class Box { abstract public int $x; }
3+
4+
function main() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
class Box { abstract public int $x { get; } }
3+
4+
function main() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
class Box { public int $x { get; } }
3+
4+
function main() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
class Box { public readonly int $x { get => 1; } }
3+
4+
function main() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
readonly class Box { public int $x { get => 1; } }
3+
4+
function main() {}

phpunit/code/hook_rule_static.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
class Box { public static int $x { get => 1; } }
3+
4+
function main() {}

phpunit/code/hook_rule_valid.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
abstract class Box { private int $b = 0; public int $x { get => $this->b; set { $this->b = $value; } } abstract public string $s { get; } }
3+
4+
function main() {}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* Zend property-hook placement rules for class/trait properties: no
5+
* hooks on static or readonly properties, abstract hooked properties
6+
* only in abstract containers with at least one bodiless hook, and a
7+
* mandatory body on every non-abstract hook.
8+
*/
9+
class PropertyHookPlacementTest extends BaseTest
10+
{
11+
public function testHooksOnStaticPropertyAreRejected(): void
12+
{
13+
$this->exec('Cannot declare hooks for static property', 'hook_rule_static.php');
14+
}
15+
16+
public function testHooksOnReadonlyPropertyAreRejected(): void
17+
{
18+
$this->exec('Hooked properties cannot be readonly', 'hook_rule_readonly.php');
19+
}
20+
21+
public function testHooksInReadonlyClassAreRejected(): void
22+
{
23+
$this->exec('Hooked properties cannot be readonly', 'hook_rule_readonly_class.php');
24+
}
25+
26+
public function testAbstractHookedPropertyRequiresAbstractClass(): void
27+
{
28+
$this->exec('Non-abstract class `Box` contains abstract hooked property `$x`', 'hook_rule_abstract_nonabstract_class.php');
29+
}
30+
31+
public function testAbstractPropertyNeedsAtLeastOneAbstractHook(): void
32+
{
33+
$this->exec('Abstract property `Box::$x` must specify at least one abstract hook', 'hook_rule_abstract_all_bodies.php');
34+
}
35+
36+
public function testOnlyHookedPropertiesMayBeAbstract(): void
37+
{
38+
$this->exec('Only hooked properties may be declared abstract', 'hook_rule_abstract_no_hooks.php');
39+
}
40+
41+
public function testNonAbstractHookMustHaveBody(): void
42+
{
43+
$this->exec('Non-abstract property hook must have a body', 'hook_rule_bodyless.php');
44+
}
45+
46+
public function testWellFormedHooksStillCompile(): void
47+
{
48+
$this->compile('hook_rule_valid.php');
49+
}
50+
}

0 commit comments

Comments
 (0)