CliDumperTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. wrapper_type: "plainfile"
  63. stream_type: "STDIO"
  64. mode: "r"
  65. unread_bytes: 0
  66. seekable: true
  67. timed_out: false
  68. blocked: true
  69. eof: false
  70. options: []
  71. }
  72. "obj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d
  73. +foo: "foo"
  74. +"bar": "bar"
  75. }
  76. "closure" => Closure {{$r}{$closure54}
  77. parameters: array:2 [
  78. "\$a" => []
  79. "&\$b" => array:2 [
  80. "typeHint" => "PDO"
  81. "default" => null
  82. ]
  83. ]
  84. file: "{$var['file']}"
  85. line: "{$var['line']} to {$var['line']}"
  86. }
  87. "line" => {$var['line']}
  88. "nobj" => array:1 [
  89. 0 => &3 {#%d}
  90. ]
  91. "recurs" => &4 array:1 [
  92. 0 => &4 array:1 [&4]
  93. ]
  94. 8 => &1 null
  95. "sobj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d}
  96. "snobj" => &3 {#%d}
  97. "snobj2" => {#%d}
  98. "file" => "{$var['file']}"
  99. b"bin-key-é" => ""
  100. ]
  101. EOTXT
  102. ,
  103. $out
  104. );
  105. }
  106. public function testXmlResource()
  107. {
  108. if (!extension_loaded('xml')) {
  109. $this->markTestSkipped('xml extension is required');
  110. }
  111. $var = xml_parser_create();
  112. $this->assertDumpMatchesFormat(
  113. <<<EOTXT
  114. xml resource {
  115. current_byte_index: %i
  116. current_column_number: %i
  117. current_line_number: 1
  118. error_code: XML_ERROR_NONE
  119. }
  120. EOTXT
  121. ,
  122. $var
  123. );
  124. }
  125. public function testClosedResource()
  126. {
  127. if (defined('HHVM_VERSION') && HHVM_VERSION_ID < 30600) {
  128. $this->markTestSkipped();
  129. }
  130. $var = fopen(__FILE__, 'r');
  131. fclose($var);
  132. $dumper = new CliDumper('php://output');
  133. $dumper->setColors(false);
  134. $cloner = new VarCloner();
  135. $data = $cloner->cloneVar($var);
  136. ob_start();
  137. $dumper->dump($data);
  138. $out = ob_get_clean();
  139. $res = (int) $var;
  140. $this->assertStringMatchesFormat(
  141. <<<EOTXT
  142. Unknown resource @{$res}
  143. EOTXT
  144. ,
  145. $out
  146. );
  147. }
  148. public function testThrowingCaster()
  149. {
  150. $out = fopen('php://memory', 'r+b');
  151. $dumper = new CliDumper();
  152. $dumper->setColors(false);
  153. $cloner = new VarCloner();
  154. $cloner->addCasters(array(
  155. ':stream' => function ($res, $a) {
  156. unset($a['wrapper_data']);
  157. return $a;
  158. },
  159. ));
  160. $cloner->addCasters(array(
  161. ':stream' => function () {
  162. throw new \Exception('Foobar');
  163. },
  164. ));
  165. $line = __LINE__ - 3;
  166. $file = __FILE__;
  167. $ref = (int) $out;
  168. $data = $cloner->cloneVar($out);
  169. $dumper->dump($data, $out);
  170. rewind($out);
  171. $out = stream_get_contents($out);
  172. $r = defined('HHVM_VERSION') ? '' : '#%d';
  173. $this->assertStringMatchesFormat(
  174. <<<EOTXT
  175. stream resource {@{$ref}
  176. wrapper_type: "PHP"
  177. stream_type: "MEMORY"
  178. mode: "%s+b"
  179. unread_bytes: 0
  180. seekable: true
  181. uri: "php://memory"
  182. timed_out: false
  183. blocked: true
  184. eof: false
  185. options: []
  186. ⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {{$r}
  187. #message: "Unexpected Exception thrown from a caster: Foobar"
  188. trace: array:1 [
  189. 0 => array:2 [
  190. "call" => "%slosure%s()"
  191. "file" => "{$file}:{$line}"
  192. ]
  193. ]
  194. }
  195. }
  196. EOTXT
  197. ,
  198. $out
  199. );
  200. }
  201. public function testRefsInProperties()
  202. {
  203. $var = (object) array('foo' => 'foo');
  204. $var->bar = &$var->foo;
  205. $dumper = new CliDumper();
  206. $dumper->setColors(false);
  207. $cloner = new VarCloner();
  208. $out = fopen('php://memory', 'r+b');
  209. $data = $cloner->cloneVar($var);
  210. $dumper->dump($data, $out);
  211. rewind($out);
  212. $out = stream_get_contents($out);
  213. $r = defined('HHVM_VERSION') ? '' : '#%d';
  214. $this->assertStringMatchesFormat(
  215. <<<EOTXT
  216. {{$r}
  217. +"foo": &1 "foo"
  218. +"bar": &1 "foo"
  219. }
  220. EOTXT
  221. ,
  222. $out
  223. );
  224. }
  225. /**
  226. * @runInSeparateProcess
  227. * @preserveGlobalState disabled
  228. */
  229. public function testSpecialVars56()
  230. {
  231. if (PHP_VERSION_ID < 50600) {
  232. $this->markTestSkipped('PHP 5.6 is required');
  233. }
  234. $var = $this->getSpecialVars();
  235. $this->assertDumpEquals(
  236. <<<EOTXT
  237. array:3 [
  238. 0 => array:1 [
  239. 0 => &1 array:1 [
  240. 0 => &1 array:1 [&1]
  241. ]
  242. ]
  243. 1 => array:1 [
  244. "GLOBALS" => &2 array:1 [
  245. "GLOBALS" => &2 array:1 [&2]
  246. ]
  247. ]
  248. 2 => &2 array:1 [&2]
  249. ]
  250. EOTXT
  251. ,
  252. $var
  253. );
  254. }
  255. /**
  256. * @runInSeparateProcess
  257. * @preserveGlobalState disabled
  258. */
  259. public function testGlobalsNoExt()
  260. {
  261. $var = $this->getSpecialVars();
  262. unset($var[0]);
  263. $out = '';
  264. $dumper = new CliDumper(function ($line, $depth) use (&$out) {
  265. if ($depth >= 0) {
  266. $out .= str_repeat(' ', $depth).$line."\n";
  267. }
  268. });
  269. $dumper->setColors(false);
  270. $cloner = new VarCloner();
  271. $refl = new \ReflectionProperty($cloner, 'useExt');
  272. $refl->setAccessible(true);
  273. $refl->setValue($cloner, false);
  274. $data = $cloner->cloneVar($var);
  275. $dumper->dump($data);
  276. $this->assertSame(
  277. <<<EOTXT
  278. array:2 [
  279. 1 => array:1 [
  280. "GLOBALS" => &1 array:1 [
  281. "GLOBALS" => &1 array:1 [&1]
  282. ]
  283. ]
  284. 2 => &1 array:1 [&1]
  285. ]
  286. EOTXT
  287. ,
  288. $out
  289. );
  290. }
  291. /**
  292. * @runInSeparateProcess
  293. * @preserveGlobalState disabled
  294. */
  295. public function testBuggyRefs()
  296. {
  297. if (PHP_VERSION_ID >= 50600) {
  298. $this->markTestSkipped('PHP 5.6 fixed refs counting');
  299. }
  300. $var = $this->getSpecialVars();
  301. $var = $var[0];
  302. $dumper = new CliDumper();
  303. $dumper->setColors(false);
  304. $cloner = new VarCloner();
  305. $data = $cloner->cloneVar($var)->withMaxDepth(3);
  306. $out = '';
  307. $dumper->dump($data, function ($line, $depth) use (&$out) {
  308. if ($depth >= 0) {
  309. $out .= str_repeat(' ', $depth).$line."\n";
  310. }
  311. });
  312. $this->assertSame(
  313. <<<EOTXT
  314. array:1 [
  315. 0 => array:1 [
  316. 0 => array:1 [
  317. 0 => array:1 [ …1]
  318. ]
  319. ]
  320. ]
  321. EOTXT
  322. ,
  323. $out
  324. );
  325. }
  326. private function getSpecialVars()
  327. {
  328. foreach (array_keys($GLOBALS) as $var) {
  329. if ('GLOBALS' !== $var) {
  330. unset($GLOBALS[$var]);
  331. }
  332. }
  333. $var = function &() {
  334. $var = array();
  335. $var[] = &$var;
  336. return $var;
  337. };
  338. return array($var(), $GLOBALS, &$GLOBALS);
  339. }
  340. }