CliDumperTest.php 11 KB

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