ReflectionCasterTest.php 6.0 KB

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