Skip to content

Commit

Permalink
Console test && empty function (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
glorand authored Apr 21, 2020
1 parent 08e0def commit df08946
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
# composer.lock
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4

before_script:
- composer selfupdate
Expand Down
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.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
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 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)
Expand Down Expand Up @@ -150,6 +151,11 @@ class User extends Model
$user = App\User::first();
```

#### Check id the settings for the entity is empty <a name="empty"></a>
```php
$user->settings()->empty();
```

#### Check settings (exist) <a name="exist"></a>
```php
$user->settings()->exist();
Expand Down
10 changes: 5 additions & 5 deletions src/Console/CreateSettingsFieldForModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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';
Expand All @@ -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("<info>Created Migration:</info> {$fileName}");

return true;
return 0;
}
}
8 changes: 4 additions & 4 deletions src/Console/CreateSettingsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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("<info>Created Migration:</info> {$fileName}");

return true;
return 0;
}
}
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 empty(): bool;

public function exist(): bool;

public function apply(array $settings = []): self;
Expand Down
13 changes: 11 additions & 2 deletions src/Managers/AbstractSettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
43 changes: 0 additions & 43 deletions tests/ConsoleCommandTest.php

This file was deleted.

40 changes: 40 additions & 0 deletions tests/CreateSettingsFieldForModelConsoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Glorand\Model\Settings\Tests;

class CreateSettingsFieldForModelConsoleTest extends TestCase
{
private $table = 'users_with_field';
private $fieldName = 'custom_settings_field';
private $alreadyExistsFieldName = 'settings';

public function testEmptyTable()
{
$this->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);
}
}
42 changes: 42 additions & 0 deletions tests/CreateSettingsTableConsoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Glorand\Model\Settings\Tests;

use Illuminate\Support\Facades\Schema;

class CreateSettingsTableConsoleTest extends TestCase
{
public function testEmptyTable()
{
config()->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);
}
}
10 changes: 10 additions & 0 deletions tests/FieldSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/RedisSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
18 changes: 18 additions & 0 deletions tests/TableSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/TextFieldSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit df08946

Please sign in to comment.