VarClonerTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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] => Symfony\Component\VarDumper\Cloner\Stub Object
  31. (
  32. [type] => array
  33. [class] => assoc
  34. [value] => 1
  35. [cut] => 0
  36. [handle] => 0
  37. [refCount] => 0
  38. [position] => 1
  39. [attr] => Array
  40. (
  41. )
  42. )
  43. )
  44. [1] => Array
  45. (
  46. [%s] => 123
  47. )
  48. )
  49. [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  50. [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  51. [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
  52. [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  53. [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  54. )
  55. EOTXT;
  56. $this->assertSame(sprintf($expected, PHP_INT_MAX), print_r($clone, true));
  57. }
  58. public function testClone()
  59. {
  60. $json = json_decode('{"1":{"var":"val"},"2":{"var":"val"}}');
  61. $cloner = new VarCloner();
  62. $clone = $cloner->cloneVar($json);
  63. $expected = <<<EOTXT
  64. Symfony\Component\VarDumper\Cloner\Data Object
  65. (
  66. [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
  67. (
  68. [0] => Array
  69. (
  70. [0] => Symfony\Component\VarDumper\Cloner\Stub Object
  71. (
  72. [type] => object
  73. [class] => stdClass
  74. [value] =>
  75. [cut] => 0
  76. [handle] => %i
  77. [refCount] => 0
  78. [position] => 1
  79. [attr] => Array
  80. (
  81. )
  82. )
  83. )
  84. [1] => Array
  85. (
  86. [\000+\0001] => Symfony\Component\VarDumper\Cloner\Stub Object
  87. (
  88. [type] => object
  89. [class] => stdClass
  90. [value] =>
  91. [cut] => 0
  92. [handle] => %i
  93. [refCount] => 0
  94. [position] => 2
  95. [attr] => Array
  96. (
  97. )
  98. )
  99. [\000+\0002] => Symfony\Component\VarDumper\Cloner\Stub Object
  100. (
  101. [type] => object
  102. [class] => stdClass
  103. [value] =>
  104. [cut] => 0
  105. [handle] => %i
  106. [refCount] => 0
  107. [position] => 3
  108. [attr] => Array
  109. (
  110. )
  111. )
  112. )
  113. [2] => Array
  114. (
  115. [\000+\000var] => val
  116. )
  117. [3] => Array
  118. (
  119. [\000+\000var] => val
  120. )
  121. )
  122. [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  123. [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  124. [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
  125. [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  126. [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  127. )
  128. EOTXT;
  129. $this->assertStringMatchesFormat($expected, print_r($clone, true));
  130. }
  131. public function testJsonCast()
  132. {
  133. if (ini_get('xdebug.overload_var_dump') == 2) {
  134. $this->markTestSkipped('xdebug is active');
  135. }
  136. $data = (array) json_decode('{"1":{}}');
  137. $cloner = new VarCloner();
  138. $clone = $cloner->cloneVar($data);
  139. $expected = <<<'EOTXT'
  140. object(Symfony\Component\VarDumper\Cloner\Data)#%i (6) {
  141. ["data":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  142. array(2) {
  143. [0]=>
  144. array(1) {
  145. [0]=>
  146. object(Symfony\Component\VarDumper\Cloner\Stub)#%i (8) {
  147. ["type"]=>
  148. string(5) "array"
  149. ["class"]=>
  150. string(5) "assoc"
  151. ["value"]=>
  152. int(1)
  153. ["cut"]=>
  154. int(0)
  155. ["handle"]=>
  156. int(0)
  157. ["refCount"]=>
  158. int(0)
  159. ["position"]=>
  160. int(1)
  161. ["attr"]=>
  162. array(0) {
  163. }
  164. }
  165. }
  166. [1]=>
  167. array(1) {
  168. ["1"]=>
  169. object(Symfony\Component\VarDumper\Cloner\Stub)#%i (8) {
  170. ["type"]=>
  171. string(6) "object"
  172. ["class"]=>
  173. string(8) "stdClass"
  174. ["value"]=>
  175. NULL
  176. ["cut"]=>
  177. int(0)
  178. ["handle"]=>
  179. int(%i)
  180. ["refCount"]=>
  181. int(0)
  182. ["position"]=>
  183. int(0)
  184. ["attr"]=>
  185. array(0) {
  186. }
  187. }
  188. }
  189. }
  190. ["position":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  191. int(0)
  192. ["key":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  193. int(0)
  194. ["maxDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  195. int(20)
  196. ["maxItemsPerDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  197. int(-1)
  198. ["useRefHandles":"Symfony\Component\VarDumper\Cloner\Data":private]=>
  199. int(-1)
  200. }
  201. EOTXT;
  202. ob_start();
  203. var_dump($clone);
  204. $this->assertStringMatchesFormat($expected, ob_get_clean());
  205. }
  206. public function testCaster()
  207. {
  208. $cloner = new VarCloner(array(
  209. '*' => function ($obj, $array) {
  210. return array('foo' => 123);
  211. },
  212. __CLASS__ => function ($obj, $array) {
  213. ++$array['foo'];
  214. return $array;
  215. },
  216. ));
  217. $clone = $cloner->cloneVar($this);
  218. $expected = <<<EOTXT
  219. Symfony\Component\VarDumper\Cloner\Data Object
  220. (
  221. [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
  222. (
  223. [0] => Array
  224. (
  225. [0] => Symfony\Component\VarDumper\Cloner\Stub Object
  226. (
  227. [type] => object
  228. [class] => %s
  229. [value] =>
  230. [cut] => 0
  231. [handle] => %i
  232. [refCount] => 0
  233. [position] => 1
  234. [attr] => Array
  235. (
  236. )
  237. )
  238. )
  239. [1] => Array
  240. (
  241. [foo] => 124
  242. )
  243. )
  244. [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  245. [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
  246. [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
  247. [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  248. [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  249. )
  250. EOTXT;
  251. $this->assertStringMatchesFormat($expected, print_r($clone, true));
  252. }
  253. }