Skip to content

Commit

Permalink
Check settings (exist) (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
glorand authored Apr 20, 2020
1 parent e5451d5 commit 08e0def
Show file tree
Hide file tree
Showing 13 changed files with 390 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `glorand/laravel-model-settings` will be documented in this file

## 3.6.2 - 2020-04-20
### Fix
- "exist()" - to check if the model has valid setting

## 3.6.1 - 2020-03-23
### Fix
- https://github.com/glorand/laravel-model-settings/issues/50
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Bug reports, feature requests, and pull requests can be submitted by following o
- [Option 3 - `HasSettingsRedis` trait](#update_models_3)
- [Default Settings](#default_settings)
- [Usage](#usage)
- [Check settings (exist)](#exist)
- [Get all model's settings](#get_all)
- [Get a specific setting](#get)
- [Add / Update setting](#add_update)
Expand Down Expand Up @@ -149,6 +150,11 @@ class User extends Model
$user = App\User::first();
```

#### Check settings (exist) <a name="exist"></a>
```php
$user->settings()->exist();
```

#### Get all model's settings <a name="get_all"></a>
```php
$user->settings()->all();
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/SettingsManagerContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public function __construct(Model $model);

public function all(): array;

public function exist(): bool;

public function apply(array $settings = []): self;

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Managers/AbstractSettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public function all(): array
return $array;
}

public function exist(): bool
{
$all = $this->all();

return count($all) > 0;
}

/**
* @param string $path
* @return bool
Expand Down
4 changes: 3 additions & 1 deletion src/Traits/HasSettingsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public function getSettingsValue(): array
throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}");
}

return json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true);
$value = json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true);

return is_array($value) ? $value : [];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/HasSettingsRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Glorand\Model\Settings\Contracts\SettingsManagerContract;
use Glorand\Model\Settings\Managers\RedisSettingsManager;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Redis;

/**
Expand All @@ -28,8 +27,9 @@ public function settings(): SettingsManagerContract
public function getSettingsValue(): array
{
$redisValue = Redis::connection()->get($this->cacheKey());
$value = json_decode($redisValue, true);

return Arr::wrap(json_decode($redisValue, true));
return is_array($value) ? $value : [];
}

public function cacheKey(string $key = null): string
Expand Down
59 changes: 38 additions & 21 deletions tests/FieldSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,23 @@ public function setUp(): void
public function testInit()
{
$traits = class_uses($this->model);
$this->assertTrue(array_key_exists(HasSettingsField::class, $traits));
$this->assertArrayHasKey(HasSettingsField::class, $traits);
}


/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testExist()
{
$this->assertFalse($this->model->settings()->exist());
$this->model->settings()->apply($this->testArray);
$this->assertTrue($this->model->settings()->exist());
}

/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testModelArraySettings()
{
$testArray = ['a' => 'b'];
Expand Down Expand Up @@ -125,29 +139,32 @@ public function testGetMultiple()
}

/**
* @throws \Exception
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testApply()
{
$this->model->settings()->apply($this->testArray);
$this->assertEquals($this->model->fresh()->settings()->all(), $this->testArray);
}

/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testPersistence()
{
$this->model->settings()->apply($this->testArray);
$this->assertEquals($this->model->fresh()->settings()->all(), $this->testArray);
$this->assertEquals($this->testArray, $this->model->fresh()->settings()->all());

$this->model->settings()->delete();

$this->model->setPersistSettings(false);
$this->model->settings()->apply($this->testArray);
$this->assertEquals($this->model->fresh()->settings()->all(), []);
$this->assertEquals([], $this->model->fresh()->settings()->all());

$this->model->setPersistSettings(false);
$this->model->settings()->apply($this->testArray);
$this->model->save();
$this->assertEquals($this->model->fresh()->settings()->all(), $this->testArray);
$this->assertEquals($this->testArray, $this->model->fresh()->settings()->all());

$this->model->settings()->delete();

Expand All @@ -164,14 +181,14 @@ public function testDelete()
{
$this->model->settings()->apply($this->testArray);

$this->assertEquals($this->model->settings()->all(), $this->testArray);
$this->assertEquals($this->model->settings()->get('user.first_name'), 'John');
$this->assertEquals($this->testArray, $this->model->settings()->all());
$this->assertEquals('John', $this->model->settings()->get('user.first_name'));

$this->model->settings()->delete('user.first_name');
$this->assertEquals($this->model->settings()->get('user.first_name'), null);
$this->assertEquals(null, $this->model->settings()->get('user.first_name'));

$this->model->settings()->delete();
$this->assertEquals($this->model->settings()->all(), []);
$this->assertEquals([], $this->model->settings()->all());
}

/**
Expand All @@ -195,51 +212,51 @@ public function testDeleteMultiple()
public function testClear()
{
$this->model->settings()->apply($this->testArray);
$this->assertEquals($this->model->settings()->all(), $this->testArray);
$this->assertEquals($this->testArray, $this->model->settings()->all());

$this->model->settings()->clear();
$this->assertEquals($this->model->settings()->all(), []);
$this->assertEquals([], $this->model->settings()->all());
}

/**
* @throws \Exception
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testSet()
{
$this->assertEquals($this->model->settings()->all(), []);
$this->assertEquals([], $this->model->settings()->all());

$this->model->settings()->set('user.age', 18);
$this->assertEquals($this->model->settings()->all(), ['user' => ['age' => 18]]);
$this->assertEquals(['user' => ['age' => 18]], $this->model->settings()->all());
}

/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testSetMultiple()
{
$this->assertEquals($this->model->settings()->all(), []);
$this->assertEquals([], $this->model->settings()->all());
$testData = [
'a' => 'a',
'b' => 'b',
];
$this->model->settings()->setMultiple($testData);
$this->assertEquals($this->model->settings()->all(), $testData);
$this->assertEquals($testData, $this->model->settings()->all());

$this->model->settings()->setMultiple($this->testArray);
$this->assertEquals($this->model->settings()->all(), array_merge($testData, $this->testArray));
$this->assertEquals(array_merge($testData, $this->testArray), $this->model->settings()->all());
}

/**
* @throws \Exception
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testUpdate()
{
$this->assertEquals($this->model->settings()->all(), []);
$this->assertEquals([], $this->model->settings()->all());

$this->model->settings()->set('user.age', 18);
$this->assertEquals($this->model->settings()->all(), ['user' => ['age' => 18]]);
$this->assertEquals(['user' => ['age' => 18]], $this->model->settings()->all());

$this->model->settings()->update('user.age', 19);
$this->assertEquals($this->model->settings()->all(), ['user' => ['age' => 19]]);
$this->assertEquals(['user' => ['age' => 19]], $this->model->settings()->all());
}
}
21 changes: 21 additions & 0 deletions tests/Models/UserWithTextField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Glorand\Model\Settings\Tests\Models;

use Glorand\Model\Settings\Traits\HasSettingsField;
use Illuminate\Database\Eloquent\Model;

class UserWithTextField extends Model
{
use HasSettingsField;

//protected $persistSettings = true;

protected $table = 'users_with_text_field';

protected $guarded = [];

protected $fillable = ['id', 'name'];

public $defaultSettings = [];
}
3 changes: 2 additions & 1 deletion tests/Models/UsersWithTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Glorand\Model\Settings\Tests\Models;

use Closure;
use Glorand\Model\Settings\Traits\HasSettingsTable;
use Illuminate\Database\Eloquent\Model;

/**
* @method static whereHas(string $string, \Closure $param)
* @method static whereHas(string $string, Closure $param)
*/
class UsersWithTable extends Model
{
Expand Down
12 changes: 12 additions & 0 deletions tests/RedisSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,23 @@ public function testInit()
$this->assertInstanceOf(MockPredisConnection::class, Redis::connection());
}


/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testExist()
{
$this->assertFalse($this->model->settings()->exist());
$this->model->settings()->apply($this->testArray);
$this->assertTrue($this->model->settings()->exist());
}

/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testAll()
{
$this->model->settings()->clear();
$this->assertEquals($this->model->settings()->all(), []);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/TableSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public function testInit()
$this->assertTrue(array_key_exists(HasSettingsTable::class, $traits));
}


/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testExist()
{
$this->assertFalse($this->model->settings()->exist());
$this->model->settings()->apply($this->testArray);
$this->assertTrue($this->model->settings()->exist());
}

/**
* @throws \Exception
*/
Expand Down
9 changes: 7 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Glorand\Model\Settings\Tests\Models\UsersWithTable;
use Glorand\Model\Settings\Tests\Models\UserWithField;
use Glorand\Model\Settings\Tests\Models\UserWithRedis;
use Glorand\Model\Settings\Tests\Models\UserWithTextField;
use Glorand\Model\Settings\Tests\Models\WrongUser;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -54,8 +55,8 @@ protected function setUpDatabase()
{
$this->createSettingsTable();

$this->createTables('users', 'users_with_table', 'users_with_field', 'wrong_users');
$this->seedModels(UserWithField::class, UsersWithTable::class, WrongUser::class, UserWithRedis::class);
$this->createTables('users', 'users_with_table', 'users_with_field', 'users_with_text_field', 'wrong_users');
$this->seedModels(UserWithField::class, UserWithTextField::class, UsersWithTable::class, WrongUser::class, UserWithRedis::class);
}

protected function createSettingsTable()
Expand All @@ -76,6 +77,10 @@ protected function createTables(...$tableNames)
if ('users_with_field' === $tableName) {
$table->json('settings')->nullable();
}

if ('users_with_text_field' === $tableName) {
$table->text('settings')->nullable();
}
});
});
}
Expand Down
Loading

0 comments on commit 08e0def

Please sign in to comment.