Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Add getStatistics method to Diff class #55

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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