diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c2c94d..066fa5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index a3161c6..ea57887 100644 --- a/README.md +++ b/README.md @@ -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) @@ -149,6 +150,11 @@ class User extends Model $user = App\User::first(); ``` +#### Check settings (exist) +```php +$user->settings()->exist(); +``` + #### Get all model's settings ```php $user->settings()->all(); diff --git a/src/Contracts/SettingsManagerContract.php b/src/Contracts/SettingsManagerContract.php index 933fc68..2624743 100644 --- a/src/Contracts/SettingsManagerContract.php +++ b/src/Contracts/SettingsManagerContract.php @@ -10,6 +10,8 @@ public function __construct(Model $model); public function all(): array; + public function exist(): bool; + public function apply(array $settings = []): self; /** diff --git a/src/Managers/AbstractSettingsManager.php b/src/Managers/AbstractSettingsManager.php index c4bf455..254eab8 100644 --- a/src/Managers/AbstractSettingsManager.php +++ b/src/Managers/AbstractSettingsManager.php @@ -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 diff --git a/src/Traits/HasSettingsField.php b/src/Traits/HasSettingsField.php index a83b313..76e1fc1 100644 --- a/src/Traits/HasSettingsField.php +++ b/src/Traits/HasSettingsField.php @@ -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 : []; } /** diff --git a/src/Traits/HasSettingsRedis.php b/src/Traits/HasSettingsRedis.php index 64403f0..aa4fb20 100644 --- a/src/Traits/HasSettingsRedis.php +++ b/src/Traits/HasSettingsRedis.php @@ -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; /** @@ -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 diff --git a/tests/FieldSettingsManagerTest.php b/tests/FieldSettingsManagerTest.php index f5a2767..22c11c5 100644 --- a/tests/FieldSettingsManagerTest.php +++ b/tests/FieldSettingsManagerTest.php @@ -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']; @@ -125,7 +139,7 @@ public function testGetMultiple() } /** - * @throws \Exception + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException */ public function testApply() { @@ -133,21 +147,24 @@ public function testApply() $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(); @@ -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()); } /** @@ -195,21 +212,21 @@ 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()); } /** @@ -217,29 +234,29 @@ public function testSet() */ 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()); } } diff --git a/tests/Models/UserWithTextField.php b/tests/Models/UserWithTextField.php new file mode 100644 index 0000000..52e53e7 --- /dev/null +++ b/tests/Models/UserWithTextField.php @@ -0,0 +1,21 @@ +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(), []); } diff --git a/tests/TableSettingsManagerTest.php b/tests/TableSettingsManagerTest.php index cef5efe..cd62ccd 100644 --- a/tests/TableSettingsManagerTest.php +++ b/tests/TableSettingsManagerTest.php @@ -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 */ diff --git a/tests/TestCase.php b/tests/TestCase.php index 1bb7e97..19259a0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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; @@ -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() @@ -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(); + } }); }); } diff --git a/tests/TextFieldSettingsManagerTest.php b/tests/TextFieldSettingsManagerTest.php new file mode 100644 index 0000000..f1f1569 --- /dev/null +++ b/tests/TextFieldSettingsManagerTest.php @@ -0,0 +1,275 @@ + [ + 'first_name' => "John", + 'last_name' => "Doe", + 'email' => "john@doe.com", + ], + ]; + /** @var array */ + protected $defaultSettingsTestArray = [ + 'project' => 'Main Project', + ]; + + public function setUp(): void + { + parent::setUp(); + $this->model = User::first(); + } + + public function testInit() + { + $traits = class_uses($this->model); + $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 testIfSettingsIsNotValidJson() + { + $this->model->settings = 'Invalid Json'; + $this->model->save(); + + $this->assertEquals([], $this->model->settings()->all()); + } + + /** + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + public function testModelArraySettings() + { + $testArray = ['a' => 'b']; + $this->model->settings = $testArray; + $this->model->save(); + $this->assertEquals($testArray, $this->model->settings()->all()); + } + + /** + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + public function testAll() + { + $this->assertEquals([], $this->model->settings()->all()); + } + + /** + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + public function testDefaultValue() + { + $this->model->defaultSettings = $this->defaultSettingsTestArray; + $this->assertEquals($this->defaultSettingsTestArray, $this->model->settings()->all()); + + $this->model->settings()->apply($this->testArray); + $this->assertEquals( + array_merge($this->defaultSettingsTestArray, $this->testArray), + $this->model->settings()->all() + ); + } + + public function testSettingsMissingSettingsField() + { + $this->expectException(ModelSettingsException::class); + $this->expectExceptionMessage('Unknown field'); + $this->model->settingsFieldName = 'test'; + $this->model->settings()->all(); + } + + /** + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + public function testHas() + { + $this->model->settings()->apply($this->testArray); + $this->assertEquals($this->testArray, $this->model->settings()->all()); + + $this->assertTrue($this->model->settings()->has('user.first_name')); + $this->assertFalse($this->model->settings()->has('user.age')); + } + + /** + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + public function testGet() + { + $this->assertEquals([], $this->model->settings()->all()); + $this->assertEquals(null, $this->model->settings()->get('user')); + $this->model->settings()->apply($this->testArray); + $this->assertEquals('John', $this->model->settings()->get('user.first_name')); + } + + /** + * @throws \Exception + */ + public function testGetMultiple() + { + $this->assertEquals([], $this->model->settings()->all()); + $values = $this->model->settings()->getMultiple(['user.first_name', 'user.last_name'], 'def_val'); + $this->assertEquals( + [ + 'user.first_name' => 'def_val', + 'user.last_name' => 'def_val', + ], + $values + ); + + $this->model->settings()->apply($this->testArray); + $values = $this->model->settings()->getMultiple( + ['user.first_name', 'user.last_name', 'user.middle_name'], + 'def_val' + ); + $this->assertEquals( + [ + 'user.first_name' => 'John', + 'user.last_name' => 'Doe', + 'user.middle_name' => 'def_val', + ], + $values + ); + } + + /** + * @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->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->model->setPersistSettings(false); + $this->model->settings()->apply($this->testArray); + $this->model->save(); + $this->assertEquals($this->testArray, $this->model->fresh()->settings()->all()); + + $this->model->settings()->delete(); + + $this->model->fresh(); + $this->model->setPersistSettings(true); + $this->model->settings()->apply($this->testArray); + $this->assertEquals($this->testArray, $this->model->fresh()->settings()->all()); + } + + /** + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + public function testDelete() + { + $this->model->settings()->apply($this->testArray); + + $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(null, $this->model->settings()->get('user.first_name')); + + $this->model->settings()->delete(); + $this->assertEquals([], $this->model->settings()->all()); + } + + /** + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + public function testDeleteMultiple() + { + $this->model->settings()->apply($this->testArray); + $this->assertEquals($this->model->settings()->all(), $this->testArray); + + $this->model->settings()->deleteMultiple(['user.first_name', 'user.last_name']); + $testData = $this->model->settings()->get('user'); + $this->assertArrayNotHasKey('first_name', $testData); + $this->assertArrayNotHasKey('last_name', $testData); + $this->assertArrayHasKey('email', $testData); + } + + /** + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + public function testClear() + { + $this->model->settings()->apply($this->testArray); + $this->assertEquals($this->testArray, $this->model->settings()->all()); + + $this->model->settings()->clear(); + $this->assertEquals([], $this->model->settings()->all()); + } + + /** + * @throws \Exception + */ + public function testSet() + { + $this->assertEquals([], $this->model->settings()->all()); + + $this->model->settings()->set('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()); + $testData = [ + 'a' => 'a', + 'b' => 'b', + ]; + $this->model->settings()->setMultiple($testData); + $this->assertEquals($testData, $this->model->settings()->all()); + + $this->model->settings()->setMultiple($this->testArray); + $this->assertEquals(array_merge($testData, $this->testArray), $this->model->settings()->all()); + } + + /** + * @throws \Exception + */ + public function testUpdate() + { + $this->assertEquals([], $this->model->settings()->all()); + + $this->model->settings()->set('user.age', 18); + $this->assertEquals(['user' => ['age' => 18]], $this->model->settings()->all()); + + $this->model->settings()->update('user.age', 19); + $this->assertEquals(['user' => ['age' => 19]], $this->model->settings()->all()); + } +}