PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/var-dumper/Tests/CliDumperTest.php

https://gitlab.com/4gdevs/online-class-record-system
PHP | 386 lines | 310 code | 48 blank | 28 comment | 7 complexity | 674920b0a4d52780a406a261e75121f2 MD5 | raw file
  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. /**
  107. * @requires extension xml
  108. */
  109. public function testXmlResource()
  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. * @requires PHP 5.6
  229. */
  230. public function testSpecialVars56()
  231. {
  232. $var = $this->getSpecialVars();
  233. $this->assertDumpEquals(
  234. <<<EOTXT
  235. array:3 [
  236. 0 => array:1 [
  237. 0 => &1 array:1 [
  238. 0 => &1 array:1 [&1]
  239. ]
  240. ]
  241. 1 => array:1 [
  242. "GLOBALS" => &2 array:1 [
  243. "GLOBALS" => &2 array:1 [&2]
  244. ]
  245. ]
  246. 2 => &2 array:1 [&2]
  247. ]
  248. EOTXT
  249. ,
  250. $var
  251. );
  252. }
  253. /**
  254. * @runInSeparateProcess
  255. * @preserveGlobalState disabled
  256. */
  257. public function testGlobalsNoExt()
  258. {
  259. $var = $this->getSpecialVars();
  260. unset($var[0]);
  261. $out = '';
  262. $dumper = new CliDumper(function ($line, $depth) use (&$out) {
  263. if ($depth >= 0) {
  264. $out .= str_repeat(' ', $depth).$line."\n";
  265. }
  266. });
  267. $dumper->setColors(false);
  268. $cloner = new VarCloner();
  269. $refl = new \ReflectionProperty($cloner, 'useExt');
  270. $refl->setAccessible(true);
  271. $refl->setValue($cloner, false);
  272. $data = $cloner->cloneVar($var);
  273. $dumper->dump($data);
  274. $this->assertSame(
  275. <<<EOTXT
  276. array:2 [
  277. 1 => array:1 [
  278. "GLOBALS" => &1 array:1 [
  279. "GLOBALS" => &1 array:1 [&1]
  280. ]
  281. ]
  282. 2 => &1 array:1 [&1]
  283. ]
  284. EOTXT
  285. ,
  286. $out
  287. );
  288. }
  289. /**
  290. * @runInSeparateProcess
  291. * @preserveGlobalState disabled
  292. */
  293. public function testBuggyRefs()
  294. {
  295. if (PHP_VERSION_ID >= 50600) {
  296. $this->markTestSkipped('PHP 5.6 fixed refs counting');
  297. }
  298. $var = $this->getSpecialVars();
  299. $var = $var[0];
  300. $dumper = new CliDumper();
  301. $dumper->setColors(false);
  302. $cloner = new VarCloner();
  303. $data = $cloner->cloneVar($var)->withMaxDepth(3);
  304. $out = '';
  305. $dumper->dump($data, function ($line, $depth) use (&$out) {
  306. if ($depth >= 0) {
  307. $out .= str_repeat(' ', $depth).$line."\n";
  308. }
  309. });
  310. $this->assertSame(
  311. <<<EOTXT
  312. array:1 [
  313. 0 => array:1 [
  314. 0 => array:1 [
  315. 0 => array:1 [ 1]
  316. ]
  317. ]
  318. ]
  319. EOTXT
  320. ,
  321. $out
  322. );
  323. }
  324. private function getSpecialVars()
  325. {
  326. foreach (array_keys($GLOBALS) as $var) {
  327. if ('GLOBALS' !== $var) {
  328. unset($GLOBALS[$var]);
  329. }
  330. }
  331. $var = function &() {
  332. $var = array();
  333. $var[] = &$var;
  334. return $var;
  335. };
  336. return array($var(), $GLOBALS, &$GLOBALS);
  337. }
  338. }