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()'],
176 // Allow `self` and `static` as class names.
178 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
179 public static function getInstance() {
185 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
186 public static function getInstance() {
192 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
193 public static function getInstance() {
199 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
200 public static function getInstance() {
206 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
207 public static function getInstance() {
213 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
214 public static function getInstance() {
220 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
221 public static function foo() {
222 return parent::bar();
227 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
228 public static function foo() {
234 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
235 public static function foo() {
236 return static::bar();
241 ['class A { static function b() { return new A; } }'],
250 ['class A {} class B { function c() { return new A; } }'],
253 ['class A { function a() { A::a(); } }'],
255 // conditionally defined classes
285 while (false) { class A {} }
289 do { class A {} } while (false);
307 // Ugh. There's gotta be a better way to test for this.
308 if (class_exists('PhpParser\ParserFactory')) {
309 // PHP 7.0 anonymous classes, only supported by PHP Parser v2.x
310 $valid[] = ['$obj = new class() {}'];
313 if (version_compare(PHP_VERSION, '5.5', '>=')) {
314 $valid[] = ['interface A {} A::class'];
315 $valid[] = ['interface A {} A::CLASS'];
316 $valid[] = ['class A {} A::class'];
317 $valid[] = ['class A {} A::CLASS'];
318 $valid[] = ['A::class'];
319 $valid[] = ['A::CLASS'];