Drupal investigation

ReflectionCasterTest.php 5.3KB

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