VarClonerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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\Cloner;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\VarDumper\Cloner\VarCloner;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class VarClonerTest extends TestCase
  17. {
  18. public function testMaxIntBoundary()
  19. {
  20. $data = array(PHP_INT_MAX => 123);
  21. $cloner = new VarCloner();
  22. $clone = $cloner->cloneVar($data);
  23. $expected = <<<EOTXT
  24. Symfony\Component\VarDumper\Cloner\Data Object
  25. (
  26. [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
  27. (
  28. [0] => Array
  29. (
  30. [0] => Array
  31. (
  32. [1] => 1
  33. )
  34. )
  35. [1] => Array
  36. (
  37. [%s] => 123
  38. )
  39. )
  40. [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  41. [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  42. [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
  43. [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  44. [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  45. )
  46. EOTXT;
  47. $this->assertSame(sprintf($expected, PHP_INT_MAX), print_r($clone, true));
  48. }
  49. public function testClone()
  50. {
  51. $json = json_decode('{"1":{"var":"val"},"2":{"var":"val"}}');
  52. $cloner = new VarCloner();
  53. $clone = $cloner->cloneVar($json);
  54. $expected = <<<EOTXT
  55. Symfony\Component\VarDumper\Cloner\Data Object
  56. (
  57. [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
  58. (
  59. [0] => Array
  60. (
  61. [0] => Symfony\Component\VarDumper\Cloner\Stub Object
  62. (
  63. [type] => 4
  64. [class] => stdClass
  65. [value] =>
  66. [cut] => 0
  67. [handle] => %i
  68. [refCount] => 0
  69. [position] => 1
  70. [attr] => Array
  71. (
  72. )
  73. )
  74. )
  75. [1] => Array
  76. (
  77. [\000+\0001] => Symfony\Component\VarDumper\Cloner\Stub Object
  78. (
  79. [type] => 4
  80. [class] => stdClass
  81. [value] =>
  82. [cut] => 0
  83. [handle] => %i
  84. [refCount] => 0
  85. [position] => 2
  86. [attr] => Array
  87. (
  88. )
  89. )
  90. [\000+\0002] => Symfony\Component\VarDumper\Cloner\Stub Object
  91. (
  92. [type] => 4
  93. [class] => stdClass
  94. [value] =>
  95. [cut] => 0
  96. [handle] => %i
  97. [refCount] => 0
  98. [position] => 3
  99. [attr] => Array
  100. (
  101. )
  102. )
  103. )
  104. [2] => Array
  105. (
  106. [\000+\000var] => val
  107. )
  108. [3] => Array
  109. (
  110. [\000+\000var] => val
  111. )
  112. )
  113. [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  114. [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  115. [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
  116. [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  117. [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  118. )
  119. EOTXT;
  120. $this->assertStringMatchesFormat($expected, print_r($clone, true));
  121. }
  122. public function testLimits()
  123. {
  124. // Level 0:
  125. $data = array(
  126. // Level 1:
  127. array(
  128. // Level 2:
  129. array(
  130. // Level 3:
  131. 'Level 3 Item 0',
  132. 'Level 3 Item 1',
  133. 'Level 3 Item 2',
  134. 'Level 3 Item 3',
  135. ),
  136. array(
  137. 'Level 3 Item 4',
  138. 'Level 3 Item 5',
  139. 'Level 3 Item 6',
  140. ),
  141. array(
  142. 'Level 3 Item 7',
  143. ),
  144. ),
  145. array(
  146. array(
  147. 'Level 3 Item 8',
  148. ),
  149. 'Level 2 Item 0',
  150. ),
  151. array(
  152. 'Level 2 Item 1',
  153. ),
  154. 'Level 1 Item 0',
  155. array(
  156. // Test setMaxString:
  157. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  158. 'SHORT',
  159. ),
  160. );
  161. $cloner = new VarCloner();
  162. $cloner->setMinDepth(2);
  163. $cloner->setMaxItems(5);
  164. $cloner->setMaxString(20);
  165. $clone = $cloner->cloneVar($data);
  166. $expected = <<<EOTXT
  167. Symfony\Component\VarDumper\Cloner\Data Object
  168. (
  169. [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
  170. (
  171. [0] => Array
  172. (
  173. [0] => Array
  174. (
  175. [2] => 1
  176. )
  177. )
  178. [1] => Array
  179. (
  180. [0] => Array
  181. (
  182. [2] => 2
  183. )
  184. [1] => Array
  185. (
  186. [2] => 3
  187. )
  188. [2] => Array
  189. (
  190. [2] => 4
  191. )
  192. [3] => Level 1 Item 0
  193. [4] => Array
  194. (
  195. [2] => 5
  196. )
  197. )
  198. [2] => Array
  199. (
  200. [0] => Array
  201. (
  202. [2] => 6
  203. )
  204. [1] => Array
  205. (
  206. [0] => 2
  207. [2] => 7
  208. )
  209. [2] => Array
  210. (
  211. [0] => 1
  212. [2] => 0
  213. )
  214. )
  215. [3] => Array
  216. (
  217. [0] => Array
  218. (
  219. [0] => 1
  220. [2] => 0
  221. )
  222. [1] => Level 2 Item 0
  223. )
  224. [4] => Array
  225. (
  226. [0] => Level 2 Item 1
  227. )
  228. [5] => Array
  229. (
  230. [0] => Symfony\Component\VarDumper\Cloner\Stub Object
  231. (
  232. [type] => 2
  233. [class] => 2
  234. [value] => ABCDEFGHIJKLMNOPQRST
  235. [cut] => 6
  236. [handle] => 0
  237. [refCount] => 0
  238. [position] => 0
  239. [attr] => Array
  240. (
  241. )
  242. )
  243. [1] => SHORT
  244. )
  245. [6] => Array
  246. (
  247. [0] => Level 3 Item 0
  248. [1] => Level 3 Item 1
  249. [2] => Level 3 Item 2
  250. [3] => Level 3 Item 3
  251. )
  252. [7] => Array
  253. (
  254. [0] => Level 3 Item 4
  255. )
  256. )
  257. [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  258. [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  259. [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
  260. [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  261. [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  262. )
  263. EOTXT;
  264. $this->assertStringMatchesFormat($expected, print_r($clone, true));
  265. }
  266. public function testJsonCast()
  267. {
  268. if (2 == ini_get('xdebug.overload_var_dump')) {
  269. $this->markTestSkipped('xdebug is active');
  270. }
  271. $data = (array) json_decode('{"1":{}}');
  272. $cloner = new VarCloner();
  273. $clone = $cloner->cloneVar($data);
  274. $expected = <<<'EOTXT'
  275. object(Symfony\Component\VarDumper\Cloner\Data)#%i (6) {
  276. ["data":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  277. array(2) {
  278. [0]=>
  279. array(1) {
  280. [0]=>
  281. array(1) {
  282. [1]=>
  283. int(1)
  284. }
  285. }
  286. [1]=>
  287. array(1) {
  288. ["1"]=>
  289. object(Symfony\Component\VarDumper\Cloner\Stub)#%i (8) {
  290. ["type"]=>
  291. int(4)
  292. ["class"]=>
  293. string(8) "stdClass"
  294. ["value"]=>
  295. NULL
  296. ["cut"]=>
  297. int(0)
  298. ["handle"]=>
  299. int(%i)
  300. ["refCount"]=>
  301. int(0)
  302. ["position"]=>
  303. int(0)
  304. ["attr"]=>
  305. array(0) {
  306. }
  307. }
  308. }
  309. }
  310. ["position":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  311. int(0)
  312. ["key":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  313. int(0)
  314. ["maxDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  315. int(20)
  316. ["maxItemsPerDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  317. int(-1)
  318. ["useRefHandles":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  319. int(-1)
  320. }
  321. EOTXT;
  322. ob_start();
  323. var_dump($clone);
  324. $this->assertStringMatchesFormat(\PHP_VERSION_ID >= 70200 ? str_replace('"1"', '1', $expected) : $expected, ob_get_clean());
  325. }
  326. public function testCaster()
  327. {
  328. $cloner = new VarCloner(array(
  329. '*' => function ($obj, $array) {
  330. return array('foo' => 123);
  331. },
  332. __CLASS__ => function ($obj, $array) {
  333. ++$array['foo'];
  334. return $array;
  335. },
  336. ));
  337. $clone = $cloner->cloneVar($this);
  338. $expected = <<<EOTXT
  339. Symfony\Component\VarDumper\Cloner\Data Object
  340. (
  341. [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
  342. (
  343. [0] => Array
  344. (
  345. [0] => Symfony\Component\VarDumper\Cloner\Stub Object
  346. (
  347. [type] => 4
  348. [class] => %s
  349. [value] =>
  350. [cut] => 0
  351. [handle] => %i
  352. [refCount] => 0
  353. [position] => 1
  354. [attr] => Array
  355. (
  356. )
  357. )
  358. )
  359. [1] => Array
  360. (
  361. [foo] => 124
  362. )
  363. )
  364. [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  365. [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  366. [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
  367. [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  368. [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  369. )
  370. EOTXT;
  371. $this->assertStringMatchesFormat($expected, print_r($clone, true));
  372. }
  373. }