CliDumperTest.php 11 KB

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