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