* @copyright 2024 Juliette Reinders Folmer. All rights reserved.
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Fixer;
use PHP_CodeSniffer\Files\LocalFile;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;
/**
* Tests for diff generation.
*
* Note: these tests are specifically about the Fixer::generateDiff() method and do not
* test running the fixer itself, nor generating a diff based on a fixer run.
*
* @covers PHP_CodeSniffer\Fixer::generateDiff
* @group Windows
*/
final class GenerateDiffTest extends TestCase
{
/**
* A \PHP_CodeSniffer\Files\File object to compare the files against.
*
* @var \PHP_CodeSniffer\Files\LocalFile
*/
private static $phpcsFile;
/**
* Initialize an \PHP_CodeSniffer\Files\File object with code.
*
* Things to take note of in the code snippet used for these tests:
* - Line endings are \n.
* - Tab indent.
* - Trailing whitespace.
*
* Also note that the Config object is deliberately created without a `tabWidth` setting to
* prevent doing tab replacement when parsing the file. This is to allow for testing a
* diff with tabs vs spaces (which wouldn't yield a diff if tabs had already been replaced).
*
* @beforeClass
*
* @return void
*/
public static function initializeFile()
{
$config = new ConfigDouble();
$ruleset = new Ruleset($config);
self::$phpcsFile = new LocalFile(__DIR__.'/Fixtures/GenerateDiffTest.inc', $ruleset, $config);
self::$phpcsFile->parse();
self::$phpcsFile->fixer->startFile(self::$phpcsFile);
}//end initializeFile()
/**
* Test generating a diff on the file object itself.
*
* @return void
*/
public function testGenerateDiffNoFile()
{
$diff = self::$phpcsFile->fixer->generateDiff(null, false);
$this->assertSame('', $diff);
}//end testGenerateDiffNoFile()
/**
* Test generating a diff between a PHPCS File object and a file on disk.
*
* @param string $filePath The path to the file to compare the File object against.
*
* @dataProvider dataGenerateDiff
*
* @return void
*/
public function testGenerateDiff($filePath)
{
$diff = self::$phpcsFile->fixer->generateDiff($filePath, false);
// Allow for the tests to pass on Windows too.
$diff = str_replace('--- tests\Core\Fixer/', '--- tests/Core/Fixer/', $diff);
$expectedDiffFile = str_replace('.inc', '.diff', $filePath);
$this->assertStringEqualsFile($expectedDiffFile, $diff);
}//end testGenerateDiff()
/**
* Data provider.
*
* @see testGenerateDiff()
*
* @return array