菜谱项目

CliDumperTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. $r = defined('HHVM_VERSION') ? '' : '#%d';
  43. $this->assertStringMatchesFormat(
  44. <<<EOTXT
  45. array:24 [
  46. "number" => 1
  47. 0 => &1 null
  48. "const" => 1.1
  49. 1 => true
  50. 2 => false
  51. 3 => NAN
  52. 4 => INF
  53. 5 => -INF
  54. 6 => {$intMax}
  55. "str" => "déjà\\n"
  56. 7 => b"é\\x00"
  57. "[]" => []
  58. "res" => stream resource {@{$res}
  59. %A wrapper_type: "plainfile"
  60. stream_type: "STDIO"
  61. mode: "r"
  62. unread_bytes: 0
  63. seekable: true
  64. %A options: []
  65. }
  66. "obj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d
  67. +foo: "foo"
  68. +"bar": "bar"
  69. }
  70. "closure" => Closure {{$r}
  71. class: "Symfony\Component\VarDumper\Tests\Dumper\CliDumperTest"
  72. this: Symfony\Component\VarDumper\Tests\Dumper\CliDumperTest {{$r} …}
  73. parameters: {
  74. \$a: {}
  75. &\$b: {
  76. typeHint: "PDO"
  77. default: null
  78. }
  79. }
  80. file: "{$var['file']}"
  81. line: "{$var['line']} to {$var['line']}"
  82. }
  83. "line" => {$var['line']}
  84. "nobj" => array:1 [
  85. 0 => &3 {#%d}
  86. ]
  87. "recurs" => &4 array:1 [
  88. 0 => &4 array:1 [&4]
  89. ]
  90. 8 => &1 null
  91. "sobj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d}
  92. "snobj" => &3 {#%d}
  93. "snobj2" => {#%d}
  94. "file" => "{$var['file']}"
  95. b"bin-key-é" => ""
  96. ]
  97. EOTXT
  98. ,
  99. $out
  100. );
  101. }
  102. /**
  103. * @dataProvider provideDumpWithCommaFlagTests
  104. */
  105. public function testDumpWithCommaFlag($expected, $flags)
  106. {
  107. $dumper = new CliDumper(null, null, $flags);
  108. $dumper->setColors(false);
  109. $cloner = new VarCloner();
  110. $var = array(
  111. 'array' => array('a', 'b'),
  112. 'string' => 'hello',
  113. 'multiline string' => "this\nis\na\multiline\nstring",
  114. );
  115. $dump = $dumper->dump($cloner->cloneVar($var), true);
  116. $this->assertSame($expected, $dump);
  117. }
  118. public function testDumpWithCommaFlagsAndExceptionCodeExcerpt()
  119. {
  120. $dumper = new CliDumper(null, null, CliDumper::DUMP_TRAILING_COMMA);
  121. $dumper->setColors(false);
  122. $cloner = new VarCloner();
  123. $ex = new \RuntimeException('foo');
  124. $dump = $dumper->dump($cloner->cloneVar($ex)->withRefHandles(false), true);
  125. $this->assertStringMatchesFormat(<<<'EOTXT'
  126. RuntimeException {
  127. #message: "foo"
  128. #code: 0
  129. #file: "%ACliDumperTest.php"
  130. #line: %d
  131. trace: {
  132. %ACliDumperTest.php:%d: {
  133. :
  134. : $ex = new \RuntimeException('foo');
  135. :
  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 array($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 array($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) array(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. if (defined('HHVM_VERSION') && HHVM_VERSION_ID < 30600) {
  262. $this->markTestSkipped();
  263. }
  264. $var = fopen(__FILE__, 'r');
  265. fclose($var);
  266. $dumper = new CliDumper('php://output');
  267. $dumper->setColors(false);
  268. $cloner = new VarCloner();
  269. $data = $cloner->cloneVar($var);
  270. ob_start();
  271. $dumper->dump($data);
  272. $out = ob_get_clean();
  273. $res = (int) $var;
  274. $this->assertStringMatchesFormat(
  275. <<<EOTXT
  276. Closed resource @{$res}
  277. EOTXT
  278. ,
  279. $out
  280. );
  281. }
  282. public function testFlags()
  283. {
  284. putenv('DUMP_LIGHT_ARRAY=1');
  285. putenv('DUMP_STRING_LENGTH=1');
  286. $var = array(
  287. range(1, 3),
  288. array('foo', 2 => 'bar'),
  289. );
  290. $this->assertDumpEquals(
  291. <<<EOTXT
  292. [
  293. [
  294. 1
  295. 2
  296. 3
  297. ]
  298. [
  299. 0 => (3) "foo"
  300. 2 => (3) "bar"
  301. ]
  302. ]
  303. EOTXT
  304. ,
  305. $var
  306. );
  307. putenv('DUMP_LIGHT_ARRAY=');
  308. putenv('DUMP_STRING_LENGTH=');
  309. }
  310. /**
  311. * @requires function Twig\Template::getSourceContext
  312. */
  313. public function testThrowingCaster()
  314. {
  315. $out = fopen('php://memory', 'r+b');
  316. require_once __DIR__.'/../Fixtures/Twig.php';
  317. $twig = new \__TwigTemplate_VarDumperFixture_u75a09(new Environment(new FilesystemLoader()));
  318. $dumper = new CliDumper();
  319. $dumper->setColors(false);
  320. $cloner = new VarCloner();
  321. $cloner->addCasters(array(
  322. ':stream' => function ($res, $a) {
  323. unset($a['wrapper_data']);
  324. return $a;
  325. },
  326. ));
  327. $cloner->addCasters(array(
  328. ':stream' => eval('return function () use ($twig) {
  329. try {
  330. $twig->render(array());
  331. } catch (\Twig\Error\RuntimeError $e) {
  332. throw $e->getPrevious();
  333. }
  334. };'),
  335. ));
  336. $ref = (int) $out;
  337. $data = $cloner->cloneVar($out);
  338. $dumper->dump($data, $out);
  339. $out = stream_get_contents($out, -1, 0);
  340. $r = defined('HHVM_VERSION') ? '' : '#%d';
  341. $this->assertStringMatchesFormat(
  342. <<<EOTXT
  343. stream resource {@{$ref}
  344. ⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {{$r}
  345. #message: "Unexpected Exception thrown from a caster: Foobar"
  346. trace: {
  347. %sTwig.php:2: {
  348. : foo bar
  349. : twig source
  350. :
  351. }
  352. %sTemplate.php:%d: {
  353. : try {
  354. : \$this->doDisplay(\$context, \$blocks);
  355. : } catch (Twig%sError \$e) {
  356. }
  357. %sTemplate.php:%d: {
  358. : {
  359. : \$this->displayWithErrorHandling(\$this->env->mergeGlobals(\$context), array_merge(\$this->blocks, \$blocks));
  360. : }
  361. }
  362. %sTemplate.php:%d: {
  363. : try {
  364. : \$this->display(\$context);
  365. : } catch (%s \$e) {
  366. }
  367. %sCliDumperTest.php:%d: {
  368. %A
  369. }
  370. }
  371. }
  372. %Awrapper_type: "PHP"
  373. stream_type: "MEMORY"
  374. mode: "%s+b"
  375. unread_bytes: 0
  376. seekable: true
  377. uri: "php://memory"
  378. %Aoptions: []
  379. }
  380. EOTXT
  381. ,
  382. $out
  383. );
  384. }
  385. public function testRefsInProperties()
  386. {
  387. $var = (object) array('foo' => 'foo');
  388. $var->bar = &$var->foo;
  389. $dumper = new CliDumper();
  390. $dumper->setColors(false);
  391. $cloner = new VarCloner();
  392. $data = $cloner->cloneVar($var);
  393. $out = $dumper->dump($data, true);
  394. $r = defined('HHVM_VERSION') ? '' : '#%d';
  395. $this->assertStringMatchesFormat(
  396. <<<EOTXT
  397. {{$r}
  398. +"foo": &1 "foo"
  399. +"bar": &1 "foo"
  400. }
  401. EOTXT
  402. ,
  403. $out
  404. );
  405. }
  406. /**
  407. * @runInSeparateProcess
  408. * @preserveGlobalState disabled
  409. * @requires PHP 5.6
  410. */
  411. public function testSpecialVars56()
  412. {
  413. $var = $this->getSpecialVars();
  414. $this->assertDumpEquals(
  415. <<<'EOTXT'
  416. array:3 [
  417. 0 => array:1 [
  418. 0 => &1 array:1 [
  419. 0 => &1 array:1 [&1]
  420. ]
  421. ]
  422. 1 => array:1 [
  423. "GLOBALS" => &2 array:1 [
  424. "GLOBALS" => &2 array:1 [&2]
  425. ]
  426. ]
  427. 2 => &2 array:1 [&2]
  428. ]
  429. EOTXT
  430. ,
  431. $var
  432. );
  433. }
  434. /**
  435. * @runInSeparateProcess
  436. * @preserveGlobalState disabled
  437. */
  438. public function testGlobalsNoExt()
  439. {
  440. $var = $this->getSpecialVars();
  441. unset($var[0]);
  442. $out = '';
  443. $dumper = new CliDumper(function ($line, $depth) use (&$out) {
  444. if ($depth >= 0) {
  445. $out .= str_repeat(' ', $depth).$line."\n";
  446. }
  447. });
  448. $dumper->setColors(false);
  449. $cloner = new VarCloner();
  450. $refl = new \ReflectionProperty($cloner, 'useExt');
  451. $refl->setAccessible(true);
  452. $refl->setValue($cloner, false);
  453. $data = $cloner->cloneVar($var);
  454. $dumper->dump($data);
  455. $this->assertSame(
  456. <<<'EOTXT'
  457. array:2 [
  458. 1 => array:1 [
  459. "GLOBALS" => &1 array:1 [
  460. "GLOBALS" => &1 array:1 [&1]
  461. ]
  462. ]
  463. 2 => &1 array:1 [&1]
  464. ]
  465. EOTXT
  466. ,
  467. $out
  468. );
  469. }
  470. /**
  471. * @runInSeparateProcess
  472. * @preserveGlobalState disabled
  473. */
  474. public function testBuggyRefs()
  475. {
  476. if (\PHP_VERSION_ID >= 50600) {
  477. $this->markTestSkipped('PHP 5.6 fixed refs counting');
  478. }
  479. $var = $this->getSpecialVars();
  480. $var = $var[0];
  481. $dumper = new CliDumper();
  482. $dumper->setColors(false);
  483. $cloner = new VarCloner();
  484. $data = $cloner->cloneVar($var)->withMaxDepth(3);
  485. $out = '';
  486. $dumper->dump($data, function ($line, $depth) use (&$out) {
  487. if ($depth >= 0) {
  488. $out .= str_repeat(' ', $depth).$line."\n";
  489. }
  490. });
  491. $this->assertSame(
  492. <<<'EOTXT'
  493. array:1 [
  494. 0 => array:1 [
  495. 0 => array:1 [
  496. 0 => array:1 [ …1]
  497. ]
  498. ]
  499. ]
  500. EOTXT
  501. ,
  502. $out
  503. );
  504. }
  505. public function testIncompleteClass()
  506. {
  507. $unserializeCallbackHandler = ini_set('unserialize_callback_func', null);
  508. $var = unserialize('O:8:"Foo\Buzz":0:{}');
  509. ini_set('unserialize_callback_func', $unserializeCallbackHandler);
  510. $this->assertDumpMatchesFormat(
  511. <<<EOTXT
  512. __PHP_Incomplete_Class(Foo\Buzz) {}
  513. EOTXT
  514. ,
  515. $var
  516. );
  517. }
  518. private function getSpecialVars()
  519. {
  520. foreach (array_keys($GLOBALS) as $var) {
  521. if ('GLOBALS' !== $var) {
  522. unset($GLOBALS[$var]);
  523. }
  524. }
  525. $var = function &() {
  526. $var = array();
  527. $var[] = &$var;
  528. return $var;
  529. };
  530. return array($var(), $GLOBALS, &$GLOBALS);
  531. }
  532. }