Skip to content

Commit

Permalink
Remove the visiblity attribute and the Revision model. This can be an…
Browse files Browse the repository at this point in the history
… app concern.
  • Loading branch information
Kurt Friars committed Jul 12, 2024
1 parent d72a079 commit d62c1e1
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 81 deletions.
11 changes: 8 additions & 3 deletions config/snapshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
|--------------------------------------------------------------------------
|
| Versions:
| This is the repository which will be used retrieve and maintain the version state
| for the Application.
| This repository will be used to retrieve and maintain the version state for the application.
|
| The interface is minimal to allow you to manage Versions in other ways if your application
| requires it.
Expand Down Expand Up @@ -87,10 +86,16 @@
| As the package assumes "no active version" as the default approach to working on content,
| when a new version is created we make sure all of the History events that occured on the
| working version also get migrated to the newly created version.
|
| Identity:
| This is the Model Observer which will be used to track the Identity of the content. It also
| acts as a flag to fully enable or disable the identity management feature. Set to `null` to
| disable all hash tracking.
|
*/
'history' => [
'observer' => \Plank\Snapshots\Observers\HistoryObserver::class,
'labeler' => \Plank\Snapshots\Listeners\LabelHistory::class,
'identity' => \Plank\Snapshots\Observers\IdentityObserver::class,
'labler' => \Plank\Snapshots\Listeners\LabelHistory::class,
],
];
11 changes: 0 additions & 11 deletions src/Concerns/AsVersionedContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
use Plank\LaravelHush\Concerns\HushesHandlers;
use Plank\Snapshots\Contracts\Version as VersionContract;
use Plank\Snapshots\Facades\Versions;
use Plank\Snapshots\ValueObjects\Revision;

/**
* @mixin Model
*
* @property-read Collection<Revision> $visibility
*/
trait AsVersionedContent
{
Expand Down Expand Up @@ -46,12 +43,4 @@ public function getTable()

return $table;
}

public function visibility(): Attribute
{
return Attribute::make(
get: function () {
return Revision::collection($this->visibleHistory());
})->shouldCache();
}
}
2 changes: 1 addition & 1 deletion src/Concerns/HasHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @mixin \Illuminate\Database\Eloquent\Model
*
* @property-read Collection<History> $history
* @property-read Collection<History> $visibileHistory
* @property-read Collection<History> $visibleHistory
* @property-read bool $hidden
*/
trait HasHistory
Expand Down
14 changes: 11 additions & 3 deletions src/Contracts/Versioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

namespace Plank\Snapshots\Contracts;

use Illuminate\Database\Eloquent\Casts\Attribute;

interface Versioned
{
public function visibility(): Attribute;
/**
* Retrieve the active version of the model.
*/
public function activeVersion(): ?static;

/**
* Get the table associated with the model.
*
* @return string
*/
public function getTable();
}
1 change: 1 addition & 0 deletions src/Models/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Plank\Snapshots\Contracts\Trackable;
use Plank\Snapshots\Enums\Operation;
use Plank\Snapshots\Facades\Versions;

/**
* @property Operation $operation
Expand Down
5 changes: 5 additions & 0 deletions src/Repository/TableRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public function dequeue(): ?TableCreated
return array_shift($this->tables);
}

public function all(): array
{
return $this->tables;
}

public function flush(): int
{
$count = count($this->tables);
Expand Down
4 changes: 2 additions & 2 deletions src/SnapshotServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ protected function listenToEvents(): self
Event::listen(MigrationsEnded::class, $copier);
}

if ($labler = config()->get('snapshots.history.labler')) {
Event::listen(TableCopied::class, $labler);
if ($labeler = config()->get('snapshots.history.labeler')) {
Event::listen(TableCopied::class, $labeler);
}

return $this;
Expand Down
42 changes: 0 additions & 42 deletions src/ValueObjects/Revision.php

This file was deleted.

10 changes: 7 additions & 3 deletions tests/Suites/Feature/History/HistoryTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Plank\Snapshots\Enums\Operation;
use Plank\Snapshots\Events\TableCopied;
use Plank\Snapshots\Exceptions\CauserException;
Expand All @@ -17,6 +18,9 @@

beforeEach(function () {
config()->set('snapshots.history.observer', HistoryObserver::class);
config()->set('snapshots.history.labeler', LabelHistory::class);

Event::listen(TableCopied::class, LabelHistory::class);
});

describe('Versioned Content has its History tracked correctly without Model Events', function () {
Expand All @@ -27,7 +31,7 @@
])->run();
});

it('tracks Create Operations correctly', function () {
it('tracks Create Operations correctly without Model Events', function () {
Document::factory()->create();

expect(History::query()->count())->toBe(1);
Expand Down Expand Up @@ -59,8 +63,8 @@
Document::factory()->create();
versions()->setActive(createMinorVersion('schema/create_for_model'));

// Now we have 2 created history items, and 2 snapshotted history items for the first
// document, and 1 snapshotted history item for the second document.
// Now we have 1 created history items and 2 snapshotted history items for the first
// document, and 1 created history item and 1 snapshotted history item for the second document.
expect(History::query()->where('operation', Operation::Created)->count())->toBe(2);
expect(History::query()->where('operation', Operation::Snapshotted)->count())->toBe(3);
});
Expand Down
31 changes: 15 additions & 16 deletions tests/Suites/Feature/History/VisibilityTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?php

use Illuminate\Support\Facades\Event;
use Plank\Snapshots\Enums\Operation;
use Plank\Snapshots\Events\TableCopied;
use Plank\Snapshots\Listeners\LabelHistory;
use Plank\Snapshots\Models\History;
use Plank\Snapshots\Observers\HistoryObserver;
use Plank\Snapshots\Tests\Models\Flag;
use Plank\Snapshots\ValueObjects\Revision;

use function Pest\Laravel\artisan;

beforeEach(function () {
config()->set('snapshots.history.observer', HistoryObserver::class);
config()->set('snapshots.history.labler', LabelHistory::class);
config()->set('snapshots.history.labeler', LabelHistory::class);

Event::listen(TableCopied::class, LabelHistory::class);
});
Expand Down Expand Up @@ -83,26 +84,24 @@

it('shows the correct visibility for each version', function () {
versions()->setActive(version('1.0.0'));
$history = Flag::query()->first()->visibleHistory();

/** @var Flag $flag */
$flag = Flag::query()->first();

$revision = function (string $number) use ($flag): ?Revision {
/** @var Revision $found */
$found = $flag->visibility->filter(function (Revision $revision) use ($number) {
return $revision->version->number->isEqualTo($number);
$revision = function (string $number) use ($history): ?History {
/** @var History $found */
$found = $history->filter(function (History $item) use ($number) {
return $item->version->number->isEqualTo($number);
})->first();

return $found;
};

expect($revision('1.0.0')->hidden)->toBeFalse();
expect($revision('1.0.1')->hidden)->toBeFalse();
expect($revision('1.1.0')->hidden)->toBeFalse();
expect($revision('2.0.0')->hidden)->toBeTrue();
expect($revision('2.1.0')->hidden)->toBeTrue();
expect($revision('2.2.0')->hidden)->toBeFalse();
expect($revision('2.2.1')->hidden)->toBeFalse();
expect($revision('1.0.0')->operation)->toBe(Operation::Created);
expect($revision('1.0.1')->operation)->toBe(Operation::Updated);
expect($revision('1.1.0')->operation)->toBe(Operation::Snapshotted);
expect($revision('2.0.0')->operation)->toBe(Operation::SoftDeleted);
expect($revision('2.1.0')->operation)->toBe(Operation::Snapshotted);
expect($revision('2.2.0')->operation)->toBe(Operation::Restored);
expect($revision('2.2.1')->operation)->toBe(Operation::Updated);
expect($revision('3.0.0'))->toBeNull();
expect($revision('4.0.0'))->toBeNull();
});
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ public function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testing');
$app['config']->set('snapshots.history.observer', null);
$app['config']->set('snapshots.history.labeler', null);
}
}

0 comments on commit d62c1e1

Please sign in to comment.