generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb28ea5
commit b36ff2d
Showing
8 changed files
with
264 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Indra\Revisor\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Relations\MorphTo; | ||
use Indra\Revisor\Facades\Revisor; | ||
|
||
trait HasRevisor | ||
{ | ||
protected string $baseTable; | ||
|
||
protected string $versionTable; | ||
|
||
public function initializeHasRevisor(): void | ||
{ | ||
$this->setBaseTable($this->getTable()); | ||
$this->setVersionTable(Revisor::getVersionTableFor($this->getTable())); | ||
$this->setPublishedTable(Revisor::getPublishedTableFor($this->getTable())); | ||
|
||
$this->mergeCasts([ | ||
'published_at' => 'datetime', | ||
'is_current' => 'boolean', | ||
'is_published' => 'boolean', | ||
]); | ||
} | ||
|
||
public function publisher(): MorphTo | ||
{ | ||
return $this->morphTo('publisher'); | ||
} | ||
|
||
public function publish(): static | ||
{ | ||
// find or make the published record | ||
$published = static::withPublishedScope()->findOrNew($this->{$this->getKey()}); | ||
|
||
// set the published attributes | ||
$this->published_at = now(); | ||
$this->is_published = true; | ||
$this->publisher()->associate(auth()->user()); | ||
|
||
// copy the attributes from the base record to the published record | ||
$published->fill($this->attributes); | ||
$published->title = 'YES!'; | ||
|
||
// save the published record quietly as it's effectively | ||
// a read-only copy of the base record | ||
$published->saveQuietly(); | ||
|
||
// save the base record | ||
$this->save(); | ||
|
||
return $this->fresh(); | ||
} | ||
|
||
public function scopeWithBaseTable(Builder $query): static | ||
{ | ||
$this->setTable($this->getBaseTable()); | ||
|
||
return $this; | ||
} | ||
|
||
public function scopeWithPublishedTable(Builder $query): static | ||
{ | ||
$this->setTable($this->getPublishedTable()); | ||
|
||
return $this; | ||
} | ||
|
||
public function scopeWithVersionTable(Builder $query): static | ||
{ | ||
$this->setTable($this->getVersionTable()); | ||
|
||
return $this; | ||
} | ||
|
||
public function getBaseTable(): string | ||
{ | ||
return $this->baseTable; | ||
} | ||
|
||
public function setBaseTable(string $table): static | ||
{ | ||
$this->baseTable = $table; | ||
|
||
return $this; | ||
} | ||
|
||
public function getVersionTable(): string | ||
{ | ||
return $this->versionTable; | ||
} | ||
|
||
public function setVersionTable(string $table): static | ||
{ | ||
$this->versionTable = $table; | ||
|
||
return $this; | ||
} | ||
|
||
public function getPublishedTable(): string | ||
{ | ||
return $this->publishedTable; | ||
} | ||
|
||
public function setPublishedTable(string $table): static | ||
{ | ||
$this->publishedTable = $table; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Indra\Revisor\Contracts; | ||
|
||
interface RevisorContract {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Indra\Revisor\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,48 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
declare(strict_types=1); | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Indra\Revisor\Facades\Revisor; | ||
|
||
it('creates revisor schemas', function () { | ||
Revisor::schemaCreate('foo', function (Blueprint $table): void { | ||
$table->id(); | ||
$table->string('title'); | ||
}); | ||
it('creates and amends revisor schemas', function () { | ||
// table creation and amendments in TestCase.php | ||
|
||
expect(Schema::hasTable('foo'))->toBeTrue(); | ||
expect(Schema::hasTable(Revisor::getVersionsTableNameFor('foo')))->toBeTrue(); | ||
expect(Schema::hasTable(Revisor::getPublishedTableNameFor('foo')))->toBeTrue(); | ||
// assert that expected tables exist | ||
expect(Schema::hasTable('pages'))->toBeTrue(); | ||
expect(Schema::hasTable(Revisor::getVersionTableFor('pages')))->toBeTrue(); | ||
expect(Schema::hasTable(Revisor::getPublishedTableFor('pages')))->toBeTrue(); | ||
|
||
// define expected columns | ||
$expectedColumns = [ | ||
'id', | ||
'title', | ||
'is_current', | ||
'publisher_type', | ||
'publisher_id', | ||
'is_published', | ||
'published_at', | ||
'published_by', | ||
'is_current', | ||
'is_published', | ||
'content', | ||
'created_at', | ||
'updated_at', | ||
]; | ||
//expect([])->toMatchArray(Schema::getColumnListing('foo')); ? | ||
expect(Schema::hasColumns('foo', $expectedColumns))->toBeTrue(); | ||
expect(Schema::hasColumns(Revisor::getVersionsTableNameFor('foo'), $expectedColumns + ['record_id']))->toBeTrue(); | ||
expect(Schema::hasColumns(Revisor::getPublishedTableNameFor('foo'), $expectedColumns))->toBeTrue(); | ||
|
||
sort($expectedColumns); | ||
|
||
// assert that expected columns exist for base table | ||
$actualColumns = Schema::getColumnListing('pages'); | ||
sort($actualColumns); | ||
expect($expectedColumns)->toMatchArray($actualColumns); | ||
|
||
// assert that expected columns exist for versions table | ||
$actualColumns = Schema::getColumnListing(Revisor::getVersionTableFor('pages')); | ||
sort($actualColumns); | ||
$expectedVersionsColumns = array_merge($expectedColumns, ['record_id']); | ||
sort($expectedVersionsColumns); | ||
expect($expectedVersionsColumns)->toMatchArray($actualColumns); | ||
|
||
// assert that expected columns exist for published table | ||
$actualColumns = Schema::getColumnListing(Revisor::getPublishedTableFor('pages')); | ||
sort($actualColumns); | ||
expect($expectedColumns)->toMatchArray($actualColumns); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Indra\Revisor\Tests\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Indra\Revisor\Concerns\HasRevisor; | ||
use Indra\Revisor\Contracts\RevisorContract; | ||
|
||
class Page extends Model implements RevisorContract | ||
{ | ||
use HasRevisor; | ||
|
||
protected $fillable = [ | ||
'title', | ||
'description', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Indra\Revisor\Tests\Models\Page; | ||
|
||
it('publishes records as expected', function () { | ||
$page = Page::create(['title' => 'Home']); | ||
$page->refresh(); | ||
|
||
expect($page->is_published)->toBe(false); | ||
|
||
$page->publish(); | ||
expect($page->is_published)->toBe(true); | ||
|
||
$published = Page::withPublishedTable()->where('id', $page->id)->ddRawSQL(); | ||
dd($published); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters