2016-12-29 16:24:10 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
2017-02-01 01:22:05 +01:00
|
|
|
use Psalm\Context;
|
2016-12-29 16:24:10 +01:00
|
|
|
|
|
|
|
class ConfigTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2017-02-01 01:22:05 +01:00
|
|
|
/** @var \PhpParser\Parser */
|
|
|
|
protected static $parser;
|
|
|
|
|
|
|
|
/** @var TestConfig */
|
|
|
|
protected static $config;
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
2017-02-01 01:22:05 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
self::$config = new TestConfig();
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-29 16:24:10 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
2016-12-29 16:24:10 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
* @psalm-return array<mixed, string>
|
|
|
|
*/
|
2016-12-30 04:31:52 +01:00
|
|
|
public static function getAllIssues()
|
|
|
|
{
|
|
|
|
return array_filter(
|
|
|
|
array_map(
|
2017-01-13 20:14:24 +01:00
|
|
|
/**
|
|
|
|
* @param string $file_name
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-30 04:31:52 +01:00
|
|
|
function ($file_name) {
|
|
|
|
return substr($file_name, 0, -4);
|
|
|
|
},
|
|
|
|
scandir(dirname(__DIR__) . '/src/Psalm/Issue')
|
|
|
|
),
|
2017-01-13 20:14:24 +01:00
|
|
|
/**
|
|
|
|
* @param string $issue_name
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-12-30 04:31:52 +01:00
|
|
|
function ($issue_name) {
|
|
|
|
return !empty($issue_name) && $issue_name !== 'CodeError' && $issue_name !== 'CodeIssue';
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-29 16:24:10 +01:00
|
|
|
public function testBarebonesConfig()
|
|
|
|
{
|
2017-02-01 01:22:05 +01:00
|
|
|
$config = Config::loadFromXML(
|
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
</psalm>'
|
|
|
|
);
|
2016-12-29 16:24:10 +01:00
|
|
|
|
2017-01-16 17:41:57 +01:00
|
|
|
$this->assertTrue($config->isInProjectDirs(realpath('src/Psalm/Type.php')));
|
|
|
|
$this->assertFalse($config->isInProjectDirs(realpath('examples/StringChecker.php')));
|
2016-12-29 16:24:10 +01:00
|
|
|
}
|
2016-12-30 00:33:03 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-30 00:33:03 +01:00
|
|
|
public function testIgnoreProjectDirectory()
|
|
|
|
{
|
2017-02-01 01:22:05 +01:00
|
|
|
$config = Config::loadFromXML(
|
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
<ignoreFiles>
|
|
|
|
<directory name="src/Psalm/Checker" />
|
|
|
|
</ignoreFiles>
|
|
|
|
</projectFiles>
|
|
|
|
</psalm>'
|
|
|
|
);
|
2016-12-30 00:33:03 +01:00
|
|
|
|
2017-01-16 17:41:57 +01:00
|
|
|
$this->assertTrue($config->isInProjectDirs(realpath('src/Psalm/Type.php')));
|
|
|
|
$this->assertFalse($config->isInProjectDirs(realpath('src/Psalm/Checker/FileChecker.php')));
|
|
|
|
$this->assertFalse($config->isInProjectDirs(realpath('examples/StringChecker.php')));
|
2016-12-30 00:33:03 +01:00
|
|
|
}
|
2016-12-30 02:07:42 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-30 02:07:42 +01:00
|
|
|
public function testIssueHandler()
|
|
|
|
{
|
2017-02-01 01:22:05 +01:00
|
|
|
$config = Config::loadFromXML(
|
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
<directory name="tests" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<issueHandlers>
|
|
|
|
<MissingReturnType errorLevel="suppress" />
|
|
|
|
</issueHandlers>
|
|
|
|
</psalm>'
|
|
|
|
);
|
2016-12-30 02:07:42 +01:00
|
|
|
|
2017-01-16 17:41:57 +01:00
|
|
|
$this->assertTrue($config->excludeIssueInFile('MissingReturnType', realpath('tests/ConfigTest.php')));
|
|
|
|
$this->assertTrue($config->excludeIssueInFile('MissingReturnType', realpath('src/Psalm/Type.php')));
|
2016-12-30 02:07:42 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-30 02:07:42 +01:00
|
|
|
public function testIssueHandlerWithCustomErrorLevels()
|
|
|
|
{
|
2017-02-01 01:22:05 +01:00
|
|
|
$config = Config::loadFromXML(
|
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
<directory name="tests" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<issueHandlers>
|
|
|
|
<MissingReturnType errorLevel="info">
|
|
|
|
<errorLevel type="suppress">
|
|
|
|
<directory name="tests" />
|
|
|
|
</errorLevel>
|
|
|
|
<errorLevel type="error">
|
|
|
|
<directory name="src/Psalm/Checker" />
|
|
|
|
</errorLevel>
|
|
|
|
</MissingReturnType>
|
|
|
|
</issueHandlers>
|
|
|
|
</psalm>'
|
|
|
|
);
|
2016-12-30 02:07:42 +01:00
|
|
|
|
2017-01-16 17:41:57 +01:00
|
|
|
$this->assertTrue($config->excludeIssueInFile('MissingReturnType', realpath('tests/ConfigTest.php')));
|
|
|
|
$this->assertFalse($config->excludeIssueInFile('MissingReturnType', realpath('src/Psalm/Type.php')));
|
|
|
|
$this->assertFalse($config->excludeIssueInFile('MissingReturnType', realpath('src/Psalm/Checker/FileChecker.php')));
|
2016-12-30 02:23:04 +01:00
|
|
|
|
2017-01-16 17:41:57 +01:00
|
|
|
$this->assertSame('info', $config->getReportingLevelForFile('MissingReturnType', realpath('src/Psalm/Type.php')));
|
|
|
|
$this->assertSame('error', $config->getReportingLevelForFile('MissingReturnType', realpath('src/Psalm/Checker/FileChecker.php')));
|
2016-12-30 02:07:42 +01:00
|
|
|
}
|
2016-12-30 04:31:52 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-30 04:31:52 +01:00
|
|
|
public function testAllPossibleIssues()
|
|
|
|
{
|
|
|
|
$all_possible_handlers = implode(
|
|
|
|
' ',
|
|
|
|
array_map(
|
2017-01-13 20:14:24 +01:00
|
|
|
/**
|
|
|
|
* @param string $issue_name
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-30 04:31:52 +01:00
|
|
|
function ($issue_name) {
|
|
|
|
return '<' . $issue_name . ' errorLevel="suppress" />' . PHP_EOL;
|
|
|
|
},
|
|
|
|
self::getAllIssues()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2017-02-08 06:28:26 +01:00
|
|
|
Config::loadFromXML(
|
2017-02-01 01:22:05 +01:00
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
2016-12-30 04:31:52 +01:00
|
|
|
|
2017-02-01 01:22:05 +01:00
|
|
|
<issueHandlers>
|
|
|
|
' . $all_possible_handlers . '
|
|
|
|
</issueHandlers>
|
|
|
|
</psalm>'
|
|
|
|
);
|
2016-12-30 04:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\ConfigException
|
2016-12-30 04:31:52 +01:00
|
|
|
* @expectedExceptionMessage This element is not expected
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-30 04:31:52 +01:00
|
|
|
*/
|
|
|
|
public function testImpossibleIssue()
|
|
|
|
{
|
2017-02-08 06:28:26 +01:00
|
|
|
Config::loadFromXML(
|
2017-02-01 01:22:05 +01:00
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<issueHandlers>
|
|
|
|
<ImpossibleIssue errorLevel="suppress" />
|
|
|
|
</issueHandlers>
|
|
|
|
</psalm>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\ConfigException
|
|
|
|
* @expectedExceptionMessage Cannot resolve stubfile path
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNonexistentStubFile()
|
|
|
|
{
|
2017-02-08 06:28:26 +01:00
|
|
|
Config::loadFromXML(
|
2017-02-01 01:22:05 +01:00
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
|
|
|
<file name="stubs/invalidfile.php" />
|
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubFile()
|
|
|
|
{
|
|
|
|
$this->project_checker->setConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
|
|
|
<file name="tests/stubs/systemclass.php" />
|
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = new SystemClass();
|
|
|
|
echo SystemClass::HELLO;
|
|
|
|
|
|
|
|
$b = $a->foo(5, "hello");
|
|
|
|
$c = SystemClass::bar(5, "hello");
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker(getcwd() . '/src/somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubFileWithExistingClassDefinition()
|
|
|
|
{
|
|
|
|
$this->project_checker->setConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
|
|
|
<file name="tests/stubs/logicexception.php" />
|
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = new LogicException("bad");
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker(getcwd() . '/src/somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-30 04:31:52 +01:00
|
|
|
}
|
2017-02-01 16:13:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage MissingReturnType
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRequireVoidReturnTypeExists()
|
|
|
|
{
|
|
|
|
$this->project_checker->setConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm
|
|
|
|
requireVoidReturnType="true">
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo() {}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker(getcwd() . '/src/somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDoNotRequireVoidReturnTypeExists()
|
|
|
|
{
|
|
|
|
$this->project_checker->setConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
'psalm.xml',
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm
|
|
|
|
requireVoidReturnType="false">
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo() {}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker(getcwd() . '/src/somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
2017-02-13 05:59:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTemplatedFiles()
|
|
|
|
{
|
|
|
|
foreach (['1.xml', '2.xml', '3.xml', '4.xml', '5.xml'] as $file_name) {
|
|
|
|
Config::loadFromXMLFile(realpath(dirname(__DIR__) . '/assets/config_levels/' . $file_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-12-29 16:24:10 +01:00
|
|
|
}
|