Aucune description

CliDumperTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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;
  11. use Symfony\Component\VarDumper\Cloner\VarCloner;
  12. use Symfony\Component\VarDumper\Dumper\CliDumper;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class CliDumperTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testGet()
  19. {
  20. require __DIR__.'/Fixtures/dumb-var.php';
  21. $dumper = new CliDumper('php://output');
  22. $dumper->setColors(false);
  23. $cloner = new VarCloner();
  24. $cloner->addCasters(array(
  25. ':stream' => function ($res, $a) {
  26. unset($a['uri']);
  27. return $a;
  28. },
  29. ));
  30. $data = $cloner->cloneVar($var);
  31. ob_start();
  32. $dumper->dump($data);
  33. $out = ob_get_clean();
  34. $closureLabel = PHP_VERSION_ID >= 50400 ? 'public method' : 'function';
  35. $out = preg_replace('/[ \t]+$/m', '', $out);
  36. $intMax = PHP_INT_MAX;
  37. $res1 = (int) $var['res'];
  38. $res2 = (int) $var[8];
  39. $this->assertStringMatchesFormat(
  40. <<<EOTXT
  41. array:25 [
  42. "number" => 1
  43. 0 => &1 null
  44. "const" => 1.1
  45. 1 => true
  46. 2 => false
  47. 3 => NAN
  48. 4 => INF
  49. 5 => -INF
  50. 6 => {$intMax}
  51. "str" => "déjà"
  52. 7 => b"é@"
  53. "[]" => []
  54. "res" => :stream {@{$res1}
  55. wrapper_type: "plainfile"
  56. stream_type: "STDIO"
  57. mode: "r"
  58. unread_bytes: 0
  59. seekable: true
  60. timed_out: false
  61. blocked: true
  62. eof: false
  63. options: []
  64. }
  65. 8 => :Unknown {@{$res2}}
  66. "obj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d
  67. +foo: "foo"
  68. +"bar": "bar"
  69. }
  70. "closure" => Closure {#%d
  71. reflection: """
  72. Closure [ <user> {$closureLabel} Symfony\Component\VarDumper\Tests\Fixture\{closure} ] {
  73. @@ {$var['file']} {$var['line']} - {$var['line']}
  74. - Parameters [2] {
  75. Parameter #0 [ <required> \$a ]
  76. Parameter #1 [ <optional> PDO or NULL &\$b = NULL ]
  77. }
  78. }
  79. """
  80. }
  81. "line" => {$var['line']}
  82. "nobj" => array:1 [
  83. 0 => &3 {#%d}
  84. ]
  85. "recurs" => &4 array:1 [
  86. 0 => &4 array:1 [&4]
  87. ]
  88. 9 => &1 null
  89. "sobj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d}
  90. "snobj" => &3 {#%d}
  91. "snobj2" => {#%d}
  92. "file" => "{$var['file']}"
  93. b"bin-key-é" => ""
  94. ]
  95. EOTXT
  96. ,
  97. $out
  98. );
  99. }
  100. /**
  101. * @runInSeparateProcess
  102. * @preserveGlobalState disabled
  103. */
  104. public function testSpecialVars56()
  105. {
  106. if (PHP_VERSION_ID < 50600) {
  107. $this->markTestSkipped('PHP 5.6 is required');
  108. }
  109. $var = $this->getSpecialVars();
  110. $dumper = new CliDumper();
  111. $dumper->setColors(false);
  112. $cloner = new VarCloner();
  113. $data = $cloner->cloneVar($var);
  114. $out = fopen('php://memory', 'r+b');
  115. $dumper->dump($data, $out);
  116. rewind($out);
  117. $out = stream_get_contents($out);
  118. $this->assertSame(
  119. <<<EOTXT
  120. array:3 [
  121. 0 => array:1 [
  122. 0 => &1 array:1 [
  123. 0 => &1 array:1 [&1]
  124. ]
  125. ]
  126. 1 => array:1 [
  127. "GLOBALS" => &2 array:1 [
  128. "GLOBALS" => &2 array:1 [&2]
  129. ]
  130. ]
  131. 2 => &2 array:1 [&2]
  132. ]
  133. EOTXT
  134. ,
  135. $out
  136. );
  137. }
  138. /**
  139. * @runInSeparateProcess
  140. * @preserveGlobalState disabled
  141. */
  142. public function testGlobalsNoExt()
  143. {
  144. $var = $this->getSpecialVars();
  145. unset($var[0]);
  146. $out = '';
  147. $dumper = new CliDumper(function ($line, $depth) use (&$out) {
  148. if ($depth >= 0) {
  149. $out .= str_repeat(' ', $depth).$line."\n";
  150. }
  151. });
  152. $dumper->setColors(false);
  153. $cloner = new VarCloner();
  154. $refl = new \ReflectionProperty($cloner, 'useExt');
  155. $refl->setAccessible(true);
  156. $refl->setValue($cloner, false);
  157. $data = $cloner->cloneVar($var);
  158. $dumper->dump($data);
  159. $this->assertSame(
  160. <<<EOTXT
  161. array:2 [
  162. 1 => array:1 [
  163. "GLOBALS" => &1 array:1 [
  164. "GLOBALS" => &1 array:1 [&1]
  165. ]
  166. ]
  167. 2 => &1 array:1 [&1]
  168. ]
  169. EOTXT
  170. ,
  171. $out
  172. );
  173. }
  174. /**
  175. * @runInSeparateProcess
  176. * @preserveGlobalState disabled
  177. */
  178. public function testBuggyRefs()
  179. {
  180. if (PHP_VERSION_ID >= 50600) {
  181. $this->markTestSkipped('PHP 5.6 fixed refs counting');
  182. }
  183. $var = $this->getSpecialVars();
  184. $var = $var[0];
  185. $dumper = new CliDumper();
  186. $dumper->setColors(false);
  187. $cloner = new VarCloner();
  188. $data = $cloner->cloneVar($var)->getLimitedClone(3, -1);
  189. $out = '';
  190. $dumper->dump($data, function ($line, $depth) use (&$out) {
  191. if ($depth >= 0) {
  192. $out .= str_repeat(' ', $depth).$line."\n";
  193. }
  194. });
  195. $this->assertSame(
  196. <<<EOTXT
  197. array:1 [
  198. 0 => array:1 [
  199. 0 => array:1 [
  200. 0 => array:1 [
  201. …1
  202. ]
  203. ]
  204. ]
  205. ]
  206. EOTXT
  207. ,
  208. $out
  209. );
  210. }
  211. private function getSpecialVars()
  212. {
  213. foreach (array_keys($GLOBALS) as $var) {
  214. if ('GLOBALS' !== $var) {
  215. unset($GLOBALS[$var]);
  216. }
  217. }
  218. $var = function &() {
  219. $var = array();
  220. $var[] =& $var;
  221. return $var;
  222. };
  223. return array($var(), $GLOBALS, &$GLOBALS);
  224. }
  225. }