ReflectionCasterTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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" => 32
  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 {
  65. %Aparameters: {
  66. \$x: {}
  67. }
  68. use: {
  69. \$a: 123
  70. \$b: & 123
  71. }
  72. file: "%sReflectionCasterTest.php"
  73. line: "68 to 68"
  74. }
  75. EOTXT
  76. , $var
  77. );
  78. }
  79. public function testClosureCasterExcludingVerbosity()
  80. {
  81. $var = function () {};
  82. $expectedDump = <<<EOTXT
  83. Closure {
  84. class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
  85. this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
  86. }
  87. EOTXT;
  88. $this->assertDumpEquals($expectedDump, $var, Caster::EXCLUDE_VERBOSE);
  89. }
  90. public function testReflectionParameter()
  91. {
  92. $var = new \ReflectionParameter(__NAMESPACE__.'\reflectionParameterFixture', 0);
  93. $this->assertDumpMatchesFormat(
  94. <<<'EOTXT'
  95. ReflectionParameter {
  96. +name: "arg1"
  97. position: 0
  98. typeHint: "Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass"
  99. default: null
  100. }
  101. EOTXT
  102. , $var
  103. );
  104. }
  105. public function testReflectionParameterScalar()
  106. {
  107. $f = eval('return function (int $a) {};');
  108. $var = new \ReflectionParameter($f, 0);
  109. $this->assertDumpMatchesFormat(
  110. <<<'EOTXT'
  111. ReflectionParameter {
  112. +name: "a"
  113. position: 0
  114. typeHint: "int"
  115. }
  116. EOTXT
  117. , $var
  118. );
  119. }
  120. public function testReturnType()
  121. {
  122. $f = eval('return function ():int {};');
  123. $line = __LINE__ - 1;
  124. $this->assertDumpMatchesFormat(
  125. <<<EOTXT
  126. Closure {
  127. returnType: "int"
  128. class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
  129. this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
  130. file: "%sReflectionCasterTest.php($line) : eval()'d code"
  131. line: "1 to 1"
  132. }
  133. EOTXT
  134. , $f
  135. );
  136. }
  137. public function testGenerator()
  138. {
  139. if (\extension_loaded('xdebug')) {
  140. $this->markTestSkipped('xdebug is active');
  141. }
  142. $generator = new GeneratorDemo();
  143. $generator = $generator->baz();
  144. $expectedDump = <<<'EODUMP'
  145. Generator {
  146. this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
  147. executing: {
  148. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo->baz() {
  149. %sGeneratorDemo.php:14 {
  150. › {
  151. › yield from bar();
  152. › }
  153. }
  154. }
  155. }
  156. closed: false
  157. }
  158. EODUMP;
  159. $this->assertDumpMatchesFormat($expectedDump, $generator);
  160. foreach ($generator as $v) {
  161. break;
  162. }
  163. $expectedDump = <<<'EODUMP'
  164. array:2 [
  165. 0 => ReflectionGenerator {
  166. this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
  167. trace: {
  168. %s%eTests%eFixtures%eGeneratorDemo.php:9 {
  169. › {
  170. › yield 1;
  171. › }
  172. }
  173. %s%eTests%eFixtures%eGeneratorDemo.php:20 { …}
  174. %s%eTests%eFixtures%eGeneratorDemo.php:14 { …}
  175. }
  176. closed: false
  177. }
  178. 1 => Generator {
  179. executing: {
  180. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo() {
  181. %sGeneratorDemo.php:10 {
  182. › yield 1;
  183. › }
  184. }
  185. }
  186. }
  187. closed: false
  188. }
  189. ]
  190. EODUMP;
  191. $r = new \ReflectionGenerator($generator);
  192. $this->assertDumpMatchesFormat($expectedDump, array($r, $r->getExecutingGenerator()));
  193. foreach ($generator as $v) {
  194. }
  195. $expectedDump = <<<'EODUMP'
  196. Generator {
  197. closed: true
  198. }
  199. EODUMP;
  200. $this->assertDumpMatchesFormat($expectedDump, $generator);
  201. }
  202. }
  203. function reflectionParameterFixture(NotLoadableClass $arg1 = null, $arg2)
  204. {
  205. }