1704b258089bf2d776e4b34b761c799162319063
[yaffs-website] / vendor / symfony / var-dumper / Tests / Caster / ReflectionCasterTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\VarDumper\Tests\Caster;
13
14 use Symfony\Component\VarDumper\Test\VarDumperTestCase;
15 use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
16 use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass;
17
18 /**
19  * @author Nicolas Grekas <p@tchwork.com>
20  */
21 class ReflectionCasterTest extends VarDumperTestCase
22 {
23     public function testReflectionCaster()
24     {
25         $var = new \ReflectionClass('ReflectionClass');
26
27         $this->assertDumpMatchesFormat(
28             <<<'EOTXT'
29 ReflectionClass {
30   +name: "ReflectionClass"
31 %Aimplements: array:%d [
32     0 => "Reflector"
33 %A]
34   constants: array:3 [
35     "IS_IMPLICIT_ABSTRACT" => 16
36     "IS_EXPLICIT_ABSTRACT" => 32
37     "IS_FINAL" => %d
38   ]
39   properties: array:%d [
40     "name" => ReflectionProperty {
41 %A    +name: "name"
42       +class: "ReflectionClass"
43 %A    modifiers: "public"
44     }
45 %A]
46   methods: array:%d [
47 %A
48     "export" => ReflectionMethod {
49       +name: "export"
50       +class: "ReflectionClass"
51 %A    parameters: {
52         $%s: ReflectionParameter {
53 %A         position: 0
54 %A
55 }
56 EOTXT
57             , $var
58         );
59     }
60
61     public function testClosureCaster()
62     {
63         $a = $b = 123;
64         $var = function ($x) use ($a, &$b) {};
65
66         $this->assertDumpMatchesFormat(
67             <<<EOTXT
68 Closure {
69 %Aparameters: {
70     \$x: {}
71   }
72   use: {
73     \$a: 123
74     \$b: & 123
75   }
76   file: "%sReflectionCasterTest.php"
77   line: "64 to 64"
78 }
79 EOTXT
80             , $var
81         );
82     }
83
84     public function testReflectionParameter()
85     {
86         $var = new \ReflectionParameter(__NAMESPACE__.'\reflectionParameterFixture', 0);
87
88         $this->assertDumpMatchesFormat(
89             <<<'EOTXT'
90 ReflectionParameter {
91   +name: "arg1"
92   position: 0
93   typeHint: "Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass"
94   default: null
95 }
96 EOTXT
97             , $var
98         );
99     }
100
101     /**
102      * @requires PHP 7.0
103      */
104     public function testReflectionParameterScalar()
105     {
106         $f = eval('return function (int $a) {};');
107         $var = new \ReflectionParameter($f, 0);
108
109         $this->assertDumpMatchesFormat(
110             <<<'EOTXT'
111 ReflectionParameter {
112   +name: "a"
113   position: 0
114   typeHint: "int"
115 }
116 EOTXT
117             , $var
118         );
119     }
120
121     /**
122      * @requires PHP 7.0
123      */
124     public function testReturnType()
125     {
126         $f = eval('return function ():int {};');
127         $line = __LINE__ - 1;
128
129         $this->assertDumpMatchesFormat(
130             <<<EOTXT
131 Closure {
132   returnType: "int"
133   class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
134   this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
135   file: "%sReflectionCasterTest.php($line) : eval()'d code"
136   line: "1 to 1"
137 }
138 EOTXT
139             , $f
140         );
141     }
142
143     /**
144      * @requires PHP 7.0
145      */
146     public function testGenerator()
147     {
148         if (extension_loaded('xdebug')) {
149             $this->markTestSkipped('xdebug is active');
150         }
151
152         $generator = new GeneratorDemo();
153         $generator = $generator->baz();
154
155         $expectedDump = <<<'EODUMP'
156 Generator {
157   this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
158   executing: {
159     Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo->baz(): {
160       %sGeneratorDemo.php:14: """
161         {\n
162             yield from bar();\n
163         }\n
164         """
165     }
166   }
167   closed: false
168 }
169 EODUMP;
170
171         $this->assertDumpMatchesFormat($expectedDump, $generator);
172
173         foreach ($generator as $v) {
174             break;
175         }
176
177         $expectedDump = <<<'EODUMP'
178 array:2 [
179   0 => ReflectionGenerator {
180     this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
181     trace: {
182       3. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo() ==> yield(): {
183         src: {
184           %sGeneratorDemo.php:9: """
185             {\n
186                 yield 1;\n
187             }\n
188             """
189         }
190       }
191       2. Symfony\Component\VarDumper\Tests\Fixtures\bar() ==> Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo(): {
192         src: {
193           %sGeneratorDemo.php:20: """
194             {\n
195                 yield from GeneratorDemo::foo();\n
196             }\n
197             """
198         }
199       }
200       1. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo->baz() ==> Symfony\Component\VarDumper\Tests\Fixtures\bar(): {
201         src: {
202           %sGeneratorDemo.php:14: """
203             {\n
204                 yield from bar();\n
205             }\n
206             """
207         }
208       }
209     }
210     closed: false
211   }
212   1 => Generator {
213     executing: {
214       Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo(): {
215         %sGeneratorDemo.php:10: """
216               yield 1;\n
217           }\n
218           \n
219           """
220       }
221     }
222     closed: false
223   }
224 ]
225 EODUMP;
226
227         $r = new \ReflectionGenerator($generator);
228         $this->assertDumpMatchesFormat($expectedDump, array($r, $r->getExecutingGenerator()));
229
230         foreach ($generator as $v) {
231         }
232
233         $expectedDump = <<<'EODUMP'
234 Generator {
235   closed: true
236 }
237 EODUMP;
238         $this->assertDumpMatchesFormat($expectedDump, $generator);
239     }
240 }
241
242 function reflectionParameterFixture(NotLoadableClass $arg1 = null, $arg2)
243 {
244 }