From 4f3988743e6816a89d67096829aa3d4fd0595706 Mon Sep 17 00:00:00 2001 From: overtrue Date: Fri, 8 Mar 2024 14:57:50 +0800 Subject: [PATCH] fix tests --- config/versionable.php | 6 ------ phpunit.xml.dist | 12 ++++++------ src/Version.php | 4 +++- src/Versionable.php | 18 ++++++++---------- tests/FeatureTest.php | 26 +------------------------- 5 files changed, 18 insertions(+), 48 deletions(-) diff --git a/config/versionable.php b/config/versionable.php index 664de60..c0d9e0a 100644 --- a/config/versionable.php +++ b/config/versionable.php @@ -7,12 +7,6 @@ */ 'migrations' => true, - /* - * Create the initial versions of model. If you're installing this on an existing application, - * you may want to create a version of the current model. - */ - 'keep_original_version' => false, - /* * Keep versions, you can redefine in target model. * Default: 0 - Keep all versions. diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b2daa31..cd92a98 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,13 @@ - - - - src/ - - + ./tests/ + + + src/ + + diff --git a/src/Version.php b/src/Version.php index 0bfe573..8a6d833 100644 --- a/src/Version.php +++ b/src/Version.php @@ -81,7 +81,9 @@ public function revert(): bool public function revertWithoutSaving(): ?Model { - return $this->versionable->forceFill($this->contents); + $original = $this->versionable->getRawOriginal(); + + return $this->versionable->setRawAttributes(array_merge($original, $this->contents)); } public function scopeOrderOldestFirst(Builder $query): Builder diff --git a/src/Versionable.php b/src/Versionable.php index 436a8b6..eb2681c 100644 --- a/src/Versionable.php +++ b/src/Versionable.php @@ -23,17 +23,15 @@ trait Versionable public static function bootVersionable() { - if (config('versionable.keep_original_version')) { - static::updating( - function (Model $model) { - if ($model->versions()->count() === 0) { - $existingModel = self::find($model->id); - - Version::createForModel($existingModel, $existingModel->only($existingModel->getVersionable())); - } + static::updating( + function (Model $model) { + if (static::$versioning && $model->versions()->count() === 0) { + $existingModel = self::find($model->id); + + Version::createForModel($existingModel, $existingModel->only($existingModel->getVersionable())); } - ); - } + } + ); static::saved( function (Model $model) { diff --git a/tests/FeatureTest.php b/tests/FeatureTest.php index 82281f4..d041557 100644 --- a/tests/FeatureTest.php +++ b/tests/FeatureTest.php @@ -3,7 +3,6 @@ namespace Tests; use Illuminate\Support\Carbon; -use Illuminate\Support\Facades\Config; use Overtrue\LaravelVersionable\Diff; use Overtrue\LaravelVersionable\Version; use Overtrue\LaravelVersionable\VersionStrategy; @@ -398,10 +397,8 @@ public function relations_will_not_in_version_contents() /** * @test */ - public function it_creates_initial_version_when_enabled() + public function it_creates_initial_version() { - Config::set('versionable.keep_original_version', true); - $post = new Post; Post::withoutVersion(function () use (&$post) { @@ -418,25 +415,4 @@ public function it_creates_initial_version_when_enabled() $this->assertSame('version1', $post->firstVersion->contents['title']); $this->assertSame('version2', $post->lastVersion->contents['title']); } - - /** - * @test - */ - public function it_doesnt_create_initial_version_when_disabled() - { - $post = new Post; - - Post::withoutVersion(function () use (&$post) { - $post = Post::create(['title' => 'version1', 'content' => 'version1 content']); - }); - - $this->assertCount(0, $post->versions); - - $post->update(['title' => 'version2']); - - $post->refresh(); - - $this->assertCount(1, $post->versions); - $this->assertNotSame('version1', $post->firstVersion->contents['title']); - } }