Drupal investigation

CliDumperTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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;
  11. use Symfony\Component\VarDumper\Cloner\VarCloner;
  12. use Symfony\Component\VarDumper\Dumper\CliDumper;
  13. use Symfony\Component\VarDumper\Test\VarDumperTestCase;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class CliDumperTest extends VarDumperTestCase
  18. {
  19. public function testGet()
  20. {
  21. require __DIR__.'/Fixtures/dumb-var.php';
  22. $dumper = new CliDumper('php://output');
  23. $dumper->setColors(false);
  24. $cloner = new VarCloner();
  25. $cloner->addCasters(array(
  26. ':stream' => function ($res, $a) {
  27. unset($a['uri'], $a['wrapper_data']);
  28. return $a;
  29. },
  30. ));
  31. $data = $cloner->cloneVar($var);
  32. ob_start();
  33. $dumper->dump($data);
  34. $out = ob_get_clean();
  35. $out = preg_replace('/[ \t]+$/m', '', $out);
  36. $intMax = PHP_INT_MAX;
  37. $res = (int) $var['res'];
  38. $closure54 = '';
  39. $r = defined('HHVM_VERSION') ? '' : '#%d';
  40. if (PHP_VERSION_ID >= 50400) {
  41. $closure54 = <<<EOTXT
  42. class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
  43. this: Symfony\Component\VarDumper\Tests\CliDumperTest {{$r} …}
  44. EOTXT;
  45. }
  46. $this->assertStringMatchesFormat(
  47. <<<EOTXT
  48. array:24 [
  49. "number" => 1
  50. 0 => &1 null
  51. "const" => 1.1
  52. 1 => true
  53. 2 => false
  54. 3 => NAN
  55. 4 => INF
  56. 5 => -INF
  57. 6 => {$intMax}
  58. "str" => "déjà\\n"
  59. 7 => b"é\\x00"
  60. "[]" => []
  61. "res" => stream resource {@{$res}
  62. %A wrapper_type: "plainfile"
  63. stream_type: "STDIO"
  64. mode: "r"
  65. unread_bytes: 0
  66. seekable: true
  67. %A options: []
  68. }
  69. "obj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d
  70. +foo: "foo"
  71. +"bar": "bar"
  72. }
  73. "closure" => Closure {{$r}{$closure54}
  74. parameters: {
  75. \$a: {}
  76. &\$b: {
  77. typeHint: "PDO"
  78. default: null
  79. }
  80. }
  81. file: "{$var['file']}"
  82. line: "{$var['line']} to {$var['line']}"
  83. }
  84. "line" => {$var['line']}
  85. "nobj" => array:1 [
  86. 0 => &3 {#%d}
  87. ]
  88. "recurs" => &4 array:1 [
  89. 0 => &4 array:1 [&4]
  90. ]
  91. 8 => &1 null
  92. "sobj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d}
  93. "snobj" => &3 {#%d}
  94. "snobj2" => {#%d}
  95. "file" => "{$var['file']}"
  96. b"bin-key-é" => ""
  97. ]
  98. EOTXT
  99. ,
  100. $out
  101. );
  102. }
  103. /**
  104. * @requires extension xml
  105. */
  106. public function testXmlResource()
  107. {
  108. $var = xml_parser_create();
  109. $this->assertDumpMatchesFormat(
  110. <<<'EOTXT'
  111. xml resource {
  112. current_byte_index: %i
  113. current_column_number: %i
  114. current_line_number: 1
  115. error_code: XML_ERROR_NONE
  116. }
  117. EOTXT
  118. ,
  119. $var
  120. );
  121. }
  122. public function testJsonCast()
  123. {
  124. $var = (array) json_decode('{"0":{},"1":null}');
  125. foreach ($var as &$v) {
  126. }
  127. $var[] = &$v;
  128. $var[''] = 2;
  129. $this->assertDumpMatchesFormat(
  130. <<<'EOTXT'
  131. array:4 [
  132. "0" => {}
  133. "1" => &1 null
  134. 0 => &1 null
  135. "" => 2
  136. ]
  137. EOTXT
  138. ,
  139. $var
  140. );
  141. }
  142. public function testObjectCast()
  143. {
  144. $var = (object) array(1 => 1);
  145. $var->{1} = 2;
  146. $this->assertDumpMatchesFormat(
  147. <<<'EOTXT'
  148. {
  149. +1: 1
  150. +"1": 2
  151. }
  152. EOTXT
  153. ,
  154. $var
  155. );
  156. }
  157. public function testClosedResource()
  158. {
  159. if (defined('HHVM_VERSION') && HHVM_VERSION_ID < 30600) {
  160. $this->markTestSkipped();
  161. }
  162. $var = fopen(__FILE__, 'r');
  163. fclose($var);
  164. $dumper = new CliDumper('php://output');
  165. $dumper->setColors(false);
  166. $cloner = new VarCloner();
  167. $data = $cloner->cloneVar($var);
  168. ob_start();
  169. $dumper->dump($data);
  170. $out = ob_get_clean();
  171. $res = (int) $var;
  172. $this->assertStringMatchesFormat(
  173. <<<EOTXT
  174. Closed resource @{$res}
  175. EOTXT
  176. ,
  177. $out
  178. );
  179. }
  180. /**
  181. * @requires function Twig_Template::getSourceContext
  182. */
  183. public function testThrowingCaster()
  184. {
  185. $out = fopen('php://memory', 'r+b');
  186. require_once __DIR__.'/Fixtures/Twig.php';
  187. $twig = new \__TwigTemplate_VarDumperFixture_u75a09(new \Twig_Environment(new \Twig_Loader_Filesystem()));
  188. $dumper = new CliDumper();
  189. $dumper->setColors(false);
  190. $cloner = new VarCloner();
  191. $cloner->addCasters(array(
  192. ':stream' => function ($res, $a) {
  193. unset($a['wrapper_data']);
  194. return $a;
  195. },
  196. ));
  197. $cloner->addCasters(array(
  198. ':stream' => eval('return function () use ($twig) {
  199. try {
  200. $twig->render(array());
  201. } catch (\Twig_Error_Runtime $e) {
  202. throw $e->getPrevious();
  203. }
  204. };'),
  205. ));
  206. $line = __LINE__ - 2;
  207. $ref = (int) $out;
  208. $data = $cloner->cloneVar($out);
  209. $dumper->dump($data, $out);
  210. rewind($out);
  211. $out = stream_get_contents($out);
  212. $r = defined('HHVM_VERSION') ? '' : '#%d';
  213. $this->assertStringMatchesFormat(
  214. <<<EOTXT
  215. stream resource {@{$ref}
  216. %Awrapper_type: "PHP"
  217. stream_type: "MEMORY"
  218. mode: "%s+b"
  219. unread_bytes: 0
  220. seekable: true
  221. uri: "php://memory"
  222. %Aoptions: []
  223. ⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {{$r}
  224. #message: "Unexpected Exception thrown from a caster: Foobar"
  225. -trace: {
  226. %d. __TwigTemplate_VarDumperFixture_u75a09->doDisplay() ==> new Exception(): {
  227. src: {
  228. %sTwig.php:21: """
  229. // line 2\\n
  230. throw new \Exception('Foobar');\\n
  231. }\\n
  232. """
  233. bar.twig:2: """
  234. foo bar\\n
  235. twig source\\n
  236. \\n
  237. """
  238. }
  239. }
  240. %d. Twig_Template->displayWithErrorHandling() ==> __TwigTemplate_VarDumperFixture_u75a09->doDisplay(): {
  241. src: {
  242. %sTemplate.php:%d: """
  243. try {\\n
  244. \$this->doDisplay(\$context, \$blocks);\\n
  245. } catch (Twig_Error \$e) {\\n
  246. """
  247. }
  248. }
  249. %d. Twig_Template->display() ==> Twig_Template->displayWithErrorHandling(): {
  250. src: {
  251. %sTemplate.php:%d: """
  252. {\\n
  253. \$this->displayWithErrorHandling(\$this->env->mergeGlobals(\$context), array_merge(\$this->blocks, \$blocks));\\n
  254. }\\n
  255. """
  256. }
  257. }
  258. %d. Twig_Template->render() ==> Twig_Template->display(): {
  259. src: {
  260. %sTemplate.php:%d: """
  261. try {\\n
  262. \$this->display(\$context);\\n
  263. } catch (%s \$e) {\\n
  264. """
  265. }
  266. }
  267. %d. %slosure%s() ==> Twig_Template->render(): {
  268. src: {
  269. %sCliDumperTest.php:{$line}: """
  270. %A
  271. """
  272. }
  273. }
  274. }
  275. }
  276. }
  277. EOTXT
  278. ,
  279. $out
  280. );
  281. }
  282. public function testRefsInProperties()
  283. {
  284. $var = (object) array('foo' => 'foo');
  285. $var->bar = &$var->foo;
  286. $dumper = new CliDumper();
  287. $dumper->setColors(false);
  288. $cloner = new VarCloner();
  289. $out = fopen('php://memory', 'r+b');
  290. $data = $cloner->cloneVar($var);
  291. $dumper->dump($data, $out);
  292. rewind($out);
  293. $out = stream_get_contents($out);
  294. $r = defined('HHVM_VERSION') ? '' : '#%d';
  295. $this->assertStringMatchesFormat(
  296. <<<EOTXT
  297. {{$r}
  298. +"foo": &1 "foo"
  299. +"bar": &1 "foo"
  300. }
  301. EOTXT
  302. ,
  303. $out
  304. );
  305. }
  306. /**
  307. * @runInSeparateProcess
  308. * @preserveGlobalState disabled
  309. * @requires PHP 5.6
  310. */
  311. public function testSpecialVars56()
  312. {
  313. $var = $this->getSpecialVars();
  314. $this->assertDumpEquals(
  315. <<<'EOTXT'
  316. array:3 [
  317. 0 => array:1 [
  318. 0 => &1 array:1 [
  319. 0 => &1 array:1 [&1]
  320. ]
  321. ]
  322. 1 => array:1 [
  323. "GLOBALS" => &2 array:1 [
  324. "GLOBALS" => &2 array:1 [&2]
  325. ]
  326. ]
  327. 2 => &2 array:1 [&2]
  328. ]
  329. EOTXT
  330. ,
  331. $var
  332. );
  333. }
  334. /**
  335. * @runInSeparateProcess
  336. * @preserveGlobalState disabled
  337. */
  338. public function testGlobalsNoExt()
  339. {
  340. $var = $this->getSpecialVars();
  341. unset($var[0]);
  342. $out = '';
  343. $dumper = new CliDumper(function ($line, $depth) use (&$out) {
  344. if ($depth >= 0) {
  345. $out .= str_repeat(' ', $depth).$line."\n";
  346. }
  347. });
  348. $dumper->setColors(false);
  349. $cloner = new VarCloner();
  350. $refl = new \ReflectionProperty($cloner, 'useExt');
  351. $refl->setAccessible(true);
  352. $refl->setValue($cloner, false);
  353. $data = $cloner->cloneVar($var);
  354. $dumper->dump($data);
  355. $this->assertSame(
  356. <<<'EOTXT'
  357. array:2 [
  358. 1 => array:1 [
  359. "GLOBALS" => &1 array:1 [
  360. "GLOBALS" => &1 array:1 [&1]
  361. ]
  362. ]
  363. 2 => &1 array:1 [&1]
  364. ]
  365. EOTXT
  366. ,
  367. $out
  368. );
  369. }
  370. /**
  371. * @runInSeparateProcess
  372. * @preserveGlobalState disabled
  373. */
  374. public function testBuggyRefs()
  375. {
  376. if (PHP_VERSION_ID >= 50600) {
  377. $this->markTestSkipped('PHP 5.6 fixed refs counting');
  378. }
  379. $var = $this->getSpecialVars();
  380. $var = $var[0];
  381. $dumper = new CliDumper();
  382. $dumper->setColors(false);
  383. $cloner = new VarCloner();
  384. $data = $cloner->cloneVar($var)->withMaxDepth(3);
  385. $out = '';
  386. $dumper->dump($data, function ($line, $depth) use (&$out) {
  387. if ($depth >= 0) {
  388. $out .= str_repeat(' ', $depth).$line."\n";
  389. }
  390. });
  391. $this->assertSame(
  392. <<<'EOTXT'
  393. array:1 [
  394. 0 => array:1 [
  395. 0 => array:1 [
  396. 0 => array:1 [ …1]
  397. ]
  398. ]
  399. ]
  400. EOTXT
  401. ,
  402. $out
  403. );
  404. }
  405. private function getSpecialVars()
  406. {
  407. foreach (array_keys($GLOBALS) as $var) {
  408. if ('GLOBALS' !== $var) {
  409. unset($GLOBALS[$var]);
  410. }
  411. }
  412. $var = function &() {
  413. $var = array();
  414. $var[] = &$var;
  415. return $var;
  416. };
  417. return array($var(), $GLOBALS, &$GLOBALS);
  418. }
  419. }