2016-12-08 04:38:57 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Checker\ProjectChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
|
|
|
|
class JsonOutputTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-12-14 18:55:23 +01:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-12-08 04:38:57 +01:00
|
|
|
protected static $parser;
|
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
|
2016-12-14 18:28:38 +01:00
|
|
|
$config = new TestConfig();
|
2016-12-08 04:38:57 +01:00
|
|
|
$config->throw_exception = false;
|
|
|
|
$config->stop_on_first_error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
|
|
|
}
|
|
|
|
|
2016-12-08 21:57:18 +01:00
|
|
|
public function testJsonOutputForReturnTypeError()
|
2016-12-08 04:38:57 +01:00
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(int $a) : string {
|
2016-12-08 04:38:57 +01:00
|
|
|
return $a + 1;
|
|
|
|
}';
|
|
|
|
|
|
|
|
$project_checker = new ProjectChecker(false, true, ProjectChecker::TYPE_JSON);
|
|
|
|
$project_checker->registerFile(
|
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php');
|
|
|
|
$file_checker->check();
|
|
|
|
$issue_data = IssueBuffer::getIssueData()[0];
|
|
|
|
$this->assertSame('somefile.php', $issue_data['file_path']);
|
|
|
|
$this->assertSame('error', $issue_data['type']);
|
|
|
|
$this->assertSame("The given return type 'string' for foo is incorrect, got 'int'", $issue_data['message']);
|
|
|
|
$this->assertSame(2, $issue_data['line_number']);
|
|
|
|
$this->assertSame(
|
|
|
|
'string',
|
|
|
|
substr($file_contents, $issue_data['from'], $issue_data['to'] - $issue_data['from'])
|
|
|
|
);
|
|
|
|
}
|
2016-12-08 21:57:18 +01:00
|
|
|
|
|
|
|
public function testJsonOutputForUndefinedVar()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(int $a) : int {
|
2016-12-08 21:57:18 +01:00
|
|
|
return $b + 1;
|
|
|
|
}';
|
|
|
|
|
|
|
|
$project_checker = new ProjectChecker(false, true, ProjectChecker::TYPE_JSON);
|
|
|
|
$project_checker->registerFile(
|
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php');
|
|
|
|
$file_checker->check();
|
|
|
|
$issue_data = IssueBuffer::getIssueData()[0];
|
|
|
|
$this->assertSame('somefile.php', $issue_data['file_path']);
|
|
|
|
$this->assertSame('error', $issue_data['type']);
|
|
|
|
$this->assertSame('Cannot find referenced variable $b', $issue_data['message']);
|
|
|
|
$this->assertSame(3, $issue_data['line_number']);
|
|
|
|
$this->assertSame(
|
|
|
|
'$b',
|
|
|
|
substr($file_contents, $issue_data['from'], $issue_data['to'] - $issue_data['from'])
|
|
|
|
);
|
|
|
|
}
|
2016-12-08 22:23:07 +01:00
|
|
|
|
|
|
|
public function testJsonOutputForUnknownParamClass()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(Badger\Bodger $a) : Badger\Bodger {
|
2016-12-08 22:23:07 +01:00
|
|
|
return $a;
|
|
|
|
}';
|
|
|
|
|
|
|
|
$project_checker = new ProjectChecker(false, true, ProjectChecker::TYPE_JSON);
|
|
|
|
$project_checker->registerFile(
|
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php');
|
|
|
|
$file_checker->check();
|
|
|
|
$issue_data = IssueBuffer::getIssueData()[0];
|
|
|
|
$this->assertSame('somefile.php', $issue_data['file_path']);
|
|
|
|
$this->assertSame('error', $issue_data['type']);
|
|
|
|
$this->assertSame('Class or interface Badger\\Bodger does not exist', $issue_data['message']);
|
|
|
|
$this->assertSame(2, $issue_data['line_number']);
|
|
|
|
$this->assertSame(
|
|
|
|
'Badger\\Bodger',
|
|
|
|
substr($file_contents, $issue_data['from'], $issue_data['to'] - $issue_data['from'])
|
|
|
|
);
|
|
|
|
}
|
2016-12-13 01:11:21 +01:00
|
|
|
|
|
|
|
public function testJsonOutputForMissingReturnType()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo() {
|
2016-12-13 01:11:21 +01:00
|
|
|
return "hello";
|
|
|
|
}';
|
|
|
|
|
|
|
|
$project_checker = new ProjectChecker(false, true, ProjectChecker::TYPE_JSON);
|
|
|
|
$project_checker->registerFile(
|
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php');
|
|
|
|
$file_checker->check();
|
|
|
|
$issue_data = IssueBuffer::getIssueData()[0];
|
|
|
|
$this->assertSame('somefile.php', $issue_data['file_path']);
|
|
|
|
$this->assertSame('error', $issue_data['type']);
|
|
|
|
$this->assertSame('Method foo does not have a return type', $issue_data['message']);
|
|
|
|
$this->assertSame(2, $issue_data['line_number']);
|
|
|
|
$this->assertSame(
|
2016-12-30 19:09:00 +01:00
|
|
|
'function fooFoo() {',
|
2016-12-13 01:11:21 +01:00
|
|
|
substr($file_contents, $issue_data['from'], $issue_data['to'] - $issue_data['from'])
|
|
|
|
);
|
|
|
|
}
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|