ReflectionCasterTest.php 4.8 KB

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