2
2
3
3
declare (strict_types=1 );
4
4
5
+ use Nette \PhpGenerator \GlobalFunction ;
5
6
use Nette \PhpGenerator \Printer ;
6
7
use Tester \Assert ;
7
8
8
9
require __DIR__ . '/../bootstrap.php ' ;
9
10
10
11
11
12
$ printer = new Printer ;
12
- $ function = new Nette \ PhpGenerator \ GlobalFunction ('func ' );
13
+ $ function = new GlobalFunction ('func ' );
13
14
$ function
14
15
->setReturnType ('stdClass ' )
15
16
->setBody ("func(); \r\nreturn 123; " )
@@ -26,30 +27,138 @@ Assert::match(<<<'XX'
26
27
XX, $ printer ->printFunction ($ function ));
27
28
28
29
29
- $ function = new Nette \ PhpGenerator \ GlobalFunction ('multi ' );
30
+ $ function = new GlobalFunction ('multi ' );
30
31
$ function ->addParameter ('foo ' )
31
32
->addAttribute ('Foo ' );
32
33
33
34
Assert::match (<<<'XX'
34
35
function multi(
35
- #[Foo] $foo,
36
+ #[Foo]
37
+ $foo,
36
38
) {
37
39
}
38
-
39
40
XX, $ printer ->printFunction ($ function ));
40
41
41
42
42
- $ function = new Nette \ PhpGenerator \ GlobalFunction ('multiType ' );
43
+ $ function = new GlobalFunction ('multiType ' );
43
44
$ function
44
45
->setReturnType ('array ' )
45
46
->addParameter ('foo ' )
46
47
->addAttribute ('Foo ' );
47
48
48
49
Assert::match (<<<'XX'
49
50
function multiType(
50
- #[Foo] $foo,
51
+ #[Foo]
52
+ $foo,
51
53
): array
52
54
{
53
55
}
56
+ XX, $ printer ->printFunction ($ function ));
57
+
58
+
59
+ $ function = new GlobalFunction ('func ' );
60
+ $ function ->addAttribute ('Foo ' , [1 , 2 , 3 ]);
61
+ $ function ->addAttribute ('Bar ' );
62
+
63
+ same (
64
+ <<<'XX'
65
+ #[Foo(1, 2, 3)]
66
+ #[Bar]
67
+ function func()
68
+ {
69
+ }
70
+
71
+ XX,
72
+ (string ) $ function ,
73
+ );
74
+
75
+
76
+ // single
77
+ $ function = new GlobalFunction ('func ' );
78
+ $ function ->addAttribute ('Foo ' , [1 , 2 , 3 ]);
79
+
80
+ Assert::match (<<<'XX'
81
+ #[Foo(1, 2, 3)]
82
+ function func()
83
+ {
84
+ }
85
+ XX, $ printer ->printFunction ($ function ));
86
+
87
+
88
+ // multiple
89
+ $ function = new GlobalFunction ('func ' );
90
+ $ function ->addAttribute ('Foo ' , [1 , 2 , 3 ]);
91
+ $ function ->addAttribute ('Bar ' );
92
+
93
+ Assert::match (<<<'XX'
94
+ #[Foo(1, 2, 3)]
95
+ #[Bar]
96
+ function func()
97
+ {
98
+ }
99
+ XX, $ printer ->printFunction ($ function ));
100
+
101
+
102
+ // multiline
103
+ $ function = new GlobalFunction ('func ' );
104
+ $ function ->addAttribute ('Foo ' , ['a ' , str_repeat ('x ' , 120 )]);
105
+
106
+ Assert::match (<<<'XX'
107
+ #[Foo(
108
+ 'a',
109
+ 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
110
+ )]
111
+ function func()
112
+ {
113
+ }
114
+ XX, $ printer ->printFunction ($ function ));
115
+
116
+
117
+ // parameter: single
118
+ $ function = new GlobalFunction ('func ' );
119
+ $ param = $ function ->addParameter ('foo ' );
120
+ $ param ->addAttribute ('Foo ' , [1 , 2 , 3 ]);
121
+
122
+ Assert::match (<<<'XX'
123
+ function func(
124
+ #[Foo(1, 2, 3)]
125
+ $foo,
126
+ ) {
127
+ }
128
+ XX, $ printer ->printFunction ($ function ));
129
+
54
130
131
+ // parameter: multiple
132
+ $ function = new GlobalFunction ('func ' );
133
+ $ param = $ function ->addParameter ('foo ' );
134
+ $ param ->addAttribute ('Foo ' , [1 , 2 , 3 ]);
135
+ $ param ->addAttribute ('Bar ' );
136
+
137
+ Assert::match (<<<'XX'
138
+ function func(
139
+ #[Foo(1, 2, 3), Bar]
140
+ $foo,
141
+ ) {
142
+ }
143
+ XX, $ printer ->printFunction ($ function ));
144
+
145
+
146
+ // parameter: multiline
147
+ $ function = new GlobalFunction ('func ' );
148
+ $ param = $ function ->addParameter ('bar ' );
149
+ $ param ->addAttribute ('Foo ' );
150
+ $ param = $ function ->addParameter ('foo ' );
151
+ $ param ->addAttribute ('Foo ' , ['a ' , str_repeat ('x ' , 120 )]);
152
+
153
+ Assert::match (<<<'XX'
154
+ function func(
155
+ #[Foo]
156
+ $bar,
157
+ #[Foo(
158
+ 'a',
159
+ 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
160
+ )]
161
+ $foo,
162
+ ) {
163
+ }
55
164
XX, $ printer ->printFunction ($ function ));
0 commit comments