83f6b60074250d6711d3ac1d0334d2e2ef4a2e2e
[yaffs-website] / vendor / symfony / var-dumper / Tests / Cloner / DataTest.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\Cloner;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\VarDumper\Caster\Caster;
16 use Symfony\Component\VarDumper\Caster\ClassStub;
17 use Symfony\Component\VarDumper\Cloner\Data;
18 use Symfony\Component\VarDumper\Cloner\VarCloner;
19
20 class DataTest extends TestCase
21 {
22     public function testBasicData()
23     {
24         $values = array(1 => 123, 4.5, 'abc', null, false);
25         $data = $this->cloneVar($values);
26         $clonedValues = array();
27
28         $this->assertInstanceOf(Data::class, $data);
29         $this->assertCount(count($values), $data);
30         $this->assertFalse(isset($data->{0}));
31         $this->assertFalse(isset($data[0]));
32
33         foreach ($data as $k => $v) {
34             $this->assertTrue(isset($data->{$k}));
35             $this->assertTrue(isset($data[$k]));
36             $this->assertSame(gettype($values[$k]), $data->seek($k)->getType());
37             $this->assertSame($values[$k], $data->seek($k)->getValue());
38             $this->assertSame($values[$k], $data->{$k});
39             $this->assertSame($values[$k], $data[$k]);
40             $this->assertSame((string) $values[$k], (string) $data->seek($k));
41
42             $clonedValues[$k] = $v->getValue();
43         }
44
45         $this->assertSame($values, $clonedValues);
46     }
47
48     public function testObject()
49     {
50         $data = $this->cloneVar(new \Exception('foo'));
51
52         $this->assertSame('Exception', $data->getType());
53
54         $this->assertSame('foo', $data->message);
55         $this->assertSame('foo', $data->{Caster::PREFIX_PROTECTED.'message'});
56
57         $this->assertSame('foo', $data['message']);
58         $this->assertSame('foo', $data[Caster::PREFIX_PROTECTED.'message']);
59
60         $this->assertStringMatchesFormat('Exception (count=%d)', (string) $data);
61     }
62
63     public function testArray()
64     {
65         $values = array(array(), array(123));
66         $data = $this->cloneVar($values);
67
68         $this->assertSame($values, $data->getValue(true));
69
70         $children = $data->getValue();
71
72         $this->assertInternalType('array', $children);
73
74         $this->assertInstanceOf(Data::class, $children[0]);
75         $this->assertInstanceOf(Data::class, $children[1]);
76
77         $this->assertEquals($children[0], $data[0]);
78         $this->assertEquals($children[1], $data[1]);
79
80         $this->assertSame($values[0], $children[0]->getValue(true));
81         $this->assertSame($values[1], $children[1]->getValue(true));
82     }
83
84     public function testStub()
85     {
86         $data = $this->cloneVar(array(new ClassStub('stdClass')));
87         $data = $data[0];
88
89         $this->assertSame('string', $data->getType());
90         $this->assertSame('stdClass', $data->getValue());
91         $this->assertSame('stdClass', (string) $data);
92     }
93
94     public function testHardRefs()
95     {
96         $values = array(array());
97         $values[1] = &$values[0];
98         $values[2][0] = &$values[2];
99
100         $data = $this->cloneVar($values);
101
102         $this->assertSame(array(), $data[0]->getValue());
103         $this->assertSame(array(), $data[1]->getValue());
104         $this->assertEquals(array($data[2]->getValue()), $data[2]->getValue(true));
105
106         $this->assertSame('array (count=3)', (string) $data);
107     }
108
109     private function cloneVar($value)
110     {
111         $cloner = new VarCloner();
112
113         return $cloner->cloneVar($value);
114     }
115 }