CliDumperTest.php 11 KB

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