ReflectionCasterTest.php 5.7 KB

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