Version 1
[yaffs-website] / vendor / symfony / var-dumper / Tests / Caster / CasterTest.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\Caster;
13
14 use Symfony\Component\VarDumper\Caster\Caster;
15 use Symfony\Component\VarDumper\Test\VarDumperTestCase;
16
17 /**
18  * @author Nicolas Grekas <p@tchwork.com>
19  */
20 class CasterTest extends VarDumperTestCase
21 {
22     private $referenceArray = array(
23         'null' => null,
24         'empty' => false,
25         'public' => 'pub',
26         "\0~\0virtual" => 'virt',
27         "\0+\0dynamic" => 'dyn',
28         "\0*\0protected" => 'prot',
29         "\0Foo\0private" => 'priv',
30     );
31
32     /**
33      * @dataProvider provideFilter
34      */
35     public function testFilter($filter, $expectedDiff, $listedProperties = null)
36     {
37         if (null === $listedProperties) {
38             $filteredArray = Caster::filter($this->referenceArray, $filter);
39         } else {
40             $filteredArray = Caster::filter($this->referenceArray, $filter, $listedProperties);
41         }
42
43         $this->assertSame($expectedDiff, array_diff_assoc($this->referenceArray, $filteredArray));
44     }
45
46     public function provideFilter()
47     {
48         return array(
49             array(
50                 0,
51                 array(),
52             ),
53             array(
54                 Caster::EXCLUDE_PUBLIC,
55                 array(
56                     'null' => null,
57                     'empty' => false,
58                     'public' => 'pub',
59                 ),
60             ),
61             array(
62                 Caster::EXCLUDE_NULL,
63                 array(
64                     'null' => null,
65                 ),
66             ),
67             array(
68                 Caster::EXCLUDE_EMPTY,
69                 array(
70                     'null' => null,
71                     'empty' => false,
72                 ),
73             ),
74             array(
75                 Caster::EXCLUDE_VIRTUAL,
76                 array(
77                     "\0~\0virtual" => 'virt',
78                 ),
79             ),
80             array(
81                 Caster::EXCLUDE_DYNAMIC,
82                 array(
83                     "\0+\0dynamic" => 'dyn',
84                 ),
85             ),
86             array(
87                 Caster::EXCLUDE_PROTECTED,
88                 array(
89                     "\0*\0protected" => 'prot',
90                 ),
91             ),
92             array(
93                 Caster::EXCLUDE_PRIVATE,
94                 array(
95                     "\0Foo\0private" => 'priv',
96                 ),
97             ),
98             array(
99                 Caster::EXCLUDE_VERBOSE,
100                 array(
101                     'public' => 'pub',
102                     "\0*\0protected" => 'prot',
103                 ),
104                 array('public', "\0*\0protected"),
105             ),
106             array(
107                 Caster::EXCLUDE_NOT_IMPORTANT,
108                 array(
109                     'null' => null,
110                     'empty' => false,
111                     "\0~\0virtual" => 'virt',
112                     "\0+\0dynamic" => 'dyn',
113                     "\0Foo\0private" => 'priv',
114                 ),
115                 array('public', "\0*\0protected"),
116             ),
117             array(
118                 Caster::EXCLUDE_VIRTUAL | Caster::EXCLUDE_DYNAMIC,
119                 array(
120                     "\0~\0virtual" => 'virt',
121                     "\0+\0dynamic" => 'dyn',
122                 ),
123             ),
124             array(
125                 Caster::EXCLUDE_NOT_IMPORTANT | Caster::EXCLUDE_VERBOSE,
126                 $this->referenceArray,
127                 array('public', "\0*\0protected"),
128             ),
129             array(
130                 Caster::EXCLUDE_NOT_IMPORTANT | Caster::EXCLUDE_EMPTY,
131                 array(
132                     'null' => null,
133                     'empty' => false,
134                     "\0~\0virtual" => 'virt',
135                     "\0+\0dynamic" => 'dyn',
136                     "\0*\0protected" => 'prot',
137                     "\0Foo\0private" => 'priv',
138                 ),
139                 array('public', 'empty'),
140             ),
141             array(
142                 Caster::EXCLUDE_VERBOSE | Caster::EXCLUDE_EMPTY | Caster::EXCLUDE_STRICT,
143                 array(
144                     'empty' => false,
145                 ),
146                 array('public', 'empty'),
147             ),
148         );
149     }
150
151     /**
152      * @requires PHP 7.0
153      */
154     public function testAnonymousClass()
155     {
156         $c = eval('return new class extends stdClass { private $foo = "foo"; };');
157
158         $this->assertDumpMatchesFormat(
159             <<<'EOTXT'
160 stdClass@anonymous {
161   -foo: "foo"
162 }
163 EOTXT
164             , $c
165         );
166
167         $c = eval('return new class { private $foo = "foo"; };');
168
169         $this->assertDumpMatchesFormat(
170             <<<'EOTXT'
171 @anonymous {
172   -foo: "foo"
173 }
174 EOTXT
175             , $c
176         );
177     }
178 }