79b51cf242e526d6142be2ba2ffb7650a795fe2f
[yaffs-website] / vendor / psy / psysh / test / Psy / Test / CodeCleaner / ValidFunctionNamePassTest.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\ValidFunctionNamePass;
15
16 class ValidFunctionNamePassTest extends CodeCleanerTestCase
17 {
18     public function setUp()
19     {
20         $this->setPass(new ValidFunctionNamePass());
21     }
22
23     /**
24      * @dataProvider getInvalidFunctions
25      * @expectedException \Psy\Exception\FatalErrorException
26      */
27     public function testProcessInvalidFunctionCallsAndDeclarations($code)
28     {
29         $stmts = $this->parse($code);
30         $this->traverse($stmts);
31     }
32
33     public function getInvalidFunctions()
34     {
35         return array(
36             // function declarations
37             array('function array_merge() {}'),
38             array('function Array_Merge() {}'),
39             array('
40                 function psy_test_codecleaner_validfunctionnamepass_alpha() {}
41                 function psy_test_codecleaner_validfunctionnamepass_alpha() {}
42             '),
43             array('
44                 namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
45                     function beta() {}
46                 }
47                 namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
48                     function beta() {}
49                 }
50             '),
51
52             // function calls
53             array('psy_test_codecleaner_validfunctionnamepass_gamma()'),
54             array('
55                 namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
56                     delta();
57                 }
58             '),
59
60             // recursion
61             array('function a() { a(); } function a() {}'),
62         );
63     }
64
65     /**
66      * @dataProvider getValidFunctions
67      */
68     public function testProcessValidFunctionCallsAndDeclarations($code)
69     {
70         $stmts = $this->parse($code);
71         $this->traverse($stmts);
72
73         // @todo a better thing to assert here?
74         $this->assertTrue(true);
75     }
76
77     public function getValidFunctions()
78     {
79         return array(
80             array('function psy_test_codecleaner_validfunctionnamepass_epsilon() {}'),
81             array('
82                 namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
83                     function zeta() {}
84                 }
85             '),
86             array('
87                 namespace {
88                     function psy_test_codecleaner_validfunctionnamepass_eta() {}
89                 }
90                 namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
91                     function psy_test_codecleaner_validfunctionnamepass_eta() {}
92                 }
93             '),
94             array('
95                 namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
96                     function psy_test_codecleaner_validfunctionnamepass_eta() {}
97                 }
98                 namespace {
99                     function psy_test_codecleaner_validfunctionnamepass_eta() {}
100                 }
101             '),
102             array('
103                 namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
104                     function array_merge() {}
105                 }
106             '),
107
108             // function calls
109             array('array_merge();'),
110             array('
111                 namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
112                     function theta() {}
113                 }
114                 namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
115                     theta();
116                 }
117             '),
118             // closures
119             array('$test = function(){};$test()'),
120             array('
121                 namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
122                     function theta() {}
123                 }
124                 namespace {
125                     Psy\\Test\\CodeCleaner\\ValidFunctionNamePass\\theta();
126                 }
127             '),
128
129             // recursion
130             array('function a() { a(); }'),
131
132             // conditionally defined functions
133             array('
134                 function a() {}
135                 if (false) {
136                     function a() {}
137                 }
138             '),
139             array('
140                 function a() {}
141                 if (true) {
142                     function a() {}
143                 } else if (false) {
144                     function a() {}
145                 } else {
146                     function a() {}
147                 }
148             '),
149             // ewww
150             array('
151                 function a() {}
152                 if (true):
153                     function a() {}
154                 elseif (false):
155                     function a() {}
156                 else:
157                     function a() {}
158                 endif;
159             '),
160             array('
161                 function a() {}
162                 while (false) { function a() {} }
163             '),
164             array('
165                 function a() {}
166                 do { function a() {} } while (false);
167             '),
168             array('
169                 function a() {}
170                 switch (1) {
171                     case 0:
172                         function a() {}
173                         break;
174                     case 1:
175                         function a() {}
176                         break;
177                     case 2:
178                         function a() {}
179                         break;
180                 }
181             '),
182         );
183     }
184 }