diff --git a/tests/IntegrationTest/PreexistingPHPCSConfigTest.php b/tests/IntegrationTest/PreexistingPHPCSConfigTest.php new file mode 100644 index 00000000..288abc3f --- /dev/null +++ b/tests/IntegrationTest/PreexistingPHPCSConfigTest.php @@ -0,0 +1,182 @@ + 'phpcs-composer-installer/preexisting-config-test', + 'require-dev' => array( + 'squizlabs/php_codesniffer' => null, + 'dealerdirect/phpcodesniffer-composer-installer' => '*', + ), + ); + + /** + * Set up test environment before each test. + */ + protected function set_up() + { + $this->createTestEnvironment(); + } + + /** + * Clean up after each test. + */ + protected function tear_down() + { + $this->removeTestEnvironment(); + } + + /** + * Test correctly handling a pre-existing PHPCS configuration file (which doesn't involve + * a pre-existing `installed_paths` setting). + * + * @dataProvider dataPHPCSVersions + * + * @param string $phpcsVersion PHPCS version to use in this test. + * This version is randomly selected from the PHPCS versions compatible + * with the PHP version used in the test. + * + * @return void + */ + public function testPreexistingNonInstalledPathsConfigIsKeptIntact($phpcsVersion) + { + $config = $this->composerConfig; + $config['require-dev']['squizlabs/php_codesniffer'] = $phpcsVersion; + + $this->writeComposerJsonFile($config, static::$tempLocalPath); + + /* + * 1. Install PHPCS and the plugin. + */ + $this->assertExecute( + sprintf('composer install -v --working-dir=%s', escapeshellarg(static::$tempLocalPath)), + 0, // Expected exit code. + null, // No stdout expectation. + null, // No stderr expectation. + 'Failed to install PHPCS.' + ); + + // Verify the CodeSniffer.conf file does not exist to start with. + $this->assertFileDoesNotExist( + static::$tempLocalPath . '/vendor/squizlabs/php_codesniffer/CodeSniffer.conf' + ); + + // Verify that the config is empty to start with. + $result = $this->executeCliCommand('"vendor/bin/phpcs" --config-show', static::$tempLocalPath); + $this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs --config-show" did not match 0 (first run)'); + $this->assertMatchesRegularExpression( + '`^\s*(Using config file:)?\s*$`', + $result['stdout'], + 'PHPCS configuration is not empty to start with.' + ); + + /* + * 2. Set some configs and verify they are registered correctly. + * + * Note: PHPCS will "show" the config settings in alphabetic order. + * These settings have been chosen to ensure there will be settings to show + * both before *and* after the `installed_paths` setting. + */ + $command = '"vendor/bin/phpcs" --config-set %s %s'; + $failureMsg = 'Exitcode for "phpcs --config-set %s %s" did not match 0'; + $settings = array( + 'default_standard' => 'PSR12', + 'colors' => '1', + 'show_progress' => '1', + ); + + foreach ($settings as $name => $value) { + $result = $this->executeCliCommand(sprintf($command, $name, $value), static::$tempLocalPath); + $this->assertSame(0, $result['exitcode'], sprintf($failureMsg, $name, $value)); + } + + // Make sure the CodeSniffer.conf file has been created. + $this->assertFileExists( + static::$tempLocalPath . '/vendor/squizlabs/php_codesniffer/CodeSniffer.conf' + ); + + // Verify that the config contains the newly set values. + $result = $this->executeCliCommand('"vendor/bin/phpcs" --config-show', static::$tempLocalPath); + $this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs --config-show" did not match 0 (second run)'); + + $regex = '`colors:\s+1\s+' + . 'default_standard:\s+PSR12\s+' + . 'show_progress:\s+1\s*$`'; + $this->assertMatchesRegularExpression( + $regex, + $result['stdout'], + 'PHPCS configuration does not show the newly set values correctly.' + ); + + /* + * 3. Install an external standard. + */ + $command = sprintf( + 'composer require --dev phpcs-composer-installer/dummy-subdir --no-ansi -v --working-dir=%s', + escapeshellarg(static::$tempLocalPath) + ); + $this->assertExecute( + $command, + 0, // Expected exit code. + 'PHP CodeSniffer Config installed_paths set to ', // Expectation for stdout. + null, // No stderr expectation. + 'Failed to install Dummy subdir standard.' + ); + + // Verify that the originally set configs are retained and the standard is registered correctly. + $result = $this->executeCliCommand('"vendor/bin/phpcs" --config-show', static::$tempLocalPath); + $this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs --config-show" did not match 0 (third run)'); + + $regex = '`colors:\s+1\s+' + . 'default_standard:\s+PSR12\s+' + . 'installed_paths:\s+[^\n\r]+/phpcs-composer-installer/dummy-subdir\s+' + . 'show_progress:\s+1\s*$`'; + $this->assertMatchesRegularExpression( + $regex, + $result['stdout'], + 'PHPCS configuration has not been retained and/or updated correctly.' + ); + } + + /** + * Data provider. + * + * @return array + */ + public function dataPHPCSVersions() + { + // Test against the highest and lowest supported PHPCS version for each major + `master` + PHPCS 4.x dev. + $versions = PHPCSVersions::getHighLowEachMajor(true, true); + return PHPCSVersions::toDataprovider($versions); + } +}