Skip to content

Commit 41547ec

Browse files
committed
Fixed #41
1 parent 366a193 commit 41547ec

File tree

10 files changed

+88
-114
lines changed

10 files changed

+88
-114
lines changed

.php-cs-fixer.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"require-dev": {
1111
"phpunit/phpunit": "^9.0",
1212
"orchestra/testbench": "^7.0",
13-
"friendsofphp/php-cs-fixer": "^3.6",
14-
"mockery/mockery": "^1.4"
13+
"mockery/mockery": "^1.4",
14+
"laravel/pint": "^1.2"
1515
},
1616
"autoload": {
1717
"psr-4": {
@@ -42,8 +42,8 @@
4242
"scripts": {
4343
"post-merge": "composer install",
4444
"cghooks": "vendor/bin/cghooks",
45-
"check-style": "php-cs-fixer fix --using-cache=no --diff --dry-run --ansi",
46-
"fix-style": "php-cs-fixer fix --using-cache=no --ansi",
45+
"check-style": "vendor/bin/pint --test",
46+
"fix-style": "vendor/bin/pint",
4747
"test": "phpunit --colors"
4848
},
4949
"scripts-descriptions": {

phpunit.xml.dist

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false">
11-
<testsuites>
12-
<testsuite name="Application Test Suite">
13-
<directory>./tests/</directory>
14-
</testsuite>
15-
</testsuites>
16-
<filter>
17-
<whitelist>
18-
<directory suffix=".php">src/</directory>
19-
</whitelist>
20-
</filter>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Application Test Suite">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
2113
</phpunit>

src/Version.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* @property Model|\Overtrue\LaravelVersionable\Versionable $versionable
1010
* @property array $contents
11-
* @property integer $id
11+
* @property int $id
1212
*/
1313
class Version extends Model
1414
{
@@ -49,8 +49,7 @@ public function versionable(): \Illuminate\Database\Eloquent\Relations\MorphTo
4949

5050
/**
5151
* @param \Illuminate\Database\Eloquent\Model $model
52-
* @param array $attributes
53-
*
52+
* @param array $attributes
5453
* @return \Overtrue\LaravelVersionable\Version
5554
*/
5655
public static function createForModel(Model $model, array $attributes = []): Version
@@ -74,7 +73,7 @@ public static function createForModel(Model $model, array $attributes = []): Ver
7473

7574
public function revert(): bool
7675
{
77-
return $this->versionable->forceFill($this->contents)->save();
76+
return $this->revertWithoutSaving()->save();
7877
}
7978

8079
public function revertWithoutSaving(): ?Model
@@ -94,7 +93,7 @@ public function nextVersion(): ?static
9493

9594
public function diff(Version $toVersion = null, array $differOptions = [], array $renderOptions = []): Diff
9695
{
97-
if (!$toVersion) {
96+
if (! $toVersion) {
9897
$toVersion = $this->previousVersion() ?? new static();
9998
}
10099

src/VersionStrategy.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
class VersionStrategy
66
{
77
public const DIFF = 'diff';
8+
89
public const SNAPSHOT = 'snapshot';
910
}

src/Versionable.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
trait Versionable
1111
{
1212
protected static bool $versioning = true;
13+
1314
protected bool $forceDeleteVersion = false;
1415

1516
// You can add these properties to you versionable model
@@ -68,12 +69,11 @@ public function firstVersion(): MorphOne
6869
/**
6970
* Get the version for a specific time.
7071
*
71-
* @param string|DateTimeInterface|null $time
72-
* @param DateTimeZone|string|null $tz
72+
* @param string|DateTimeInterface|null $time
73+
* @param DateTimeZone|string|null $tz
74+
* @return static
7375
*
7476
* @throws \Carbon\Exceptions\InvalidFormatException
75-
*
76-
* @return static
7777
*/
7878
public function versionAt($time = null, $tz = null): ?Version
7979
{
@@ -157,7 +157,7 @@ public function forceRemoveAllVersions(): void
157157

158158
public function shouldVersioning(): bool
159159
{
160-
return !empty($this->getVersionableAttributes());
160+
return ! empty($this->getVersionableAttributes());
161161
}
162162

163163
public function getVersionableAttributes(): array
@@ -168,21 +168,23 @@ public function getVersionableAttributes(): array
168168
return [];
169169
}
170170

171-
$contents = $this->attributesToArray();
171+
$changes = $this->versionableFromArray($changes);
172+
$changedKeys = array_keys($changes);
172173

173-
if ($this->getVersionStrategy() == VersionStrategy::DIFF) {
174-
$contents = $this->only(\array_keys($changes));
174+
if ($this->getVersionStrategy() === VersionStrategy::SNAPSHOT && ! empty($changes)) {
175+
$changedKeys = array_keys($this->getAttributes());
175176
}
176177

177-
return $this->versionableFromArray($contents);
178+
// to keep casts and mutators works, we need to get the updated attributes from the model
179+
return $this->only($changedKeys);
178180
}
179181

180182
/**
181183
* @throws \Exception
182184
*/
183185
public function setVersionable(array $attributes): static
184186
{
185-
if (!\property_exists($this, 'versionable')) {
187+
if (! \property_exists($this, 'versionable')) {
186188
throw new \Exception('Property $versionable not exist.');
187189
}
188190

@@ -196,7 +198,7 @@ public function setVersionable(array $attributes): static
196198
*/
197199
public function setDontVersionable(array $attributes): static
198200
{
199-
if (!\property_exists($this, 'dontVersionable')) {
201+
if (! \property_exists($this, 'dontVersionable')) {
200202
throw new \Exception('Property $dontVersionable not exist.');
201203
}
202204

@@ -225,7 +227,7 @@ public function getVersionStrategy(): string
225227
*/
226228
public function setVersionStrategy(string $strategy): static
227229
{
228-
if (!\property_exists($this, 'versionStrategy')) {
230+
if (! \property_exists($this, 'versionStrategy')) {
229231
throw new \Exception('Property $versionStrategy not exist.');
230232
}
231233

tests/DiffTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function test_diff_to_context_text()
4040

4141
$this->assertSame(
4242
[
43-
'content' => DiffHelper::calculate("version1 content", "version2 content", "Context"),
44-
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "Context"),
43+
'content' => DiffHelper::calculate('version1 content', 'version2 content', 'Context'),
44+
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'Context'),
4545
],
4646
(new Diff($new, $old))->toContextText()
4747
);
@@ -54,8 +54,8 @@ public function test_diff_to_text()
5454

5555
$this->assertSame(
5656
[
57-
'content' => DiffHelper::calculate("version1 content", "version2 content", "Unified"),
58-
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "Unified"),
57+
'content' => DiffHelper::calculate('version1 content', 'version2 content', 'Unified'),
58+
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'Unified'),
5959
],
6060
(new Diff($new, $old))->toText()
6161
);
@@ -68,8 +68,8 @@ public function test_diff_to_json_text()
6868

6969
$this->assertSame(
7070
[
71-
'content' => DiffHelper::calculate("version1 content", "version2 content", "JsonText"),
72-
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "JsonText"),
71+
'content' => DiffHelper::calculate('version1 content', 'version2 content', 'JsonText'),
72+
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'JsonText'),
7373
],
7474
(new Diff($new, $old))->toJsonText()
7575
);
@@ -82,8 +82,8 @@ public function test_diff_to_html()
8282

8383
$this->assertSame(
8484
[
85-
'content' => DiffHelper::calculate("version1 content", "version2 content", "Combined"),
86-
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "Combined"),
85+
'content' => DiffHelper::calculate('version1 content', 'version2 content', 'Combined'),
86+
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'Combined'),
8787
],
8888
(new Diff($new, $old))->toHtml()
8989
);
@@ -96,8 +96,8 @@ public function test_diff_to_inline_html()
9696

9797
$this->assertSame(
9898
[
99-
'content' => DiffHelper::calculate("version1 content", "version2 content", "Inline"),
100-
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "Inline"),
99+
'content' => DiffHelper::calculate('version1 content', 'version2 content', 'Inline'),
100+
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'Inline'),
101101
],
102102
(new Diff($new, $old))->toInlineHtml()
103103
);
@@ -110,8 +110,8 @@ public function test_diff_to_json_html()
110110

111111
$this->assertSame(
112112
[
113-
'content' => DiffHelper::calculate("version1 content", "version2 content", "JsonHtml"),
114-
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "JsonHtml"),
113+
'content' => DiffHelper::calculate('version1 content', 'version2 content', 'JsonHtml'),
114+
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'JsonHtml'),
115115
],
116116
(new Diff($new, $old))->toJsonHtml()
117117
);
@@ -124,8 +124,8 @@ public function test_diff_to_side_by_side_html()
124124

125125
$this->assertSame(
126126
[
127-
'content' => DiffHelper::calculate("version1 content", "version2 content", "SideBySide"),
128-
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), "SideBySide"),
127+
'content' => DiffHelper::calculate('version1 content', 'version2 content', 'SideBySide'),
128+
'user_id' => DiffHelper::calculate(json_encode(null), json_encode(123), 'SideBySide'),
129129
],
130130
(new Diff($new, $old))->toSideBySideHtml()
131131
);

0 commit comments

Comments
 (0)