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