This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 48
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 DarkGhostHunter/master
Better filtering scopes, auto-delete
- Loading branch information
Showing
7 changed files
with
203 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace DarkGhostHunter\Laraconfig\Eloquent\Scopes; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Scope; | ||
|
||
class FilterBags implements Scope | ||
{ | ||
/** | ||
* FilterBags constructor. | ||
* | ||
* @param array $bags | ||
*/ | ||
public function __construct(protected array $bags) | ||
{ | ||
} | ||
|
||
/** | ||
* Apply the scope to a given Eloquent query builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* | ||
* @return void | ||
*/ | ||
public function apply(Builder $builder, Model $model): void | ||
{ | ||
$builder->whereHas('metadata', function (Builder $query): void { | ||
$query->whereIn('bag', $this->bags); | ||
}); | ||
} | ||
} |
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
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
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace Tests; | ||
|
||
use DarkGhostHunter\Laraconfig\Eloquent\Metadata; | ||
use DarkGhostHunter\Laraconfig\Eloquent\Scopes\FilterBags; | ||
use DarkGhostHunter\Laraconfig\Eloquent\Setting; | ||
use DarkGhostHunter\Laraconfig\HasConfig; | ||
use DarkGhostHunter\Laraconfig\SettingsCollection; | ||
|
@@ -12,8 +13,11 @@ | |
use Illuminate\Contracts\Cache\Repository; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\HigherOrderCollectionProxy; | ||
use Mockery; | ||
use RuntimeException; | ||
|
@@ -855,4 +859,106 @@ public function test_get_allows_pass_to_higher_order_proxy(): void | |
|
||
static::assertInstanceOf(HigherOrderCollectionProxy::class, $user->settings->map); | ||
} | ||
|
||
public function test_deletes_settings_when_model_deletes_itself(): void | ||
{ | ||
DummyModel::find(1)->delete(); | ||
|
||
$this->assertDatabaseMissing('user_settings', ['settable_id' => 1]); | ||
} | ||
|
||
public function test_deletes_settings_when_model_force_deletes_itself(): void | ||
{ | ||
Metadata::forceCreate([ | ||
'name' => 'bar', | ||
'type' => 'string', | ||
'default' => 'quz', | ||
'bag' => 'test-users', | ||
'group' => 'default', | ||
]); | ||
|
||
Schema::table('users', function (Blueprint $table) { | ||
$table->softDeletes(); | ||
}); | ||
|
||
$user = new class extends Model { | ||
use SoftDeletes; | ||
use HasConfig; | ||
protected $table = 'users'; | ||
protected $attributes = [ | ||
'name' => 'john', | ||
'email' => '[email protected]', | ||
'password' => '123456' | ||
]; | ||
}; | ||
|
||
$user->save(); | ||
|
||
$this->assertDatabaseHas('user_settings', ['settable_id' => 2]); | ||
|
||
$user->delete(); | ||
|
||
$this->assertDatabaseHas('user_settings', ['settable_id' => 2]); | ||
|
||
$user->restore(); | ||
|
||
$this->assertDatabaseHas('user_settings', ['settable_id' => 2]); | ||
|
||
$user->forceDelete(); | ||
|
||
$this->assertDatabaseMissing('user_settings', ['settable_id' => 2]); | ||
} | ||
|
||
public function test_allows_for_removing_bags_filter_on_query(): void | ||
{ | ||
Setting::forceCreate([ | ||
'value' => 'quz', | ||
'settable_id' => 1, | ||
'settable_type' => (new DummyModel())->getMorphClass(), | ||
'metadata_id' => Metadata::forceCreate([ | ||
'name' => 'bar', | ||
'type' => 'string', | ||
'default' => 'quz', | ||
'bag' => 'test-users', | ||
'group' => 'default', | ||
])->getKey() | ||
]); | ||
|
||
$user = DummyModel::find(1); | ||
|
||
$settings = $user->settings()->withoutGlobalScope(FilterBags::class)->get(); | ||
|
||
static::assertCount(2, $settings); | ||
} | ||
|
||
public function test_allows_to_disable_bag_filter(): void | ||
{ | ||
Metadata::forceCreate([ | ||
'name' => 'bar', | ||
'type' => 'string', | ||
'default' => 'quz', | ||
'bag' => 'test-users', | ||
'group' => 'default', | ||
]); | ||
|
||
$user = new class extends Model { | ||
use SoftDeletes; | ||
use HasConfig; | ||
protected $table = 'users'; | ||
protected $attributes = [ | ||
'name' => 'john', | ||
'email' => '[email protected]', | ||
'password' => '123456' | ||
]; | ||
public function filterBags() { | ||
return []; | ||
} | ||
}; | ||
|
||
$user->save(); | ||
|
||
$settings = $user->settings()->get(); | ||
|
||
static::assertCount(2, $settings); | ||
} | ||
} |