Skip to content

Commit 4f39887

Browse files
committed
fix tests
1 parent 8c2b6ab commit 4f39887

File tree

5 files changed

+18
-48
lines changed

5 files changed

+18
-48
lines changed

config/versionable.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
*/
88
'migrations' => true,
99

10-
/*
11-
* Create the initial versions of model. If you're installing this on an existing application,
12-
* you may want to create a version of the current model.
13-
*/
14-
'keep_original_version' => false,
15-
1610
/*
1711
* Keep versions, you can redefine in target model.
1812
* Default: 0 - Keep all versions.

phpunit.xml.dist

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
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>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
83
<testsuites>
94
<testsuite name="Application Test Suite">
105
<directory>./tests/</directory>
116
</testsuite>
127
</testsuites>
8+
<source>
9+
<include>
10+
<directory suffix=".php">src/</directory>
11+
</include>
12+
</source>
1313
</phpunit>

src/Version.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public function revert(): bool
8181

8282
public function revertWithoutSaving(): ?Model
8383
{
84-
return $this->versionable->forceFill($this->contents);
84+
$original = $this->versionable->getRawOriginal();
85+
86+
return $this->versionable->setRawAttributes(array_merge($original, $this->contents));
8587
}
8688

8789
public function scopeOrderOldestFirst(Builder $query): Builder

src/Versionable.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ trait Versionable
2323

2424
public static function bootVersionable()
2525
{
26-
if (config('versionable.keep_original_version')) {
27-
static::updating(
28-
function (Model $model) {
29-
if ($model->versions()->count() === 0) {
30-
$existingModel = self::find($model->id);
31-
32-
Version::createForModel($existingModel, $existingModel->only($existingModel->getVersionable()));
33-
}
26+
static::updating(
27+
function (Model $model) {
28+
if (static::$versioning && $model->versions()->count() === 0) {
29+
$existingModel = self::find($model->id);
30+
31+
Version::createForModel($existingModel, $existingModel->only($existingModel->getVersionable()));
3432
}
35-
);
36-
}
33+
}
34+
);
3735

3836
static::saved(
3937
function (Model $model) {

tests/FeatureTest.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Tests;
44

55
use Illuminate\Support\Carbon;
6-
use Illuminate\Support\Facades\Config;
76
use Overtrue\LaravelVersionable\Diff;
87
use Overtrue\LaravelVersionable\Version;
98
use Overtrue\LaravelVersionable\VersionStrategy;
@@ -398,10 +397,8 @@ public function relations_will_not_in_version_contents()
398397
/**
399398
* @test
400399
*/
401-
public function it_creates_initial_version_when_enabled()
400+
public function it_creates_initial_version()
402401
{
403-
Config::set('versionable.keep_original_version', true);
404-
405402
$post = new Post;
406403

407404
Post::withoutVersion(function () use (&$post) {
@@ -418,25 +415,4 @@ public function it_creates_initial_version_when_enabled()
418415
$this->assertSame('version1', $post->firstVersion->contents['title']);
419416
$this->assertSame('version2', $post->lastVersion->contents['title']);
420417
}
421-
422-
/**
423-
* @test
424-
*/
425-
public function it_doesnt_create_initial_version_when_disabled()
426-
{
427-
$post = new Post;
428-
429-
Post::withoutVersion(function () use (&$post) {
430-
$post = Post::create(['title' => 'version1', 'content' => 'version1 content']);
431-
});
432-
433-
$this->assertCount(0, $post->versions);
434-
435-
$post->update(['title' => 'version2']);
436-
437-
$post->refresh();
438-
439-
$this->assertCount(1, $post->versions);
440-
$this->assertNotSame('version1', $post->firstVersion->contents['title']);
441-
}
442418
}

0 commit comments

Comments
 (0)