PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

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