generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ajGulati05/versionable
Versionable
- Loading branch information
Showing
11 changed files
with
198 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<?php | ||
|
||
use AjGulati\Concurrency\Tests\TestCase; | ||
use AjGulati05\LaravelConcurrencyControl\Tests\TestCase; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
||
uses(TestCase::class)->in(__DIR__); | ||
uses(TestCase::class, RefreshDatabase::class)->in('Feature', 'Unit'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace AjGulati05\LaravelConcurrencyControl\Tests; | ||
|
||
use AjGulati05\LaravelConcurrencyControl\Versioned; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class TestModel extends Model | ||
{ | ||
use Versioned; | ||
|
||
public $table = 'test_models'; | ||
|
||
protected $guarded = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace AjGulati05\LaravelConcurrencyControl\Tests; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class TestModelResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'name' => $this->name, | ||
|
||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace AjGulati05\LaravelConcurrencyControl\Tests; | ||
|
||
use AjGulati05\LaravelConcurrencyControl\Versioned; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\DB; | ||
use RuntimeException; | ||
|
||
beforeEach(function () { | ||
|
||
$this->model = new class extends \Illuminate\Database\Eloquent\Model | ||
{ | ||
use Versioned; | ||
|
||
protected $guarded = []; | ||
|
||
protected $table = 'test'; | ||
}; | ||
|
||
DB::statement('CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT, created_at DATETIME,updated_at DATETIME)'); | ||
}); | ||
|
||
it('boots versioned', function () { | ||
|
||
$this->model->fill(['name' => 'Test', 'updated_at' => Carbon::now()])->save(); | ||
$this->assertArrayHasKey('versionId', $this->model->toArray()); | ||
}); | ||
|
||
it('checks the version attribute as timestamp is equal', function () { | ||
|
||
$now = Carbon::now(); | ||
$this->model->fill(['name' => 'Test', 'updated_at' => $now])->save(); | ||
$this->assertEquals($now->timestamp, $this->model->versionId); | ||
}); | ||
|
||
it('Tests the getVersionedColumn function', function () { | ||
|
||
Config::set('concurrency.version_datetime', 'name'); | ||
$this->expectException(RuntimeException::class); | ||
$this->model->getVersionedColumn(); | ||
}); | ||
|
||
it('Tests the validateVersionedColumn function ', function () { | ||
|
||
$this->model2 = new class extends \Illuminate\Database\Eloquent\Model | ||
{ | ||
use Versioned; | ||
|
||
protected $guarded = []; | ||
|
||
protected $table = 'test'; | ||
|
||
public function publicValidateVersionedColumn($column) | ||
{ | ||
return $this->validateVersionedColumn($column); | ||
} | ||
}; | ||
|
||
$this->expectException(RuntimeException::class); | ||
$this->model2->publicValidateVersionedColumn('unknown_column'); | ||
|
||
}); | ||
|
||
it('Tests the update function ', function () { | ||
$now = Carbon::now(); | ||
$this->model->fill(['name' => 'Test', 'updated_at' => $now])->save(); | ||
|
||
$this->model->update(['name' => 'New Test', 'versionId' => $now->timestamp]); | ||
|
||
$this->assertEquals('New Test', $this->model->name); | ||
}); | ||
|
||
it('Tests the updateWithoutExpectingVersionId function ', function () { | ||
$this->model->fill(['name' => 'Test', 'updated_at' => Carbon::now()])->save(); | ||
|
||
$this->model->updateWithoutExpectingVersionId(['name' => 'New Test']); | ||
|
||
$this->assertEquals('New Test', $this->model->name); | ||
}); |