Skip to content

Commit

Permalink
[FEAT] test it can set new order without global scope
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanBl committed Dec 7, 2023
1 parent 027cf37 commit 2117e30
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/DummyWithGlobalScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Spatie\EloquentSortable\Test;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class DummyWithGlobalScope extends Model implements Sortable
{
use SortableTrait;

protected $table = 'dummies';
protected $guarded = [];
public $timestamps = false;

/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();

parent::addGlobalScope('ActiveScope', function(Builder $builder) {
$builder->where('is_active', true);
});
}
}
14 changes: 14 additions & 0 deletions tests/SortableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ public function it_can_set_a_new_order_by_custom_column_from_collection()
}
}

/** @test */
public function it_can_set_new_order_without_global_scopes_models()
{
$this->setUpIsActiveFieldForGlobalScope();

$newOrder = Collection::make(Dummy::all()->pluck('id'))->shuffle()->toArray();

DummyWithGlobalScope::setNewOrder($newOrder, 1, null, ['ActiveScope']);

foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) {
$this->assertEquals($newOrder[$i], $dummy->id);
}
}

/** @test */
public function it_can_set_a_new_order_with_trashed_models()
{
Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ protected function setUpSoftDeletes()
$table->softDeletes();
});
}

protected function setUpIsActiveFieldForGlobalScope()
{
$this->app['db']->connection()->getSchemaBuilder()->table('dummies', function (Blueprint $table) {
$table->boolean('is_active')->default(false);
});
}
}

0 comments on commit 2117e30

Please sign in to comment.