ListFilesCommandTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Console\Command;
  13. use PhpCsFixer\Console\Application;
  14. use PhpCsFixer\Console\Command\ListFilesCommand;
  15. use PhpCsFixer\Tests\TestCase;
  16. use PhpCsFixer\ToolInfo;
  17. use Symfony\Component\Console\Tester\CommandTester;
  18. use Symfony\Component\Filesystem\Filesystem;
  19. use Symfony\Component\Filesystem\Path;
  20. /**
  21. * @internal
  22. *
  23. * @covers \PhpCsFixer\Console\Command\ListFilesCommand
  24. */
  25. final class ListFilesCommandTest extends TestCase
  26. {
  27. private static ?Filesystem $filesystem;
  28. public static function setUpBeforeClass(): void
  29. {
  30. self::$filesystem = new Filesystem();
  31. }
  32. public static function tearDownAfterClass(): void
  33. {
  34. self::$filesystem = null;
  35. }
  36. public function testListWithConfig(): void
  37. {
  38. $commandTester = $this->doTestExecute([
  39. '--config' => __DIR__.'/../../Fixtures/ListFilesTest/.php-cs-fixer.php',
  40. ]);
  41. $expectedPath = './tests/Fixtures/ListFilesTest/needs-fixing/needs-fixing.php';
  42. // make the test also work on Windows
  43. $expectedPath = str_replace('/', \DIRECTORY_SEPARATOR, $expectedPath);
  44. self::assertSame(0, $commandTester->getStatusCode());
  45. self::assertSame(escapeshellarg($expectedPath).PHP_EOL, $commandTester->getDisplay());
  46. }
  47. /**
  48. * @requires OS Linux|Darwin
  49. *
  50. * Skip test on Windows as `getcwd()` includes the drive letter with a colon `:` which is illegal in filenames.
  51. */
  52. public function testListFilesDoesNotCorruptListWithGetcwdInName(): void
  53. {
  54. try {
  55. $tmpDir = __DIR__.'/../../Fixtures/ListFilesTest/using-getcwd';
  56. $tmpFile = $tmpDir.'/'.ltrim(getcwd(), '/').'-out.php';
  57. self::$filesystem->dumpFile($tmpFile, '<?php function a() { }');
  58. $tmpFile = realpath($tmpFile);
  59. self::assertFileExists($tmpFile);
  60. $commandTester = $this->doTestExecute([
  61. '--config' => __DIR__.'/../../Fixtures/ListFilesTest/.php-cs-fixer.using-getcwd.php',
  62. ]);
  63. $expectedPath = str_replace('/', \DIRECTORY_SEPARATOR, './'.Path::makeRelative($tmpFile, getcwd()));
  64. self::assertSame(0, $commandTester->getStatusCode());
  65. self::assertSame(escapeshellarg($expectedPath).PHP_EOL, $commandTester->getDisplay());
  66. } finally {
  67. self::$filesystem->remove($tmpDir);
  68. }
  69. }
  70. /**
  71. * @param array<string, bool|string> $arguments
  72. */
  73. private function doTestExecute(array $arguments): CommandTester
  74. {
  75. $application = new Application();
  76. $application->add(new ListFilesCommand(new ToolInfo()));
  77. $command = $application->find('list-files');
  78. $commandTester = new CommandTester($command);
  79. $commandTester->execute($arguments);
  80. return $commandTester;
  81. }
  82. }