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