Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c9fe145

Browse files
committedSep 9, 2023
command to compare two file
Signed-off-by: rahul <rcsofttech85@gmail.com> Signed-off-by: rahul <rcsofttech85@gmail.com>
1 parent ebfcaf7 commit c9fe145

File tree

5 files changed

+881
-7
lines changed

5 files changed

+881
-7
lines changed
 

‎bin/file-diff

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Console\SingleCommandApplication;
12+
use Symfony\Component\Console\Style\SymfonyStyle;
13+
14+
15+
$command = (new SingleCommandApplication(name: 'file-diff'))
16+
->addArgument('oldFile', InputArgument::REQUIRED, 'old file name')
17+
->addArgument('newFile', InputArgument::REQUIRED, 'new file name')
18+
->setCode(function (InputInterface $input, OutputInterface $output): int {
19+
$io = new SymfonyStyle($input, $output);
20+
$oldFile = $input->getArgument('oldFile');
21+
$newFile = $input->getArgument("newFile");
22+
23+
if (!file_exists($oldFile) || !file_exists($newFile)) {
24+
$io->error("file does not exists");
25+
return Command::FAILURE;
26+
}
27+
28+
fileDiff($oldFile, $newFile);
29+
return Command::SUCCESS;
30+
})->run();
31+
32+
33+
function fileDiff($oldFilePath, $newFilePath)
34+
{
35+
$oldLines = file($oldFilePath, FILE_IGNORE_NEW_LINES);
36+
$newLines = file($newFilePath, FILE_IGNORE_NEW_LINES);
37+
38+
39+
$oldLineCount = count($oldLines);
40+
$newLineCount = count($newLines);
41+
42+
43+
$maxLineCount = max($oldLineCount, $newLineCount);
44+
45+
$changes = [];
46+
for ($i = 0; $i < $maxLineCount; $i++) {
47+
$oldLine = $i < $oldLineCount ? $oldLines[$i] : null;
48+
$newLine = $i < $newLineCount ? $newLines[$i] : null;
49+
50+
if ($oldLine === $newLine) {
51+
continue;
52+
}
53+
54+
$colorGreen = "\e[32m";
55+
$colorRed = "\e[31m";
56+
$colorReset = "\e[0m";
57+
58+
59+
$oldLineNumber = $i + 1;
60+
$newLineNumber = $i + 1;
61+
62+
63+
$changes[] = ($oldLine === null ? "$colorGreen+ New (Line $newLineNumber): " : "$colorRed- Old (Line $oldLineNumber): ") . ($oldLine ?? $newLine) . "$colorReset";
64+
65+
if ($oldLine !== null && $newLine !== null) {
66+
$changes[] = "$colorGreen+ New (Line $newLineNumber): " . $newLine . "$colorReset";
67+
}
68+
}
69+
70+
71+
$console = fopen("php://stdout", "w");
72+
foreach ($changes as $change) {
73+
fwrite($console, $change . PHP_EOL);
74+
}
75+
76+
fclose($console);
77+
}

‎composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
"ext-sodium": "*",
2121
"ext-ctype": "*",
2222
"ext-zip": "*",
23-
"ext-fileinfo": "*"
23+
"ext-fileinfo": "*",
24+
"symfony/console": "^6.3"
2425
},
2526
"require-dev": {
2627
"phpunit/phpunit": "^10",
2728
"squizlabs/php_codesniffer": "^3.7",
2829
"symfony/dotenv": "^6.3"
29-
}
30+
},
31+
"bin": [
32+
"bin/file-diff"
33+
]
3034
}

‎composer.lock

Lines changed: 714 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Integration;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class FileDiffCommandTest extends TestCase
10+
{
11+
public static function tearDownAfterClass(): void
12+
{
13+
parent::tearDownAfterClass();
14+
15+
unlink('new');
16+
unlink('old');
17+
}
18+
19+
public static function commandArgumentProvider(): iterable
20+
{
21+
file_put_contents("old", "this is old file" . PHP_EOL, FILE_APPEND);
22+
file_put_contents("new", "this is new file" . PHP_EOL, FILE_APPEND);
23+
24+
file_put_contents("old", "this line has some difference" . PHP_EOL, FILE_APPEND);
25+
file_put_contents("new", "this line has same old code" . PHP_EOL, FILE_APPEND);
26+
27+
28+
yield ['old', 'new', 'Old (Line 1):'];
29+
yield ['old', 'new', 'Old (Line 2):'];
30+
}
31+
32+
public static function matchingDataProvider(): iterable
33+
{
34+
file_put_contents("old", "this has matching content" . PHP_EOL, FILE_APPEND);
35+
file_put_contents("new", "this has matching content" . PHP_EOL, FILE_APPEND);
36+
37+
38+
yield ['old', 'new', 'Old (Line 3):'];
39+
}
40+
41+
#[Test]
42+
#[DataProvider('commandArgumentProvider')]
43+
public function fileDiffShowsCorrectChanges(string $oldFile, string $newFile, string $expected)
44+
{
45+
$command = "php bin/file-diff $oldFile $newFile";
46+
exec($command, $output, $exitCode);
47+
48+
$actualOutput = implode("\n", $output);
49+
50+
$this->assertStringContainsString($expected, $actualOutput);
51+
52+
53+
$this->assertEquals(0, $exitCode);
54+
}
55+
56+
#[Test]
57+
public function throwsExceptionIfArgumentIsNotValidFile()
58+
{
59+
$command = "php bin/file-diff unknown unknown";
60+
exec($command, $output, $exitCode);
61+
62+
$actualOutput = implode("\n", $output);
63+
$expectedOutput = "file does not exists";
64+
65+
66+
$this->assertSame(1, $exitCode);
67+
$this->assertStringContainsString($expectedOutput, $actualOutput);
68+
}
69+
70+
#[Test]
71+
#[DataProvider('matchingDataProvider')]
72+
public function sameContentShouldNotBeDisplayedInTheResult(string $oldFile, string $newFile, string $expected)
73+
{
74+
$command = "php bin/file-diff $oldFile $newFile";
75+
exec($command, $output, $exitCode);
76+
77+
$actualOutput = implode("\n", $output);
78+
79+
$expected = "Old (Line 3)";
80+
81+
$this->assertStringNotContainsString($expected, $actualOutput);
82+
$this->assertEquals(0, $exitCode);
83+
}
84+
}

‎tests/Integration/StreamTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,4 @@ public static function wrongStreamDataProvider(): iterable
5959
{
6060
yield ["output.html", "https://gist.github"];
6161
}
62-
63-
6462
}

0 commit comments

Comments
 (0)
Please sign in to comment.