Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
backupStaticProperties="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true"
>

<testsuites>
Expand All @@ -17,12 +13,6 @@
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

<php>
<env name="APP_ENV" value="testing"/>
</php>
Expand Down
19 changes: 10 additions & 9 deletions src/Venturecraft/Revisionable/Revisionable.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php namespace Venturecraft\Revisionable;

use Illuminate\Support\Arr;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Arr;

/*
* This file is part of the Revisionable package by Venture Craft
Expand Down Expand Up @@ -163,8 +164,8 @@ public function postSave()
'old_value' => Arr::get($this->originalData, $key),
'new_value' => $this->updatedData[$key],
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
);
}

Expand Down Expand Up @@ -198,8 +199,8 @@ public function postCreate()
'old_value' => null,
'new_value' => $this->{self::CREATED_AT},
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
);

$revision = static::newModel();
Expand All @@ -222,8 +223,8 @@ public function postDelete()
'old_value' => null,
'new_value' => $this->{$this->getDeletedAtColumn()},
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
);
$revision = static::newModel();
\DB::table($revision->getTable())->insert($revisions);
Expand Down Expand Up @@ -251,8 +252,8 @@ public function postForceDelete()
'old_value' => $this->{self::CREATED_AT},
'new_value' => null,
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
);

$revision = Revisionable::newModel();
Expand Down
17 changes: 9 additions & 8 deletions src/Venturecraft/Revisionable/RevisionableTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Venturecraft\Revisionable;

use Carbon\Carbon;
use Illuminate\Support\Arr;

/*
Expand Down Expand Up @@ -195,8 +196,8 @@ public function postSave()
'old_value' => Arr::get($this->originalData, $key),
'new_value' => $this->updatedData[$key],
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
);

$revisions[] = array_merge($original, $this->getAdditionalFields());
Expand Down Expand Up @@ -239,8 +240,8 @@ public function postCreate()
'old_value' => null,
'new_value' => $this->{self::CREATED_AT},
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
);

//Determine if there are any additional fields we'd like to add to our model contained in the config file, and
Expand Down Expand Up @@ -270,8 +271,8 @@ public function postDelete()
'old_value' => null,
'new_value' => $this->{$this->getDeletedAtColumn()},
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
);

//Since there is only one revision because it's deleted, let's just merge into revision[0]
Expand Down Expand Up @@ -304,8 +305,8 @@ public function postForceDelete()
'old_value' => $this->{self::CREATED_AT},
'new_value' => null,
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
);

$revision = Revisionable::newModel();
Expand Down
55 changes: 46 additions & 9 deletions tests/RevisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

namespace Venturecraft\Revisionable\Tests;

use Carbon\Carbon;
use Venturecraft\Revisionable\Tests\Models\User;

class RevisionTest extends \Orchestra\Testbench\TestCase
{
/**
* Setup the test environment.
*/
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->loadLaravelMigrations(['--database' => 'testing']);
$this->loadLaravelMigrations();

// call migrations specific to our tests, e.g. to seed the db
// the path option should be an absolute path.
$this->loadMigrationsFrom([
'--database' => 'testing',
'--path' => realpath(__DIR__.'/../src/migrations'),
]);
}
Expand Down Expand Up @@ -78,6 +75,49 @@ public function testRevisionsStored()

// we should have two revisions to my name
$this->assertCount(2, $user->revisionHistory);

$firstRevision = $user->revisionHistory->first();
$this->assertEquals('name', $firstRevision->key);
$this->assertEquals('James Judd', $firstRevision->old_value);
$this->assertEquals('Judd', $firstRevision->new_value);
$secondRevision = $user->revisionHistory->last();
$this->assertEquals('name', $secondRevision->key);
$this->assertEquals('Judd', $secondRevision->old_value);
$this->assertEquals('James', $secondRevision->new_value);
}

public function testDatesRespectCarbonConfiguration(): void
{
$testDate1 = Carbon::create(2024, 1, 1, 12, 0, 0);
Carbon::setTestNow($testDate1);

$user = User::create([
'name' => 'James Judd',
'email' => 'james.judd@revisionable.test',
'password' => \Hash::make('456'),
]);

$user->update([
'name' => 'Judd'
]);

$testDate2 = Carbon::create(2026, 1, 1, 12, 0, 0);
Carbon::setTestNow($testDate2);

$user->update([
'name' => 'James'
]);

$firstRevision = $user->revisionHistory->first();
static::assertInstanceOf(Carbon::class, $firstRevision->created_at);
static::assertInstanceOf(Carbon::class, $firstRevision->updated_at);
$this->assertEquals($testDate1, $firstRevision->created_at);
$this->assertEquals($testDate1, $firstRevision->updated_at);
$secondRevision = $user->revisionHistory->last();
static::assertInstanceOf(Carbon::class, $secondRevision->created_at);
static::assertInstanceOf(Carbon::class, $secondRevision->updated_at);
$this->assertEquals($testDate2, $secondRevision->created_at);
$this->assertEquals($testDate2, $secondRevision->updated_at);
}

/**
Expand All @@ -86,7 +126,6 @@ public function testRevisionsStored()
public function testRevisionStoredAdditionalFields()
{
$this->loadMigrationsFrom([
'--database' => 'testing',
'--path' => realpath(__DIR__.'/migrations'),
]);

Expand Down Expand Up @@ -117,7 +156,6 @@ public function testRevisionStoredAdditionalFields()
public function testRevisionSkipsAdditionalFieldsWhenNotAvailable()
{
$this->loadMigrationsFrom([
'--database' => 'testing',
'--path' => realpath(__DIR__.'/migrations'),
]);

Expand Down Expand Up @@ -147,7 +185,6 @@ public function testRevisionSkipsAdditionalFieldsWhenNotAvailable()
public function testRevisionSkipsAdditionalFieldsWhenMisconfigured()
{
$this->loadMigrationsFrom([
'--database' => 'testing',
'--path' => realpath(__DIR__.'/migrations'),
]);

Expand Down