Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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             ['Psy\Test\CodeCleaner\Fixtures\TraitWithStatic::doStuff()'],
176
177             // Allow `self` and `static` as class names.
178             ['
179                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
180                     public static function getInstance() {
181                         return new self();
182                     }
183                 }
184             '],
185             ['
186                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
187                     public static function getInstance() {
188                         return new SELF();
189                     }
190                 }
191             '],
192             ['
193                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
194                     public static function getInstance() {
195                         return new self;
196                     }
197                 }
198             '],
199             ['
200                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
201                     public static function getInstance() {
202                         return new static();
203                     }
204                 }
205             '],
206             ['
207                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
208                     public static function getInstance() {
209                         return new Static();
210                     }
211                 }
212             '],
213             ['
214                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
215                     public static function getInstance() {
216                         return new static;
217                     }
218                 }
219             '],
220             ['
221                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
222                     public static function foo() {
223                         return parent::bar();
224                     }
225                 }
226             '],
227             ['
228                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
229                     public static function foo() {
230                         return self::bar();
231                     }
232                 }
233             '],
234             ['
235                 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
236                     public static function foo() {
237                         return static::bar();
238                     }
239                 }
240             '],
241
242             ['class A { static function b() { return new A; } }'],
243             ['
244                 class A {
245                     const B = 123;
246                     function c() {
247                         return A::B;
248                     }
249                 }
250             '],
251             ['class A {} class B { function c() { return new A; } }'],
252
253             // recursion
254             ['class A { function a() { A::a(); } }'],
255
256             // conditionally defined classes
257             ['
258                 class A {}
259                 if (false) {
260                     class A {}
261                 }
262             '],
263             ['
264                 class A {}
265                 if (true) {
266                     class A {}
267                 } else if (false) {
268                     class A {}
269                 } else {
270                     class A {}
271                 }
272             '],
273             // ewww
274             ['
275                 class A {}
276                 if (true):
277                     class A {}
278                 elseif (false):
279                     class A {}
280                 else:
281                     class A {}
282                 endif;
283             '],
284             ['
285                 class A {}
286                 while (false) { class A {} }
287             '],
288             ['
289                 class A {}
290                 do { class A {} } while (false);
291             '],
292             ['
293                 class A {}
294                 switch (1) {
295                     case 0:
296                         class A {}
297                         break;
298                     case 1:
299                         class A {}
300                         break;
301                     case 2:
302                         class A {}
303                         break;
304                 }
305             '],
306         ];
307
308         // Ugh. There's gotta be a better way to test for this.
309         if (\class_exists('PhpParser\ParserFactory')) {
310             // PHP 7.0 anonymous classes, only supported by PHP Parser v2.x
311             $valid[] = ['$obj = new class() {}'];
312         }
313
314         if (\version_compare(PHP_VERSION, '5.5', '>=')) {
315             $valid[] = ['interface A {} A::class'];
316             $valid[] = ['interface A {} A::CLASS'];
317             $valid[] = ['class A {} A::class'];
318             $valid[] = ['class A {} A::CLASS'];
319             $valid[] = ['A::class'];
320             $valid[] = ['A::CLASS'];
321         }
322
323         return $valid;
324     }
325 }