Skip to content

Commit

Permalink
Feat/Add getStatistics method to Diff class (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
mansoorkhan96 committed Mar 8, 2024
1 parent e778ab3 commit 36157a2
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Overtrue\LaravelVersionable;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Jfcherng\Diff\Differ;
use Jfcherng\Diff\DiffHelper;

/**
Expand Down Expand Up @@ -92,4 +94,34 @@ public function render(?string $renderer = null, array $differOptions = [], arra

return $diff;
}

public function getStatistics(array $differOptions = []): array
{
if (empty($differOptions)) {
$differOptions = $this->differOptions;
}

$oldContents = $this->fromVersion->contents;
$newContents = $this->toVersion->contents;

$diffStats = new Collection;

foreach ($oldContents as $key => $value) {
if ($newContents[$key] !== $oldContents[$key]) {
$diffStats->push(
(new Differ(
explode("\n", $newContents[$key]),
explode("\n", $oldContents[$key]),
))->getStatistics()
);
}
}

return [
'inserted' => $diffStats->sum('inserted'),
'deleted' => $diffStats->sum('deleted'),
'unmodified' => $diffStats->sum('unmodified'),
'changedRatio' => $diffStats->sum('changedRatio'),
];
}
}
73 changes: 73 additions & 0 deletions tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,79 @@ public function test_diff_to_side_by_side_html()
);
}

/**
* @test
*/
public function it_can_return_the_diff_statistics()
{
$old = new Version(['contents' => ['title' => 'example title', 'content' => 'example content']]);

// we are modifying everything
$new = new Version(['contents' => ['title' => 'changing the title', 'content' => 'changing the content']]);

$stats = (new Diff($new, $old))->getStatistics();

$this->assertIsArray($stats);
$this->assertArrayHasKey('inserted', $stats);
$this->assertArrayHasKey('deleted', $stats);
$this->assertArrayHasKey('unmodified', $stats);

$this->assertEquals(2, $stats['inserted']); // two new lines inserted
$this->assertEquals(2, $stats['deleted']); // two lines deleted
$this->assertEquals(0, $stats['unmodified']); // modified everything
}

/**
* @test
*/
public function it_can_return_correct_statistics_for_new_line_insertions()
{
$old = new Version(['contents' => ['title' => 'example title', 'content' => 'example content']]);

// we are adding two new lines
$new = new Version(['contents' => ['content' => "example content\n adding new line \n another new line"]]);

$stats = (new Diff($new, $old))->getStatistics();

$this->assertEquals(2, $stats['inserted']); // two new lines inserted
$this->assertEquals(0, $stats['deleted']); // no deletions
$this->assertEquals(1, $stats['unmodified']); // one line unmodified
}

/**
* @test
*/
public function it_can_return_correct_statistics_for_deleted_lines()
{
$old = new Version(['contents' => ['title' => 'example title', 'content' => "example content\n adding new line \n another new line"]]);

// we are removing last two lines
$new = new Version(['contents' => ['content' => 'example content']]);

$stats = (new Diff($new, $old))->getStatistics();

$this->assertEquals(0, $stats['inserted']); // no insertions
$this->assertEquals(2, $stats['deleted']); // two lines deleted
$this->assertEquals(1, $stats['unmodified']); // one line unmodified
}

/**
* @test
*/
public function it_can_return_correct_statistics_for_unmodified_lines()
{
$old = new Version(['contents' => ['title' => 'example title', 'content' => "example content\n adding new line \n another new line"]]);

// we are just removing the last line
$new = new Version(['contents' => ['content' => "example content\n adding new line "]]);

$stats = (new Diff($new, $old))->getStatistics();

$this->assertEquals(0, $stats['inserted']); // no insertions
$this->assertEquals(1, $stats['deleted']); // one line deleted
$this->assertEquals(2, $stats['unmodified']); // two lines unmodified
}

public function test_diff_nested_array_to_array()
{
$oldContent = [
Expand Down

0 comments on commit 36157a2

Please sign in to comment.