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);
134 public function getValid()
137 // class declarations
138 array('class Psy_Test_CodeCleaner_ValidClassNamePass_Epsilon {}'),
139 array('namespace Psy\Test\CodeCleaner\ValidClassNamePass; class Zeta {}'),
141 namespace { class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}; }
142 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
143 class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}
146 array('namespace Psy\Test\CodeCleaner\ValidClassNamePass { class stdClass {} }'),
148 // class instantiations
149 array('new stdClass();'),
150 array('new stdClass();'),
152 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
155 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
160 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
166 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
170 new \\Psy\\Test\\CodeCleaner\\ValidClassNamePass\\Kappa();
174 // Class constant fetch (ValidConstantPassTest validates the actual constant)
175 array('class A {} A::FOO'),
176 array('$a = new DateTime; $a::ATOM'),
177 array('interface A { const B = 1; } A::B'),
180 array('DateTime::createFromFormat()'),
181 array('DateTime::$someMethod()'),
182 array('Psy\Test\CodeCleaner\Fixtures\ClassWithStatic::doStuff()'),
183 array('Psy\Test\CodeCleaner\Fixtures\ClassWithCallStatic::doStuff()'),
185 // Allow `self` and `static` as class names.
187 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
188 public static function getInstance() {
194 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
195 public static function getInstance() {
201 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
202 public static function getInstance() {
208 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
209 public static function getInstance() {
215 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
216 public static function getInstance() {
222 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
223 public static function getInstance() {
229 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
230 public static function foo() {
231 return parent::bar();
236 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
237 public static function foo() {
243 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
244 public static function foo() {
245 return static::bar();
250 array('class A { static function b() { return new A; } }'),
259 array('class A {} class B { function c() { return new A; } }'),
262 array('class A { function a() { A::a(); } }'),
264 // conditionally defined classes
294 while (false) { class A {} }
298 do { class A {} } while (false);
316 // Ugh. There's gotta be a better way to test for this.
317 if (class_exists('PhpParser\ParserFactory')) {
318 // PHP 7.0 anonymous classes, only supported by PHP Parser v2.x
319 $valid[] = array('$obj = new class() {}');
322 if (version_compare(PHP_VERSION, '5.5', '>=')) {
323 $valid[] = array('interface A {} A::class');
324 $valid[] = array('interface A {} A::CLASS');
325 $valid[] = array('class A {} A::class');
326 $valid[] = array('class A {} A::CLASS');
327 $valid[] = array('A::class');
328 $valid[] = array('A::CLASS');