VarClonerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. */
  15. class VarClonerTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testMaxIntBoundary()
  18. {
  19. $data = array(PHP_INT_MAX => 123);
  20. $cloner = new VarCloner();
  21. $clone = $cloner->cloneVar($data);
  22. $expected = <<<EOTXT
  23. Symfony\Component\VarDumper\Cloner\Data Object
  24. (
  25. [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
  26. (
  27. [0] => Array
  28. (
  29. [0] => Symfony\Component\VarDumper\Cloner\Stub Object
  30. (
  31. [type] => array
  32. [class] => assoc
  33. [value] => 1
  34. [cut] => 0
  35. [handle] => 0
  36. [refCount] => 0
  37. [position] => 1
  38. )
  39. )
  40. [1] => Array
  41. (
  42. [%s] => 123
  43. )
  44. )
  45. [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
  46. [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  47. [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  48. )
  49. EOTXT;
  50. $this->assertSame(sprintf($expected, PHP_INT_MAX), print_r($clone, true));
  51. }
  52. public function testClone()
  53. {
  54. $json = json_decode('{"1":{"var":"val"},"2":{"var":"val"}}');
  55. $cloner = new VarCloner();
  56. $clone = $cloner->cloneVar($json);
  57. $expected = <<<EOTXT
  58. Symfony\Component\VarDumper\Cloner\Data Object
  59. (
  60. [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
  61. (
  62. [0] => Array
  63. (
  64. [0] => Symfony\Component\VarDumper\Cloner\Stub Object
  65. (
  66. [type] => object
  67. [class] => stdClass
  68. [value] =>
  69. [cut] => 0
  70. [handle] => %i
  71. [refCount] => 0
  72. [position] => 1
  73. )
  74. )
  75. [1] => Array
  76. (
  77. [\000+\0001] => Symfony\Component\VarDumper\Cloner\Stub Object
  78. (
  79. [type] => object
  80. [class] => stdClass
  81. [value] =>
  82. [cut] => 0
  83. [handle] => %i
  84. [refCount] => 0
  85. [position] => 2
  86. )
  87. [\000+\0002] => Symfony\Component\VarDumper\Cloner\Stub Object
  88. (
  89. [type] => object
  90. [class] => stdClass
  91. [value] =>
  92. [cut] => 0
  93. [handle] => %i
  94. [refCount] => 0
  95. [position] => 3
  96. )
  97. )
  98. [2] => Array
  99. (
  100. [\000+\000var] => val
  101. )
  102. [3] => Array
  103. (
  104. [\000+\000var] => val
  105. )
  106. )
  107. [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
  108. [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  109. [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  110. )
  111. EOTXT;
  112. $this->assertStringMatchesFormat($expected, print_r($clone, true));
  113. }
  114. public function testCaster()
  115. {
  116. $cloner = new VarCloner(array(
  117. '*' => function ($obj, $array) {
  118. return array('foo' => 123);
  119. },
  120. __CLASS__ => function ($obj, $array) {
  121. ++$array['foo'];
  122. return $array;
  123. },
  124. ));
  125. $clone = $cloner->cloneVar($this);
  126. $expected = <<<EOTXT
  127. Symfony\Component\VarDumper\Cloner\Data Object
  128. (
  129. [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
  130. (
  131. [0] => Array
  132. (
  133. [0] => Symfony\Component\VarDumper\Cloner\Stub Object
  134. (
  135. [type] => object
  136. [class] => %s
  137. [value] =>
  138. [cut] => 0
  139. [handle] => %i
  140. [refCount] => 0
  141. [position] => 1
  142. )
  143. )
  144. [1] => Array
  145. (
  146. [foo] => 124
  147. )
  148. )
  149. [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
  150. [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  151. [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
  152. )
  153. EOTXT;
  154. $this->assertStringMatchesFormat($expected, print_r($clone, true));
  155. }
  156. }