2016-12-08 04:38:57 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Checker\ProjectChecker;
|
2018-01-21 16:22:04 +01:00
|
|
|
use Psalm\Context;
|
2016-12-08 04:38:57 +01:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class JsonOutputTest extends TestCase
|
2016-12-08 04:38:57 +01:00
|
|
|
{
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-08 04:38:57 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
// `TestCase::setUp()` creates its own ProjectChecker and Config instance, but we don't want to do that in this
|
|
|
|
// case, so don't run a `parent::setUp()` call here.
|
2016-12-08 04:38:57 +01:00
|
|
|
FileChecker::clearCache();
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->file_provider = new Provider\FakeFileProvider();
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
2018-01-02 03:17:23 +01:00
|
|
|
$this->project_checker = new ProjectChecker(
|
2018-01-21 16:22:04 +01:00
|
|
|
$config,
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->file_provider,
|
2017-10-15 17:57:44 +02:00
|
|
|
new Provider\FakeParserCacheProvider(),
|
2017-07-25 22:11:02 +02:00
|
|
|
false,
|
|
|
|
true,
|
|
|
|
ProjectChecker::TYPE_JSON
|
|
|
|
);
|
2017-02-01 01:22:05 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$this->project_checker->getCodebase()->collectReferences();
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @dataProvider providerTestJsonOutputErrors
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-04-25 05:45:02 +02:00
|
|
|
* @param string $code
|
|
|
|
* @param string $message
|
2017-05-27 02:16:18 +02:00
|
|
|
* @param int $line_number
|
2017-04-25 05:45:02 +02:00
|
|
|
* @param string $error
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function testJsonOutputErrors($code, $message, $line_number, $error)
|
2016-12-08 04:38:57 +01:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile('somefile.php', $code);
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-07-25 22:11:02 +02:00
|
|
|
$issue_data = IssueBuffer::getIssuesData()[0];
|
2016-12-08 22:23:07 +01:00
|
|
|
|
|
|
|
$this->assertSame('somefile.php', $issue_data['file_path']);
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->assertSame('error', $issue_data['severity']);
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->assertSame($message, $issue_data['message']);
|
|
|
|
$this->assertSame($line_number, $issue_data['line_number']);
|
2016-12-08 22:23:07 +01:00
|
|
|
$this->assertSame(
|
2017-04-25 05:45:02 +02:00
|
|
|
$error,
|
|
|
|
substr($code, $issue_data['from'], $issue_data['to'] - $issue_data['from'])
|
2017-01-02 05:30:59 +01:00
|
|
|
);
|
|
|
|
}
|
2017-01-16 05:18:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testJsonOutputForGetPsalmDotOrg()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function psalmCanVerify(int $your_code): ?string {
|
2017-01-16 05:18:26 +01:00
|
|
|
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-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
2017-01-16 05:18:26 +01:00
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$this->project_checker->getCodebase()->classlikes->checkClassReferences();
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$issue_data = IssueBuffer::getIssuesData();
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame(
|
2017-01-16 05:18:26 +01:00
|
|
|
[
|
|
|
|
[
|
2017-07-25 22:11:02 +02:00
|
|
|
'severity' => 'error',
|
2017-01-16 05:18:26 +01:00
|
|
|
'line_number' => 7,
|
2017-07-25 22:11:02 +02:00
|
|
|
'type' => 'UndefinedConstant',
|
2017-01-16 05:18:26 +01:00
|
|
|
'message' => 'Const CHANGE_ME is not defined',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => 'echo CHANGE_ME;',
|
2018-01-02 02:04:03 +01:00
|
|
|
'selected_text' => 'CHANGE_ME',
|
2018-01-11 21:50:45 +01:00
|
|
|
'from' => 125,
|
|
|
|
'to' => 134,
|
|
|
|
'snippet_from' => 120,
|
|
|
|
'snippet_to' => 135,
|
2017-07-25 22:11:02 +02:00
|
|
|
'column' => 6,
|
2017-01-16 05:18:26 +01:00
|
|
|
],
|
|
|
|
[
|
2017-07-25 22:11:02 +02:00
|
|
|
'severity' => 'error',
|
2017-01-16 05:18:26 +01:00
|
|
|
'line_number' => 15,
|
2017-12-06 06:56:00 +01:00
|
|
|
'type' => 'PossiblyUndefinedGlobalVariable',
|
|
|
|
'message' => 'Possibly undefined global variable $a, first seen on line 10',
|
2017-01-16 05:18:26 +01:00
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => 'echo $a',
|
2018-01-02 02:04:03 +01:00
|
|
|
'selected_text' => '$a',
|
2018-01-11 21:50:45 +01:00
|
|
|
'from' => 201,
|
|
|
|
'to' => 203,
|
|
|
|
'snippet_from' => 196,
|
|
|
|
'snippet_to' => 203,
|
2017-07-25 22:11:02 +02:00
|
|
|
'column' => 6,
|
2017-01-16 05:18:26 +01:00
|
|
|
],
|
|
|
|
[
|
2017-07-25 22:11:02 +02:00
|
|
|
'severity' => 'error',
|
2017-01-16 05:18:26 +01:00
|
|
|
'line_number' => 3,
|
2017-07-25 22:11:02 +02:00
|
|
|
'type' => 'UndefinedVariable',
|
2017-01-16 05:18:26 +01:00
|
|
|
'message' => 'Cannot find referenced variable $as_you',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => ' return $as_you . "type";',
|
2018-01-02 02:04:03 +01:00
|
|
|
'selected_text' => '$as_you',
|
2018-01-11 21:50:45 +01:00
|
|
|
'from' => 66,
|
|
|
|
'to' => 73,
|
|
|
|
'snippet_from' => 57,
|
|
|
|
'snippet_to' => 83,
|
2017-07-25 22:11:02 +02:00
|
|
|
'column' => 10,
|
2017-01-16 05:18:26 +01:00
|
|
|
],
|
2017-12-30 03:28:21 +01:00
|
|
|
[
|
|
|
|
'severity' => 'error',
|
|
|
|
'line_number' => 2,
|
|
|
|
'type' => 'UnusedParam',
|
|
|
|
'message' => 'Param $your_code is never referenced in this method',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
2018-01-11 21:50:45 +01:00
|
|
|
'snippet' => 'function psalmCanVerify(int $your_code): ?string {',
|
2018-01-02 02:04:03 +01:00
|
|
|
'selected_text' => '$your_code',
|
2017-12-30 03:28:21 +01:00
|
|
|
'from' => 34,
|
|
|
|
'to' => 44,
|
|
|
|
'snippet_from' => 6,
|
2018-01-11 21:50:45 +01:00
|
|
|
'snippet_to' => 56,
|
2017-12-30 03:28:21 +01:00
|
|
|
'column' => 29,
|
|
|
|
],
|
2017-01-16 05:18:26 +01:00
|
|
|
[
|
2017-07-25 22:11:02 +02:00
|
|
|
'severity' => 'error',
|
2017-01-16 05:18:26 +01:00
|
|
|
'line_number' => 2,
|
2017-07-25 22:11:02 +02:00
|
|
|
'type' => 'MixedInferredReturnType',
|
2017-01-16 05:18:26 +01:00
|
|
|
'message' => 'Could not verify return type \'string|null\' for psalmCanVerify',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
2018-01-11 21:50:45 +01:00
|
|
|
'snippet' => 'function psalmCanVerify(int $your_code): ?string {
|
2017-01-16 05:18:26 +01:00
|
|
|
return $as_you . "type";
|
|
|
|
}',
|
2018-01-02 02:04:03 +01:00
|
|
|
'selected_text' => '?string',
|
2018-01-11 21:50:45 +01:00
|
|
|
'from' => 47,
|
|
|
|
'to' => 54,
|
2017-07-25 22:11:02 +02:00
|
|
|
'snippet_from' => 6,
|
2018-01-11 21:50:45 +01:00
|
|
|
'snippet_to' => 85,
|
|
|
|
'column' => 42,
|
2017-01-16 05:18:26 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
$issue_data
|
|
|
|
);
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerTestJsonOutputErrors()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'returnTypeError' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(int $a): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a + 1;
|
|
|
|
}',
|
2017-12-07 21:50:25 +01:00
|
|
|
'message' => "The type 'int' does not match the declared return type 'string' for fooFoo",
|
|
|
|
'line' => 3,
|
|
|
|
'error' => 'return $a + 1;',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'undefinedVar' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(int $a): int {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $b + 1;
|
|
|
|
}',
|
|
|
|
'message' => 'Cannot find referenced variable $b',
|
|
|
|
'line' => 3,
|
2017-05-27 02:05:57 +02:00
|
|
|
'error' => '$b',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'unknownParamClass' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(Badger\Bodger $a): Badger\Bodger {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a;
|
|
|
|
}',
|
|
|
|
'message' => 'Class or interface Badger\\Bodger does not exist',
|
|
|
|
'line' => 2,
|
2017-05-27 02:05:57 +02:00
|
|
|
'error' => 'Badger\\Bodger',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingReturnType' => [
|
|
|
|
'<?php
|
|
|
|
function fooFoo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'message' => 'Method fooFoo does not have a return type, expecting string',
|
|
|
|
'line' => 2,
|
2017-05-27 02:05:57 +02:00
|
|
|
'error' => 'function fooFoo() {',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'wrongMultilineReturnType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function fooFoo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
2017-12-07 21:50:25 +01:00
|
|
|
'message' => "The type 'string' does not match the declared return type 'int' for fooFoo",
|
|
|
|
'line' => 6,
|
|
|
|
'error' => 'return "hello";',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|