Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Mar 8, 2024
1 parent 8c2b6ab commit 4f39887
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 48 deletions.
6 changes: 0 additions & 6 deletions config/versionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<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">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
4 changes: 3 additions & 1 deletion src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 8 additions & 10 deletions src/Versionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 1 addition & 25 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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']);
}
}

0 comments on commit 4f39887

Please sign in to comment.