2017-09-08 17:18:48 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use LSS\XML2Array;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Checker\ProjectChecker;
|
2018-01-21 16:22:04 +01:00
|
|
|
use Psalm\Context;
|
2017-09-08 17:18:48 +02:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
|
|
|
|
class ReportOutputTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
// `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.
|
|
|
|
FileChecker::clearCache();
|
|
|
|
$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-09-08 17:18:48 +02:00
|
|
|
$this->file_provider,
|
2017-10-15 17:57:44 +02:00
|
|
|
new Provider\FakeParserCacheProvider(),
|
2018-02-19 06:27:39 +01:00
|
|
|
new \Psalm\Provider\NoCache\NoFileStorageCacheProvider(),
|
|
|
|
new \Psalm\Provider\NoCache\NoClassLikeStorageCacheProvider(),
|
2017-09-08 17:18:48 +02:00
|
|
|
false
|
|
|
|
);
|
|
|
|
$this->project_checker->reports['json'] = __DIR__ . '/test-report.json';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReportFormatValid()
|
|
|
|
{
|
2018-01-21 16:22:04 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
// No exception
|
|
|
|
foreach (['.xml', '.txt', '.json', '.emacs'] as $extension) {
|
2018-01-02 03:17:23 +01:00
|
|
|
new ProjectChecker(
|
2018-01-21 16:22:04 +01:00
|
|
|
$config,
|
2017-09-08 17:18:48 +02:00
|
|
|
$this->file_provider,
|
2017-10-15 17:57:44 +02:00
|
|
|
new Provider\FakeParserCacheProvider(),
|
2018-02-19 06:27:39 +01:00
|
|
|
new \Psalm\Provider\NoCache\NoFileStorageCacheProvider(),
|
|
|
|
new \Psalm\Provider\NoCache\NoClassLikeStorageCacheProvider(),
|
2017-09-08 17:18:48 +02:00
|
|
|
false,
|
|
|
|
true,
|
2018-01-02 03:17:23 +01:00
|
|
|
ProjectChecker::TYPE_CONSOLE,
|
2017-09-08 17:18:48 +02:00
|
|
|
1,
|
|
|
|
false,
|
|
|
|
'/tmp/report' . $extension
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \UnexpectedValueException
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReportFormatException()
|
|
|
|
{
|
2018-01-21 16:22:04 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
2018-01-02 03:17:23 +01:00
|
|
|
new ProjectChecker(
|
2018-01-21 16:22:04 +01:00
|
|
|
$config,
|
2017-09-08 17:18:48 +02:00
|
|
|
$this->file_provider,
|
2017-10-15 17:57:44 +02:00
|
|
|
new Provider\FakeParserCacheProvider(),
|
2018-02-19 06:27:39 +01:00
|
|
|
new \Psalm\Provider\NoCache\NoFileStorageCacheProvider(),
|
|
|
|
new \Psalm\Provider\NoCache\NoClassLikeStorageCacheProvider(),
|
2017-09-08 17:18:48 +02:00
|
|
|
false,
|
|
|
|
true,
|
2018-01-02 03:17:23 +01:00
|
|
|
ProjectChecker::TYPE_CONSOLE,
|
2017-09-08 17:18:48 +02:00
|
|
|
1,
|
|
|
|
false,
|
|
|
|
'/tmp/report.log'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testJsonOutputForGetPsalmDotOrg()
|
|
|
|
{
|
|
|
|
$file_contents = '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function psalmCanVerify(int $your_code): ?string {
|
2017-09-08 17:18:48 +02: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;';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
$file_contents
|
|
|
|
);
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
$issue_data = [
|
|
|
|
[
|
2018-02-20 00:16:09 +01:00
|
|
|
'severity' => 'error',
|
|
|
|
'line_from' => 7,
|
|
|
|
'line_to' => 7,
|
|
|
|
'type' => 'UndefinedConstant',
|
|
|
|
'message' => 'Const CHANGE_ME is not defined',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => 'echo CHANGE_ME;',
|
|
|
|
'selected_text' => 'CHANGE_ME',
|
|
|
|
'from' => 125,
|
|
|
|
'to' => 134,
|
|
|
|
'snippet_from' => 120,
|
|
|
|
'snippet_to' => 135,
|
|
|
|
'column_from' => 6,
|
|
|
|
'column_to' => 15
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'severity' => 'error',
|
|
|
|
'line_from' => 15,
|
|
|
|
'line_to' => 15,
|
|
|
|
'type' => 'PossiblyUndefinedGlobalVariable',
|
|
|
|
'message' => 'Possibly undefined global variable $a, first seen on line 10',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => 'echo $a',
|
|
|
|
'selected_text' => '$a',
|
|
|
|
'from' => 201,
|
|
|
|
'to' => 203,
|
|
|
|
'snippet_from' => 196,
|
|
|
|
'snippet_to' => 203,
|
|
|
|
'column_from' => 6,
|
|
|
|
'column_to' => 8
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'severity' => 'error',
|
|
|
|
'line_from' => 3,
|
|
|
|
'line_to' => 3,
|
|
|
|
'type' => 'UndefinedVariable',
|
|
|
|
'message' => 'Cannot find referenced variable $as_you',
|
|
|
|
'file_name' => 'somefile.php',
|
|
|
|
'file_path' => 'somefile.php',
|
|
|
|
'snippet' => ' return $as_you . "type";',
|
|
|
|
'selected_text' => '$as_you',
|
|
|
|
'from' => 66,
|
|
|
|
'to' => 73,
|
|
|
|
'snippet_from' => 57,
|
|
|
|
'snippet_to' => 83,
|
|
|
|
'column_from' => 10,
|
|
|
|
'column_to' => 17
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'severity' => 'error',
|
|
|
|
'line_from' => 2,
|
|
|
|
'line_to' => 4,
|
|
|
|
'type' => 'MixedInferredReturnType',
|
|
|
|
'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 {
|
2017-09-08 17:18:48 +02:00
|
|
|
return $as_you . "type";
|
|
|
|
}',
|
2018-02-20 00:16:09 +01:00
|
|
|
'selected_text' => '?string',
|
|
|
|
'from' => 47,
|
|
|
|
'to' => 54,
|
|
|
|
'snippet_from' => 6,
|
|
|
|
'snippet_to' => 85,
|
|
|
|
'column_from' => 42,
|
|
|
|
'column_to' => 49
|
|
|
|
],
|
2017-09-08 17:18:48 +02:00
|
|
|
];
|
2018-02-20 00:16:09 +01:00
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
$emacs = 'somefile.php:7:6:error - Const CHANGE_ME is not defined
|
2017-12-06 06:56:00 +01:00
|
|
|
somefile.php:15:6:error - Possibly undefined global variable $a, first seen on line 10
|
2017-09-08 17:18:48 +02:00
|
|
|
somefile.php:3:10:error - Cannot find referenced variable $as_you
|
2018-01-11 21:50:45 +01:00
|
|
|
somefile.php:2:42:error - Could not verify return type \'string|null\' for psalmCanVerify
|
2017-09-08 17:18:48 +02:00
|
|
|
';
|
|
|
|
$this->assertSame(
|
|
|
|
$issue_data,
|
|
|
|
json_decode(IssueBuffer::getOutput(ProjectChecker::TYPE_JSON, false), true)
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
$emacs,
|
|
|
|
IssueBuffer::getOutput(ProjectChecker::TYPE_EMACS, false)
|
|
|
|
);
|
|
|
|
// FIXME: The XML parser only return strings, all int value are casted, so the assertSame failed
|
|
|
|
//$this->assertSame(
|
|
|
|
// ['report' => ['item' => $issue_data]],
|
|
|
|
// XML2Array::createArray(IssueBuffer::getOutput(ProjectChecker::TYPE_XML, false), LIBXML_NOCDATA)
|
|
|
|
//);
|
|
|
|
}
|
2017-09-27 00:18:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testEmptyReportIfNotError()
|
|
|
|
{
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php ?>'
|
|
|
|
);
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-09-27 00:18:24 +02:00
|
|
|
$this->assertSame(
|
|
|
|
'[]
|
|
|
|
',
|
|
|
|
IssueBuffer::getOutput(ProjectChecker::TYPE_JSON, false)
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
'',
|
|
|
|
IssueBuffer::getOutput(ProjectChecker::TYPE_EMACS, false)
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<report>
|
|
|
|
<item/>
|
|
|
|
</report>
|
|
|
|
',
|
|
|
|
IssueBuffer::getOutput(ProjectChecker::TYPE_XML, false)
|
|
|
|
);
|
2017-09-27 00:33:55 +02:00
|
|
|
|
2018-01-09 17:49:10 +01:00
|
|
|
IssueBuffer::finish($this->project_checker, true, 0);
|
2017-09-27 00:33:55 +02:00
|
|
|
$this->assertFileExists(__DIR__ . '/test-report.json');
|
|
|
|
$this->assertSame('[]
|
|
|
|
', file_get_contents(__DIR__ . '/test-report.json'));
|
|
|
|
unlink(__DIR__ . '/test-report.json');
|
2017-09-27 00:18:24 +02:00
|
|
|
}
|
2017-09-08 17:18:48 +02:00
|
|
|
}
|