DifferTestTest.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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;
  11. use PHPUnit\Framework\TestCase;
  12. /**
  13. * @requires OS Linux
  14. */
  15. final class DifferTestTest extends TestCase
  16. {
  17. private $fileFrom;
  18. private $filePatch;
  19. protected function setUp()
  20. {
  21. $dir = \realpath(__DIR__ . '/../') . '/';
  22. $this->fileFrom = $dir . 'from.txt';
  23. $this->filePatch = $dir . 'patch.txt';
  24. }
  25. /**
  26. * @dataProvider provideDiffWithLineNumbers
  27. */
  28. public function testTheTestProvideDiffWithLineNumbers($expected, $from, $to)
  29. {
  30. $this->runThisTest($expected, $from, $to);
  31. }
  32. public function provideDiffWithLineNumbers()
  33. {
  34. require_once __DIR__ . '/DifferTest.php';
  35. $test = new DifferTest();
  36. $tests = $test->provideDiffWithLineNumbers();
  37. $tests = \array_filter(
  38. $tests,
  39. function ($key) {
  40. return !\is_string($key) || false === \strpos($key, 'non_patch_compat');
  41. },
  42. ARRAY_FILTER_USE_KEY
  43. );
  44. return $tests;
  45. }
  46. private function runThisTest(string $expected, string $from, string $to)
  47. {
  48. $expected = \str_replace('--- Original', '--- from.txt', $expected);
  49. $expected = \str_replace('+++ New', '+++ from.txt', $expected);
  50. @\unlink($this->fileFrom);
  51. @\unlink($this->filePatch);
  52. $this->assertNotFalse(\file_put_contents($this->fileFrom, $from));
  53. $this->assertNotFalse(\file_put_contents($this->filePatch, $expected));
  54. $command = \sprintf(
  55. 'patch -u --verbose %s < %s', // --posix
  56. \escapeshellarg($this->fileFrom),
  57. \escapeshellarg($this->filePatch)
  58. );
  59. \exec($command, $output, $d);
  60. $this->assertSame(0, $d, \sprintf('%s | %s', $command, \implode("\n", $output)));
  61. $patched = \file_get_contents($this->fileFrom);
  62. $this->assertSame($patched, $to);
  63. @\unlink($this->fileFrom . '.orig');
  64. @\unlink($this->fileFrom);
  65. @\unlink($this->filePatch);
  66. }
  67. }