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), ); } public function testEnableExtensions(): void { file_put_contents($this->file_path, trim(' ')); $config_file = new ConfigFile((string)getcwd(), $this->file_path); $config = $config_file->getConfig(); $this->assertTrue($config->php_extensions["mysqli"]); $this->assertTrue($config->php_extensions["pdo"]); } public function testDisableExtensions(): void { file_put_contents($this->file_path, trim(' ')); $config_file = new ConfigFile((string)getcwd(), $this->file_path); $config = $config_file->getConfig(); $this->assertFalse($config->php_extensions["mysqli"]); $this->assertFalse($config->php_extensions["pdo"]); } public function testInvalidExtension(): void { $this->expectException(ConfigException::class); file_put_contents($this->file_path, trim(' ')); (new ConfigFile((string)getcwd(), $this->file_path))->getConfig(); } /** * @psalm-pure */ protected static function compareContentWithTemplateAndTrailingLineEnding(string $expected_template, string $contents): bool { $passed = false; foreach ([PHP_EOL, "\n", "\r", "\r\n"] as $eol) { if (!$passed && $contents === ($expected_template . $eol)) { $passed = true; } } return $passed; } }