Skip to content

Commit efbf7d0

Browse files
committed
Update tests with more complex examples and edge cases.
1 parent 6bf867a commit efbf7d0

File tree

3 files changed

+155
-44
lines changed

3 files changed

+155
-44
lines changed

Zend/tests/attributes/002_rfcexample.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
--TEST--
2-
Attributes: RFC Example
2+
Attributes: Example from Attributes RFC
33
--FILE--
44
<?php
5+
// https://wiki.php.net/rfc/attributes_v2#attribute_syntax
56
namespace My\Attributes {
67
use PhpAttribute;
78

Zend/tests/attributes/003_ast_nodes.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ var_dump(count($args));
5050
var_dump($args[0] === 'foo');
5151
var_dump($args[1] === C1::BAR);
5252

53+
<<ExampleWithShift(4 >> 1)>>
54+
class C4 {}
55+
$ref = new \ReflectionClass(C4::class);
56+
var_dump($ref->getAttributes()[0]->getArguments());
57+
5358
?>
5459
--EXPECT--
5560
int(1)
@@ -66,3 +71,7 @@ int(1)
6671
int(2)
6772
bool(true)
6873
bool(true)
74+
array(1) {
75+
[0]=>
76+
int(2)
77+
}

Zend/tests/attributes/009_doctrine_annotations_example.phpt

Lines changed: 144 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,158 @@
22
Doctrine-like attributes usage
33
--FILE--
44
<?php
5-
namespace Doctrine\ORM {
6-
7-
class Entity {
8-
private $name;
9-
public function __construct($name) {
10-
$this->name = $name;
11-
}
12-
}
13-
14-
function GetClassAttributes($class_name) {
15-
$reflClass = new \ReflectionClass($class_name);
16-
$attrs = $reflClass->getAttributes();
17-
$values = [];
18-
foreach ($attrs as $attribute) {
19-
$class = $attribute->getName();
20-
$values[$attribute->getName()] = new $class(...$attribute->getArguments());
21-
}
22-
return $values;
23-
}
24-
}
25-
26-
namespace Doctrine\ORM\Mapping {
27-
class Entity {
28-
public $tableName;
29-
public $repository;
30-
31-
public function __construct(array $values)
32-
{
33-
foreach ($values as $k => $v) {
34-
$this->$k = $v;
35-
}
36-
}
37-
}
5+
6+
namespace Doctrine\ORM\Attributes {
7+
class Annotation { public $values; public function construct() { $this->values = func_get_args(); } }
8+
class Entity extends Annotation {}
9+
class Id extends Annotation {}
10+
class Column extends Annotation { const UNIQUE = 'unique'; const T_INTEGER = 'integer'; }
11+
class GeneratedValue extends Annotation {}
12+
class JoinTable extends Annotation {}
13+
class ManyToMany extends Annotation {}
14+
class JoinColumn extends Annotation { const UNIQUE = 'unique'; }
15+
class InverseJoinColumn extends Annotation {}
16+
}
17+
18+
namespace Symfony\Component\Validator\Constraints {
19+
class Annotation { public $values; public function construct() { $this->values = func_get_args(); } }
20+
class Email extends Annotation {}
21+
class Range extends Annotation {}
3822
}
3923

4024
namespace {
41-
use Doctrine\ORM\Mapping as ORM;
25+
use Doctrine\ORM\Attributes as ORM;
26+
use Symfony\Component\Validator\Constraints as Assert;
27+
28+
<<ORM\Entity>>
29+
/** @ORM\Entity */
30+
class User
31+
{
32+
/** @ORM\Id @ORM\Column(type="integer"*) @ORM\GeneratedValue */
33+
<<ORM\Id>><<ORM\Column("integer")>><<ORM\GeneratedValue>>
34+
private $id;
35+
36+
/**
37+
* @ORM\Column(type="string", unique=true)
38+
* @Assert\Email(message="The email '{{ value }}' is not a valid email.")
39+
*/
40+
<<ORM\Column("string", ORM\Column::UNIQUE)>>
41+
<<Assert\Email(array("message" => "The email '{{ value }}' is not a valid email."))>>
42+
private $email;
43+
44+
/**
45+
* @ORM\Column(type="integer")
46+
* @Assert\Range(
47+
* min = 120,
48+
* max = 180,
49+
* minMessage = "You must be at least {{ limit }}cm tall to enter",
50+
* maxMessage = "You cannot be taller than {{ limit }}cm to enter"
51+
* )
52+
*/
53+
<<Assert\Range(["min" => 120, "max" => 180, "minMessage" => "You must be at least {{ limit }}cm tall to enter"])>>
54+
<<ORM\Column(ORM\Column::T_INTEGER)>>
55+
protected $height;
56+
57+
/**
58+
* @ORM\ManyToMany(targetEntity="Phonenumber")
59+
* @ORM\JoinTable(name="users_phonenumbers",
60+
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
61+
* inverseJoinColumns={@ORM\JoinColumn(name="phonenumber_id", referencedColumnName="id", unique=true)}
62+
* )
63+
*/
64+
<<ORM\ManyToMany(Phonenumber::class)>>
65+
<<ORM\JoinTable("users_phonenumbers")>>
66+
<<ORM\JoinColumn("user_id", "id")>>
67+
<<ORM\InverseJoinColumn("phonenumber_id", "id", ORM\JoinColumn::UNIQUE)>>
68+
private $phonenumbers;
69+
}
70+
71+
$class = new ReflectionClass(User::class);
72+
$attributes = $class->getAttributes();
73+
74+
foreach ($attributes as $attribute) {
75+
var_dump($attribute->getName(), $attribute->getArguments());
76+
}
4277

43-
<<ORM\Entity(["tableName" => "user", "repository" => UserRepository::class])>>
44-
class User {}
78+
foreach ($class->getProperties() as $property) {
79+
$attributes = $property->getAttributes();
4580

46-
var_dump(Doctrine\ORM\GetClassAttributes("User"));
81+
foreach ($attributes as $attribute) {
82+
var_dump($attribute->getName(), $attribute->getArguments());
83+
}
84+
}
4785
}
4886
?>
4987
--EXPECT--
88+
string(30) "Doctrine\ORM\Attributes\Entity"
89+
array(0) {
90+
}
91+
string(26) "Doctrine\ORM\Attributes\Id"
92+
array(0) {
93+
}
94+
string(30) "Doctrine\ORM\Attributes\Column"
5095
array(1) {
51-
["Doctrine\ORM\Mapping\Entity"]=>
52-
object(Doctrine\ORM\Mapping\Entity)#3 (2) {
53-
["tableName"]=>
54-
string(4) "user"
55-
["repository"]=>
56-
string(14) "UserRepository"
96+
[0]=>
97+
string(7) "integer"
98+
}
99+
string(38) "Doctrine\ORM\Attributes\GeneratedValue"
100+
array(0) {
101+
}
102+
string(30) "Doctrine\ORM\Attributes\Column"
103+
array(2) {
104+
[0]=>
105+
string(6) "string"
106+
[1]=>
107+
string(6) "unique"
108+
}
109+
string(45) "Symfony\Component\Validator\Constraints\Email"
110+
array(1) {
111+
[0]=>
112+
array(1) {
113+
["message"]=>
114+
string(45) "The email '{{ value }}' is not a valid email."
57115
}
58116
}
117+
string(45) "Symfony\Component\Validator\Constraints\Range"
118+
array(1) {
119+
[0]=>
120+
array(3) {
121+
["min"]=>
122+
int(120)
123+
["max"]=>
124+
int(180)
125+
["minMessage"]=>
126+
string(48) "You must be at least {{ limit }}cm tall to enter"
127+
}
128+
}
129+
string(30) "Doctrine\ORM\Attributes\Column"
130+
array(1) {
131+
[0]=>
132+
string(7) "integer"
133+
}
134+
string(34) "Doctrine\ORM\Attributes\ManyToMany"
135+
array(1) {
136+
[0]=>
137+
string(11) "Phonenumber"
138+
}
139+
string(33) "Doctrine\ORM\Attributes\JoinTable"
140+
array(1) {
141+
[0]=>
142+
string(18) "users_phonenumbers"
143+
}
144+
string(34) "Doctrine\ORM\Attributes\JoinColumn"
145+
array(2) {
146+
[0]=>
147+
string(7) "user_id"
148+
[1]=>
149+
string(2) "id"
150+
}
151+
string(41) "Doctrine\ORM\Attributes\InverseJoinColumn"
152+
array(3) {
153+
[0]=>
154+
string(14) "phonenumber_id"
155+
[1]=>
156+
string(2) "id"
157+
[2]=>
158+
string(6) "unique"
159+
}

0 commit comments

Comments
 (0)