ReflectionCasterTest.php 5.7 KB

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