mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
dabfb16e34
* Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
134 lines
4.4 KiB
PHP
134 lines
4.4 KiB
PHP
<?php
|
|
namespace Psalm\Tests;
|
|
|
|
use Psalm\Context;
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
|
use Psalm\Internal\RuntimeCaches;
|
|
use Psalm\IssueBuffer;
|
|
use Psalm\Tests\Internal\Provider;
|
|
use function substr;
|
|
|
|
class JsonOutputTest extends TestCase
|
|
{
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function setUp() : void
|
|
{
|
|
// `TestCase::setUp()` creates its own ProjectAnalyzer and Config instance, but we don't want to do that in this
|
|
// case, so don't run a `parent::setUp()` call here.
|
|
RuntimeCaches::clearAll();
|
|
$this->file_provider = new Provider\FakeFileProvider();
|
|
|
|
$config = new TestConfig();
|
|
$config->throw_exception = false;
|
|
|
|
$stdout_report_options = new \Psalm\Report\ReportOptions();
|
|
$stdout_report_options->format = \Psalm\Report::TYPE_JSON;
|
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
|
$config,
|
|
new \Psalm\Internal\Provider\Providers(
|
|
$this->file_provider,
|
|
new Provider\FakeParserCacheProvider()
|
|
),
|
|
$stdout_report_options
|
|
);
|
|
|
|
$this->project_analyzer->getCodebase()->reportUnusedCode();
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerTestJsonOutputErrors
|
|
*
|
|
* @param string $code
|
|
* @param string $message
|
|
* @param int $line_number
|
|
* @param string $error
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testJsonOutputErrors($code, $message, $line_number, $error)
|
|
{
|
|
$this->addFile('somefile.php', $code);
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
$issue_data = IssueBuffer::getIssuesData()['somefile.php'][0];
|
|
|
|
$this->assertSame('somefile.php', $issue_data->file_path);
|
|
$this->assertSame('error', $issue_data->severity);
|
|
$this->assertSame($message, $issue_data->message);
|
|
$this->assertSame($line_number, $issue_data->line_from);
|
|
$this->assertSame(
|
|
$error,
|
|
substr($code, $issue_data->from, $issue_data->to - $issue_data->from)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return array<string,array{string,message:string,line:int,error:string}>
|
|
*/
|
|
public function providerTestJsonOutputErrors()
|
|
{
|
|
return [
|
|
'returnTypeError' => [
|
|
'<?php
|
|
function fooFoo(int $a): string {
|
|
return $a + 1;
|
|
}',
|
|
'message' => "The inferred type 'int' does not match the declared return type 'string' for fooFoo",
|
|
'line' => 3,
|
|
'error' => '$a + 1',
|
|
],
|
|
'undefinedVar' => [
|
|
'<?php
|
|
function fooFoo(int $a): int {
|
|
return $b + 1;
|
|
}',
|
|
'message' => 'Cannot find referenced variable $b',
|
|
'line' => 3,
|
|
'error' => '$b',
|
|
],
|
|
'unknownParamClass' => [
|
|
'<?php
|
|
function fooFoo(Badger\Bodger $a): Badger\Bodger {
|
|
return $a;
|
|
}',
|
|
'message' => 'Class or interface Badger\\Bodger does not exist',
|
|
'line' => 2,
|
|
'error' => 'Badger\\Bodger',
|
|
],
|
|
'missingReturnType' => [
|
|
'<?php
|
|
function fooFoo() {
|
|
return "hello";
|
|
}',
|
|
'message' => 'Method fooFoo does not have a return type, expecting string',
|
|
'line' => 2,
|
|
'error' => 'fooFoo',
|
|
],
|
|
'wrongMultilineReturnType' => [
|
|
'<?php
|
|
/**
|
|
* @return int
|
|
*/
|
|
function fooFoo() {
|
|
return "hello";
|
|
}',
|
|
'message' => "The inferred type 'string(hello)' does not match the declared return type 'int' for fooFoo",
|
|
'line' => 6,
|
|
'error' => '"hello"',
|
|
],
|
|
'assertCancelsMixedAssignment' => [
|
|
'<?php
|
|
$a = $_GET["hello"];
|
|
assert(is_int($a));
|
|
if (is_int($a)) {}',
|
|
'message' => "Found a redundant condition when evaluating docblock-defined type \$a and trying to reconcile type 'int' to int",
|
|
'line' => 4,
|
|
'error' => 'is_int($a)',
|
|
],
|
|
];
|
|
}
|
|
}
|