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;
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-08 04:38:57 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-08 04:38:57 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
2017-02-18 19:41:27 +01:00
|
|
|
$this->project_checker = new ProjectChecker(false, true, ProjectChecker::TYPE_JSON);
|
2017-02-01 01:22:05 +01:00
|
|
|
|
|
|
|
$config = new TestConfig();
|
|
|
|
$config->throw_exception = false;
|
|
|
|
$config->stop_on_first_error = false;
|
|
|
|
$this->project_checker->setConfig($config);
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-08 21:57:18 +01:00
|
|
|
public function testJsonOutputForReturnTypeError()
|
2016-12-08 04:38:57 +01:00
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2017-01-02 05:30:59 +01:00
|
|
|
function fooFoo(int $a) : string {
|
|
|
|
return $a + 1;
|
|
|
|
}';
|
2016-12-08 04:38:57 +01:00
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
$this->project_checker->registerFile(
|
2016-12-08 04:38:57 +01:00
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-08 04:38:57 +01:00
|
|
|
$issue_data = IssueBuffer::getIssueData()[0];
|
|
|
|
$this->assertSame('somefile.php', $issue_data['file_path']);
|
|
|
|
$this->assertSame('error', $issue_data['type']);
|
2017-03-18 17:18:17 +01:00
|
|
|
$this->assertSame("The declared return type 'string' for fooFoo is incorrect, got 'int'", $issue_data['message']);
|
2016-12-08 04:38:57 +01:00
|
|
|
$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
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-08 21:57:18 +01:00
|
|
|
public function testJsonOutputForUndefinedVar()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2017-01-02 05:30:59 +01:00
|
|
|
function fooFoo(int $a) : int {
|
|
|
|
return $b + 1;
|
|
|
|
}';
|
2016-12-08 21:57:18 +01:00
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
$this->project_checker->registerFile(
|
2016-12-08 21:57:18 +01:00
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-08 21:57:18 +01:00
|
|
|
$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
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-08 22:23:07 +01:00
|
|
|
public function testJsonOutputForUnknownParamClass()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2017-01-02 05:30:59 +01:00
|
|
|
function fooFoo(Badger\Bodger $a) : Badger\Bodger {
|
|
|
|
return $a;
|
|
|
|
}';
|
2016-12-08 22:23:07 +01:00
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
$this->project_checker->registerFile(
|
2016-12-08 22:23:07 +01:00
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-08 22:23:07 +01:00
|
|
|
$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
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-13 01:11:21 +01:00
|
|
|
public function testJsonOutputForMissingReturnType()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2017-01-02 05:30:59 +01:00
|
|
|
function fooFoo() {
|
|
|
|
return "hello";
|
|
|
|
}';
|
2016-12-13 01:11:21 +01:00
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
$this->project_checker->registerFile(
|
2016-12-13 01:11:21 +01:00
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-13 01:11:21 +01:00
|
|
|
$issue_data = IssueBuffer::getIssueData()[0];
|
|
|
|
$this->assertSame('somefile.php', $issue_data['file_path']);
|
|
|
|
$this->assertSame('error', $issue_data['type']);
|
2017-01-24 08:28:54 +01:00
|
|
|
$this->assertSame('Method fooFoo does not have a return type, expecting string', $issue_data['message']);
|
2016-12-13 01:11:21 +01:00
|
|
|
$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'])
|
|
|
|
);
|
|
|
|
}
|
2017-01-02 05:30:59 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 05:30:59 +01:00
|
|
|
public function testJsonOutputForWrongMultilineReturnType()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function fooFoo() {
|
|
|
|
return "hello";
|
|
|
|
}';
|
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
$this->project_checker->registerFile(
|
2017-01-02 05:30:59 +01:00
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2017-01-02 05:30:59 +01:00
|
|
|
$issue_data = IssueBuffer::getIssueData()[0];
|
|
|
|
$this->assertSame('somefile.php', $issue_data['file_path']);
|
|
|
|
$this->assertSame('error', $issue_data['type']);
|
2017-03-18 17:18:17 +01:00
|
|
|
$this->assertSame('The declared return type \'int\' for fooFoo is incorrect, got \'string\'', $issue_data['message']);
|
2017-01-02 05:30:59 +01:00
|
|
|
$this->assertSame(3, $issue_data['line_number']);
|
|
|
|
$this->assertSame(
|
|
|
|
'@return int',
|
|
|
|
substr($file_contents, $issue_data['from'], $issue_data['to'] - $issue_data['from'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 05:30:59 +01:00
|
|
|
public function testJsonOutputForWrongSingleLineReturnType()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
|
|
|
/** @return int */
|
|
|
|
function fooFoo() {
|
|
|
|
return "hello";
|
|
|
|
}';
|
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
$this->project_checker->registerFile(
|
2017-01-02 05:30:59 +01:00
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2017-01-02 05:30:59 +01:00
|
|
|
$issue_data = IssueBuffer::getIssueData()[0];
|
|
|
|
$this->assertSame('somefile.php', $issue_data['file_path']);
|
|
|
|
$this->assertSame('error', $issue_data['type']);
|
2017-03-18 17:18:17 +01:00
|
|
|
$this->assertSame('The declared return type \'int\' for fooFoo is incorrect, got \'string\'', $issue_data['message']);
|
2017-01-02 05:30:59 +01:00
|
|
|
$this->assertSame(2, $issue_data['line_number']);
|
|
|
|
$this->assertSame(
|
|
|
|
'@return int',
|
|
|
|
substr($file_contents, $issue_data['from'], $issue_data['to'] - $issue_data['from'])
|
|
|
|
);
|
|
|
|
}
|
2017-01-16 05:18:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testJsonOutputForGetPsalmDotOrg()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
|
|
|
function psalmCanVerify(int $your_code) : ?string {
|
|
|
|
return $as_you . "type";
|
|
|
|
}
|
|
|
|
|
|
|
|
// and it supports PHP 5.4 - 7.1
|
|
|
|
echo CHANGE_ME;
|
|
|
|
|
|
|
|
if (rand(0, 100) > 10) {
|
|
|
|
$a = 5;
|
|
|
|
} else {
|
|
|
|
//$a = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;';
|
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
$this->project_checker->registerFile(
|
2017-01-16 05:18:26 +01:00
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
$issue_data = IssueBuffer::getIssueData();
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'type' => 'error',
|
|
|
|
'line_number' => 7,
|
|
|
|
'message' => 'Const CHANGE_ME is not defined',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => 'echo CHANGE_ME;',
|
|
|
|
'from' => 126,
|
|
|
|
'to' => 135,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type' => 'error',
|
|
|
|
'line_number' => 15,
|
|
|
|
'message' => 'Possibly undefined variable $a, first seen on line 10',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => 'echo $a',
|
|
|
|
'from' => 202,
|
|
|
|
'to' => 204,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type' => 'error',
|
|
|
|
'line_number' => 3,
|
|
|
|
'message' => 'Cannot find referenced variable $as_you',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => ' return $as_you . "type";',
|
|
|
|
'from' => 67,
|
|
|
|
'to' => 74,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type' => 'error',
|
|
|
|
'line_number' => 2,
|
|
|
|
'message' => 'Could not verify return type \'string|null\' for psalmCanVerify',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => 'function psalmCanVerify(int $your_code) : ?string {
|
|
|
|
return $as_you . "type";
|
|
|
|
}',
|
|
|
|
'from' => 48,
|
|
|
|
'to' => 55,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
$issue_data
|
|
|
|
);
|
|
|
|
}
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|