Skip to content

Commit

Permalink
Make version model configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Sep 6, 2018
1 parent d8c3783 commit 9abd756
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 135 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
composer.lock
/vendor/
/vendor/
.idea
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
"illuminate/support": "~5.3"
},
"require-dev": {
"phpunit/phpunit": "5.*",
"phpunit/phpunit": "7.*",
"mockery/mockery": "^1.0",
"illuminate/database": "~5.3",
"illuminate/events": "~5.3"
"orchestra/testbench": "^3.7"
},
"autoload": {
"classmap": [
Expand All @@ -50,4 +49,4 @@
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ServiceProvider extends LaravelServiceProvider
*/
public function register ()
{
$this->mergeConfigFrom(__DIR__.'/../../../config/config.php', 'versionable');
}

/**
Expand All @@ -22,6 +23,11 @@ public function register ()
*/
public function boot()
{
$this->loadMigrationsFrom(__DIR__ . '/../../migrations');
$this->loadMigrationsFrom(__DIR__ . '/../../../migrations');


$this->publishes([
__DIR__.'/../../../config/config.php' => config_path('versionable.php'),
], 'config');
}
}
8 changes: 5 additions & 3 deletions src/Mpociot/Versionable/VersionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ trait VersionableTrait
*/
protected function getVersionClass()
{
if( property_exists( self::class, 'versionClass') )
return $this->versionClass ;
return Version::class ;
if( property_exists( self::class, 'versionClass') ) {
return $this->versionClass;
}

return config('versionable.version_model', Version::class);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [

/*
* The version model to use.
* Feel free to change this, if you need specific version
* model logic.
*/
'version_model' => \Mpociot\Versionable\Version::class

];
118 changes: 32 additions & 86 deletions tests/VersionableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@
class VersionableTest extends VersionableTestCase
{

public function setUp()
{
parent::setUp();

TestVersionableUser::flushEventListeners();
TestVersionableUser::boot();

TestVersionableSoftDeleteUser::flushEventListeners();
TestVersionableSoftDeleteUser::boot();

TestPartialVersionableUser::flushEventListeners();
TestPartialVersionableUser::boot();
}

public function tearDown()
{
m::close();
Expand All @@ -33,9 +19,6 @@ public function tearDown()

public function testVersionableRelation()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableUser();
$user->name = "Marcel";
$user->email = "[email protected]";
Expand All @@ -49,9 +32,6 @@ public function testVersionableRelation()

public function testInitialSaveShouldCreateVersion()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableUser();
$user->name = "Marcel";
$user->email = "[email protected]";
Expand All @@ -64,9 +44,6 @@ public function testInitialSaveShouldCreateVersion()

public function testRetrievePreviousVersionFails()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableUser();
$user->name = "Marcel";
$user->email = "[email protected]";
Expand All @@ -80,9 +57,6 @@ public function testRetrievePreviousVersionFails()

public function testRetrievePreviousVersionExists()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableUser();
$user->name = "Marcel";
$user->email = "[email protected]";
Expand All @@ -103,9 +77,6 @@ public function testRetrievePreviousVersionExists()

public function testVersionAndModelAreEqual()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableUser();
$user->name = "Marcel";
$user->email = "[email protected]";
Expand Down Expand Up @@ -142,28 +113,18 @@ public function testVersionsAreRelatedToUsers()

public function testGetResponsibleUserAttribute()
{
Auth::shouldReceive('check')
->andReturn( true );

Auth::shouldReceive('id')
->andReturn( 1 );


$responsibleOrigUser = new TestVersionableUser();
$responsibleOrigUser->name = "Marcel";
$responsibleOrigUser->email = "[email protected]";
$responsibleOrigUser->password = "12345";
$responsibleOrigUser->last_login = $responsibleOrigUser->freshTimestamp();
$responsibleOrigUser->save();

auth()->login($responsibleOrigUser);

// Needed because otherwise timestamps are exactly the same
sleep(1);

Config::shouldReceive('get')
->once()
->with("auth.providers.users.model")
->andReturn('TestVersionableUser');

$user = new TestVersionableUser();
$user->name = "John";
$user->email = "[email protected]";
Expand All @@ -182,9 +143,6 @@ public function testGetResponsibleUserAttribute()

public function testDontVersionEveryAttribute()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestPartialVersionableUser();
$user->name = "Marcel";
$user->email = "[email protected]";
Expand All @@ -201,9 +159,6 @@ public function testDontVersionEveryAttribute()

public function testVersionEveryAttribute()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableUser();
$user->name = "Marcel";
$user->email = "[email protected]";
Expand All @@ -219,9 +174,6 @@ public function testVersionEveryAttribute()

public function testCheckForVersioningEnabled()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableUser();
$user->disableVersioning();

Expand All @@ -246,9 +198,6 @@ public function testCheckForVersioningEnabled()

public function testCheckForVersioningEnabledLaterOn()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableUser();

$user->name = "Marcel";
Expand All @@ -266,9 +215,6 @@ public function testCheckForVersioningEnabledLaterOn()

public function testCanRevertVersion()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableUser();

$user->name = "Marcel";
Expand All @@ -294,9 +240,6 @@ public function testCanRevertVersion()

public function testCanRevertSoftDeleteVersion()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableSoftDeleteUser();

$user->name = "Marcel";
Expand All @@ -323,9 +266,6 @@ public function testCanRevertSoftDeleteVersion()

public function testGetVersionModel()
{
Auth::shouldReceive('check')
->andReturn( false );

// Create 3 versions
$user = new TestVersionableUser();
$user->name = "Marcel";
Expand All @@ -351,9 +291,6 @@ public function testGetVersionModel()

public function testUseReasonAttribute()
{
Auth::shouldReceive('check')
->andReturn( false );

// Create 3 versions
$user = new TestVersionableUser();
$user->name = "Marcel";
Expand All @@ -368,9 +305,6 @@ public function testUseReasonAttribute()

public function testIgnoreDeleteTimestamp()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableSoftDeleteUser();
$user->name = "Marcel";
$user->email = "[email protected]";
Expand All @@ -391,8 +325,6 @@ public function testIgnoreDeleteTimestamp()

public function testDiffTwoVersions()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableUser();
$user->name = "Marcel";
Expand All @@ -414,9 +346,6 @@ public function testDiffTwoVersions()

public function testDiffIgnoresTimestamps()
{
Auth::shouldReceive('check')
->andReturn( false );

$user = new TestVersionableSoftDeleteUser();
$user->name = "Marcel";
$user->email = "[email protected]";
Expand All @@ -440,9 +369,6 @@ public function testDiffIgnoresTimestamps()

public function testDiffSpecificVersions()
{
Auth::shouldReceive('check')
->andReturn( false );

// Create 3 versions
$user = new TestVersionableSoftDeleteUser();
$user->name = "Marcel";
Expand Down Expand Up @@ -477,24 +403,21 @@ public function testDiffSpecificVersions()

public function testDynamicVersionModel()
{
Auth::shouldReceive('check')
->andReturn( false );

$name_v1 = 'first' ;
$name_v2 = 'second' ;

$model = new ModelWithDynamicVersion();
$model->name = $name_v1 ;
$model->save();

sleep(1);

$model->name = $name_v2 ;
$model->save();

// Assert that no row in default Version table
$this->assertEquals( 0, Version::all()->count() );

// But are in Custom version table
$this->assertEquals( 2, DynamicVersionModel::all()->count() );

Expand All @@ -509,11 +432,34 @@ public function testDynamicVersionModel()
$this->assertEquals( $name_v1, $model->name );
}

public function testKeepMaxVersionCount()
public function testItUsesConfigurableVersionClass()
{
Auth::shouldReceive('check')
->andReturn( false );
$this->app['config']->set('versionable.version_model', DynamicVersionModel::class);


$name_v1 = 'first' ;
$name_v2 = 'second' ;

$model = new TestVersionableUser();
$model->name = $name_v1 ;
$model->email = $name_v1 ;
$model->password = $name_v1 ;
$model->save();

sleep(1);

$model->name = $name_v2 ;
$model->save();

// Assert that no row in default Version table
$this->assertCount(0, Version::all());

// But are in Custom version table
$this->assertCount(2, DynamicVersionModel::all());
}

public function testKeepMaxVersionCount()
{
$name_v1 = 'first' ;
$name_v2 = 'second' ;
$name_v3 = 'third' ;
Expand Down Expand Up @@ -558,7 +504,7 @@ public function testKeepMaxVersionCount()



class TestVersionableUser extends Illuminate\Database\Eloquent\Model {
class TestVersionableUser extends \Illuminate\Foundation\Auth\User {
use \Mpociot\Versionable\VersionableTrait;

protected $table = "users";
Expand Down
Loading

0 comments on commit 9abd756

Please sign in to comment.