4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Test\CodeCleaner;
14 use Psy\CodeCleaner\ValidClassNamePass;
15 use Psy\Exception\Exception;
17 class ValidClassNamePassTest extends CodeCleanerTestCase
19 public function setUp()
21 $this->setPass(new ValidClassNamePass());
25 * @dataProvider getInvalid
27 public function testProcessInvalid($code, $php54 = false)
30 $stmts = $this->parse($code);
31 $this->traverse($stmts);
33 } catch (Exception $e) {
34 if ($php54 && version_compare(PHP_VERSION, '5.4', '<')) {
35 $this->assertInstanceOf('Psy\Exception\ParseErrorException', $e);
37 $this->assertInstanceOf('Psy\Exception\FatalErrorException', $e);
42 public function getInvalid()
47 array('class stdClass {}'),
49 array('class stdClass {}'),
51 // collisions with interfaces and traits
52 array('interface stdClass {}'),
53 array('trait stdClass {}', true),
55 // collisions inside the same code snippet
57 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
58 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
61 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
62 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
65 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
66 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
69 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
70 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
73 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
74 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
77 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
78 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
81 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
82 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
85 // namespaced collisions
87 namespace Psy\\Test\\CodeCleaner {
88 class ValidClassNamePassTest {}
92 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
95 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
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 {}'),
108 // class instantiations
109 array('new Psy_Test_CodeCleaner_ValidClassNamePass_Gamma();'),
111 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
112 new Psy_Test_CodeCleaner_ValidClassNamePass_Delta();
116 // class constant fetch
117 array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::FOO'),
120 array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::foo()'),
121 array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::$foo()'),
126 * @dataProvider getValid
128 public function testProcessValid($code)
130 $stmts = $this->parse($code);
131 $this->traverse($stmts);
133 // @todo a better thing to assert here?
134 $this->assertTrue(true);
137 public function getValid()
140 // class declarations
141 array('class Psy_Test_CodeCleaner_ValidClassNamePass_Epsilon {}'),
142 array('namespace Psy\Test\CodeCleaner\ValidClassNamePass; class Zeta {}'),
144 namespace { class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}; }
145 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
146 class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}
149 array('namespace Psy\Test\CodeCleaner\ValidClassNamePass { class stdClass {} }'),
151 // class instantiations
152 array('new stdClass();'),
153 array('new stdClass();'),
155 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
158 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
163 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
169 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
173 new \\Psy\\Test\\CodeCleaner\\ValidClassNamePass\\Kappa();
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'),
183 array('DateTime::createFromFormat()'),
184 array('DateTime::$someMethod()'),
185 array('Psy\Test\CodeCleaner\Fixtures\ClassWithStatic::doStuff()'),
186 array('Psy\Test\CodeCleaner\Fixtures\ClassWithCallStatic::doStuff()'),
188 // Allow `self` and `static` as class names.
190 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
191 public static function getInstance() {
197 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
198 public static function getInstance() {
204 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
205 public static function getInstance() {
211 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
212 public static function getInstance() {
218 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
219 public static function getInstance() {
225 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
226 public static function getInstance() {
232 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
233 public static function foo() {
234 return parent::bar();
239 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
240 public static function foo() {
246 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
247 public static function foo() {
248 return static::bar();
253 array('class A { static function b() { return new A; } }'),
262 array('class A {} class B { function c() { return new A; } }'),
265 array('class A { function a() { A::a(); } }'),
267 // conditionally defined classes
297 while (false) { class A {} }
301 do { class A {} } while (false);
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() {}');
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');