4 * This file is part of Psy Shell.
6 * (c) 2012-2018 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;
16 class ValidClassNamePassTest extends CodeCleanerTestCase
18 public function setUp()
20 $this->setPass(new ValidClassNamePass());
24 * @dataProvider getInvalid
25 * @expectedException \Psy\Exception\FatalErrorException
27 public function testProcessInvalid($code)
29 $this->parseAndTraverse($code);
32 public function getInvalid()
37 ['class stdClass {}'],
39 ['class stdClass {}'],
41 // collisions with interfaces and traits
42 ['interface stdClass {}'],
43 ['trait stdClass {}'],
45 // collisions inside the same code snippet
47 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
48 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
51 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
52 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
55 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
56 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
59 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
60 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
63 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
64 trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
67 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
68 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
71 class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
72 interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
75 // namespaced collisions
77 namespace Psy\\Test\\CodeCleaner {
78 class ValidClassNamePassTest {}
82 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
85 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
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 {}'],
98 // class instantiations
99 ['new Psy_Test_CodeCleaner_ValidClassNamePass_Gamma();'],
101 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
102 new Psy_Test_CodeCleaner_ValidClassNamePass_Delta();
106 // class constant fetch
107 ['Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::FOO'],
110 ['Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::foo()'],
111 ['Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::$foo()'],
112 ['Psy\\Test\\CodeCleaner\\ValidClassNamePassTest::notAMethod()'],
117 * @dataProvider getValid
119 public function testProcessValid($code)
121 $this->parseAndTraverse($code);
122 $this->assertTrue(true);
125 public function getValid()
128 // class declarations
129 ['class Psy_Test_CodeCleaner_ValidClassNamePass_Epsilon {}'],
130 ['namespace Psy\Test\CodeCleaner\ValidClassNamePass; class Zeta {}'],
132 namespace { class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}; }
133 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
134 class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}
137 ['namespace Psy\Test\CodeCleaner\ValidClassNamePass { class stdClass {} }'],
139 // class instantiations
143 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
146 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
151 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
157 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
161 new \\Psy\\Test\\CodeCleaner\\ValidClassNamePass\\Kappa();
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'],
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()'],
177 // Allow `self` and `static` as class names.
179 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
180 public static function getInstance() {
186 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
187 public static function getInstance() {
193 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
194 public static function getInstance() {
200 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
201 public static function getInstance() {
207 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
208 public static function getInstance() {
214 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
215 public static function getInstance() {
221 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
222 public static function foo() {
223 return parent::bar();
228 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
229 public static function foo() {
235 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
236 public static function foo() {
237 return static::bar();
242 ['class A { static function b() { return new A; } }'],
251 ['class A {} class B { function c() { return new A; } }'],
254 ['class A { function a() { A::a(); } }'],
256 // conditionally defined classes
286 while (false) { class A {} }
290 do { class A {} } while (false);
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() {}'];
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'];