Skip to content

Commit 29eebf5

Browse files
new testcases
1 parent 0c9a574 commit 29eebf5

File tree

6 files changed

+112
-5
lines changed

6 files changed

+112
-5
lines changed

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
Store settings in your Laravel application.
88

9-
## Caution
10-
11-
This application is still in development and could implement breaking changes. Please use at your own risk.
12-
139
## Installation
1410

1511
You can install the package with composer
@@ -49,7 +45,7 @@ return [
4945
];
5046
```
5147

52-
If you chose database as your driver, you should create the migration file and execute it
48+
If you chose the default database driver, you should publish the migration file and execute it
5349

5450
```sh
5551
php artisan vendor:publish --tag="laravel-settings-migration"
@@ -122,6 +118,7 @@ settings()->delete('site_name');
122118
```
123119

124120
## User settings
121+
125122
In addition to default settings, you can also use this package to store user settings.
126123
Add the `HasSettings` trait to your User model
127124

@@ -148,12 +145,25 @@ settings()->forUser(1)->get('preferred_language');
148145
```
149146

150147
## Language specific settings
148+
151149
It's possible to store / access settings for specific locales.
152150
```php
153151
settings()->forLocale('en')->set('website', 'my-personal-blog.com/en');
154152
```
155153

156154
## Note on how this works
155+
156+
### Caching
157+
158+
Whenever a read operation is executed, the package will check if the value is present in the cache.
159+
If it's not found, a read operation will be performed to the database (all values are fetched) and stored in the cache for other calls to consume.
160+
161+
This means that performing multiple reads (even with multiple keys) will only perform one database call.
162+
163+
If a write operation is performed, the cache is cleared. Resulting in a new read from database when a new settings is queried.
164+
165+
### User and locale settings
166+
157167
The default driver always stores a `locale` and `user_id` field in the database (defaults to `null`).
158168
Fetching data from the database will also query on these parameters.
159169

src/Concerns/HasSettings.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ public function settings()
88
{
99
return settings()->forUser($this->id);
1010
}
11+
12+
public function getSettingsAttribute()
13+
{
14+
return settings()->forUser($this->id);
15+
}
1116
}

tests/Feature/DatabaseSettingsTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Justijndepover\Settings\Tests;
44

55
use Illuminate\Support\Facades\DB;
6+
use Justijndepover\Settings\Tests\Models\User;
67

78
class DatabaseSettingsTest extends TestCase
89
{
@@ -233,4 +234,35 @@ public function it_can_store_locale_settings_and_normal_settings()
233234
$setting = DB::table('settings')->where('key', '=', 'name')->where('locale', '=', null)->first();
234235
$this->assertEquals($setting->value, 'value');
235236
}
237+
238+
/** @test */
239+
public function it_can_store_different_locale_settings()
240+
{
241+
$this->settings->forLocale('nl')->set('name', 'value');
242+
$this->settings->forLocale('fr')->set('name', 'value-fr');
243+
244+
$setting = DB::table('settings')->where('key', '=', 'name')->where('locale', '=', 'nl')->first();
245+
$this->assertEquals($setting->value, 'value');
246+
247+
$setting = DB::table('settings')->where('key', '=', 'name')->where('locale', '=', 'fr')->first();
248+
$this->assertEquals($setting->value, 'value-fr');
249+
}
250+
251+
/** @test */
252+
public function it_has_a_working_user_setting_trait()
253+
{
254+
$this->settings->forUser(1)->set('name', 'value');
255+
256+
$setting = DB::table('settings')->where('key', '=', 'name')->where('user_id', '=', 1)->first();
257+
$this->assertEquals($setting->value, 'value');
258+
259+
$user = User::create([
260+
'name' => 'Foo Bar',
261+
'email' => 'foo@bar.com',
262+
'password' => 'foobar',
263+
]);
264+
265+
$this->assertEquals($user->settings->get('name'), 'value');
266+
$this->assertEquals($user->settings()->get('name'), 'value');
267+
}
236268
}

tests/Models/User.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Justijndepover\Settings\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Foundation\Auth\User as Authenticatable;
7+
use Illuminate\Notifications\Notifiable;
8+
use Justijndepover\Settings\Concerns\HasSettings;
9+
10+
class User extends Authenticatable
11+
{
12+
use HasFactory, Notifiable, HasSettings;
13+
14+
protected $fillable = ['name', 'email', 'password'];
15+
protected $hidden = ['password', 'remember_token'];
16+
17+
protected $casts = [
18+
'email_verified_at' => 'datetime',
19+
];
20+
}
21+

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,8 @@ public function getEnvironmentSetUp($app)
4141

4242
include_once __DIR__.'/../database/migrations/create_settings_table.php';
4343
(new \CreateSettingsTable())->up();
44+
45+
include_once __DIR__.'/migrations/2014_10_12_000000_create_users_table.php';
46+
(new \CreateUsersTable())->up();
4447
}
4548
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateUsersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('users', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name');
19+
$table->string('email')->unique();
20+
$table->timestamp('email_verified_at')->nullable();
21+
$table->string('password');
22+
$table->rememberToken();
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('users');
35+
}
36+
}

0 commit comments

Comments
 (0)