6be71072c31022e323864320ffd98b9926f51020
[yaffs-website] / vendor / psy / psysh / test / Psy / Test / CodeCleaner / ValidClassNamePassTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
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 Psy\Test\CodeCleaner;
13
14 use Psy\CodeCleaner\ValidClassNamePass;
15 use Psy\Exception\Exception;
16
17 class ValidClassNamePassTest extends CodeCleanerTestCase
18 {
19     public function setUp()
20     {
21         $this->setPass(new ValidClassNamePass());
22     }
23
24     /**
25      * @dataProvider getInvalid
26      */
27     public function testProcessInvalid($code, $php54 = false)
28     {
29         try {
30             $stmts = $this->parse($code);
31             $this->traverse($stmts);
32             $this->fail();
33         } catch (Exception $e) {
34             if ($php54 && version_compare(PHP_VERSION, '5.4', '<')) {
35                 $this->assertInstanceOf('Psy\Exception\ParseErrorException', $e);
36             } else {
37                 $this->assertInstanceOf('Psy\Exception\FatalErrorException', $e);
38             }
39         }
40     }
41
42     public function getInvalid()
43     {
44         // class declarations
45         return array(
46             // core class
47             array('class stdClass {}'),
48             // capitalization
49             array('class stdClass {}'),
50
51             // collisions with interfaces and traits
52             array('interface stdClass {}'),
53             array('trait stdClass {}', true),
54
55             // collisions inside the same code snippet
56             array('
57                 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
58                 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
59             '),
60             array('
61                 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
62                 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
63             ', true),
64             array('
65                 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
66                 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
67             ', true),
68             array('
69                 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
70                 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
71             ', true),
72             array('
73                 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
74                 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
75             ', true),
76             array('
77                 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
78                 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
79             '),
80             array('
81                 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
82                 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
83             '),
84
85             // namespaced collisions
86             array('
87                 namespace Psy\\Test\\CodeCleaner {
88                     class ValidClassNamePassTest {}
89                 }
90             '),
91             array('
92                 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
93                     class Beta {}
94                 }
95                 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
96                     class Beta {}
97                 }
98             '),
99
100             // extends and implements
101             array('class ValidClassNamePassTest extends NotAClass {}'),
102             array('class ValidClassNamePassTest extends ArrayAccess {}'),
103             array('class ValidClassNamePassTest implements stdClass {}'),
104             array('class ValidClassNamePassTest implements ArrayAccess, stdClass {}'),
105             array('interface ValidClassNamePassTest extends stdClass {}'),
106             array('interface ValidClassNamePassTest extends ArrayAccess, stdClass {}'),
107
108             // class instantiations
109             array('new Psy_Test_CodeCleaner_ValidClassNamePass_Gamma();'),
110             array('
111                 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
112                     new Psy_Test_CodeCleaner_ValidClassNamePass_Delta();
113                 }
114             '),
115
116             // class constant fetch
117             array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::FOO'),
118
119             // static call
120             array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::foo()'),
121             array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::$foo()'),
122         );
123     }
124
125     /**
126      * @dataProvider getValid
127      */
128     public function testProcessValid($code)
129     {
130         $stmts = $this->parse($code);
131         $this->traverse($stmts);
132
133         // @todo a better thing to assert here?
134         $this->assertTrue(true);
135     }
136
137     public function getValid()
138     {
139         $valid = array(
140             // class declarations
141             array('class Psy_Test_CodeCleaner_ValidClassNamePass_Epsilon {}'),
142             array('namespace Psy\Test\CodeCleaner\ValidClassNamePass; class Zeta {}'),
143             array('
144                 namespace { class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}; }
145                 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
146                     class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}
147                 }
148             '),
149             array('namespace Psy\Test\CodeCleaner\ValidClassNamePass { class stdClass {} }'),
150
151             // class instantiations
152             array('new stdClass();'),
153             array('new stdClass();'),
154             array('
155                 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
156                     class Theta {}
157                 }
158                 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
159                     new Theta();
160                 }
161             '),
162             array('
163                 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
164                     class Iota {}
165                     new Iota();
166                 }
167             '),
168             array('
169                 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
170                     class Kappa {}
171                 }
172                 namespace {
173                     new \\Psy\\Test\\CodeCleaner\\ValidClassNamePass\\Kappa();
174                 }
175             '),
176
177             // Class constant fetch (ValidConstantPassTest validates the actual constant)
178             array('class A {} A::FOO'),
179             array('$a = new DateTime; $a::ATOM'),
180             array('interface A { const B = 1; } A::B'),
181
182             // static call
183             array('DateTime::createFromFormat()'),
184             array('DateTime::$someMethod()'),
185             array('Psy\Test\CodeCleaner\Fixtures\ClassWithStatic::doStuff()'),
186             array('Psy\Test\CodeCleaner\Fixtures\ClassWithCallStatic::doStuff()'),
187
188             // Allow `self` and `static` as class names.
189             array('
190                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
191                     public static function getInstance() {
192                         return new self();
193                     }
194                 }
195             '),
196             array('
197                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
198                     public static function getInstance() {
199                         return new SELF();
200                     }
201                 }
202             '),
203             array('
204                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
205                     public static function getInstance() {
206                         return new self;
207                     }
208                 }
209             '),
210             array('
211                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
212                     public static function getInstance() {
213                         return new static();
214                     }
215                 }
216             '),
217             array('
218                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
219                     public static function getInstance() {
220                         return new Static();
221                     }
222                 }
223             '),
224             array('
225                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
226                     public static function getInstance() {
227                         return new static;
228                     }
229                 }
230             '),
231             array('
232                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
233                     public static function foo() {
234                         return parent::bar();
235                     }
236                 }
237             '),
238             array('
239                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
240                     public static function foo() {
241                         return self::bar();
242                     }
243                 }
244             '),
245             array('
246                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
247                     public static function foo() {
248                         return static::bar();
249                     }
250                 }
251             '),
252
253             array('class A { static function b() { return new A; } }'),
254             array('
255                 class A {
256                     const B = 123;
257                     function c() {
258                         return A::B;
259                     }
260                 }
261             '),
262             array('class A {} class B { function c() { return new A; } }'),
263
264             // recursion
265             array('class A { function a() { A::a(); } }'),
266
267             // conditionally defined classes
268             array('
269                 class A {}
270                 if (false) {
271                     class A {}
272                 }
273             '),
274             array('
275                 class A {}
276                 if (true) {
277                     class A {}
278                 } else if (false) {
279                     class A {}
280                 } else {
281                     class A {}
282                 }
283             '),
284             // ewww
285             array('
286                 class A {}
287                 if (true):
288                     class A {}
289                 elseif (false):
290                     class A {}
291                 else:
292                     class A {}
293                 endif;
294             '),
295             array('
296                 class A {}
297                 while (false) { class A {} }
298             '),
299             array('
300                 class A {}
301                 do { class A {} } while (false);
302             '),
303             array('
304                 class A {}
305                 switch (1) {
306                     case 0:
307                         class A {}
308                         break;
309                     case 1:
310                         class A {}
311                         break;
312                     case 2:
313                         class A {}
314                         break;
315                 }
316             '),
317         );
318
319         // Ugh. There's gotta be a better way to test for this.
320         if (class_exists('PhpParser\ParserFactory')) {
321             // PHP 7.0 anonymous classes, only supported by PHP Parser v2.x
322             $valid[] = array('$obj = new class() {}');
323         }
324
325         if (version_compare(PHP_VERSION, '5.5', '>=')) {
326             $valid[] = array('interface A {} A::class');
327             $valid[] = array('interface A {} A::CLASS');
328             $valid[] = array('class A {} A::class');
329             $valid[] = array('class A {} A::CLASS');
330             $valid[] = array('A::class');
331             $valid[] = array('A::CLASS');
332         }
333
334         return $valid;
335     }
336 }