ReflectionCasterTest.php 5.8 KB

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