CliDumperTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class CliDumperTest extends \PHPUnit_Framework_TestCase
  18. {
  19. use VarDumperTestTrait;
  20. public function testGet()
  21. {
  22. require __DIR__.'/Fixtures/dumb-var.php';
  23. $dumper = new CliDumper('php://output');
  24. $dumper->setColors(false);
  25. $cloner = new VarCloner();
  26. $cloner->addCasters(array(
  27. ':stream' => function ($res, $a) {
  28. unset($a['uri'], $a['wrapper_data']);
  29. return $a;
  30. },
  31. ));
  32. $data = $cloner->cloneVar($var);
  33. ob_start();
  34. $dumper->dump($data);
  35. $out = ob_get_clean();
  36. $out = preg_replace('/[ \t]+$/m', '', $out);
  37. $intMax = PHP_INT_MAX;
  38. $res = (int) $var['res'];
  39. $r = defined('HHVM_VERSION') ? '' : '#%d';
  40. $this->assertStringMatchesFormat(
  41. <<<EOTXT
  42. array:24 [
  43. "number" => 1
  44. 0 => &1 null
  45. "const" => 1.1
  46. 1 => true
  47. 2 => false
  48. 3 => NAN
  49. 4 => INF
  50. 5 => -INF
  51. 6 => {$intMax}
  52. "str" => "déjà\\n"
  53. 7 => b"é\\x00"
  54. "[]" => []
  55. "res" => stream resource {@{$res}
  56. %A wrapper_type: "plainfile"
  57. stream_type: "STDIO"
  58. mode: "r"
  59. unread_bytes: 0
  60. seekable: true
  61. %A options: []
  62. }
  63. "obj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d
  64. +foo: "foo"
  65. +"bar": "bar"
  66. }
  67. "closure" => Closure {{$r}
  68. class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
  69. this: Symfony\Component\VarDumper\Tests\CliDumperTest {{$r} …}
  70. parameters: {
  71. \$a: {}
  72. &\$b: {
  73. typeHint: "PDO"
  74. default: null
  75. }
  76. }
  77. file: "{$var['file']}"
  78. line: "{$var['line']} to {$var['line']}"
  79. }
  80. "line" => {$var['line']}
  81. "nobj" => array:1 [
  82. 0 => &3 {#%d}
  83. ]
  84. "recurs" => &4 array:1 [
  85. 0 => &4 array:1 [&4]
  86. ]
  87. 8 => &1 null
  88. "sobj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d}
  89. "snobj" => &3 {#%d}
  90. "snobj2" => {#%d}
  91. "file" => "{$var['file']}"
  92. b"bin-key-é" => ""
  93. ]
  94. EOTXT
  95. ,
  96. $out
  97. );
  98. }
  99. /**
  100. * @requires extension xml
  101. */
  102. public function testXmlResource()
  103. {
  104. $var = xml_parser_create();
  105. $this->assertDumpMatchesFormat(
  106. <<<EOTXT
  107. xml resource {
  108. current_byte_index: %i
  109. current_column_number: %i
  110. current_line_number: 1
  111. error_code: XML_ERROR_NONE
  112. }
  113. EOTXT
  114. ,
  115. $var
  116. );
  117. }
  118. public function testJsonCast()
  119. {
  120. $var = (array) json_decode('{"0":{},"1":null}');
  121. foreach ($var as &$v) {
  122. }
  123. $var[] = &$v;
  124. $var[''] = 2;
  125. $this->assertDumpMatchesFormat(
  126. <<<EOTXT
  127. array:4 [
  128. "0" => {}
  129. "1" => &1 null
  130. 0 => &1 null
  131. "" => 2
  132. ]
  133. EOTXT
  134. ,
  135. $var
  136. );
  137. }
  138. public function testObjectCast()
  139. {
  140. $var = (object) array(1 => 1);
  141. $var->{1} = 2;
  142. $this->assertDumpMatchesFormat(
  143. <<<EOTXT
  144. {
  145. +1: 1
  146. +"1": 2
  147. }
  148. EOTXT
  149. ,
  150. $var
  151. );
  152. }
  153. public function testClosedResource()
  154. {
  155. if (defined('HHVM_VERSION') && HHVM_VERSION_ID < 30600) {
  156. $this->markTestSkipped();
  157. }
  158. $var = fopen(__FILE__, 'r');
  159. fclose($var);
  160. $dumper = new CliDumper('php://output');
  161. $dumper->setColors(false);
  162. $cloner = new VarCloner();
  163. $data = $cloner->cloneVar($var);
  164. ob_start();
  165. $dumper->dump($data);
  166. $out = ob_get_clean();
  167. $res = (int) $var;
  168. $this->assertStringMatchesFormat(
  169. <<<EOTXT
  170. Unknown resource @{$res}
  171. EOTXT
  172. ,
  173. $out
  174. );
  175. }
  176. public function testThrowingCaster()
  177. {
  178. $out = fopen('php://memory', 'r+b');
  179. require_once __DIR__.'/Fixtures/Twig.php';
  180. $twig = new \__TwigTemplate_VarDumperFixture_u75a09(new \Twig_Environment(new \Twig_Loader_Filesystem()));
  181. $dumper = new CliDumper();
  182. $dumper->setColors(false);
  183. $cloner = new VarCloner();
  184. $cloner->addCasters(array(
  185. ':stream' => function ($res, $a) {
  186. unset($a['wrapper_data']);
  187. return $a;
  188. },
  189. ));
  190. $cloner->addCasters(array(
  191. ':stream' => eval('return function () use ($twig) {
  192. try {
  193. $twig->render(array());
  194. } catch (\Twig_Error_Runtime $e) {
  195. throw $e->getPrevious();
  196. }
  197. };'),
  198. ));
  199. $line = __LINE__ - 2;
  200. $ref = (int) $out;
  201. $data = $cloner->cloneVar($out);
  202. $dumper->dump($data, $out);
  203. rewind($out);
  204. $out = stream_get_contents($out);
  205. if (method_exists($twig, 'getSource')) {
  206. $twig = <<<EOTXT
  207. foo.twig:2: """
  208. foo bar\\n
  209. twig source\\n
  210. \\n
  211. """
  212. EOTXT;
  213. } else {
  214. $twig = '';
  215. }
  216. $r = defined('HHVM_VERSION') ? '' : '#%d';
  217. $this->assertStringMatchesFormat(
  218. <<<EOTXT
  219. stream resource {@{$ref}
  220. %Awrapper_type: "PHP"
  221. stream_type: "MEMORY"
  222. mode: "%s+b"
  223. unread_bytes: 0
  224. seekable: true
  225. uri: "php://memory"
  226. %Aoptions: []
  227. ⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {{$r}
  228. #message: "Unexpected Exception thrown from a caster: Foobar"
  229. -trace: {
  230. %d. __TwigTemplate_VarDumperFixture_u75a09->doDisplay() ==> new Exception(): {
  231. src: {
  232. %sTwig.php:19: """
  233. // line 2\\n
  234. throw new \Exception('Foobar');\\n
  235. }\\n
  236. """
  237. {$twig} }
  238. }
  239. %d. Twig_Template->displayWithErrorHandling() ==> __TwigTemplate_VarDumperFixture_u75a09->doDisplay(): {
  240. src: {
  241. %sTemplate.php:%d: """
  242. try {\\n
  243. \$this->doDisplay(\$context, \$blocks);\\n
  244. } catch (Twig_Error \$e) {\\n
  245. """
  246. }
  247. }
  248. %d. Twig_Template->display() ==> Twig_Template->displayWithErrorHandling(): {
  249. src: {
  250. %sTemplate.php:%d: """
  251. {\\n
  252. \$this->displayWithErrorHandling(\$this->env->mergeGlobals(\$context), array_merge(\$this->blocks, \$blocks));\\n
  253. }\\n
  254. """
  255. }
  256. }
  257. %d. Twig_Template->render() ==> Twig_Template->display(): {
  258. src: {
  259. %sTemplate.php:%d: """
  260. try {\\n
  261. \$this->display(\$context);\\n
  262. } catch (Exception \$e) {\\n
  263. """
  264. }
  265. }
  266. %d. %slosure%s() ==> Twig_Template->render(): {
  267. src: {
  268. %sCliDumperTest.php:{$line}: """
  269. }\\n
  270. };'),\\n
  271. ));\\n
  272. """
  273. }
  274. }
  275. }
  276. }
  277. }
  278. EOTXT
  279. ,
  280. $out
  281. );
  282. }
  283. public function testRefsInProperties()
  284. {
  285. $var = (object) array('foo' => 'foo');
  286. $var->bar = &$var->foo;
  287. $dumper = new CliDumper();
  288. $dumper->setColors(false);
  289. $cloner = new VarCloner();
  290. $out = fopen('php://memory', 'r+b');
  291. $data = $cloner->cloneVar($var);
  292. $dumper->dump($data, $out);
  293. rewind($out);
  294. $out = stream_get_contents($out);
  295. $r = defined('HHVM_VERSION') ? '' : '#%d';
  296. $this->assertStringMatchesFormat(
  297. <<<EOTXT
  298. {{$r}
  299. +"foo": &1 "foo"
  300. +"bar": &1 "foo"
  301. }
  302. EOTXT
  303. ,
  304. $out
  305. );
  306. }
  307. /**
  308. * @runInSeparateProcess
  309. * @preserveGlobalState disabled
  310. * @requires PHP 5.6
  311. */
  312. public function testSpecialVars56()
  313. {
  314. $var = $this->getSpecialVars();
  315. $this->assertDumpEquals(
  316. <<<EOTXT
  317. array:3 [
  318. 0 => array:1 [
  319. 0 => &1 array:1 [
  320. 0 => &1 array:1 [&1]
  321. ]
  322. ]
  323. 1 => array:1 [
  324. "GLOBALS" => &2 array:1 [
  325. "GLOBALS" => &2 array:1 [&2]
  326. ]
  327. ]
  328. 2 => &2 array:1 [&2]
  329. ]
  330. EOTXT
  331. ,
  332. $var
  333. );
  334. }
  335. /**
  336. * @runInSeparateProcess
  337. * @preserveGlobalState disabled
  338. */
  339. public function testGlobalsNoExt()
  340. {
  341. $var = $this->getSpecialVars();
  342. unset($var[0]);
  343. $out = '';
  344. $dumper = new CliDumper(function ($line, $depth) use (&$out) {
  345. if ($depth >= 0) {
  346. $out .= str_repeat(' ', $depth).$line."\n";
  347. }
  348. });
  349. $dumper->setColors(false);
  350. $cloner = new VarCloner();
  351. $refl = new \ReflectionProperty($cloner, 'useExt');
  352. $refl->setAccessible(true);
  353. $refl->setValue($cloner, false);
  354. $data = $cloner->cloneVar($var);
  355. $dumper->dump($data);
  356. $this->assertSame(
  357. <<<EOTXT
  358. array:2 [
  359. 1 => array:1 [
  360. "GLOBALS" => &1 array:1 [
  361. "GLOBALS" => &1 array:1 [&1]
  362. ]
  363. ]
  364. 2 => &1 array:1 [&1]
  365. ]
  366. EOTXT
  367. ,
  368. $out
  369. );
  370. }
  371. /**
  372. * @runInSeparateProcess
  373. * @preserveGlobalState disabled
  374. */
  375. public function testBuggyRefs()
  376. {
  377. if (PHP_VERSION_ID >= 50600) {
  378. $this->markTestSkipped('PHP 5.6 fixed refs counting');
  379. }
  380. $var = $this->getSpecialVars();
  381. $var = $var[0];
  382. $dumper = new CliDumper();
  383. $dumper->setColors(false);
  384. $cloner = new VarCloner();
  385. $data = $cloner->cloneVar($var)->withMaxDepth(3);
  386. $out = '';
  387. $dumper->dump($data, function ($line, $depth) use (&$out) {
  388. if ($depth >= 0) {
  389. $out .= str_repeat(' ', $depth).$line."\n";
  390. }
  391. });
  392. $this->assertSame(
  393. <<<EOTXT
  394. array:1 [
  395. 0 => array:1 [
  396. 0 => array:1 [
  397. 0 => array:1 [ …1]
  398. ]
  399. ]
  400. ]
  401. EOTXT
  402. ,
  403. $out
  404. );
  405. }
  406. private function getSpecialVars()
  407. {
  408. foreach (array_keys($GLOBALS) as $var) {
  409. if ('GLOBALS' !== $var) {
  410. unset($GLOBALS[$var]);
  411. }
  412. }
  413. $var = function &() {
  414. $var = array();
  415. $var[] = &$var;
  416. return $var;
  417. };
  418. return array($var(), $GLOBALS, &$GLOBALS);
  419. }
  420. }