Skip to content

Commit

Permalink
Merge pull request #84 from warren32/fix/morph-map-to-class
Browse files Browse the repository at this point in the history
Get actual class name from morph map
  • Loading branch information
nonoesp authored Dec 16, 2021
2 parents 043b311 + 069321a commit 66d811b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Mpociot/Versionable/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function getModel()
? stream_get_contents($this->model_data,-1,0)
: $this->model_data;

$model = new $this->versionable_type();
$className = self::getActualClassNameForMorph($this->versionable_type);
$model = new $className();
$model->unguard();
$model->fill(unserialize($modelData));
$model->exists = true;
Expand Down
20 changes: 18 additions & 2 deletions tests/VersionableTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

use Mockery as m;
use Carbon\Carbon;
use Mpociot\Versionable\Version;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Mockery as m;
use Illuminate\Database\Eloquent\Model;
use Mpociot\Versionable\Version;
use Mpociot\Versionable\VersionableTrait;
use Illuminate\Database\Eloquent\Relations\Relation;

class VersionableTest extends VersionableTestCase
{
Expand Down Expand Up @@ -513,6 +514,21 @@ public function testAllowHiddenFields() {

$this->assertArrayNotHasKey('password', $user->toArray());
}

public function testWhereModelHasMorphMap()
{
Relation::morphMap(['users' => TestVersionableUser::class]);
$user = new TestVersionableUser();
$user->name = "Test";
$user->email = "example@test.php";
$user->password = "12345";
$user->last_login = $user->freshTimestamp();
$user->save();

$version = $user->currentVersion();
$this->assertEquals( $user->attributesToArray(), $version->getModel()->attributesToArray() );
Relation::morphMap([], false);
}

}

Expand Down

0 comments on commit 66d811b

Please sign in to comment.