CliDumperTest.php 11 KB

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