UnifiedDiffAssertTraitIntegrationTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/diff.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Diff\Utils;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Process\Process;
  13. /**
  14. * @requires OS Linux
  15. *
  16. * @coversNothing
  17. */
  18. final class UnifiedDiffAssertTraitIntegrationTest extends TestCase
  19. {
  20. use UnifiedDiffAssertTrait;
  21. private $filePatch;
  22. protected function setUp(): void
  23. {
  24. $this->filePatch = __DIR__ . '/../fixtures/out/patch.txt';
  25. $this->cleanUpTempFiles();
  26. }
  27. protected function tearDown(): void
  28. {
  29. $this->cleanUpTempFiles();
  30. }
  31. /**
  32. * @param string $fileFrom
  33. * @param string $fileTo
  34. *
  35. * @dataProvider provideFilePairsCases
  36. */
  37. public function testValidPatches(string $fileFrom, string $fileTo): void
  38. {
  39. $command = \sprintf(
  40. 'diff -u %s %s > %s',
  41. \escapeshellarg(\realpath($fileFrom)),
  42. \escapeshellarg(\realpath($fileTo)),
  43. \escapeshellarg($this->filePatch)
  44. );
  45. $p = new Process($command);
  46. $p->run();
  47. $exitCode = $p->getExitCode();
  48. if (0 === $exitCode) {
  49. // odd case when two files have the same content. Test after executing as it is more efficient than to read the files and check the contents every time.
  50. $this->addToAssertionCount(1);
  51. return;
  52. }
  53. $this->assertSame(
  54. 1, // means `diff` found a diff between the files we gave it
  55. $exitCode,
  56. \sprintf(
  57. "Command exec. was not successful:\n\"%s\"\nOutput:\n\"%s\"\nStdErr:\n\"%s\"\nExit code %d.\n",
  58. $command,
  59. $p->getOutput(),
  60. $p->getErrorOutput(),
  61. $p->getExitCode()
  62. )
  63. );
  64. $this->assertValidUnifiedDiffFormat(FileUtils::getFileContent($this->filePatch));
  65. }
  66. /**
  67. * @return array<string, array<string, string>>
  68. */
  69. public function provideFilePairsCases(): array
  70. {
  71. $cases = [];
  72. // created cases based on dedicated fixtures
  73. $dir = \realpath(__DIR__ . '/../fixtures/UnifiedDiffAssertTraitIntegrationTest');
  74. $dirLength = \strlen($dir);
  75. for ($i = 1;; ++$i) {
  76. $fromFile = \sprintf('%s/%d_a.txt', $dir, $i);
  77. $toFile = \sprintf('%s/%d_b.txt', $dir, $i);
  78. if (!\file_exists($fromFile)) {
  79. break;
  80. }
  81. $this->assertFileExists($toFile);
  82. $cases[\sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", \substr(\realpath($fromFile), $dirLength), \substr(\realpath($toFile), $dirLength))] = [$fromFile, $toFile];
  83. }
  84. // create cases based on PHP files within the vendor directory for integration testing
  85. $dir = \realpath(__DIR__ . '/../../vendor');
  86. $dirLength = \strlen($dir);
  87. $fileIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS));
  88. $fromFile = __FILE__;
  89. /** @var \SplFileInfo $file */
  90. foreach ($fileIterator as $file) {
  91. if ('php' !== $file->getExtension()) {
  92. continue;
  93. }
  94. $toFile = $file->getPathname();
  95. $cases[\sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", \substr(\realpath($fromFile), $dirLength), \substr(\realpath($toFile), $dirLength))] = [$fromFile, $toFile];
  96. $fromFile = $toFile;
  97. }
  98. return $cases;
  99. }
  100. private function cleanUpTempFiles(): void
  101. {
  102. @\unlink($this->filePatch);
  103. }
  104. }