22
33declare (strict_types=1 );
44
5+ use Nette \PhpGenerator \GlobalFunction ;
56use Nette \PhpGenerator \Printer ;
67use Tester \Assert ;
78
89require __DIR__ . '/../bootstrap.php ' ;
910
1011
1112$ printer = new Printer ;
12- $ function = new Nette \ PhpGenerator \ GlobalFunction ('func ' );
13+ $ function = new GlobalFunction ('func ' );
1314$ function
1415 ->setReturnType ('stdClass ' )
1516 ->setBody ("func(); \r\nreturn 123; " )
@@ -26,30 +27,138 @@ Assert::match(<<<'XX'
2627 XX, $ printer ->printFunction ($ function ));
2728
2829
29- $ function = new Nette \ PhpGenerator \ GlobalFunction ('multi ' );
30+ $ function = new GlobalFunction ('multi ' );
3031$ function ->addParameter ('foo ' )
3132 ->addAttribute ('Foo ' );
3233
3334Assert::match (<<<'XX'
3435 function multi(
35- #[Foo] $foo,
36+ #[Foo]
37+ $foo,
3638 ) {
3739 }
38-
3940 XX, $ printer ->printFunction ($ function ));
4041
4142
42- $ function = new Nette \ PhpGenerator \ GlobalFunction ('multiType ' );
43+ $ function = new GlobalFunction ('multiType ' );
4344$ function
4445 ->setReturnType ('array ' )
4546 ->addParameter ('foo ' )
4647 ->addAttribute ('Foo ' );
4748
4849Assert::match (<<<'XX'
4950 function multiType(
50- #[Foo] $foo,
51+ #[Foo]
52+ $foo,
5153 ): array
5254 {
5355 }
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+
54130
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+ }
55164 XX, $ printer ->printFunction ($ function ));
0 commit comments