Yaffs site version 1.1
[yaffs-website] / vendor / symfony / var-dumper / Tests / VarClonerTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\VarDumper\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\VarDumper\Cloner\VarCloner;
16
17 /**
18  * @author Nicolas Grekas <p@tchwork.com>
19  */
20 class VarClonerTest extends TestCase
21 {
22     public function testMaxIntBoundary()
23     {
24         $data = array(PHP_INT_MAX => 123);
25
26         $cloner = new VarCloner();
27         $clone = $cloner->cloneVar($data);
28
29         $expected = <<<EOTXT
30 Symfony\Component\VarDumper\Cloner\Data Object
31 (
32     [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
33         (
34             [0] => Array
35                 (
36                     [0] => Symfony\Component\VarDumper\Cloner\Stub Object
37                         (
38                             [type] => array
39                             [class] => assoc
40                             [value] => 1
41                             [cut] => 0
42                             [handle] => 0
43                             [refCount] => 0
44                             [position] => 1
45                         )
46
47                 )
48
49             [1] => Array
50                 (
51                     [%s] => 123
52                 )
53
54         )
55
56     [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
57     [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
58     [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
59 )
60
61 EOTXT;
62         $this->assertSame(sprintf($expected, PHP_INT_MAX), print_r($clone, true));
63     }
64
65     public function testClone()
66     {
67         $json = json_decode('{"1":{"var":"val"},"2":{"var":"val"}}');
68
69         $cloner = new VarCloner();
70         $clone = $cloner->cloneVar($json);
71
72         $expected = <<<EOTXT
73 Symfony\Component\VarDumper\Cloner\Data Object
74 (
75     [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
76         (
77             [0] => Array
78                 (
79                     [0] => Symfony\Component\VarDumper\Cloner\Stub Object
80                         (
81                             [type] => object
82                             [class] => stdClass
83                             [value] => 
84                             [cut] => 0
85                             [handle] => %i
86                             [refCount] => 0
87                             [position] => 1
88                         )
89
90                 )
91
92             [1] => Array
93                 (
94                     [\000+\0001] => Symfony\Component\VarDumper\Cloner\Stub Object
95                         (
96                             [type] => object
97                             [class] => stdClass
98                             [value] => 
99                             [cut] => 0
100                             [handle] => %i
101                             [refCount] => 0
102                             [position] => 2
103                         )
104
105                     [\000+\0002] => Symfony\Component\VarDumper\Cloner\Stub Object
106                         (
107                             [type] => object
108                             [class] => stdClass
109                             [value] => 
110                             [cut] => 0
111                             [handle] => %i
112                             [refCount] => 0
113                             [position] => 3
114                         )
115
116                 )
117
118             [2] => Array
119                 (
120                     [\000+\000var] => val
121                 )
122
123             [3] => Array
124                 (
125                     [\000+\000var] => val
126                 )
127
128         )
129
130     [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
131     [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
132     [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
133 )
134
135 EOTXT;
136         $this->assertStringMatchesFormat($expected, print_r($clone, true));
137     }
138
139     public function testJsonCast()
140     {
141         if (ini_get('xdebug.overload_var_dump') == 2) {
142             $this->markTestSkipped('xdebug is active');
143         }
144
145         $data = (array) json_decode('{"1":{}}');
146
147         $cloner = new VarCloner();
148         $clone = $cloner->cloneVar($data);
149
150         $expected = <<<'EOTXT'
151 object(Symfony\Component\VarDumper\Cloner\Data)#%i (4) {
152   ["data":"Symfony\Component\VarDumper\Cloner\Data":private]=>
153   array(2) {
154     [0]=>
155     array(1) {
156       [0]=>
157       object(Symfony\Component\VarDumper\Cloner\Stub)#%i (7) {
158         ["type"]=>
159         string(5) "array"
160         ["class"]=>
161         string(5) "assoc"
162         ["value"]=>
163         int(1)
164         ["cut"]=>
165         int(0)
166         ["handle"]=>
167         int(0)
168         ["refCount"]=>
169         int(0)
170         ["position"]=>
171         int(1)
172       }
173     }
174     [1]=>
175     array(1) {
176       ["1"]=>
177       object(Symfony\Component\VarDumper\Cloner\Stub)#%i (7) {
178         ["type"]=>
179         string(6) "object"
180         ["class"]=>
181         string(8) "stdClass"
182         ["value"]=>
183         NULL
184         ["cut"]=>
185         int(0)
186         ["handle"]=>
187         int(%i)
188         ["refCount"]=>
189         int(0)
190         ["position"]=>
191         int(0)
192       }
193     }
194   }
195   ["maxDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
196   int(20)
197   ["maxItemsPerDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
198   int(-1)
199   ["useRefHandles":"Symfony\Component\VarDumper\Cloner\Data":private]=>
200   int(-1)
201 }
202
203 EOTXT;
204         ob_start();
205         var_dump($clone);
206         $this->assertStringMatchesFormat($expected, ob_get_clean());
207     }
208
209     public function testCaster()
210     {
211         $cloner = new VarCloner(array(
212             '*' => function ($obj, $array) {
213                 return array('foo' => 123);
214             },
215             __CLASS__ => function ($obj, $array) {
216                 ++$array['foo'];
217
218                 return $array;
219             },
220         ));
221         $clone = $cloner->cloneVar($this);
222
223         $expected = <<<EOTXT
224 Symfony\Component\VarDumper\Cloner\Data Object
225 (
226     [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
227         (
228             [0] => Array
229                 (
230                     [0] => Symfony\Component\VarDumper\Cloner\Stub Object
231                         (
232                             [type] => object
233                             [class] => %s
234                             [value] => 
235                             [cut] => 0
236                             [handle] => %i
237                             [refCount] => 0
238                             [position] => 1
239                         )
240
241                 )
242
243             [1] => Array
244                 (
245                     [foo] => 124
246                 )
247
248         )
249
250     [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
251     [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
252     [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
253 )
254
255 EOTXT;
256         $this->assertStringMatchesFormat($expected, print_r($clone, true));
257     }
258 }