1--TEST--
2Testing Closure::fromCallable() functionality: Errors
3--FILE--
4<?php
5
6include('closure_from_callable.inc');
7
8echo 'Cannot access privateInstance method statically'."\n";
9try {
10    $fn = Closure::fromCallable(['Foo', 'privateInstanceFunc']);
11    echo "Test failed to fail and return was : ".var_export($fn, true)."\n";
12}
13catch (\TypeError $te) {
14    //This is the expected outcome.
15}
16catch (\Throwable $t) {
17    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
18}
19
20
21echo 'Cannot access privateInstance method statically with colon scheme'."\n";
22try {
23    $fn = Closure::fromCallable('Foo::privateInstanceFunc');
24    echo "Test failed to fail and return was : ".var_export($fn, true)."\n";
25}
26catch (\TypeError $te) {
27    //This is the expected outcome.
28}
29catch (\Throwable $t) {
30    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
31}
32
33echo 'Cannot access privateInstance method'."\n";
34try {
35    $fn = Closure::fromCallable([new Foo, 'privateInstanceFunc']);
36    echo "Test failed to fail and return was : ".var_export($fn, true)."\n";
37}
38catch (\TypeError $te) {
39    //This is the expected outcome.
40}
41catch (\Throwable $t) {
42    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
43}
44
45echo 'SubClass cannot access private instance method'."\n";
46try {
47    $fn = Closure::fromCallable([new SubFoo, 'privateInstanceFunc']);
48    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
49}
50catch (\TypeError $te) {
51    //This is the expected outcome.
52}
53catch (\Throwable $t) {
54    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
55}
56
57echo 'Cannot access private static function of instance'."\n";
58try {
59    $fn = Closure::fromCallable([new Foo, 'privateStaticFunction']);
60    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
61}
62catch (\TypeError $te) {
63    //This is the expected outcome.
64}
65catch (\Throwable $t) {
66    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
67}
68
69echo 'Cannot access private static method statically'."\n";
70try {
71    $fn = Closure::fromCallable(['Foo', 'privateStaticFunction']);
72    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
73}
74catch (\TypeError $te) {
75    //This is the expected outcome.
76}
77catch (\Throwable $t) {
78    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
79}
80
81echo 'Cannot access private static method statically with colon scheme'."\n";
82try {
83    $fn = Closure::fromCallable('Foo::privateStaticFunction');
84    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
85}
86catch (\TypeError $te) {
87    //This is the expected outcome.
88}
89catch (\Throwable $t) {
90    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
91}
92
93echo 'Non-existent method should fail'."\n";
94try {
95    $fn = Closure::fromCallable('Foo::nonExistentFunction');
96    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
97}
98catch (\TypeError $te) {
99    //This is the expected outcome.
100}
101catch (\Throwable $t) {
102    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
103}
104
105echo 'Non-existent class should fail'."\n";
106try {
107    $fn = Closure::fromCallable(['NonExistentClass', 'foo']);
108    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
109}
110catch (\TypeError $te) {
111    //This is the expected outcome.
112}
113catch (\Throwable $t) {
114    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
115}
116
117echo 'Non-existent function should fail'."\n";
118try {
119    $fn = Closure::fromCallable('thisDoesNotExist');
120    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
121}
122catch (\TypeError $te) {
123    //This is the expected outcome.
124}
125catch (\Throwable $t) {
126    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
127}
128
129
130echo 'Subclass cannot closure over parent private instance method'."\n";
131try {
132    $subFoo = new SubFoo;
133    $fn = $subFoo->closePrivateInvalid();
134    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
135}
136catch (\TypeError $te) {
137    //This is the expected outcome.
138}
139catch (\Throwable $t) {
140    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
141}
142
143echo 'Subclass cannot closure over parent private static method'."\n";
144try {
145    $subFoo = new SubFoo;
146    $fn = $subFoo->closePrivateStaticInvalid();
147    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
148}
149catch (\TypeError $te) {
150    //This is the expected outcome.
151}
152catch (\Throwable $t) {
153    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
154}
155
156echo 'Function scope cannot closure over protected instance method'."\n";
157try {
158    $fn = functionAccessProtected();
159    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
160}
161catch (\TypeError $te) {
162    //This is the expected outcome.
163}
164catch (\Throwable $t) {
165    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
166}
167
168echo 'Function scope cannot closure over private instance method'."\n";
169try {
170    $fn = functionAccessPrivate();
171    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
172}
173catch (\TypeError $te) {
174    //This is the expected outcome.
175}
176catch (\Throwable $t) {
177    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
178}
179
180echo 'Access private instance method of parent object through "self::" to parent method'."\n";
181try {
182    $foo = new SubFoo;
183    $fn = $foo->getSelfColonParentPrivateInstanceMethod();
184    echo "Test failed to fail, closure is : ".var_export($fn, true)."\n";
185}
186catch (\TypeError $te) {
187    //This is the expected outcome.
188}
189catch (\Throwable $t) {
190    echo "Wrong exception type thrown: ".get_class($t)." : ".$t->getMessage()."\n";
191}
192
193echo "OK\n";
194
195?>
196--EXPECTF--
197Cannot access privateInstance method statically
198Cannot access privateInstance method statically with colon scheme
199Cannot access privateInstance method
200SubClass cannot access private instance method
201Cannot access private static function of instance
202Cannot access private static method statically
203Cannot access private static method statically with colon scheme
204Non-existent method should fail
205Non-existent class should fail
206Non-existent function should fail
207Subclass cannot closure over parent private instance method
208Subclass cannot closure over parent private static method
209Function scope cannot closure over protected instance method
210Function scope cannot closure over private instance method
211Access private instance method of parent object through "self::" to parent method
212
213Deprecated: Use of "self" in callables is deprecated in %s on line %d
214OK
215