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()'],
116 * @dataProvider getValid
118 public function testProcessValid($code)
120 $this->parseAndTraverse($code);
121 $this->assertTrue(true);
124 public function getValid()
127 // class declarations
128 ['class Psy_Test_CodeCleaner_ValidClassNamePass_Epsilon {}'],
129 ['namespace Psy\Test\CodeCleaner\ValidClassNamePass; class Zeta {}'],
131 namespace { class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}; }
132 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
133 class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}
136 ['namespace Psy\Test\CodeCleaner\ValidClassNamePass { class stdClass {} }'],
138 // class instantiations
142 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
145 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
150 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
156 namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
160 new \\Psy\\Test\\CodeCleaner\\ValidClassNamePass\\Kappa();
164 // Class constant fetch (ValidConstantPassTest validates the actual constant)
165 ['class A {} A::FOO'],
166 ['$a = new DateTime; $a::ATOM'],
167 ['interface A { const B = 1; } A::B'],
170 ['DateTime::createFromFormat()'],
171 ['DateTime::$someMethod()'],
172 ['Psy\Test\CodeCleaner\Fixtures\ClassWithStatic::doStuff()'],
173 ['Psy\Test\CodeCleaner\Fixtures\ClassWithCallStatic::doStuff()'],
175 // Allow `self` and `static` as class names.
177 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
178 public static function getInstance() {
184 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
185 public static function getInstance() {
191 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
192 public static function getInstance() {
198 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
199 public static function getInstance() {
205 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
206 public static function getInstance() {
212 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
213 public static function getInstance() {
219 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
220 public static function foo() {
221 return parent::bar();
226 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
227 public static function foo() {
233 class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
234 public static function foo() {
235 return static::bar();
240 ['class A { static function b() { return new A; } }'],
249 ['class A {} class B { function c() { return new A; } }'],
252 ['class A { function a() { A::a(); } }'],
254 // conditionally defined classes
284 while (false) { class A {} }
288 do { class A {} } while (false);
306 // Ugh. There's gotta be a better way to test for this.
307 if (class_exists('PhpParser\ParserFactory')) {
308 // PHP 7.0 anonymous classes, only supported by PHP Parser v2.x
309 $valid[] = ['$obj = new class() {}'];
312 if (version_compare(PHP_VERSION, '5.5', '>=')) {
313 $valid[] = ['interface A {} A::class'];
314 $valid[] = ['interface A {} A::CLASS'];
315 $valid[] = ['class A {} A::class'];
316 $valid[] = ['class A {} A::CLASS'];
317 $valid[] = ['A::class'];
318 $valid[] = ['A::CLASS'];