diff --git a/phpunit.xml b/phpunit.xml index 96d96447..abf702fe 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,14 +1,10 @@ @@ -17,12 +13,6 @@ - - - src/ - - - diff --git a/src/Venturecraft/Revisionable/Revisionable.php b/src/Venturecraft/Revisionable/Revisionable.php index 5134af12..cecb7b24 100644 --- a/src/Venturecraft/Revisionable/Revisionable.php +++ b/src/Venturecraft/Revisionable/Revisionable.php @@ -1,7 +1,8 @@ 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(), ); } @@ -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(); @@ -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); @@ -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(); diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index 0def63f2..e91af15c 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -1,5 +1,6 @@ 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()); @@ -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 @@ -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] @@ -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(); diff --git a/tests/RevisionTest.php b/tests/RevisionTest.php index 7c29b8b5..e20f258f 100644 --- a/tests/RevisionTest.php +++ b/tests/RevisionTest.php @@ -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'), ]); } @@ -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); } /** @@ -86,7 +126,6 @@ public function testRevisionsStored() public function testRevisionStoredAdditionalFields() { $this->loadMigrationsFrom([ - '--database' => 'testing', '--path' => realpath(__DIR__.'/migrations'), ]); @@ -117,7 +156,6 @@ public function testRevisionStoredAdditionalFields() public function testRevisionSkipsAdditionalFieldsWhenNotAvailable() { $this->loadMigrationsFrom([ - '--database' => 'testing', '--path' => realpath(__DIR__.'/migrations'), ]); @@ -147,7 +185,6 @@ public function testRevisionSkipsAdditionalFieldsWhenNotAvailable() public function testRevisionSkipsAdditionalFieldsWhenMisconfigured() { $this->loadMigrationsFrom([ - '--database' => 'testing', '--path' => realpath(__DIR__.'/migrations'), ]);