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