diff --git a/.gitignore b/.gitignore
index a36ab04..2c572f2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,7 +3,7 @@ vendor/
/.idea
.php_cs.cache
composer.lock
-
+/.phpunit.result.cache
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
-# composer.lock
\ No newline at end of file
+# composer.lock
diff --git a/.travis.yml b/.travis.yml
index e7df03d..8a92e27 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,6 +4,7 @@ php:
- 7.1
- 7.2
- 7.3
+ - 7.4
before_script:
- composer selfupdate
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 066fa5d..a54b7cd 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.3 - 2020-04-21
+### Fix
+- "empty()" - to check if the model has empty setting
+
## 3.6.2 - 2020-04-20
### Fix
- "exist()" - to check if the model has valid setting
diff --git a/README.md b/README.md
index ea57887..ed67b76 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 id the settings for the entity is empty (exist)](#empty)
- [Check settings (exist)](#exist)
- [Get all model's settings](#get_all)
- [Get a specific setting](#get)
@@ -150,6 +151,11 @@ class User extends Model
$user = App\User::first();
```
+#### Check id the settings for the entity is empty
+```php
+$user->settings()->empty();
+```
+
#### Check settings (exist)
```php
$user->settings()->exist();
diff --git a/src/Console/CreateSettingsFieldForModel.php b/src/Console/CreateSettingsFieldForModel.php
index 68ee931..7c7d9d0 100644
--- a/src/Console/CreateSettingsFieldForModel.php
+++ b/src/Console/CreateSettingsFieldForModel.php
@@ -27,12 +27,12 @@ public function handle(Filesystem $file)
if (empty($table)) {
$this->error('The name of the table is required!');
- return false;
+ return 1;
}
if (!Schema::hasTable($table)) {
$this->error('Unable to find table "' . $table . '" on the current DB!');
- return false;
+ return 2;
}
$defaultFieldName = config('model_settings.settings_field_name');
@@ -45,7 +45,7 @@ public function handle(Filesystem $file)
if (Schema::hasColumn($table, $fieldName)) {
$this->error('Field "' . $fieldName . '" already exists on table "' . $table . '"');
- return false;
+ return 3;
}
$fileName = date('Y_m_d_His') . '_update_' . $table . '_table_add_' . $fieldName . '.php';
@@ -58,10 +58,10 @@ public function handle(Filesystem $file)
$stub = str_replace('DummyTable', $table, $stub);
$stub = str_replace('settingsFieldName', $fieldName, $stub);
- $file->put($path, $stub);
+ $file->replace($path, $stub);
$this->line("Created Migration: {$fileName}");
- return true;
+ return 0;
}
}
diff --git a/src/Console/CreateSettingsTable.php b/src/Console/CreateSettingsTable.php
index 5a4b0f7..d2f501a 100644
--- a/src/Console/CreateSettingsTable.php
+++ b/src/Console/CreateSettingsTable.php
@@ -27,13 +27,13 @@ public function handle(Filesystem $file)
if (empty($table)) {
$this->error('The name of the table is required!');
- return false;
+ return 1;
}
if (Schema::hasTable($table)) {
$this->error('Table "' . $table . '" already exists!');
- return false;
+ return 2;
}
$fileName = date('Y_m_d_His') . '_create_' . $table . '_table.php';
@@ -45,9 +45,9 @@ public function handle(Filesystem $file)
$stub = str_replace('DummyClass', $className, $stub);
$stub = str_replace('DummyTable', $table, $stub);
- $file->put($path, $stub);
+ $file->replace($path, $stub);
$this->line("Created Migration: {$fileName}");
- return true;
+ return 0;
}
}
diff --git a/src/Contracts/SettingsManagerContract.php b/src/Contracts/SettingsManagerContract.php
index 2624743..7a83682 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 empty(): bool;
+
public function exist(): bool;
public function apply(array $settings = []): self;
diff --git a/src/Managers/AbstractSettingsManager.php b/src/Managers/AbstractSettingsManager.php
index 254eab8..89f4631 100644
--- a/src/Managers/AbstractSettingsManager.php
+++ b/src/Managers/AbstractSettingsManager.php
@@ -46,11 +46,20 @@ public function all(): array
return $array;
}
+ /**
+ * @return bool
+ */
public function exist(): bool
{
- $all = $this->all();
+ return count($this->all()) > 0;
+ }
- return count($all) > 0;
+ /**
+ * @return bool
+ */
+ public function empty(): bool
+ {
+ return count($this->all()) <= 0;
}
/**
diff --git a/tests/ConsoleCommandTest.php b/tests/ConsoleCommandTest.php
deleted file mode 100644
index 5201368..0000000
--- a/tests/ConsoleCommandTest.php
+++ /dev/null
@@ -1,43 +0,0 @@
-artisan('model-settings:model-settings-table')
- ->assertExitCode(0);
-
- /*config(['model_settings.settings_table_name' => 'custom_settings_table']);
- $this->artisan('model-settings:model-settings-table')
- ->assertExitCode(0);*/
-
- config(['model_settings.settings_table_name' => null]);
- $this->artisan('model-settings:model-settings-table')
- ->assertExitCode(0);
- }
-
- public function testModelSettingsFieldCommand()
- {
- $this->artisan('model-settings:model-settings-field')
- ->expectsQuestion('What is the name of the table?', '')
- ->assertExitCode(0);
-
- $this->artisan('model-settings:model-settings-field')
- ->expectsQuestion('What is the name of the table?', 'users_with_field')
- ->expectsQuestion('What is the name of the settings field name?', 'settings')
- ->assertExitCode(0);
-
- $table = 'users_with_table';
- $fieldName = 'custom_settings_field';
- /*$this->artisan('model-settings:model-settings-field')
- ->expectsQuestion('What is the name of the table?', $table)
- ->expectsQuestion('What is the name of the settings field name?', $fieldName)
- ->assertExitCode(0);*/
-
- $this->artisan('model-settings:model-settings-field')
- ->expectsQuestion('What is the name of the table?', $table . '_wrong')
- ->assertExitCode(0);
- }
-}
diff --git a/tests/CreateSettingsFieldForModelConsoleTest.php b/tests/CreateSettingsFieldForModelConsoleTest.php
new file mode 100644
index 0000000..abfe818
--- /dev/null
+++ b/tests/CreateSettingsFieldForModelConsoleTest.php
@@ -0,0 +1,40 @@
+artisan('model-settings:model-settings-field')
+ ->expectsQuestion('What is the name of the table?', '')
+ ->assertExitCode(1);
+ }
+
+ public function testMissingTable()
+ {
+ $this->artisan('model-settings:model-settings-field')
+ ->expectsQuestion('What is the name of the table?', $this->table . '_wrong')
+ ->assertExitCode(2);
+ }
+
+ public function testAlreadyExistsField()
+ {
+ $this->artisan('model-settings:model-settings-field')
+ ->expectsQuestion('What is the name of the table?', $this->table)
+ ->expectsQuestion('What is the name of the settings field name?', $this->alreadyExistsFieldName)
+ ->assertExitCode(3);
+ }
+
+ public function testCreateMigrationFile()
+ {
+ $this->artisan('model-settings:model-settings-field')
+ ->expectsQuestion('What is the name of the table?', $this->table)
+ ->expectsQuestion('What is the name of the settings field name?', $this->fieldName)
+ ->assertExitCode(0);
+ }
+}
diff --git a/tests/CreateSettingsTableConsoleTest.php b/tests/CreateSettingsTableConsoleTest.php
new file mode 100644
index 0000000..95ebdf7
--- /dev/null
+++ b/tests/CreateSettingsTableConsoleTest.php
@@ -0,0 +1,42 @@
+set('model_settings.settings_table_name', null);
+ $this->assertEquals(null, config('model_settings.settings_table_name'));
+ $this->artisan('model-settings:model-settings-table')
+ ->assertExitCode(1);
+ }
+
+ public function testAlreadyExistsTable()
+ {
+ config()->set('model_settings.settings_table_name', 'model_settings');
+ $this->artisan('model-settings:model-settings-table')
+ ->assertExitCode(2);
+ }
+
+ public function testCreateMigration()
+ {
+ config()->set('model_settings.settings_table_name', 'model_settings');
+ Schema::dropIfExists(config('model_settings.settings_table_name'));
+ $this->artisan('model-settings:model-settings-table')
+ ->assertExitCode(0);
+ }
+
+ public function testWithUpdateConfig()
+ {
+ $this->assertEquals('model_settings', config('model_settings.settings_table_name'));
+ $newTableName = 'custom_table_settings';
+ config()->set('model_settings.settings_table_name', $newTableName);
+ $this->assertEquals($newTableName, config('model_settings.settings_table_name'));
+ Schema::dropIfExists($newTableName);
+ $this->artisan('model-settings:model-settings-table')
+ ->assertExitCode(0);
+ }
+}
diff --git a/tests/FieldSettingsManagerTest.php b/tests/FieldSettingsManagerTest.php
index 22c11c5..5259387 100644
--- a/tests/FieldSettingsManagerTest.php
+++ b/tests/FieldSettingsManagerTest.php
@@ -35,6 +35,16 @@ public function testInit()
$this->assertArrayHasKey(HasSettingsField::class, $traits);
}
+ /**
+ * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
+ */
+ public function testEmpty()
+ {
+ $this->model->settings()->clear();
+ $this->assertTrue($this->model->settings()->empty());
+ $this->model->settings()->apply($this->testArray);
+ $this->assertFalse($this->model->settings()->empty());
+ }
/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
diff --git a/tests/RedisSettingsManagerTest.php b/tests/RedisSettingsManagerTest.php
index 93b1b23..c150b99 100644
--- a/tests/RedisSettingsManagerTest.php
+++ b/tests/RedisSettingsManagerTest.php
@@ -38,12 +38,23 @@ public function testInit()
$this->assertInstanceOf(MockPredisConnection::class, Redis::connection());
}
+ /**
+ * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
+ */
+ public function testEmpty()
+ {
+ $this->model->settings()->clear();
+ $this->assertTrue($this->model->settings()->empty());
+ $this->model->settings()->apply($this->testArray);
+ $this->assertFalse($this->model->settings()->empty());
+ }
/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testExist()
{
+ $this->model->settings()->clear();
$this->assertFalse($this->model->settings()->exist());
$this->model->settings()->apply($this->testArray);
$this->assertTrue($this->model->settings()->exist());
diff --git a/tests/TableSettingsManagerTest.php b/tests/TableSettingsManagerTest.php
index cd62ccd..eb3d4df 100644
--- a/tests/TableSettingsManagerTest.php
+++ b/tests/TableSettingsManagerTest.php
@@ -36,6 +36,16 @@ public function testInit()
$this->assertTrue(array_key_exists(HasSettingsTable::class, $traits));
}
+ /**
+ * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
+ */
+ public function testEmpty()
+ {
+ $this->model->settings()->clear();
+ $this->assertTrue($this->model->settings()->empty());
+ $this->model->settings()->apply($this->testArray);
+ $this->assertFalse($this->model->settings()->empty());
+ }
/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
@@ -79,6 +89,14 @@ public function testDefaultValue()
$this->testArray,
cache()->get($this->model->getSettingsCacheKey())
);
+
+ $this->assertTrue(config('model_settings.settings_table_use_cache'));
+ config()->set('model_settings.settings_table_use_cache', false);
+ $this->assertFalse(config('model_settings.settings_table_use_cache'));
+ $this->assertEquals(
+ array_merge($this->defaultSettingsTestArray, $this->testArray),
+ $this->model->settings()->all()
+ );
}
/**
diff --git a/tests/TextFieldSettingsManagerTest.php b/tests/TextFieldSettingsManagerTest.php
index f1f1569..d169f8a 100644
--- a/tests/TextFieldSettingsManagerTest.php
+++ b/tests/TextFieldSettingsManagerTest.php
@@ -34,6 +34,16 @@ public function testInit()
$traits = class_uses($this->model);
$this->assertArrayHasKey(HasSettingsField::class, $traits);
}
+ /**
+ * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
+ */
+ public function testEmpty()
+ {
+ $this->model->settings()->clear();
+ $this->assertTrue($this->model->settings()->empty());
+ $this->model->settings()->apply($this->testArray);
+ $this->assertFalse($this->model->settings()->empty());
+ }
/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException