1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-13 17:57:37 +01:00
psalm/tests/ErrorBaselineTest.php

529 lines
16 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psalm\ErrorBaseline;
use Psalm\Exception\ConfigException;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Provider\FileProvider;
Test parallelization (#4045) * 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
2020-08-23 16:32:07 +02:00
use Psalm\Internal\RuntimeCaches;
use const LIBXML_NOBLANKS;
class ErrorBaselineTest extends TestCase
{
use ProphecyTrait;
/** @var ObjectProphecy */
private $fileProvider;
2019-05-17 00:36:36 +02:00
public function setUp() : void
{
Test parallelization (#4045) * 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
2020-08-23 16:32:07 +02:00
RuntimeCaches::clearAll();
$this->fileProvider = $this->prophesize(FileProvider::class);
}
/**
* @return void
*/
public function testLoadShouldParseXmlBaselineToPhpArray()
{
$baselineFilePath = 'baseline.xml';
$this->fileProvider->fileExists($baselineFilePath)->willReturn(true);
$this->fileProvider->getContents($baselineFilePath)->willReturn(
'<?xml version="1.0" encoding="UTF-8"?>
<files>
<file src="sample/sample-file.php">
<MixedAssignment occurrences="2">
<code>foo</code>
<code>bar</code>
</MixedAssignment>
<InvalidReturnStatement occurrences="1"/>
</file>
<file src="sample\sample-file2.php">
<PossiblyUnusedMethod occurrences="2">
<code>foo</code>
<code>bar</code>
</PossiblyUnusedMethod>
</file>
</files>'
);
$expectedParsedBaseline = [
'sample/sample-file.php' => [
'MixedAssignment' => ['o' => 2, 's' => ['foo', 'bar']],
'InvalidReturnStatement' => ['o' => 1, 's' => []],
],
'sample/sample-file2.php' => [
'PossiblyUnusedMethod' => ['o' => 2, 's' => ['foo', 'bar']],
],
];
2019-03-23 19:27:54 +01:00
$this->assertSame(
$expectedParsedBaseline,
ErrorBaseline::read($this->fileProvider->reveal(), $baselineFilePath)
);
}
public function testLoadShouldIgnoreLineEndingsInIssueSnippet(): void
{
$baselineFilePath = 'baseline.xml';
$this->fileProvider->fileExists($baselineFilePath)->willReturn(true);
$this->fileProvider->getContents($baselineFilePath)->willReturn(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<files>
<file src=\"sample/sample-file.php\">
<MixedAssignment occurrences=\"1\">
<code>foo\r</code>
</MixedAssignment>
</file>
</files>"
);
$expectedParsedBaseline = [
'sample/sample-file.php' => [
'MixedAssignment' => ['o' => 1, 's' => ['foo']],
],
];
$this->assertSame(
$expectedParsedBaseline,
ErrorBaseline::read($this->fileProvider->reveal(), $baselineFilePath)
);
}
/**
* @return void
*/
public function testLoadShouldThrowExceptionWhenFilesAreNotDefinedInBaselineFile()
{
$this->expectException(ConfigException::class);
$baselineFile = 'baseline.xml';
$this->fileProvider->fileExists($baselineFile)->willReturn(true);
$this->fileProvider->getContents($baselineFile)->willReturn(
'<?xml version="1.0" encoding="UTF-8"?>
<other>
</other>
'
);
ErrorBaseline::read($this->fileProvider->reveal(), $baselineFile);
}
/**
* @return void
*/
public function testLoadShouldThrowExceptionWhenBaselineFileDoesNotExist()
{
$this->expectException(ConfigException::class);
$baselineFile = 'baseline.xml';
$this->fileProvider->fileExists($baselineFile)->willReturn(false);
ErrorBaseline::read($this->fileProvider->reveal(), $baselineFile);
}
/**
* @return void
*/
public function testCountTotalIssuesShouldReturnCorrectNumber()
{
$existingIssues = [
'sample/sample-file.php' => [
'MixedAssignment' => ['o' => 2, 's' => ['bar']],
'MixedOperand' => ['o' => 2, 's' => []],
],
'sample/sample-file2.php' => [
'TypeCoercion' => ['o' => 1, 's' => []],
],
];
$totalIssues = ErrorBaseline::countTotalIssues($existingIssues);
2019-03-23 19:27:54 +01:00
$this->assertSame($totalIssues, 5);
}
/**
* @return void
*/
public function testCreateShouldAggregateIssuesPerFile()
{
$baselineFile = 'baseline.xml';
$documentContent = null;
$this->fileProvider->setContents(
$baselineFile,
Argument::that(function (string $document) use (&$documentContent): bool {
$documentContent = $document;
return true;
})
);
2020-01-22 03:22:38 +01:00
ErrorBaseline::create(
$this->fileProvider->reveal(),
$baselineFile,
[
2020-01-22 03:22:38 +01:00
'sample/sample-file.php' => [
2020-02-17 00:24:40 +01:00
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'MixedAssignment',
'Message',
'sample/sample-file.php',
'sample/sample-file.php',
'foo',
'foo',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'MixedAssignment',
'Message',
'sample/sample-file.php',
'sample/sample-file.php',
'bar',
'bar',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'MixedAssignment',
'Message',
'sample/sample-file.php',
'sample/sample-file.php',
'bat',
'bat',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'MixedOperand',
'Message',
'sample/sample-file.php',
'sample/sample-file.php',
'bing',
'bing',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
new \Psalm\Internal\Analyzer\IssueData(
'info',
0,
0,
'AssignmentToVoid',
'Message',
'sample/sample-file.php',
'sample/sample-file.php',
'bong',
'bong',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
2020-01-22 03:22:38 +01:00
],
'sample/sample-file2.php' => [
2020-02-17 00:24:40 +01:00
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'MixedAssignment',
'Message',
'sample/sample-file2.php',
'sample/sample-file2.php',
'boardy',
'boardy',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'MixedAssignment',
'Message',
'sample/sample-file2.php',
'sample/sample-file2.php',
'bardy',
'bardy',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'TypeCoercion',
'Message',
'sample/sample-file2.php',
'sample/sample-file2.php',
'hardy' . "\n",
'hardy' . "\n",
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
2020-01-22 03:22:38 +01:00
],
],
2020-01-22 03:22:38 +01:00
false
);
$baselineDocument = new \DOMDocument();
$baselineDocument->loadXML($documentContent, LIBXML_NOBLANKS);
/** @var \DOMElement[] $files */
$files = $baselineDocument->getElementsByTagName('files')[0]->childNodes;
[$file1, $file2] = $files;
2019-03-23 19:27:54 +01:00
$this->assertSame('sample/sample-file.php', $file1->getAttribute('src'));
$this->assertSame('sample/sample-file2.php', $file2->getAttribute('src'));
/** @var \DOMElement[] $file1Issues */
$file1Issues = $file1->childNodes;
/** @var \DOMElement[] $file2Issues */
$file2Issues = $file2->childNodes;
2019-03-23 19:27:54 +01:00
$this->assertSame('MixedAssignment', $file1Issues[0]->tagName);
$this->assertSame('3', $file1Issues[0]->getAttribute('occurrences'));
$this->assertSame('MixedOperand', $file1Issues[1]->tagName);
$this->assertSame('1', $file1Issues[1]->getAttribute('occurrences'));
2019-03-23 19:27:54 +01:00
$this->assertSame('MixedAssignment', $file2Issues[0]->tagName);
$this->assertSame('2', $file2Issues[0]->getAttribute('occurrences'));
$this->assertSame('TypeCoercion', $file2Issues[1]->tagName);
$this->assertSame('1', $file2Issues[1]->getAttribute('occurrences'));
}
/**
* @return void
*/
public function testUpdateShouldRemoveExistingIssuesWithoutAddingNewOnes()
{
$baselineFile = 'baseline.xml';
$this->fileProvider->fileExists($baselineFile)->willReturn(true);
$this->fileProvider->getContents($baselineFile)->willReturn(
'<?xml version="1.0" encoding="UTF-8"?>
<files>
<file src="sample/sample-file.php">
<MixedAssignment occurrences="3">
<code>bar</code>
<code>bat</code>
</MixedAssignment>
<MixedOperand occurrences="1"/>
</file>
<file src="sample/sample-file2.php">
<MixedAssignment occurrences="2"/>
<TypeCoercion occurrences="1"/>
</file>
<file src="sample/sample-file3.php">
<MixedAssignment occurrences="1"/>
</file>
</files>'
);
$this->fileProvider->setContents(Argument::cetera());
$newIssues = [
2020-01-22 03:22:38 +01:00
'sample/sample-file.php' => [
2020-02-17 00:24:40 +01:00
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'MixedAssignment',
'Message',
'sample/sample-file.php',
'sample/sample-file.php',
'foo',
'foo',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'MixedAssignment',
'Message',
'sample/sample-file.php',
'sample/sample-file.php',
'bar',
'bar',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'MixedOperand',
'Message',
'sample/sample-file.php',
'sample/sample-file.php',
'bat',
'bat',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'MixedOperand',
'Message',
'sample/sample-file.php',
'sample/sample-file.php',
'bam',
'bam',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
],
2020-01-22 03:22:38 +01:00
'sample/sample-file2.php' => [
2020-02-17 00:24:40 +01:00
new \Psalm\Internal\Analyzer\IssueData(
'error',
0,
0,
'TypeCoercion',
'Message',
'sample/sample-file2.php',
'sample/sample-file2.php',
'tar',
'tar',
0,
0,
0,
0,
0,
2020-02-17 00:30:05 +01:00
0
2020-02-17 00:24:40 +01:00
),
],
];
$remainingBaseline = ErrorBaseline::update(
$this->fileProvider->reveal(),
$baselineFile,
$newIssues,
false
);
2019-03-23 19:27:54 +01:00
$this->assertSame([
'sample/sample-file.php' => [
'MixedAssignment' => ['o' => 2, 's' => ['bar']],
'MixedOperand' => ['o' => 1, 's' => []],
],
'sample/sample-file2.php' => [
'TypeCoercion' => ['o' => 1, 's' => []],
],
], $remainingBaseline);
}
/**
* @return void
*/
public function testAddingACommentInBaselineDoesntTriggerNotice()
{
$baselineFilePath = 'baseline.xml';
$this->fileProvider->fileExists($baselineFilePath)->willReturn(true);
$this->fileProvider->getContents($baselineFilePath)->willReturn(
'<?xml version="1.0" encoding="UTF-8"?>
<files>
<file src="sample/sample-file.php">
<!-- here is a comment ! //-->
<MixedAssignment occurrences="2">
<code>foo</code>
<code>bar</code>
</MixedAssignment>
<InvalidReturnStatement occurrences="1"/>
</file>
<!-- And another one ! //-->
<file src="sample\sample-file2.php">
<PossiblyUnusedMethod occurrences="2">
<code>foo</code>
<code>bar</code>
</PossiblyUnusedMethod>
</file>
</files>'
);
$expectedParsedBaseline = [
'sample/sample-file.php' => [
'MixedAssignment' => ['o' => 2, 's' => ['foo', 'bar']],
'InvalidReturnStatement' => ['o' => 1, 's' => []],
],
'sample/sample-file2.php' => [
'PossiblyUnusedMethod' => ['o' => 2, 's' => ['foo', 'bar']],
],
];
$this->assertSame(
$expectedParsedBaseline,
ErrorBaseline::read($this->fileProvider->reveal(), $baselineFilePath)
);
}
}