Update v1.0.6
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use PHPParser_NodeTraverser as NodeTraverser;
|
||||
use Psy\CodeCleaner\AbstractClassPass;
|
||||
|
||||
class AbstractClassPassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->pass = new AbstractClassPass();
|
||||
$this->traverser = new NodeTraverser();
|
||||
$this->traverser->addVisitor($this->pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidStatements
|
||||
* @expectedException \Psy\Exception\FatalErrorException
|
||||
*/
|
||||
public function testProcessStatementFails($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function invalidStatements()
|
||||
{
|
||||
return array(
|
||||
array('class A { abstract function a(); }'),
|
||||
array('abstract class B { abstract function b() {} }'),
|
||||
array('abstract class B { abstract function b() { echo "yep"; } }'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validStatements
|
||||
*/
|
||||
public function testProcessStatementPasses($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function validStatements()
|
||||
{
|
||||
return array(
|
||||
array('abstract class C { function c() {} }'),
|
||||
array('abstract class D { abstract function d(); }'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use PHPParser_NodeTraverser as NodeTraverser;
|
||||
use Psy\CodeCleaner\AssignThisVariablePass;
|
||||
|
||||
class AssignThisVariablePassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->pass = new AssignThisVariablePass();
|
||||
$this->traverser = new NodeTraverser();
|
||||
$this->traverser->addVisitor($this->pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidStatements
|
||||
* @expectedException \Psy\Exception\FatalErrorException
|
||||
*/
|
||||
public function testProcessStatementFails($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function invalidStatements()
|
||||
{
|
||||
return array(
|
||||
array('$this = 3'),
|
||||
array('strtolower($this = "this")'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validStatements
|
||||
*/
|
||||
public function testProcessStatementPasses($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function validStatements()
|
||||
{
|
||||
return array(
|
||||
array('$this'),
|
||||
array('$a = $this'),
|
||||
array('$a = "this"; $$a = 3'),
|
||||
array('$$this = "b"'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use PHPParser_NodeTraverser as NodeTraverser;
|
||||
use Psy\CodeCleaner\CallTimePassByReferencePass;
|
||||
|
||||
class CallTimePassByReferencePassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->pass = new CallTimePassByReferencePass();
|
||||
$this->traverser = new NodeTraverser();
|
||||
$this->traverser->addVisitor($this->pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidStatements
|
||||
* @expectedException \Psy\Exception\FatalErrorException
|
||||
*/
|
||||
public function testProcessStatementFails($code)
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '5.4', '<')) {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function invalidStatements()
|
||||
{
|
||||
return array(
|
||||
array('f(&$arg)'),
|
||||
array('$object->method($first, &$arg)'),
|
||||
array('$closure($first, &$arg, $last)'),
|
||||
array('A::b(&$arg)'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validStatements
|
||||
*/
|
||||
public function testProcessStatementPasses($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function validStatements()
|
||||
{
|
||||
$data = array(
|
||||
array('array(&$var)'),
|
||||
array('$a = &$b'),
|
||||
array('f(array(&$b))'),
|
||||
);
|
||||
|
||||
if (version_compare(PHP_VERSION, '5.4', '<')) {
|
||||
$data = array_merge($data, $this->invalidStatements());
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use PHPParser_NodeTraverser as NodeTraverser;
|
||||
use Psy\CodeCleaner\CalledClassPass;
|
||||
|
||||
class CalledClassPassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->pass = new CalledClassPass();
|
||||
$this->traverser = new NodeTraverser();
|
||||
$this->traverser->addVisitor($this->pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidStatements
|
||||
* @expectedException \Psy\Exception\ErrorException
|
||||
*/
|
||||
public function testProcessStatementFails($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function invalidStatements()
|
||||
{
|
||||
return array(
|
||||
array('get_class()'),
|
||||
array('get_class(null)'),
|
||||
array('get_called_class()'),
|
||||
array('get_called_class(null)'),
|
||||
array('function foo() { return get_class(); }'),
|
||||
array('function foo() { return get_class(null); }'),
|
||||
array('function foo() { return get_called_class(); }'),
|
||||
array('function foo() { return get_called_class(null); }'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validStatements
|
||||
*/
|
||||
public function testProcessStatementPasses($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function validStatements()
|
||||
{
|
||||
return array(
|
||||
array('get_class($foo)'),
|
||||
array('get_class(bar())'),
|
||||
array('get_called_class($foo)'),
|
||||
array('get_called_class(bar())'),
|
||||
array('function foo($bar) { return get_class($bar); }'),
|
||||
array('function foo($bar) { return get_called_class($bar); }'),
|
||||
array('class Foo { function bar() { return get_class(); } }'),
|
||||
array('class Foo { function bar() { return get_class(null); } }'),
|
||||
array('class Foo { function bar() { return get_called_class(); } }'),
|
||||
array('class Foo { function bar() { return get_called_class(null); } }'),
|
||||
array('$foo = function () {}; $foo()'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validTraitStatements
|
||||
*/
|
||||
public function testProcessTraitStatementPasses($code)
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '5.4', '<')) {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function validTraitStatements()
|
||||
{
|
||||
return array(
|
||||
array('trait Foo { function bar() { return get_class(); } }'),
|
||||
array('trait Foo { function bar() { return get_class(null); } }'),
|
||||
array('trait Foo { function bar() { return get_called_class(); } }'),
|
||||
array('trait Foo { function bar() { return get_called_class(null); } }'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use PHPParser_Lexer as Lexer;
|
||||
use PHPParser_NodeTraverser as NodeTraverser;
|
||||
use PHPParser_Parser as Parser;
|
||||
use PHPParser_PrettyPrinter_Default as Printer;
|
||||
use Psy\CodeCleaner\CodeCleanerPass;
|
||||
use Psy\Exception\ParseErrorException;
|
||||
|
||||
class CodeCleanerTestCase extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $pass;
|
||||
protected $traverser;
|
||||
private $parser;
|
||||
private $printer;
|
||||
|
||||
protected function setPass(CodeCleanerPass $pass)
|
||||
{
|
||||
$this->pass = $pass;
|
||||
if (!isset($this->traverser)) {
|
||||
$this->traverser = new NodeTraverser();
|
||||
}
|
||||
$this->traverser->addVisitor($this->pass);
|
||||
}
|
||||
|
||||
protected function parse($code, $prefix = '<?php ')
|
||||
{
|
||||
$code = $prefix . $code;
|
||||
try {
|
||||
return $this->getParser()->parse($code);
|
||||
} catch (\PHPParser_Error $e) {
|
||||
if (!$this->parseErrorIsEOF($e)) {
|
||||
throw ParseErrorException::fromParseError($e);
|
||||
}
|
||||
|
||||
try {
|
||||
// Unexpected EOF, try again with an implicit semicolon
|
||||
return $this->getParser()->parse($code . ';');
|
||||
} catch (\PHPParser_Error $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function traverse(array $stmts)
|
||||
{
|
||||
return $this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
protected function prettyPrint(array $stmts)
|
||||
{
|
||||
return $this->getPrinter()->prettyPrint($stmts);
|
||||
}
|
||||
|
||||
protected function assertProcessesAs($from, $to)
|
||||
{
|
||||
$stmts = $this->parse($from);
|
||||
$stmts = $this->traverse($stmts);
|
||||
$this->assertEquals($to, $this->prettyPrint($stmts));
|
||||
}
|
||||
|
||||
private function getParser()
|
||||
{
|
||||
if (!isset($this->parser)) {
|
||||
$this->parser = new Parser(new Lexer());
|
||||
}
|
||||
|
||||
return $this->parser;
|
||||
}
|
||||
|
||||
private function getPrinter()
|
||||
{
|
||||
if (!isset($this->printer)) {
|
||||
$this->printer = new Printer();
|
||||
}
|
||||
|
||||
return $this->printer;
|
||||
}
|
||||
|
||||
private function parseErrorIsEOF(\PHPParser_Error $e)
|
||||
{
|
||||
$msg = $e->getRawMessage();
|
||||
|
||||
return ($msg === "Unexpected token EOF") || (strpos($msg, "Syntax error, unexpected EOF") !== false);
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner\Fixtures;
|
||||
|
||||
class ClassWithCallStatic
|
||||
{
|
||||
public static function __callStatic($name, $arguments)
|
||||
{
|
||||
// wheee!
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner\Fixtures;
|
||||
|
||||
class ClassWithStatic
|
||||
{
|
||||
public static function doStuff()
|
||||
{
|
||||
// Don't actually do stuff.
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use PHPParser_NodeTraverser as NodeTraverser;
|
||||
use Psy\CodeCleaner\FunctionReturnInWriteContextPass;
|
||||
use Psy\Exception\FatalErrorException;
|
||||
|
||||
class FunctionReturnInWriteContextPassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->pass = new FunctionReturnInWriteContextPass();
|
||||
$this->traverser = new NodeTraverser();
|
||||
$this->traverser->addVisitor($this->pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidStatements
|
||||
* @expectedException \Psy\Exception\FatalErrorException
|
||||
* @expectedExceptionMessage Can't use function return value in write context
|
||||
*/
|
||||
public function testProcessStatementFails($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function invalidStatements()
|
||||
{
|
||||
return array(
|
||||
array('f(&g())'),
|
||||
array('array(& $object->method())'),
|
||||
array('$a->method(& $closure())'),
|
||||
array('array(& A::b())'),
|
||||
array('f() = 5'),
|
||||
);
|
||||
}
|
||||
|
||||
public function testIsset()
|
||||
{
|
||||
try {
|
||||
$this->traverser->traverse($this->parse('isset(strtolower("A"))'));
|
||||
$this->fail();
|
||||
} catch (FatalErrorException $e) {
|
||||
if (version_compare(PHP_VERSION, '5.5', '>=')) {
|
||||
$this->assertContains('Cannot use isset() on the result of a function call (you can use "null !== func()" instead)', $e->getMessage());
|
||||
} else {
|
||||
$this->assertContains("Can't use function return value in write context", $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Psy\Exception\FatalErrorException
|
||||
* @expectedExceptionMessage Can't use function return value in write context
|
||||
*/
|
||||
public function testEmpty()
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '5.5', '>=')) {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
||||
$this->traverser->traverse($this->parse('empty(strtolower("A"))'));
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner\ImplicitReturnPass;
|
||||
|
||||
class ImplicitReturnPassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->setPass(new ImplicitReturnPass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider implicitReturns
|
||||
*/
|
||||
public function testProcess($from, $to)
|
||||
{
|
||||
$this->assertProcessesAs($from, $to);
|
||||
}
|
||||
|
||||
public function implicitReturns()
|
||||
{
|
||||
return array(
|
||||
array('4', 'return 4;'),
|
||||
array('foo()', 'return foo();'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner\InstanceOfPass;
|
||||
|
||||
class InstanceOfPassTest extends CodeCleanerTestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
$this->setPass(new InstanceOfPass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidStatements
|
||||
* @expectedException \Psy\Exception\FatalErrorException
|
||||
*/
|
||||
public function testProcessInvalidStatement($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function invalidStatements()
|
||||
{
|
||||
return array(
|
||||
array('null instanceof stdClass'),
|
||||
array('true instanceof stdClass'),
|
||||
array('9 instanceof stdClass'),
|
||||
array('1.0 instanceof stdClass'),
|
||||
array('"foo" instanceof stdClass'),
|
||||
array('__DIR__ instanceof stdClass'),
|
||||
array('PHP_SAPI instanceof stdClass'),
|
||||
array('1+1 instanceof stdClass'),
|
||||
array('true && false instanceof stdClass'),
|
||||
array('"a"."b" instanceof stdClass'),
|
||||
array('!5 instanceof stdClass'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validStatements
|
||||
*/
|
||||
public function testProcessValidStatement($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function validStatements()
|
||||
{
|
||||
$data = array(
|
||||
array('$a instanceof stdClass'),
|
||||
array('strtolower("foo") instanceof stdClass'),
|
||||
array('array(1) instanceof stdClass'),
|
||||
array('(string) "foo" instanceof stdClass'),
|
||||
array('(1+1) instanceof stdClass'),
|
||||
array('"foo ${foo} $bar" instanceof stdClass'),
|
||||
array('DateTime::ISO8601 instanceof stdClass'),
|
||||
|
||||
);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner\LeavePsyshAlonePass;
|
||||
|
||||
class LeavePsyshAlonePassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->setPass(new LeavePsyshAlonePass());
|
||||
}
|
||||
|
||||
public function testPassesInlineHtmlThroughJustFine()
|
||||
{
|
||||
$inline = $this->parse('not php at all!', '');
|
||||
$this->traverse($inline);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validStatements
|
||||
*/
|
||||
public function testProcessStatementPasses($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverse($stmts);
|
||||
}
|
||||
|
||||
public function validStatements()
|
||||
{
|
||||
return array(
|
||||
array('array_merge()'),
|
||||
array('__psysh__()'),
|
||||
array('$this'),
|
||||
array('$psysh'),
|
||||
array('$__psysh'),
|
||||
array('$banana'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidStatements
|
||||
* @expectedException \Psy\Exception\RuntimeException
|
||||
*/
|
||||
public function testProcessStatementFails($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverse($stmts);
|
||||
}
|
||||
|
||||
public function invalidStatements()
|
||||
{
|
||||
return array(
|
||||
array('$__psysh__'),
|
||||
array('var_dump($__psysh__)'),
|
||||
array('$__psysh__ = "your mom"'),
|
||||
array('$__psysh__->fakeFunctionCall()'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner\LegacyEmptyPass;
|
||||
|
||||
class LegacyEmptyPassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->setPass(new LegacyEmptyPass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidStatements
|
||||
* @expectedException \Psy\Exception\ParseErrorException
|
||||
*/
|
||||
public function testProcessInvalidStatement($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function invalidStatements()
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '5.5', '>=')) {
|
||||
return array(
|
||||
array('empty()'),
|
||||
);
|
||||
}
|
||||
|
||||
return array(
|
||||
array('empty()'),
|
||||
array('empty(null)'),
|
||||
array('empty(PHP_EOL)'),
|
||||
array('empty("wat")'),
|
||||
array('empty(1.1)'),
|
||||
array('empty(Foo::$bar)'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validStatements
|
||||
*/
|
||||
public function testProcessValidStatement($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function validStatements()
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '5.5', '<')) {
|
||||
return array(
|
||||
array('empty($foo)'),
|
||||
);
|
||||
}
|
||||
|
||||
return array(
|
||||
array('empty($foo)'),
|
||||
array('empty(null)'),
|
||||
array('empty(PHP_EOL)'),
|
||||
array('empty("wat")'),
|
||||
array('empty(1.1)'),
|
||||
array('empty(Foo::$bar)'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner\MagicConstantsPass;
|
||||
|
||||
class MagicConstantsPassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->setPass(new MagicConstantsPass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider magicConstants
|
||||
*/
|
||||
public function testProcess($from, $to)
|
||||
{
|
||||
$this->assertProcessesAs($from, $to);
|
||||
}
|
||||
|
||||
public function magicConstants()
|
||||
{
|
||||
return array(
|
||||
array('__DIR__;', 'getcwd();'),
|
||||
array('__FILE__;', "'';"),
|
||||
array('___FILE___;', "___FILE___;"),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner;
|
||||
use Psy\CodeCleaner\NamespacePass;
|
||||
|
||||
class NamespacePassTest extends CodeCleanerTestCase
|
||||
{
|
||||
private $cleaner;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->cleaner = new CodeCleaner();
|
||||
$this->setPass(new NamespacePass($this->cleaner));
|
||||
}
|
||||
|
||||
public function testProcess()
|
||||
{
|
||||
$this->process('array_merge()');
|
||||
$this->assertNull($this->cleaner->getNamespace());
|
||||
|
||||
// A non-block namespace statement should set the current namespace.
|
||||
$this->process('namespace Alpha');
|
||||
$this->assertEquals(array('Alpha'), $this->cleaner->getNamespace());
|
||||
|
||||
// A new non-block namespace statement should override the current namespace.
|
||||
$this->process('namespace Beta');
|
||||
$this->assertEquals(array('Beta'), $this->cleaner->getNamespace());
|
||||
|
||||
$this->process('namespace Gamma { array_merge(); }');
|
||||
$this->assertNull($this->cleaner->getNamespace());
|
||||
}
|
||||
|
||||
private function process($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverse($stmts);
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner\StaticConstructorPass;
|
||||
|
||||
class StaticConstructorPassTest extends CodeCleanerTestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
$this->setPass(new StaticConstructorPass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidStatements
|
||||
* @expectedException \Psy\Exception\FatalErrorException
|
||||
*/
|
||||
public function testProcessInvalidStatement($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidParserStatements
|
||||
* @expectedException \Psy\Exception\ParseErrorException
|
||||
*/
|
||||
public function testProcessInvalidStatementCatchedByParser($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function invalidStatements()
|
||||
{
|
||||
$statements = array(
|
||||
array('class A { public static function A() {}}'),
|
||||
array('class A { private static function A() {}}'),
|
||||
);
|
||||
|
||||
if (version_compare(PHP_VERSION, '5.3.3', '<')) {
|
||||
$statements[] = array('namespace B; class A { private static function A() {}}');
|
||||
}
|
||||
|
||||
return $statements;
|
||||
}
|
||||
|
||||
public function invalidParserStatements()
|
||||
{
|
||||
$statements = array(
|
||||
array('class A { public static function __construct() {}}'),
|
||||
array('class A { private static function __construct() {}}'),
|
||||
array('class A { private static function __construct() {} public function A() {}}'),
|
||||
array('namespace B; class A { private static function __construct() {}}'),
|
||||
);
|
||||
|
||||
return $statements;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validStatements
|
||||
*/
|
||||
public function testProcessValidStatement($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverser->traverse($stmts);
|
||||
}
|
||||
|
||||
public function validStatements()
|
||||
{
|
||||
$statements = array(
|
||||
array('class A { public static function A() {} public function __construct() {}}'),
|
||||
array('class A { private function __construct() {} public static function A() {}}'),
|
||||
);
|
||||
|
||||
if (version_compare(PHP_VERSION, '5.3.3', '>=')) {
|
||||
$statements[] = array('namespace B; class A { private static function A() {}}');
|
||||
}
|
||||
|
||||
return $statements;
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner\UseStatementPass;
|
||||
|
||||
class UseStatementPassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->setPass(new UseStatementPass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider useStatements
|
||||
*/
|
||||
public function testProcess($from, $to)
|
||||
{
|
||||
$this->assertProcessesAs($from, $to);
|
||||
}
|
||||
|
||||
public function useStatements()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
"use StdClass as NotSoStd;\n\$std = new NotSoStd();",
|
||||
"\$std = new \\StdClass();",
|
||||
),
|
||||
array(
|
||||
"namespace Foo;\n\nuse StdClass as S;\n\$std = new S();",
|
||||
"namespace Foo;\n\n\$std = new \\StdClass();",
|
||||
),
|
||||
array(
|
||||
"namespace Foo;\n\nuse \\StdClass as S;\n\$std = new S();",
|
||||
"namespace Foo;\n\n\$std = new \\StdClass();",
|
||||
),
|
||||
array(
|
||||
"use Foo\\Bar as fb;\n\$baz = new fb\\Baz();",
|
||||
"\$baz = new \\Foo\\Bar\\Baz();",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner\ValidClassNamePass;
|
||||
use Psy\Exception\Exception;
|
||||
|
||||
class ValidClassNamePassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->setPass(new ValidClassNamePass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalid
|
||||
*/
|
||||
public function testProcessInvalid($code, $php54 = false)
|
||||
{
|
||||
try {
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverse($stmts);
|
||||
$this->fail();
|
||||
} catch (Exception $e) {
|
||||
if ($php54 && version_compare(PHP_VERSION, '5.4', '<')) {
|
||||
$this->assertInstanceOf('Psy\Exception\ParseErrorException', $e);
|
||||
} else {
|
||||
$this->assertInstanceOf('Psy\Exception\FatalErrorException', $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getInvalid()
|
||||
{
|
||||
// class declarations
|
||||
return array(
|
||||
// core class
|
||||
array('class stdClass {}'),
|
||||
// capitalization
|
||||
array('class stdClass {}'),
|
||||
|
||||
// collisions with interfaces and traits
|
||||
array('interface stdClass {}'),
|
||||
array('trait stdClass {}', true),
|
||||
|
||||
// collisions inside the same code snippet
|
||||
array("
|
||||
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
"),
|
||||
array("
|
||||
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
", true),
|
||||
array("
|
||||
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
", true),
|
||||
array("
|
||||
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
", true),
|
||||
array("
|
||||
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
", true),
|
||||
array("
|
||||
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
"),
|
||||
array("
|
||||
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
|
||||
"),
|
||||
|
||||
// namespaced collisions
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner {
|
||||
class ValidClassNamePassTest {}
|
||||
}
|
||||
"),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
|
||||
class Beta {}
|
||||
}
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
|
||||
class Beta {}
|
||||
}
|
||||
"),
|
||||
|
||||
// extends and implements
|
||||
array('class ValidClassNamePassTest extends NotAClass {}'),
|
||||
array('class ValidClassNamePassTest extends ArrayAccess {}'),
|
||||
array('class ValidClassNamePassTest implements stdClass {}'),
|
||||
array('class ValidClassNamePassTest implements ArrayAccess, stdClass {}'),
|
||||
array('interface ValidClassNamePassTest extends stdClass {}'),
|
||||
array('interface ValidClassNamePassTest extends ArrayAccess, stdClass {}'),
|
||||
|
||||
// class instantiations
|
||||
array('new Psy_Test_CodeCleaner_ValidClassNamePass_Gamma();'),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
|
||||
new Psy_Test_CodeCleaner_ValidClassNamePass_Delta();
|
||||
}
|
||||
"),
|
||||
|
||||
// class constant fetch
|
||||
array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::FOO'),
|
||||
|
||||
// static call
|
||||
array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::foo()'),
|
||||
array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::$foo()'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getValid
|
||||
*/
|
||||
public function testProcessValid($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverse($stmts);
|
||||
}
|
||||
|
||||
public function getValid()
|
||||
{
|
||||
return array(
|
||||
// class declarations
|
||||
array('class Psy_Test_CodeCleaner_ValidClassNamePass_Epsilon {}'),
|
||||
array('namespace Psy\Test\CodeCleaner\ValidClassNamePass; class Zeta {}'),
|
||||
array("
|
||||
namespace { class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}; }
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
|
||||
class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}
|
||||
}
|
||||
"),
|
||||
array('namespace Psy\Test\CodeCleaner\ValidClassNamePass { class stdClass {} }'),
|
||||
|
||||
// class instantiations
|
||||
array('new stdClass();'),
|
||||
array('new stdClass();'),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
|
||||
class Theta {}
|
||||
}
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
|
||||
new Theta();
|
||||
}
|
||||
"),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
|
||||
class Iota {}
|
||||
new Iota();
|
||||
}
|
||||
"),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
|
||||
class Kappa {}
|
||||
}
|
||||
namespace {
|
||||
new \\Psy\\Test\\CodeCleaner\\ValidClassNamePass\\Kappa();
|
||||
}
|
||||
"),
|
||||
|
||||
// Class constant fetch (ValidConstantPassTest validates the actual constant)
|
||||
array('class A {} A::FOO'),
|
||||
array('$a = new DateTime; $a::ATOM'),
|
||||
|
||||
// static call
|
||||
array('DateTime::createFromFormat()'),
|
||||
array('DateTime::$someMethod()'),
|
||||
array('Psy\Test\CodeCleaner\Fixtures\ClassWithStatic::doStuff()'),
|
||||
array('Psy\Test\CodeCleaner\Fixtures\ClassWithCallStatic::doStuff()'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner\ValidConstantPass;
|
||||
|
||||
class ValidConstantPassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->setPass(new ValidConstantPass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidReferences
|
||||
* @expectedException \Psy\Exception\FatalErrorException
|
||||
*/
|
||||
public function testProcessInvalidConstantReferences($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverse($stmts);
|
||||
}
|
||||
|
||||
public function getInvalidReferences()
|
||||
{
|
||||
return array(
|
||||
array('Foo\BAR'),
|
||||
|
||||
// class constant fetch
|
||||
array('Psy\Test\CodeCleaner\ValidConstantPassTest::FOO'),
|
||||
array('DateTime::BACON'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getValidReferences
|
||||
*/
|
||||
public function testProcessValidConstantReferences($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverse($stmts);
|
||||
}
|
||||
|
||||
public function getValidReferences()
|
||||
{
|
||||
return array(
|
||||
array('PHP_EOL'),
|
||||
|
||||
// class constant fetch
|
||||
array('NotAClass::FOO'),
|
||||
array('DateTime::ATOM'),
|
||||
array('$a = new DateTime; $a::ATOM'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell
|
||||
*
|
||||
* (c) 2012-2014 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Test\CodeCleaner;
|
||||
|
||||
use Psy\CodeCleaner\ValidFunctionNamePass;
|
||||
|
||||
class ValidFunctionNamePassTest extends CodeCleanerTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->setPass(new ValidFunctionNamePass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidFunctions
|
||||
* @expectedException \Psy\Exception\FatalErrorException
|
||||
*/
|
||||
public function testProcessInvalidFunctionCallsAndDeclarations($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverse($stmts);
|
||||
}
|
||||
|
||||
public function getInvalidFunctions()
|
||||
{
|
||||
return array(
|
||||
// function declarations
|
||||
array('function array_merge() {}'),
|
||||
array('function Array_Merge() {}'),
|
||||
array("
|
||||
function psy_test_codecleaner_validfunctionnamepass_alpha() {}
|
||||
function psy_test_codecleaner_validfunctionnamepass_alpha() {}
|
||||
"),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
|
||||
function beta() {}
|
||||
}
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
|
||||
function beta() {}
|
||||
}
|
||||
"),
|
||||
|
||||
// function calls
|
||||
array('psy_test_codecleaner_validfunctionnamepass_gamma()'),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
|
||||
delta();
|
||||
}
|
||||
"),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getValidFunctions
|
||||
*/
|
||||
public function testProcessValidFunctionCallsAndDeclarations($code)
|
||||
{
|
||||
$stmts = $this->parse($code);
|
||||
$this->traverse($stmts);
|
||||
}
|
||||
|
||||
public function getValidFunctions()
|
||||
{
|
||||
return array(
|
||||
array('function psy_test_codecleaner_validfunctionnamepass_epsilon() {}'),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
|
||||
function zeta() {}
|
||||
}
|
||||
"),
|
||||
array("
|
||||
namespace {
|
||||
function psy_test_codecleaner_validfunctionnamepass_eta() {}
|
||||
}
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
|
||||
function psy_test_codecleaner_validfunctionnamepass_eta() {}
|
||||
}
|
||||
"),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
|
||||
function psy_test_codecleaner_validfunctionnamepass_eta() {}
|
||||
}
|
||||
namespace {
|
||||
function psy_test_codecleaner_validfunctionnamepass_eta() {}
|
||||
}
|
||||
"),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
|
||||
function array_merge() {}
|
||||
}
|
||||
"),
|
||||
|
||||
// function calls
|
||||
array('array_merge();'),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
|
||||
function theta() {}
|
||||
}
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
|
||||
theta();
|
||||
}
|
||||
"),
|
||||
// closures
|
||||
array('$test = function(){};$test()'),
|
||||
array("
|
||||
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
|
||||
function theta() {}
|
||||
}
|
||||
namespace {
|
||||
Psy\\Test\\CodeCleaner\\ValidFunctionNamePass\\theta();
|
||||
}
|
||||
"),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user