|
3 | 3 | namespace Tests; |
4 | 4 |
|
5 | 5 | use DarkGhostHunter\Laraconfig\Eloquent\Metadata; |
| 6 | +use DarkGhostHunter\Laraconfig\Eloquent\Scopes\FilterBags; |
6 | 7 | use DarkGhostHunter\Laraconfig\Eloquent\Setting; |
7 | 8 | use DarkGhostHunter\Laraconfig\HasConfig; |
8 | 9 | use DarkGhostHunter\Laraconfig\SettingsCollection; |
|
12 | 13 | use Illuminate\Contracts\Cache\Repository; |
13 | 14 | use Illuminate\Database\Eloquent\Collection; |
14 | 15 | use Illuminate\Database\Eloquent\Model; |
| 16 | +use Illuminate\Database\Eloquent\SoftDeletes; |
| 17 | +use Illuminate\Database\Schema\Blueprint; |
15 | 18 | use Illuminate\Foundation\Testing\RefreshDatabase; |
16 | 19 | use Illuminate\Support\Facades\Cache; |
| 20 | +use Illuminate\Support\Facades\Schema; |
17 | 21 | use Illuminate\Support\HigherOrderCollectionProxy; |
18 | 22 | use Mockery; |
19 | 23 | use RuntimeException; |
@@ -855,4 +859,106 @@ public function test_get_allows_pass_to_higher_order_proxy(): void |
855 | 859 |
|
856 | 860 | static::assertInstanceOf(HigherOrderCollectionProxy::class, $user->settings->map); |
857 | 861 | } |
| 862 | + |
| 863 | + public function test_deletes_settings_when_model_deletes_itself(): void |
| 864 | + { |
| 865 | + DummyModel::find(1)->delete(); |
| 866 | + |
| 867 | + $this->assertDatabaseMissing('user_settings', ['settable_id' => 1]); |
| 868 | + } |
| 869 | + |
| 870 | + public function test_deletes_settings_when_model_force_deletes_itself(): void |
| 871 | + { |
| 872 | + Metadata::forceCreate([ |
| 873 | + 'name' => 'bar', |
| 874 | + 'type' => 'string', |
| 875 | + 'default' => 'quz', |
| 876 | + 'bag' => 'test-users', |
| 877 | + 'group' => 'default', |
| 878 | + ]); |
| 879 | + |
| 880 | + Schema::table('users', function (Blueprint $table) { |
| 881 | + $table->softDeletes(); |
| 882 | + }); |
| 883 | + |
| 884 | + $user = new class extends Model { |
| 885 | + use SoftDeletes; |
| 886 | + use HasConfig; |
| 887 | + protected $table = 'users'; |
| 888 | + protected $attributes = [ |
| 889 | + 'name' => 'john', |
| 890 | + 'email' => 'email@email.com', |
| 891 | + 'password' => '123456' |
| 892 | + ]; |
| 893 | + }; |
| 894 | + |
| 895 | + $user->save(); |
| 896 | + |
| 897 | + $this->assertDatabaseHas('user_settings', ['settable_id' => 2]); |
| 898 | + |
| 899 | + $user->delete(); |
| 900 | + |
| 901 | + $this->assertDatabaseHas('user_settings', ['settable_id' => 2]); |
| 902 | + |
| 903 | + $user->restore(); |
| 904 | + |
| 905 | + $this->assertDatabaseHas('user_settings', ['settable_id' => 2]); |
| 906 | + |
| 907 | + $user->forceDelete(); |
| 908 | + |
| 909 | + $this->assertDatabaseMissing('user_settings', ['settable_id' => 2]); |
| 910 | + } |
| 911 | + |
| 912 | + public function test_allows_for_removing_bags_filter_on_query(): void |
| 913 | + { |
| 914 | + Setting::forceCreate([ |
| 915 | + 'value' => 'quz', |
| 916 | + 'settable_id' => 1, |
| 917 | + 'settable_type' => (new DummyModel())->getMorphClass(), |
| 918 | + 'metadata_id' => Metadata::forceCreate([ |
| 919 | + 'name' => 'bar', |
| 920 | + 'type' => 'string', |
| 921 | + 'default' => 'quz', |
| 922 | + 'bag' => 'test-users', |
| 923 | + 'group' => 'default', |
| 924 | + ])->getKey() |
| 925 | + ]); |
| 926 | + |
| 927 | + $user = DummyModel::find(1); |
| 928 | + |
| 929 | + $settings = $user->settings()->withoutGlobalScope(FilterBags::class)->get(); |
| 930 | + |
| 931 | + static::assertCount(2, $settings); |
| 932 | + } |
| 933 | + |
| 934 | + public function test_allows_to_disable_bag_filter(): void |
| 935 | + { |
| 936 | + Metadata::forceCreate([ |
| 937 | + 'name' => 'bar', |
| 938 | + 'type' => 'string', |
| 939 | + 'default' => 'quz', |
| 940 | + 'bag' => 'test-users', |
| 941 | + 'group' => 'default', |
| 942 | + ]); |
| 943 | + |
| 944 | + $user = new class extends Model { |
| 945 | + use SoftDeletes; |
| 946 | + use HasConfig; |
| 947 | + protected $table = 'users'; |
| 948 | + protected $attributes = [ |
| 949 | + 'name' => 'john', |
| 950 | + 'email' => 'email@email.com', |
| 951 | + 'password' => '123456' |
| 952 | + ]; |
| 953 | + public function filterBags() { |
| 954 | + return []; |
| 955 | + } |
| 956 | + }; |
| 957 | + |
| 958 | + $user->save(); |
| 959 | + |
| 960 | + $settings = $user->settings()->get(); |
| 961 | + |
| 962 | + static::assertCount(2, $settings); |
| 963 | + } |
858 | 964 | } |
0 commit comments