Drupal investigation

VarClonerTest.php 6.9KB

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