@@ -80,20 +80,16 @@ function getName($ref)
8080
8181
8282test ('global function ' , function () {
83- Assert::same ('trim ' , Callback::unwrap (Callback:: closure ('trim ' )));
83+ Assert::same ('trim ' , Callback::unwrap (Closure:: fromCallable ('trim ' )));
8484 Assert::same ('trim ' , Callback::toString ('trim ' ));
85- Assert::same ('{closure trim} ' , Callback::toString (Callback:: closure ('trim ' )));
85+ Assert::same ('{closure trim} ' , Callback::toString (Closure:: fromCallable ('trim ' )));
8686 Assert::same ('trim ' , getName (Callback::toReflection ('trim ' )));
87- Assert::same ('trim ' , getName (Callback::toReflection (Callback:: closure ('trim ' ))));
88- Assert::same ('x ' , Callback:: closure ('trim ' )->__invoke (' x ' ));
87+ Assert::same ('trim ' , getName (Callback::toReflection (Closure:: fromCallable ('trim ' ))));
88+ Assert::same ('x ' , Closure:: fromCallable ('trim ' )->__invoke (' x ' ));
8989
9090
9191 Assert::same ('undefined ' , Callback::toString ('undefined ' ));
9292
93- Assert::exception (function () {
94- Callback::closure ('undefined ' );
95- }, Nette \InvalidArgumentException::class, '%a% function %c%undefined%c% not found %a% ' );
96-
9793 Assert::exception (function () {
9894 Callback::toReflection ('undefined ' );
9995 }, ReflectionException::class, 'Function undefined() does not exist ' );
@@ -105,113 +101,106 @@ test('closure', function () {
105101 $ a = __FUNCTION__ ;
106102 return $ a ;
107103 };
108- Assert::same ($ closure , Callback:: closure ($ closure ));
104+ Assert::same ($ closure , Closure:: fromCallable ($ closure ));
109105 Assert::same ($ closure , Callback::unwrap ($ closure ));
110106 Assert::same ('{closure} ' , Callback::toString ($ closure ));
111107 Assert::same ('{closure} ' , getName (Callback::toReflection ($ closure )));
112- Assert::same ('{closure} ' , Callback:: closure ($ closure )(...[&$ res ]));
108+ Assert::same ('{closure} ' , Closure:: fromCallable ($ closure )(...[&$ res ]));
113109 Assert::same ('{closure} ' , $ res );
114110});
115111
116112
117113test ('invokable object ' , function () {
118114 $ test = new Test ;
119- Assert::same ([$ test , '__invoke ' ], Callback::unwrap (Callback:: closure ($ test )));
115+ Assert::same ([$ test , '__invoke ' ], Callback::unwrap (Closure:: fromCallable ($ test )));
120116 Assert::same ('Test::__invoke ' , Callback::toString ($ test ));
121- Assert::same ('{closure Test::__invoke} ' , Callback::toString (Callback:: closure ($ test )));
117+ Assert::same ('{closure Test::__invoke} ' , Callback::toString (Closure:: fromCallable ($ test )));
122118 Assert::same ('Test::__invoke ' , getName (Callback::toReflection ($ test )));
123- Assert::same ('Test::__invoke ' , getName (Callback::toReflection (Callback:: closure ($ test ))));
124- Assert::same ('Test::__invoke* ' , Callback:: closure ($ test )->__invoke ('* ' ));
119+ Assert::same ('Test::__invoke ' , getName (Callback::toReflection (Closure:: fromCallable ($ test ))));
120+ Assert::same ('Test::__invoke* ' , Closure:: fromCallable ($ test )->__invoke ('* ' ));
125121});
126122
127123
128124test ('object methods ' , function () {
129125 $ test = new Test ;
130- Assert::same ([$ test , 'publicFun ' ], Callback::unwrap (Callback::closure ($ test , 'publicFun ' )));
131- Assert::same ([$ test , 'publicFun ' ], Callback::unwrap (Callback::closure ([$ test , 'publicFun ' ])));
126+ Assert::same ([$ test , 'publicFun ' ], Callback::unwrap (Closure::fromCallable ([$ test , 'publicFun ' ])));
132127
133128 Assert::same ('Test::publicFun ' , Callback::toString ([$ test , 'publicFun ' ]));
134- Assert::same ('{closure Test::publicFun} ' , Callback::toString (Callback:: closure ( $ test , 'publicFun ' )));
129+ Assert::same ('{closure Test::publicFun} ' , Callback::toString (Closure:: fromCallable ([ $ test , 'publicFun ' ] )));
135130
136131 Assert::same ('Test::publicFun ' , getName (Callback::toReflection ([$ test , 'publicFun ' ])));
137- Assert::same ('Test::publicFun ' , getName (Callback::toReflection (Callback:: closure ( $ test , 'publicFun ' ))));
132+ Assert::same ('Test::publicFun ' , getName (Callback::toReflection (Closure:: fromCallable ([ $ test , 'publicFun ' ] ))));
138133
139- Assert::same ('Test::publicFun* ' , Callback:: closure ( $ test , 'publicFun ' )->__invoke ('* ' ));
134+ Assert::same ('Test::publicFun* ' , Closure:: fromCallable ([ $ test , 'publicFun ' ] )->__invoke ('* ' ));
140135
141136
142- Assert::same ([$ test , 'privateFun ' ], Callback::unwrap (Callback::closure ($ test , 'privateFun ' )));
143- Assert::same ([$ test , 'privateFun ' ], Callback::unwrap (Callback::closure ([$ test , 'privateFun ' ])));
137+ Assert::same ([$ test , 'privateFun ' ], Callback::unwrap (Closure::fromCallable ([$ test , 'privateFun ' ])));
144138
145139 Assert::same ('Test::privateFun ' , Callback::toString ([$ test , 'privateFun ' ]));
146- Assert::same ('{closure Test::privateFun} ' , Callback::toString (Callback:: closure ( $ test , 'privateFun ' )));
140+ Assert::same ('{closure Test::privateFun} ' , Callback::toString (Closure:: fromCallable ([ $ test , 'privateFun ' ] )));
147141
148142 Assert::same ('Test::privateFun ' , getName (Callback::toReflection ([$ test , 'privateFun ' ])));
149- Assert::same ('Test::privateFun ' , getName (Callback::toReflection (Callback:: closure ( $ test , 'privateFun ' ))));
143+ Assert::same ('Test::privateFun ' , getName (Callback::toReflection (Closure:: fromCallable ([ $ test , 'privateFun ' ] ))));
150144
151- Assert::same ('Test::__call privateFun * ' , Callback:: closure ( $ test , 'privateFun ' )->__invoke ('* ' ));
145+ Assert::same ('Test::__call privateFun * ' , Closure:: fromCallable ([ $ test , 'privateFun ' ] )->__invoke ('* ' ));
152146
153- Assert::same ('Test::ref ' , Callback:: closure ( $ test , 'ref ' )(...[&$ res ]));
147+ Assert::same ('Test::ref ' , Closure:: fromCallable ([ $ test , 'ref ' ] )(...[&$ res ]));
154148 Assert::same ('Test::ref ' , $ res );
155149});
156150
157151
158152test ('static methods ' , function () {
159153 $ test = new Test ;
160- Assert::same (['Test ' , 'publicStatic ' ], Callback::unwrap (Callback::closure ('Test ' , 'publicStatic ' )));
161- Assert::same (['Test ' , 'publicStatic ' ], Callback::unwrap (Callback::closure (['Test ' , 'publicStatic ' ])));
162- Assert::same (['Test ' , 'publicStatic ' ], Callback::unwrap (Callback::closure ('Test::publicStatic ' )));
154+ Assert::same (['Test ' , 'publicStatic ' ], Callback::unwrap (Closure::fromCallable (['Test ' , 'publicStatic ' ])));
155+ Assert::same (['Test ' , 'publicStatic ' ], Callback::unwrap (Closure::fromCallable ('Test::publicStatic ' )));
163156
164157 Assert::same ('Test::publicStatic ' , Callback::toString (['Test ' , 'publicStatic ' ]));
165158 Assert::same ('Test::publicStatic ' , Callback::toString ([$ test , 'publicStatic ' ]));
166159 Assert::same ('Test::publicStatic ' , Callback::toString ('Test::publicStatic ' ));
167- Assert::same ('{closure Test::publicStatic} ' , Callback::toString (Callback:: closure ('Test::publicStatic ' )));
160+ Assert::same ('{closure Test::publicStatic} ' , Callback::toString (Closure:: fromCallable ('Test::publicStatic ' )));
168161
169162 Assert::same ('Test::publicStatic ' , getName (Callback::toReflection (['Test ' , 'publicStatic ' ])));
170163 Assert::same ('Test::publicStatic ' , getName (Callback::toReflection ([$ test , 'publicStatic ' ])));
171164 Assert::same ('Test::publicStatic ' , getName (Callback::toReflection ('Test::publicStatic ' )));
172- Assert::same ('Test::publicStatic ' , getName (Callback::toReflection (Callback:: closure ('Test::publicStatic ' ))));
165+ Assert::same ('Test::publicStatic ' , getName (Callback::toReflection (Closure:: fromCallable ('Test::publicStatic ' ))));
173166
174- Assert::same ('Test::publicStatic* ' , Callback:: closure ( 'Test ' , 'publicStatic ' )->__invoke ('* ' ));
175- Assert::same ('Test::publicStatic* ' , Callback:: closure ( $ test , 'publicStatic ' )->__invoke ('* ' ));
167+ Assert::same ('Test::publicStatic* ' , Closure:: fromCallable ([ 'Test ' , 'publicStatic ' ] )->__invoke ('* ' ));
168+ Assert::same ('Test::publicStatic* ' , Closure:: fromCallable ([ $ test , 'publicStatic ' ] )->__invoke ('* ' ));
176169
177170
178- Assert::same (['Test ' , 'privateStatic ' ], Callback::unwrap (Callback:: closure ('Test::privateStatic ' )));
171+ Assert::same (['Test ' , 'privateStatic ' ], Callback::unwrap (Closure:: fromCallable ('Test::privateStatic ' )));
179172 Assert::same ('Test::privateStatic ' , Callback::toString ('Test::privateStatic ' ));
180- Assert::same ('{closure Test::privateStatic} ' , Callback::toString (Callback:: closure ('Test::privateStatic ' )));
173+ Assert::same ('{closure Test::privateStatic} ' , Callback::toString (Closure:: fromCallable ('Test::privateStatic ' )));
181174 Assert::same ('Test::privateStatic ' , getName (Callback::toReflection ('Test::privateStatic ' )));
182- Assert::same ('Test::privateStatic ' , getName (Callback::toReflection (Callback:: closure ('Test::privateStatic ' ))));
175+ Assert::same ('Test::privateStatic ' , getName (Callback::toReflection (Closure:: fromCallable ('Test::privateStatic ' ))));
183176
184- Assert::same ('Test::__callStatic privateStatic * ' , Callback:: closure ('Test::privateStatic ' )->__invoke ('* ' ));
177+ Assert::same ('Test::__callStatic privateStatic * ' , Closure:: fromCallable ('Test::privateStatic ' )->__invoke ('* ' ));
185178});
186179
187180
188181test ('magic methods ' , function () {
189182 $ test = new Test ;
190- Assert::same ([$ test , 'magic ' ], Callback::unwrap (Callback:: closure ( $ test , 'magic ' )));
183+ Assert::same ([$ test , 'magic ' ], Callback::unwrap (Closure:: fromCallable ([ $ test , 'magic ' ] )));
191184 Assert::same ('Test::magic ' , Callback::toString ([$ test , 'magic ' ]));
192- Assert::same ('{closure Test::magic} ' , Callback::toString (Callback:: closure ( $ test , 'magic ' )));
193- Assert::same ('Test::__call magic * ' , Callback:: closure ( $ test , 'magic ' )->__invoke ('* ' ));
185+ Assert::same ('{closure Test::magic} ' , Callback::toString (Closure:: fromCallable ([ $ test , 'magic ' ] )));
186+ Assert::same ('Test::__call magic * ' , Closure:: fromCallable ([ $ test , 'magic ' ] )->__invoke ('* ' ));
194187
195- Assert::same (['Test ' , 'magic ' ], Callback::unwrap (Callback:: closure ('Test::magic ' )));
188+ Assert::same (['Test ' , 'magic ' ], Callback::unwrap (Closure:: fromCallable ('Test::magic ' )));
196189 Assert::same ('Test::magic ' , Callback::toString ('Test::magic ' ));
197- Assert::same ('{closure Test::magic} ' , Callback::toString (Callback:: closure ('Test::magic ' )));
198- Assert::same ('Test::__callStatic magic * ' , Callback:: closure ('Test::magic ' )->__invoke ('* ' ));
190+ Assert::same ('{closure Test::magic} ' , Callback::toString (Closure:: fromCallable ('Test::magic ' )));
191+ Assert::same ('Test::__callStatic magic * ' , Closure:: fromCallable ('Test::magic ' )->__invoke ('* ' ));
199192
200193 Assert::exception (function () {
201194 Callback::toReflection ([new Test , 'magic ' ]);
202195 }, ReflectionException::class, 'Method Test::magic() does not exist ' );
203196
204197 Assert::exception (function () {
205- Callback::toReflection (Callback:: closure ( new Test , 'magic ' ));
198+ Callback::toReflection (Closure:: fromCallable ([ new Test , 'magic ' ] ));
206199 }, ReflectionException::class, 'Method Test::magic() does not exist ' );
207200});
208201
209202
210203test ('PHP bugs - is_callable($object, true) fails ' , function () {
211- Assert::exception (function () {
212- Callback::closure (new stdClass );
213- }, Nette \InvalidArgumentException::class, 'Failed to create closure from callable: no array or string given ' );
214-
215204 Assert::same ('stdClass::__invoke ' , Callback::toString (new stdClass ));
216205
217206 Assert::exception (function () {
0 commit comments