Skip to content

Commit

Permalink
new testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
justijndepover committed Sep 29, 2021
1 parent 0c9a574 commit 29eebf5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

Store settings in your Laravel application.

## Caution

This application is still in development and could implement breaking changes. Please use at your own risk.

## Installation

You can install the package with composer
Expand Down Expand Up @@ -49,7 +45,7 @@ return [
];
```

If you chose database as your driver, you should create the migration file and execute it
If you chose the default database driver, you should publish the migration file and execute it

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

## User settings

In addition to default settings, you can also use this package to store user settings.
Add the `HasSettings` trait to your User model

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

## Language specific settings

It's possible to store / access settings for specific locales.
```php
settings()->forLocale('en')->set('website', 'my-personal-blog.com/en');
```

## Note on how this works

### Caching

Whenever a read operation is executed, the package will check if the value is present in the cache.
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.

This means that performing multiple reads (even with multiple keys) will only perform one database call.

If a write operation is performed, the cache is cleared. Resulting in a new read from database when a new settings is queried.

### User and locale settings

The default driver always stores a `locale` and `user_id` field in the database (defaults to `null`).
Fetching data from the database will also query on these parameters.

Expand Down
5 changes: 5 additions & 0 deletions src/Concerns/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public function settings()
{
return settings()->forUser($this->id);
}

public function getSettingsAttribute()
{
return settings()->forUser($this->id);
}
}
32 changes: 32 additions & 0 deletions tests/Feature/DatabaseSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Justijndepover\Settings\Tests;

use Illuminate\Support\Facades\DB;
use Justijndepover\Settings\Tests\Models\User;

class DatabaseSettingsTest extends TestCase
{
Expand Down Expand Up @@ -233,4 +234,35 @@ public function it_can_store_locale_settings_and_normal_settings()
$setting = DB::table('settings')->where('key', '=', 'name')->where('locale', '=', null)->first();
$this->assertEquals($setting->value, 'value');
}

/** @test */
public function it_can_store_different_locale_settings()
{
$this->settings->forLocale('nl')->set('name', 'value');
$this->settings->forLocale('fr')->set('name', 'value-fr');

$setting = DB::table('settings')->where('key', '=', 'name')->where('locale', '=', 'nl')->first();
$this->assertEquals($setting->value, 'value');

$setting = DB::table('settings')->where('key', '=', 'name')->where('locale', '=', 'fr')->first();
$this->assertEquals($setting->value, 'value-fr');
}

/** @test */
public function it_has_a_working_user_setting_trait()
{
$this->settings->forUser(1)->set('name', 'value');

$setting = DB::table('settings')->where('key', '=', 'name')->where('user_id', '=', 1)->first();
$this->assertEquals($setting->value, 'value');

$user = User::create([
'name' => 'Foo Bar',
'email' => '[email protected]',
'password' => 'foobar',
]);

$this->assertEquals($user->settings->get('name'), 'value');
$this->assertEquals($user->settings()->get('name'), 'value');
}
}
21 changes: 21 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Justijndepover\Settings\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Justijndepover\Settings\Concerns\HasSettings;

class User extends Authenticatable
{
use HasFactory, Notifiable, HasSettings;

protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];

protected $casts = [
'email_verified_at' => 'datetime',
];
}

3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ public function getEnvironmentSetUp($app)

include_once __DIR__.'/../database/migrations/create_settings_table.php';
(new \CreateSettingsTable())->up();

include_once __DIR__.'/migrations/2014_10_12_000000_create_users_table.php';
(new \CreateUsersTable())->up();
}
}
36 changes: 36 additions & 0 deletions tests/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}

0 comments on commit 29eebf5

Please sign in to comment.