|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of sebastian/diff. |
| 4 | + * |
| 5 | + * (c) Sebastian Bergmann <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace SebastianBergmann\Diff; |
| 12 | + |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * @requires OS Linux |
| 17 | + */ |
| 18 | +final class DifferTestTest extends TestCase |
| 19 | +{ |
| 20 | + private $fileFrom; |
| 21 | + private $filePatch; |
| 22 | + private $fileTo; |
| 23 | + |
| 24 | + protected function setUp() |
| 25 | + { |
| 26 | + $dir = \realpath(__DIR__ . '/../') . '/'; |
| 27 | + $this->fileFrom = $dir . 'from.txt'; |
| 28 | + $this->filePatch = $dir . 'patch.txt'; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @dataProvider provideDiffWithLineNumbers |
| 33 | + */ |
| 34 | + public function testTheTestProvideDiffWithLineNumbers($expected, $from, $to) |
| 35 | + { |
| 36 | + $this->runThisTest($expected, $from, $to); |
| 37 | + } |
| 38 | + |
| 39 | + public function provideDiffWithLineNumbers() |
| 40 | + { |
| 41 | + require_once __DIR__ . '/DifferTest.php'; |
| 42 | + $test = new DifferTest(); |
| 43 | + $tests = $test->provideDiffWithLineNumbers(); |
| 44 | + |
| 45 | + $tests = \array_filter( |
| 46 | + $tests, |
| 47 | + function ($key) { |
| 48 | + return !\is_string($key) || false === \strpos($key, 'non_patch_compat'); |
| 49 | + }, |
| 50 | + ARRAY_FILTER_USE_KEY |
| 51 | + ); |
| 52 | + |
| 53 | + return $tests; |
| 54 | + } |
| 55 | + |
| 56 | + private function runThisTest(string $expected, string $from, string $to) |
| 57 | + { |
| 58 | + $expected = \str_replace('--- Original', '--- from.txt', $expected); |
| 59 | + $expected = \str_replace('+++ New', '+++ from.txt', $expected); |
| 60 | + |
| 61 | + @\unlink($this->fileFrom); |
| 62 | + @\unlink($this->filePatch); |
| 63 | + |
| 64 | + $this->assertNotFalse(\file_put_contents($this->fileFrom, $from)); |
| 65 | + $this->assertNotFalse(\file_put_contents($this->filePatch, $expected)); |
| 66 | + |
| 67 | + $command = \sprintf( |
| 68 | + 'patch -u --verbose %s < %s', // --posix |
| 69 | + \escapeshellarg($this->fileFrom), |
| 70 | + \escapeshellarg($this->filePatch) |
| 71 | + ); |
| 72 | + |
| 73 | + \exec($command, $output, $d); |
| 74 | + |
| 75 | + $this->assertSame(0, $d, \sprintf('%s | %s', $command, \implode("\n", $output))); |
| 76 | + |
| 77 | + $patched = \file_get_contents($this->fileFrom); |
| 78 | + $this->assertSame($patched, $to); |
| 79 | + |
| 80 | + @\unlink($this->fileFrom); |
| 81 | + @\unlink($this->filePatch); |
| 82 | + } |
| 83 | +} |
0 commit comments