ReflectionCasterTest.php 4.9 KB

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