From 41547ec7c3be68ee95719e7e2b560fb56473e293 Mon Sep 17 00:00:00 2001 From: overtrue Date: Fri, 7 Oct 2022 00:49:56 +0800 Subject: [PATCH] Fixed #41 --- .php-cs-fixer.php | 49 ----------------------------------------- composer.json | 8 +++---- phpunit.xml.dist | 30 +++++++++---------------- src/Version.php | 9 ++++---- src/VersionStrategy.php | 1 + src/Versionable.php | 26 ++++++++++++---------- tests/DiffTest.php | 28 +++++++++++------------ tests/FeatureTest.php | 43 +++++++++++++++++++++++++++++++----- tests/TestCase.php | 5 ++--- tests/VersionAtTest.php | 3 +-- 10 files changed, 88 insertions(+), 114 deletions(-) delete mode 100644 .php-cs-fixer.php diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php deleted file mode 100644 index 8695228..0000000 --- a/.php-cs-fixer.php +++ /dev/null @@ -1,49 +0,0 @@ -setRules([ - '@PSR12' => true, - 'binary_operator_spaces' => true, - 'blank_line_after_opening_tag' => true, - 'compact_nullable_typehint' => true, - 'declare_equal_normalize' => true, - 'lowercase_cast' => true, - 'lowercase_static_reference' => true, - 'new_with_braces' => true, - 'no_blank_lines_after_class_opening' => true, - 'no_leading_import_slash' => true, - 'no_whitespace_in_blank_line' => true, - 'no_unused_imports' => true, - 'ordered_class_elements' => [ - 'order' => [ - 'use_trait', - ], - ], - 'ordered_imports' => [ - 'imports_order' => [ - 'class', - 'function', - 'const', - ], - 'sort_algorithm' => 'none', - ], - 'return_type_declaration' => true, - 'short_scalar_cast' => true, - 'single_blank_line_before_namespace' => true, - 'single_trait_insert_per_statement' => true, - 'ternary_operator_spaces' => true, - 'unary_operator_spaces' => true, - 'visibility_required' => [ - 'elements' => [ - 'const', - 'method', - 'property', - ], - ], - ]) - ->setFinder( - PhpCsFixer\Finder::create() - ->exclude('vendor') - ->in([__DIR__.'/src/', __DIR__.'/tests/']) - ) -; diff --git a/composer.json b/composer.json index 3253d81..8fbac1c 100644 --- a/composer.json +++ b/composer.json @@ -10,8 +10,8 @@ "require-dev": { "phpunit/phpunit": "^9.0", "orchestra/testbench": "^7.0", - "friendsofphp/php-cs-fixer": "^3.6", - "mockery/mockery": "^1.4" + "mockery/mockery": "^1.4", + "laravel/pint": "^1.2" }, "autoload": { "psr-4": { @@ -42,8 +42,8 @@ "scripts": { "post-merge": "composer install", "cghooks": "vendor/bin/cghooks", - "check-style": "php-cs-fixer fix --using-cache=no --diff --dry-run --ansi", - "fix-style": "php-cs-fixer fix --using-cache=no --ansi", + "check-style": "vendor/bin/pint --test", + "fix-style": "vendor/bin/pint", "test": "phpunit --colors" }, "scripts-descriptions": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e47284c..b2daa31 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,21 +1,13 @@ - - - - ./tests/ - - - - - src/ - - + + + + src/ + + + + + ./tests/ + + diff --git a/src/Version.php b/src/Version.php index 293176d..65dc68e 100644 --- a/src/Version.php +++ b/src/Version.php @@ -8,7 +8,7 @@ /** * @property Model|\Overtrue\LaravelVersionable\Versionable $versionable * @property array $contents - * @property integer $id + * @property int $id */ class Version extends Model { @@ -49,8 +49,7 @@ public function versionable(): \Illuminate\Database\Eloquent\Relations\MorphTo /** * @param \Illuminate\Database\Eloquent\Model $model - * @param array $attributes - * + * @param array $attributes * @return \Overtrue\LaravelVersionable\Version */ public static function createForModel(Model $model, array $attributes = []): Version @@ -74,7 +73,7 @@ public static function createForModel(Model $model, array $attributes = []): Ver public function revert(): bool { - return $this->versionable->forceFill($this->contents)->save(); + return $this->revertWithoutSaving()->save(); } public function revertWithoutSaving(): ?Model @@ -94,7 +93,7 @@ public function nextVersion(): ?static public function diff(Version $toVersion = null, array $differOptions = [], array $renderOptions = []): Diff { - if (!$toVersion) { + if (! $toVersion) { $toVersion = $this->previousVersion() ?? new static(); } diff --git a/src/VersionStrategy.php b/src/VersionStrategy.php index cbbc452..3ec4533 100644 --- a/src/VersionStrategy.php +++ b/src/VersionStrategy.php @@ -5,5 +5,6 @@ class VersionStrategy { public const DIFF = 'diff'; + public const SNAPSHOT = 'snapshot'; } diff --git a/src/Versionable.php b/src/Versionable.php index 9f2570a..104fe2c 100644 --- a/src/Versionable.php +++ b/src/Versionable.php @@ -10,6 +10,7 @@ trait Versionable { protected static bool $versioning = true; + protected bool $forceDeleteVersion = false; // You can add these properties to you versionable model @@ -68,12 +69,11 @@ public function firstVersion(): MorphOne /** * Get the version for a specific time. * - * @param string|DateTimeInterface|null $time - * @param DateTimeZone|string|null $tz + * @param string|DateTimeInterface|null $time + * @param DateTimeZone|string|null $tz + * @return static * * @throws \Carbon\Exceptions\InvalidFormatException - * - * @return static */ public function versionAt($time = null, $tz = null): ?Version { @@ -157,7 +157,7 @@ public function forceRemoveAllVersions(): void public function shouldVersioning(): bool { - return !empty($this->getVersionableAttributes()); + return ! empty($this->getVersionableAttributes()); } public function getVersionableAttributes(): array @@ -168,13 +168,15 @@ public function getVersionableAttributes(): array return []; } - $contents = $this->attributesToArray(); + $changes = $this->versionableFromArray($changes); + $changedKeys = array_keys($changes); - if ($this->getVersionStrategy() == VersionStrategy::DIFF) { - $contents = $this->only(\array_keys($changes)); + if ($this->getVersionStrategy() === VersionStrategy::SNAPSHOT && ! empty($changes)) { + $changedKeys = array_keys($this->getAttributes()); } - return $this->versionableFromArray($contents); + // to keep casts and mutators works, we need to get the updated attributes from the model + return $this->only($changedKeys); } /** @@ -182,7 +184,7 @@ public function getVersionableAttributes(): array */ public function setVersionable(array $attributes): static { - if (!\property_exists($this, 'versionable')) { + if (! \property_exists($this, 'versionable')) { throw new \Exception('Property $versionable not exist.'); } @@ -196,7 +198,7 @@ public function setVersionable(array $attributes): static */ public function setDontVersionable(array $attributes): static { - if (!\property_exists($this, 'dontVersionable')) { + if (! \property_exists($this, 'dontVersionable')) { throw new \Exception('Property $dontVersionable not exist.'); } @@ -225,7 +227,7 @@ public function getVersionStrategy(): string */ public function setVersionStrategy(string $strategy): static { - if (!\property_exists($this, 'versionStrategy')) { + if (! \property_exists($this, 'versionStrategy')) { throw new \Exception('Property $versionStrategy not exist.'); } diff --git a/tests/DiffTest.php b/tests/DiffTest.php index b335243..74b01ca 100644 --- a/tests/DiffTest.php +++ b/tests/DiffTest.php @@ -40,8 +40,8 @@ public function test_diff_to_context_text() $this->assertSame( [ - 'content' => DiffHelper::calculate("version1 content", "version2 content", "Context"), - 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "Context"), + 'content' => DiffHelper::calculate('version1 content', 'version2 content', 'Context'), + 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'Context'), ], (new Diff($new, $old))->toContextText() ); @@ -54,8 +54,8 @@ public function test_diff_to_text() $this->assertSame( [ - 'content' => DiffHelper::calculate("version1 content", "version2 content", "Unified"), - 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "Unified"), + 'content' => DiffHelper::calculate('version1 content', 'version2 content', 'Unified'), + 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'Unified'), ], (new Diff($new, $old))->toText() ); @@ -68,8 +68,8 @@ public function test_diff_to_json_text() $this->assertSame( [ - 'content' => DiffHelper::calculate("version1 content", "version2 content", "JsonText"), - 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "JsonText"), + 'content' => DiffHelper::calculate('version1 content', 'version2 content', 'JsonText'), + 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'JsonText'), ], (new Diff($new, $old))->toJsonText() ); @@ -82,8 +82,8 @@ public function test_diff_to_html() $this->assertSame( [ - 'content' => DiffHelper::calculate("version1 content", "version2 content", "Combined"), - 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "Combined"), + 'content' => DiffHelper::calculate('version1 content', 'version2 content', 'Combined'), + 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'Combined'), ], (new Diff($new, $old))->toHtml() ); @@ -96,8 +96,8 @@ public function test_diff_to_inline_html() $this->assertSame( [ - 'content' => DiffHelper::calculate("version1 content", "version2 content", "Inline"), - 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "Inline"), + 'content' => DiffHelper::calculate('version1 content', 'version2 content', 'Inline'), + 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'Inline'), ], (new Diff($new, $old))->toInlineHtml() ); @@ -110,8 +110,8 @@ public function test_diff_to_json_html() $this->assertSame( [ - 'content' => DiffHelper::calculate("version1 content", "version2 content", "JsonHtml"), - 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "JsonHtml"), + 'content' => DiffHelper::calculate('version1 content', 'version2 content', 'JsonHtml'), + 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'JsonHtml'), ], (new Diff($new, $old))->toJsonHtml() ); @@ -124,8 +124,8 @@ public function test_diff_to_side_by_side_html() $this->assertSame( [ - 'content' => DiffHelper::calculate("version1 content", "version2 content", "SideBySide"), - 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "SideBySide"), + 'content' => DiffHelper::calculate('version1 content', 'version2 content', 'SideBySide'), + 'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'SideBySide'), ], (new Diff($new, $old))->toSideBySideHtml() ); diff --git a/tests/FeatureTest.php b/tests/FeatureTest.php index cc20dee..e5f6e86 100644 --- a/tests/FeatureTest.php +++ b/tests/FeatureTest.php @@ -18,7 +18,7 @@ protected function setUp(): void config([ 'auth.providers.users.model' => User::class, 'versionable.user_model' => User::class, - ]); + ]); $this->user = User::create(['name' => 'overtrue']); $this->actingAs($this->user); @@ -34,12 +34,37 @@ public function post_has_versions() $this->assertCount(1, $post->versions); $this->assertSame($post->only('title', 'content'), $post->lastVersion->contents); + $this->assertDatabaseCount('versions', 1); + // version2 $post->update(['title' => 'version2']); $post->refresh(); $this->assertCount(2, $post->versions); $this->assertSame($post->only('title'), $post->lastVersion->contents); + $this->assertDatabaseCount('versions', 2); + } + + /** + * @test + */ + public function it_can_work_with_snapshot_strategy() + { + $post = new Post(['title' => 'Title', 'content' => 'Content']); + $post->setVersionStrategy(VersionStrategy::SNAPSHOT); + + $post->save(); + $this->assertDatabaseCount('versions', 1); + + $this->travelTo(now()->addMinute()); + + $post->setVersionable(['title']); + $post->update(['title' => 'title changed']); + $this->assertDatabaseCount('versions', 2); + + // content is not versionable + $post->update(['content' => 'content changed']); + $this->assertDatabaseCount('versions', 2); } /** @@ -59,7 +84,12 @@ public function post_create_version_with_strategy() $post->refresh(); $this->assertCount(2, $post->versions); - $this->assertSame($post->only('title', 'content'), $post->lastVersion->contents); + $this->assertArrayHasKey('title', $post->lastVersion->contents); + $this->assertArrayHasKey('content', $post->lastVersion->contents); + $this->assertArrayHasKey('created_at', $post->lastVersion->contents); + $this->assertArrayHasKey('updated_at', $post->lastVersion->contents); + $this->assertArrayHasKey('id', $post->lastVersion->contents); + $this->assertArrayHasKey('user_id', $post->lastVersion->contents); $this->assertSame('version1 content', $post->lastVersion->contents['content']); } @@ -76,16 +106,20 @@ public function post_can_revert_to_target_version() // #29 $version = $post->firstVersion; $post = $version->revertWithoutSaving(); + $this->assertSame('version1', $post->title); $this->assertSame('version1 content', $post->content); + $post->refresh(); + // revert version 2 - $post->revertToVersion(2); + $post->revertToVersion($post->firstVersion->nextVersion()->id); $post->refresh(); // only title updated $this->assertSame('version2', $post->title); $this->assertSame('version4 content', $post->content); + $this->assertSame(['foo' => 'bar'], $post->extends); // revert version 3 @@ -133,7 +167,6 @@ public function post_will_keep_versions() $this->assertCount(0, $post->versions); } - /** * @test */ @@ -196,7 +229,6 @@ public function post_version_soft_delete_and_restore() $post->refresh(); $this->assertDatabaseCount('versions', 3); - // soft delete $post->refresh(); // first @@ -242,7 +274,6 @@ public function post_version_forced_delete() $post->refresh(); $this->assertDatabaseCount('versions', 3); - // forced delete $post->enableForceDeleteVersion(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 6cbc705..5e9850a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,8 +9,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase /** * Load package service provider. * - * @param \Illuminate\Foundation\Application $app - * + * @param \Illuminate\Foundation\Application $app * @return array */ protected function getPackageProviders($app) @@ -21,7 +20,7 @@ protected function getPackageProviders($app) /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app */ protected function getEnvironmentSetUp($app) { diff --git a/tests/VersionAtTest.php b/tests/VersionAtTest.php index ac942e4..1ab5f0c 100644 --- a/tests/VersionAtTest.php +++ b/tests/VersionAtTest.php @@ -3,7 +3,6 @@ namespace Tests; use Illuminate\Support\Carbon; -use Overtrue\LaravelVersionable\Diff; class VersionAtTest extends TestCase { @@ -18,7 +17,7 @@ protected function setUp(): void config([ 'auth.providers.users.model' => User::class, 'versionable.user_model' => User::class, - ]); + ]); $this->user = User::create(['name' => 'marijoo']); $this->actingAs($this->user);