file_path = tempnam(sys_get_temp_dir(), 'psalm-test-config');
}
public function tearDown() : void
{
@unlink($this->file_path);
}
/**
* @test
*/
public function canCreateConfigObject(): void
{
file_put_contents($this->file_path, trim('
'));
$config_file = new ConfigFile((string)getcwd(), $this->file_path);
$this->assertInstanceOf(Config::class, $config_file->getConfig());
}
/**
* @test
*/
public function addCanAddPluginClassToExistingPluginsNode(): void
{
file_put_contents(
$this->file_path,
'
' . PHP_EOL
);
$config_file = new ConfigFile((string)getcwd(), $this->file_path);
$config_file->addPlugin('a\b\c');
$this->assertTrue(static::compareContentWithTemplateAndTrailingLineEnding(
'
',
file_get_contents($this->file_path)
));
}
/**
* @test
*/
public function addCanCreateMissingPluginsNode(): void
{
file_put_contents(
$this->file_path,
'
' . PHP_EOL
);
$config_file = new ConfigFile((string)getcwd(), $this->file_path);
$config_file->addPlugin('a\b\c');
$this->assertTrue(static::compareContentWithTemplateAndTrailingLineEnding(
'
',
file_get_contents($this->file_path)
));
}
/**
* @test
*/
public function removeDoesNothingWhenThereIsNoPluginsNode(): void
{
$noPlugins = '
' . 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)
);
}
/**
* @test
*/
public function removeKillsEmptyPluginsNode(): void
{
$noPlugins = '
' . PHP_EOL;
$emptyPlugins = trim('
');
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)
);
}
/**
* @test
*/
public function removeKillsSpecifiedPlugin(): void
{
$noPlugins = trim('
');
$abcEnabled = trim('
');
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)
);
}
/**
* @test
*/
public function removeKillsSpecifiedPluginWithOneRemaining(): void
{
$noPlugins = trim('
');
$abcEnabled = trim('
');
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
*
*
* @psalm-pure
*/
protected static function compareContentWithTemplateAndTrailingLineEnding($expected_template, $contents): bool
{
$passed = false;
foreach ([PHP_EOL, "\n", "\r", "\r\n"] as $eol) {
if (!$passed && $contents === ($expected_template . $eol)) {
$passed = true;
}
}
return $passed;
}
}