1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00
psalm/tests/Config/ConfigFileTest.php
Bruce Weirdan 1cf5153700
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 10:32:07 -04:00

234 lines
5.8 KiB
PHP

<?php
namespace Psalm\Tests\Config;
use function file_get_contents;
use function file_put_contents;
use function getcwd;
use const PHP_EOL;
use Psalm\Config;
use Psalm\Internal\PluginManager\ConfigFile;
use Psalm\Internal\RuntimeCaches;
use function sys_get_temp_dir;
use function tempnam;
use function trim;
use function unlink;
/** @group PluginManager */
class ConfigFileTest extends \Psalm\Tests\TestCase
{
/** @var string */
private $file_path;
/** @return void */
public function setUp() : void
{
RuntimeCaches::clearAll();
$this->file_path = tempnam(sys_get_temp_dir(), 'psalm-test-config');
}
/** @return void */
public function tearDown() : void
{
@unlink($this->file_path);
}
/**
* @return void
* @test
*/
public function canCreateConfigObject()
{
file_put_contents($this->file_path, trim('
<?xml version="1.0"?>
<psalm></psalm>
'));
$config_file = new ConfigFile((string)getcwd(), $this->file_path);
$this->assertInstanceOf(Config::class, $config_file->getConfig());
}
/**
* @return void
* @test
*/
public function addCanAddPluginClassToExistingPluginsNode()
{
file_put_contents(
$this->file_path,
'<?xml version="1.0" encoding="UTF-8"?>
<psalm
name="bar"
>
<plugins></plugins>
</psalm>' . PHP_EOL
);
$config_file = new ConfigFile((string)getcwd(), $this->file_path);
$config_file->addPlugin('a\b\c');
$this->assertTrue(static::compareContentWithTemplateAndTrailingLineEnding(
'<?xml version="1.0" encoding="UTF-8"?>
<psalm
name="bar"
>
<plugins><pluginClass xmlns="' . ConfigFile::NS . '" class="a\b\c"/></plugins>
</psalm>',
file_get_contents($this->file_path)
));
}
/**
* @return void
* @test
*/
public function addCanCreateMissingPluginsNode()
{
file_put_contents(
$this->file_path,
'<?xml version="1.0"?>
<psalm></psalm>' . PHP_EOL
);
$config_file = new ConfigFile((string)getcwd(), $this->file_path);
$config_file->addPlugin('a\b\c');
$this->assertTrue(static::compareContentWithTemplateAndTrailingLineEnding(
'<?xml version="1.0"?>
<psalm><plugins><pluginClass xmlns="' . ConfigFile::NS . '" class="a\b\c"/></plugins></psalm>',
file_get_contents($this->file_path)
));
}
/**
* @return void
* @test
*/
public function removeDoesNothingWhenThereIsNoPluginsNode()
{
$noPlugins = '<?xml version="1.0"?>
<psalm></psalm>' . PHP_EOL;
file_put_contents($this->file_path, $noPlugins);
$config_file = new ConfigFile((string)getcwd(), $this->file_path);
$config_file->removePlugin('a\b\c');
$this->assertSame(
$noPlugins,
file_get_contents($this->file_path)
);
}
/**
* @return void
* @test
*/
public function removeKillsEmptyPluginsNode()
{
$noPlugins = '<?xml version="1.0" encoding="UTF-8"?>
<psalm/>' . PHP_EOL;
$emptyPlugins = trim('
<?xml version="1.0"?>
<psalm><plugins></plugins></psalm>
');
file_put_contents($this->file_path, $emptyPlugins);
$config_file = new ConfigFile((string)getcwd(), $this->file_path);
$config_file->removePlugin('a\b\c');
$this->assertXmlStringEqualsXmlString(
$noPlugins,
file_get_contents($this->file_path)
);
}
/**
* @return void
* @test
*/
public function removeKillsSpecifiedPlugin()
{
$noPlugins = trim('
<?xml version="1.0"?>
<psalm/>
');
$abcEnabled = trim('
<?xml version="1.0"?>
<psalm><plugins><pluginClass class="a\b\c"/></plugins></psalm>
');
file_put_contents($this->file_path, $abcEnabled);
$config_file = new ConfigFile((string)getcwd(), $this->file_path);
$config_file->removePlugin('a\b\c');
$this->assertXmlStringEqualsXmlString(
$noPlugins,
file_get_contents($this->file_path)
);
}
/**
* @return void
* @test
*/
public function removeKillsSpecifiedPluginWithOneRemaining()
{
$noPlugins = trim('
<?xml version="1.0"?>
<psalm
totallyTyped="false"
>
<plugins>
<pluginClass class="d\e\f"/>
</plugins>
</psalm>
');
$abcEnabled = trim('
<?xml version="1.0"?>
<psalm
totallyTyped="false"
>
<plugins>
<pluginClass class="a\b\c"/>
<pluginClass class="d\e\f"/>
</plugins>
</psalm>
');
file_put_contents($this->file_path, $abcEnabled);
$config_file = new ConfigFile((string)getcwd(), $this->file_path);
$config_file->removePlugin('a\b\c');
$this->assertXmlStringEqualsXmlString(
$noPlugins,
file_get_contents($this->file_path)
);
}
/**
* @param string $expected_template
* @param string $contents
*
* @return bool
*/
protected static function compareContentWithTemplateAndTrailingLineEnding($expected_template, $contents)
{
$passed = false;
foreach ([PHP_EOL, "\n", "\r", "\r\n"] as $eol) {
if (!$passed && $contents === ($expected_template . $eol)) {
$passed = true;
}
}
return $passed;
}
}