diff --git a/CHANGELOG.md b/CHANGELOG.md index 19f6459..023957b 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,22 @@ # Changelog All notable changes to `laravel-config` will be documented in this file. -## [Unreleased] +## [Unreleased] - 2024-04-09 + +- ### Braking Change: Made the Models and Traits Publishable, so you can add SoftDeletes, auditing, and/or caching if you want (YOU MUST RUN THE INSTALLATION COMMAND) +- ### Braking Change: Check your Namespaces +- Moved the config functions to a Trait so adding functions is as simple as editing the published Trait or adding another Trait +- I left the original `LaravelConfig` class as an alies of the Model, but it needs more testing +- ### Braking Change: The Update function needed to be renamed from `update` to `update_config` +- ### Braking Change: The Update function Only takes one parameter just the `ConfigItem` +- ### Braking Change: The `update_config` helper Only takes one parameter just the `ConfigItem` +- ### Braking Change: The Delete function needed to be renamed from `delete` to `delete_config` +- Because of the Braking Changes I also added a `get_config`, `set_config`, `has_config`, and `create_config` aliases for consistency +- NON Braking Change: Removed the `all` function as it's not needed anymore since it's a function of a Model +- I'm adding more functions to another Trait that can be added by including it in the Model these are all traits that I've found useful +- Added comands to `get_config` and `set_config` from the console +- Added more datatypes + ## [5.1.0] - 2024-04-08 diff --git a/README.md b/README.md index c689166..22012dc 100755 --- a/README.md +++ b/README.md @@ -16,21 +16,27 @@ You can install the package via composer: ```bash composer require tarfin-labs/laravel-config ``` -Next, you should publish the Laravel config migration file using the vendor:publish Artisan command. +Next, you MUST publish Models, Factories, and Traits +When updating you might need to rerun the installation command be warned this will overwrite any changes made to any of the published Models, Factories, and Traits +```bash +php artisan laravel-config:install ``` +Next, you should publish the Laravel config migration file using the vendor:publish Artisan command. + +```bash php artisan vendor:publish --provider="TarfinLabs\LaravelConfig\LaravelConfigServiceProvider" --tag="laravel-config" ``` If you want to use Laravel Config database factory, you can publish it too, using the command: -``` +```bash php artisan vendor:publish --provider="TarfinLabs\LaravelConfig\LaravelConfigServiceProvider" --tag="laravel-config-factories" ``` Finally, you should run your database migrations: -``` +```bash php artisan migrate ``` @@ -40,7 +46,7 @@ Simple usage example of laravel-config package in your Laravel app. Create new config parameter: -``` php +```php $factory = new ConfigFactory(); $configItem = $factory->setName('key') ->setType(ConfigDataType::BOOLEAN) @@ -54,37 +60,37 @@ LaravelConfig::create($configItem); Get value with config name: -``` php +```php LaravelConfig::get('key'); ``` Set value with config name and value: -``` php +```php LaravelConfig::set('key', 'value'); ``` Get all config parameters: -``` php +```php LaravelConfig::all(); ``` Get config items by tag: -``` php +```php LaravelConfig::getByTag('key'); ``` Check if the config exists: -``` php +```php LaravelConfig::has('key'); ``` Update config with new values: -``` php +```php $factory = new ConfigFactory($configId); $configItem = $factory->setName('updated-key') ->setType(ConfigDataType::BOOLEAN) @@ -93,13 +99,13 @@ $configItem = $factory->setName('updated-key') ->setDescription('updated description') ->get(); -LaravelConfig::update($configItem); +LaravelConfig::update_config($configItem); ``` Remove config: -``` php -LaravelConfig::delete('key'); +```php +LaravelConfig::delete_config('key'); ``` ### Nested Parameters @@ -141,7 +147,7 @@ LaravelConfig::getNested('foo'); ### Helpers You can also use helper functions: -``` php +```php // Creating config item $factory = new ConfigFactory(); $configItem = $factory->setName('key') @@ -182,7 +188,7 @@ read_nested('foo.bar'); ### Testing -``` bash +```bash composer test ``` diff --git a/config/config.php b/config/config.php index 3a388ca..cd8a3dc 100755 --- a/config/config.php +++ b/config/config.php @@ -1,8 +1,5 @@ env('LARAVEL_CONFIG_TABLE', 'laravel_config'), ]; diff --git a/database/factories/ConfigFactory.php b/database/factories/ConfigFactory.php index f47bbe8..63f11c1 100644 --- a/database/factories/ConfigFactory.php +++ b/database/factories/ConfigFactory.php @@ -1,13 +1,13 @@ define(Config::class, function (Faker $faker, array $attributes = []) { +$factory->define(ConfigModel::class, function (Faker $faker, array $attributes = []) { return [ 'name' => $attributes['name'] ?? $faker->word().$faker->asciify('*****'), 'type' => $attributes['type'] ?? $faker->randomElement(['boolean', 'text']), diff --git a/src/Casts/ConfigValueCast.php b/src/Casts/ConfigValueCast.php index c446cb2..c0d3c68 100644 --- a/src/Casts/ConfigValueCast.php +++ b/src/Casts/ConfigValueCast.php @@ -4,12 +4,20 @@ use Carbon\Carbon; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; -use Illuminate\Database\Eloquent\Model; use TarfinLabs\LaravelConfig\Enums\ConfigDataType; class ConfigValueCast implements CastsAttributes { - public function get(Model $model, string $key, mixed $value, array $attributes) + /** + * Transform the attribute from the underlying model values. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @param mixed $value + * @param array $attributes + * @return TGet|null + */ + public function get($model, string $key, mixed $value, array $attributes) { return match ($attributes['type']) { ConfigDataType::BOOLEAN->value => (bool) $value, @@ -21,7 +29,16 @@ public function get(Model $model, string $key, mixed $value, array $attributes) }; } - public function set(Model $model, string $key, mixed $value, array $attributes) + /** + * Transform the attribute to its underlying model values. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @param TSet|null $value + * @param array $attributes + * @return mixed + */ + public function set($model, string $key, mixed $value, array $attributes) { return $value; } diff --git a/src/Config/ConfigFactory.php b/src/Config/ConfigFactory.php index 77cc1cf..e0e2096 100644 --- a/src/Config/ConfigFactory.php +++ b/src/Config/ConfigFactory.php @@ -2,19 +2,21 @@ namespace TarfinLabs\LaravelConfig\Config; +use App\Models\Config as ConfigModel; + class ConfigFactory { /** * @var ConfigItem */ - protected $configItem; + protected ConfigItem $configItem; /** * ConfigFactory constructor. * - * @param Config|null $config + * @param ConfigModel|null $config */ - public function __construct(Config $config = null) + public function __construct(ConfigModel $config = null) { $this->configItem = new ConfigItem(); diff --git a/src/Console/GetCommand.php b/src/Console/GetCommand.php new file mode 100644 index 0000000..e743c10 --- /dev/null +++ b/src/Console/GetCommand.php @@ -0,0 +1,25 @@ +argument('key')); + $this->line($value); + } +} diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php new file mode 100644 index 0000000..6c146d7 --- /dev/null +++ b/src/Console/InstallCommand.php @@ -0,0 +1,45 @@ +ensureDirectoryExists(app_path('Models/')); + (new Filesystem)->copyDirectory(__DIR__.'/../Models', app_path('Models/')); + + // Traits... + (new Filesystem)->ensureDirectoryExists(app_path('Traits/')); + (new Filesystem)->copyDirectory(__DIR__.'/../Traits', app_path('Traits/')); + + // Factory... + (new Filesystem)->ensureDirectoryExists(database_path('factories/')); + (new Filesystem)->copy(__DIR__.'/../../database/factories/ConfigFactory.php', database_path('factories/ConfigFactory.php')); + + $this->info('Laravel-config installed successfully.'); + } +} diff --git a/src/Console/SetCommand.php b/src/Console/SetCommand.php new file mode 100644 index 0000000..0e0d71e --- /dev/null +++ b/src/Console/SetCommand.php @@ -0,0 +1,25 @@ +argument('key'), $this->argument('value')); + } +} diff --git a/src/Enums/ConfigDataType.php b/src/Enums/ConfigDataType.php index 56b8d20..88ece9f 100644 --- a/src/Enums/ConfigDataType.php +++ b/src/Enums/ConfigDataType.php @@ -4,6 +4,9 @@ enum ConfigDataType: string { + case STRING = 'string'; + case NUMERIC = 'numeric'; + case NONE = 'None'; case INTEGER = 'integer'; case BOOLEAN = 'boolean'; case JSON = 'json'; diff --git a/src/LaravelConfig.php b/src/LaravelConfig.php old mode 100755 new mode 100644 index 8cd9b7b..07f6476 --- a/src/LaravelConfig.php +++ b/src/LaravelConfig.php @@ -3,8 +3,8 @@ namespace TarfinLabs\LaravelConfig; use Illuminate\Support\Collection; -use TarfinLabs\LaravelConfig\Config\Config; use TarfinLabs\LaravelConfig\Config\ConfigItem; +use TarfinLabs\LaravelConfig\LaravelConfigFacade as ConfigFacade; class LaravelConfig { @@ -15,15 +15,13 @@ class LaravelConfig * @param $default * @return mixed */ - public function get(string $name, $default = null) + public function get(string $name, $default = null): mixed { if (! $this->has($name)) { return $default; } - $config = Config::where('name', $name)->first(); - - return $config->val; + return ConfigFacade::get($name); } /** @@ -34,37 +32,20 @@ public function get(string $name, $default = null) */ public function getNested(string $namespace): Collection { - $params = Config::where('name', 'LIKE', "{$namespace}.%")->get(); - - $config = collect(); - - foreach ($params as $param) { - $keys = explode('.', str_replace("{$namespace}.", '', $param->name)); - $name = ''; - - foreach ($keys as $key) { - $name .= $key.'.'; - } - - $param->name = rtrim($name, '.'); - - $config->push($param); - } - - return $config; + return ConfigFacade::getNested($namespace); } /** * @param $tags * @return Collection */ - public function getByTag($tags): ?Collection + public function getByTag($tags): Collection|null { if (! is_array($tags)) { $tags = [$tags]; } - return Config::whereJsonContains('tags', $tags)->get(); + return ConfigFacade::getByTag($tags); } /** @@ -74,18 +55,13 @@ public function getByTag($tags): ?Collection * @param $value * @return mixed */ - public function set(string $name, $value) + public function set(string $name, $value): mixed { if (! $this->has($name)) { - return; + return null; } - $config = Config::where('name', $name)->first(); - - $config->val = $value; - $config->save(); - - return $value; + return ConfigFacade::set($name, $value); } /** @@ -96,7 +72,7 @@ public function set(string $name, $value) */ public function has(string $name): bool { - return Config::where('name', $name)->count() > 0; + return ConfigFacade::has($name); } /** @@ -104,9 +80,9 @@ public function has(string $name): bool * * @return mixed */ - public function all() + public function all(): mixed { - return Config::all(); + return ConfigFacade::all(); } /** @@ -121,9 +97,7 @@ public function create(ConfigItem $configItem): bool return false; } - $config = new Config(); - - return $this->fillColumns($config, $configItem)->save(); + return ConfigFacade::create($configItem); } /** @@ -133,37 +107,19 @@ public function create(ConfigItem $configItem): bool * @param ConfigItem $configItem * @return mixed */ - public function update(Config $config, ConfigItem $configItem) + public function update($config, ConfigItem $configItem): mixed { - return $this->fillColumns($config, $configItem)->save(); + return ConfigFacade::update_config($configItem); } /** * Delete config parameter. * - * @param Config $config + * @param $config * @return int */ - public function delete(Config $config): int - { - return Config::destroy($config->id); - } - - /** - * Fill config paremeter columns. - * - * @param Config $config - * @param ConfigItem $configItem - * @return Config - */ - private function fillColumns(Config $config, ConfigItem $configItem): Config + public function delete($config): int { - $config->name = $configItem->name; - $config->val = $configItem->val; - $config->type = $configItem->type; - $config->description = $configItem->description; - $config->tags = $configItem->tags; - - return $config; + return ConfigFacade::delete_config($config); } } diff --git a/src/LaravelConfigServiceProvider.php b/src/LaravelConfigServiceProvider.php index b7694f4..1f7ffe7 100755 --- a/src/LaravelConfigServiceProvider.php +++ b/src/LaravelConfigServiceProvider.php @@ -2,6 +2,7 @@ namespace TarfinLabs\LaravelConfig; +use App\Models\Config as ConfigModel; use Illuminate\Support\ServiceProvider; class LaravelConfigServiceProvider extends ServiceProvider @@ -11,9 +12,6 @@ class LaravelConfigServiceProvider extends ServiceProvider */ public function boot(): void { - /* - * Optional methods to load your package assets - */ $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); if ($this->app->runningInConsole()) { @@ -24,6 +22,15 @@ public function boot(): void $this->publishes([ __DIR__.'/../database/factories/ConfigFactory.php' => database_path('factories/ConfigFactory.php'), ], 'laravel-config-factories'); + $this->publishes([ + __DIR__.'/Traits' => app_path('Traits'), + __DIR__.'/Models' => app_path('Models'), + ], 'laravel-config-models'); + $this->commands([ + Console\InstallCommand::class, + Console\SetCommand::class, + Console\GetCommand::class, + ]); } } @@ -36,8 +43,8 @@ public function register(): void $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-config'); // Register the main class to use with the facade - $this->app->singleton('laravel-config', function () { - return new LaravelConfig; + $this->app->singleton('laravel-config', static function () { + return new ConfigModel(); }); } } diff --git a/src/Config/Config.php b/src/Models/Config.php similarity index 86% rename from src/Config/Config.php rename to src/Models/Config.php index 6eb374d..64dc802 100644 --- a/src/Config/Config.php +++ b/src/Models/Config.php @@ -1,12 +1,15 @@ first(); + + if ($config === null) { + return null; + } + + $config->val = $value; + $config->save(); + + return $value; + } + + /** + * Set config discription with given data by id. + * + * @param int $id + * @param string $value + * @return string|null + */ + public function set_discription_by_id(int $id, string $value): string|null + { + $config = parent::where('id', $id)->first(); + + if ($config === null) { + return null; + } + + $config->description = $value; + $config->save(); + + return $value; + } + + /** + * Set config description with given data by id. + * + * @param int $id + * @param mixed $value + * @return array|null + */ + public function set_tags_by_id(int $id, mixed $value): mixed + { + $config = parent::where('id', $id)->first(); + + if ($config === null) { + return null; + } + + $config->tags = $value; + $config->save(); + + return $value; + } + + /** + * Get config by given id. + * + * @param int $id + * @param mixed $default + * @return mixed + */ + public function get_by_id(int $id, mixed $default = null): mixed + { + $config = parent::where('id', $id)->first(); + + if ($config === null) { + return $default; + } + + return $config; + } +} diff --git a/src/Traits/LaravelConfigTrait.php b/src/Traits/LaravelConfigTrait.php new file mode 100755 index 0000000..33169d7 --- /dev/null +++ b/src/Traits/LaravelConfigTrait.php @@ -0,0 +1,211 @@ +has($name)) { + return $default; + } + + $config = parent::where('name', $name)->first(); + + return $config->val; + } + + /** + * Get config by given name. + * this is an alias of the get function + * for consistency. + * + * @param string $name + * @param $default + * @return mixed + */ + public function get_config(string $name, $default = null): mixed + { + return $this->get($name, $default); + } + + /** + * Get nested config params. + * + * @param string $namespace + * @return Collection + */ + public function getNested(string $namespace): Collection + { + $params = parent::where('name', 'LIKE', "{$namespace}.%")->get(); + + $config = collect(); + + foreach ($params as $param) { + $keys = explode('.', str_replace("{$namespace}.", '', $param->name)); + $name = ''; + + foreach ($keys as $key) { + $name .= $key.'.'; + } + + $param->name = rtrim($name, '.'); + + $config->push($param); + } + + return $config; + } + + /** + * @param $tags + * @return Collection + */ + public function getByTag($tags): Collection|null + { + if (! is_array($tags)) { + $tags = [$tags]; + } + + return parent::whereJsonContains('tags', $tags)->get(); + } + + /** + * Set config with given data. + * + * @param string $name + * @param $value + * @return mixed + */ + public function set(string $name, $value): mixed + { + if (! $this->has($name)) { + return null; + } + + $config = parent::where('name', $name)->first(); + + $config->val = $value; + $config->save(); + + return $value; + } + + /** + * Set config with given data. + * this is an alias of the set function + * for consistency. + * + * @param string $name + * @param $value + * @return mixed + */ + public function set_config(string $name, $value): mixed + { + return $this->set($name, $value); + } + + /** + * Check whether a config parameter is set. + * + * @param string $name + * @return bool + */ + public function has(string $name): bool + { + return parent::where('name', $name)->count() > 0; + } + + /** + * Check whether a config parameter is set. + * this is an alias of the has function + * for consistency. + * + * @param string $name + * @return bool + */ + public function has_config(string $name): bool + { + return $this->has($name); + } + + /** + * Create a new config parameter. + * + * @param ConfigItem $configItem + * @return bool + */ + public function create(ConfigItem $configItem): bool + { + if ($this->has($configItem->name)) { + return false; + } + + return $this->fillColumns($configItem)->save(); + } + + /** + * Create a new config parameter. + * this is an alias of the create function + * for consistency. + * + * @param ConfigItem $configItem + * @return bool + */ + public function create_config(ConfigItem $configItem): bool + { + return $this->create($configItem); + } + + /** + * Update config paremeter. + * + * @param ConfigItem $configItem + * @return mixed + */ + public function update_config(ConfigItem $configItem): mixed + { + $config = parent::where('name', $configItem->name)->first(); + + return $config->fillColumns($configItem)->save(); + } + + /** + * Delete config parameter. + * + * @param Config $config + * @return int + */ + public function delete_config(parent $config): int + { + return parent::destroy($config->id); + } + + /** + * Fill config paremeter columns. + * + * @param Config $config + * @param ConfigItem $configItem + * @return parent + */ + private function fillColumns(ConfigItem $configItem): parent + { + $this->name = $configItem->name; + $this->val = $configItem->val; + $this->type = $configItem->type; + $this->description = $configItem->description; + $this->tags = $configItem->tags; + + return $this; + } +} diff --git a/src/helpers.php b/src/helpers.php index 6c26441..eddf269 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,6 +1,6 @@ create($configItem); } @@ -23,9 +23,9 @@ function create_config(ConfigItem $configItem) * @param string|null $key * @return mixed */ - function read_config(string $key = null) + function read_config(string|null $key = null): mixed { - if (is_null($key)) { + if ($key === null) { return app('laravel-config')->all(); } @@ -40,7 +40,7 @@ function read_config(string $key = null) * @param string|null $key * @return mixed */ - function read_nested(string $key) + function read_nested(string $key): mixed { return app('laravel-config')->getNested($key); } @@ -50,13 +50,12 @@ function read_nested(string $key) /** * Update given config item by given data. * - * @param Config $config * @param ConfigItem $configItem * @return mixed */ - function update_config(Config $config, ConfigItem $configItem) + function update_config(ConfigItem $configItem): mixed { - return app('laravel-config')->update($config, $configItem); + return app('laravel-config')->update_config($configItem); } } @@ -64,12 +63,12 @@ function update_config(Config $config, ConfigItem $configItem) /** * Delete given config item. * - * @param Config $config + * @param ConfigModel $config * @return mixed */ - function delete_config(Config $config) + function delete_config(ConfigModel $config): mixed { - return app('laravel-config')->delete($config); + return app('laravel-config')->delete_config($config); } } @@ -81,7 +80,7 @@ function delete_config(Config $config) * @param $value * @return mixed */ - function set_config_value(string $key, $value) + function set_config_value(string $key, $value): mixed { return app('laravel-config')->set($key, $value); } @@ -94,7 +93,7 @@ function set_config_value(string $key, $value) * @param string $key * @return bool */ - function has_config(string $key) + function has_config(string $key): bool { return app('laravel-config')->has($key); } diff --git a/tests/LaravelConfigOldTest.php b/tests/LaravelConfigOldTest.php new file mode 100644 index 0000000..c302f49 --- /dev/null +++ b/tests/LaravelConfigOldTest.php @@ -0,0 +1,285 @@ +laravelConfig = new LaravelConfig(); + } + + /** @test */ + public function it_create_a_new_config_parameter(): void + { + $factory = new ConfigFactory(); + $configItem = $factory->setName(Str::random(5)) + ->setType(ConfigDataType::BOOLEAN) + ->setValue('1') + ->setDescription(Str::random(50)) + ->get(); + + $this->laravelConfig->create($configItem); + + $this->assertDatabaseHas(config('laravel-config.table'), [ + 'name' => $configItem->name, + 'val' => $configItem->val, + 'type' => $configItem->type, + 'description' => $configItem->description, + ]); + } + + /** @test */ + public function it_create_a_new_config_parameter_with_tag(): void + { + $factory = new ConfigFactory(); + $configItem = $factory->setName(Str::random(5)) + ->setType(ConfigDataType::BOOLEAN) + ->setValue('1') + ->setTags(['system']) + ->setDescription(Str::random(50)) + ->get(); + + $this->laravelConfig->create($configItem); + + $this->assertDatabaseHas(config('laravel-config.table'), [ + 'name' => $configItem->name, + 'val' => $configItem->val, + 'type' => $configItem->type, + 'description' => $configItem->description, + ]); + + $this->assertTrue($this->laravelConfig->getByTag(['system'])->count() > 0); + } + + /** @test */ + public function it_does_not_create_a_config_parameter_with_the_same_name(): void + { + $config = factory(ConfigModel::class)->create(); + $this->assertDatabaseHas(config('laravel-config.table'), ['name' => $config->name]); + + $factory = new ConfigFactory(); + $configItem = $factory->setName($config->name) + ->setType(ConfigDataType::BOOLEAN) + ->setValue('1') + ->setDescription(Str::random(50)) + ->get(); + + $response = $this->laravelConfig->create($configItem); + + $this->assertFalse($response); + } + + /** @test */ + public function it_updates_existing_config_parameter(): void + { + $config = factory(ConfigModel::class)->create(['val' => '0']); + $this->assertDatabaseHas(config('laravel-config.table'), ['name' => $config->name, 'val' => $config->val]); + + $factory = new ConfigFactory($config); + $configItem = $factory->setType(ConfigDataType::BOOLEAN) + ->setValue('0') + ->setDescription('updated-description') + ->get(); + + $this->laravelConfig->update($config, $configItem); + + $this->assertDatabaseHas(config('laravel-config.table'), [ + 'name' => $config->name, + 'val' => $configItem->val, + 'type' => $configItem->type, + 'description' => $configItem->description, + ]); + } + + /** @test */ + public function it_deletes_an_existing_config_parameter(): void + { + $name = 'dummy-name'; + $config = factory(ConfigModel::class)->create(['name' => $name]); + $this->assertDatabaseHas(config('laravel-config.table'), ['name' => $config->name]); + + $this->laravelConfig->delete($config); + + $this->assertDatabaseMissing(config('laravel-config.table'), ['name' => $name]); + } + + /** @test */ + public function it_sets_a_value_to_existing_config_parameter(): void + { + $config = factory(ConfigModel::class)->create(['val' => '1']); + + $response = $this->laravelConfig->set($config->name, '0'); + + $this->assertEquals('0', $response); + } + + /** @test */ + public function it_does_not_set_a_value_to_not_existing_config_parameter(): void + { + $response = $this->laravelConfig->set('dummy', '1'); + + $this->assertNull($response); + } + + /** @test */ + public function it_returns_a_config_parameter_value_by_given_name(): void + { + $config = factory(ConfigModel::class)->create(); + + $response = $this->laravelConfig->get($config->name); + + $this->assertEquals($config->val, $response); + } + + /** @test */ + public function it_returns_config_collection_by_tag_name(): void + { + factory(ConfigModel::class, 3) + ->create(); + + $config = factory(ConfigModel::class, 5)->create([ + 'tags' => ['system'], + ]); + + $response = $this->laravelConfig->getByTag(['system']); + + $this->assertEquals($config->count(), $response->count()); + } + + /** @test */ + public function it_does_not_return_a_not_existing_config_parameter(): void + { + $response = $this->laravelConfig->get('dummy'); + + $this->assertNull($response); + } + + /** @test */ + public function it_returns_if_a_config_parameter_is_exist(): void + { + $name = 'dummy'; + $response = $this->laravelConfig->has($name); + $this->assertFalse($response); + + factory(ConfigModel::class)->create(['name' => $name]); + $response = $this->laravelConfig->has($name); + $this->assertTrue($response); + } + + /** @test */ + public function it_returns_all_config_parameters(): void + { + factory(ConfigModel::class)->times(2)->create(); + + $response = $this->laravelConfig->all(); + + $this->assertEquals(2, $response->count()); + } + + /** @test */ + public function it_returns_nested_config_parameters(): void + { + factory(ConfigModel::class)->create([ + 'name' => 'foo.bar', + 'val' => true, + ]); + + factory(ConfigModel::class)->create([ + 'name' => 'foo.baz', + 'val' => false, + ]); + + $response = $this->laravelConfig->getNested('foo'); + + $this->assertEquals(2, $response->count()); + $this->assertEquals('bar', $response->first()->name); + $this->assertEquals('baz', $response->last()->name); + } + + /** @test */ + public function it_returns_boolean_value_for_boolean_type_config_parameter_if_exists(): void + { + $config = factory(ConfigModel::class)->create([ + 'name' => 'yunus.was.here', + 'val' => '1', + 'type' => ConfigDataType::BOOLEAN, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertTrue($response); + } + + /** @test */ + public function it_returns_integer_value_for_integer_type_config_parameter_if_exists(): void + { + $config = factory(ConfigModel::class)->create([ + 'name' => 'yunus.was.here', + 'val' => '123456', + 'type' => ConfigDataType::INTEGER, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertIsInt($response); + } + + /** @test */ + public function it_returns_datetime_value_for_datetime_type_config_parameter_if_exists(): void + { + $config = factory(ConfigModel::class)->create([ + 'name' => 'yunus.was.here', + 'val' => '2024-02-29 12:00', + 'type' => ConfigDataType::DATE_TIME, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertInstanceOf(Carbon::class, $response); + } + + /** @test */ + public function it_returns_date_value_for_date_type_config_parameter_if_exists(): void + { + $config = factory(ConfigModel::class)->create([ + 'name' => 'yunus.was.here', + 'val' => '2024-02-29', + 'type' => ConfigDataType::DATE, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertInstanceOf(Carbon::class, $response); + } + + /** @test */ + public function it_returns_json_value_for_json_type_config_parameter_if_exists(): void + { + $config = factory(ConfigModel::class)->create([ + 'name' => 'yunus.was.here', + 'val' => '{"9":[7,8,9],"2":[7,8,9],"31":[10,11,12]}', + 'type' => ConfigDataType::JSON, + ]); + + $response = $this->laravelConfig->get($config->name); + + $this->assertIsArray($response); + + $this->assertArrayHasKey(9, $response); + $this->assertArrayHasKey(2, $response); + $this->assertArrayHasKey(31, $response); + } +} diff --git a/tests/LaravelConfigTest.php b/tests/LaravelConfigTest.php index 714a139..f24e8c8 100644 --- a/tests/LaravelConfigTest.php +++ b/tests/LaravelConfigTest.php @@ -2,23 +2,22 @@ namespace TarfinLabs\LaravelConfig\Tests; +use App\Models\Config as ConfigModel; use Carbon\Carbon; use Illuminate\Support\Str; -use TarfinLabs\LaravelConfig\Config\Config; use TarfinLabs\LaravelConfig\Config\ConfigFactory; use TarfinLabs\LaravelConfig\Enums\ConfigDataType; -use TarfinLabs\LaravelConfig\LaravelConfig; class LaravelConfigTest extends TestCase { - /** @var LaravelConfig */ + /** @var ConfigModel */ protected $laravelConfig; public function setUp(): void { parent::setUp(); - $this->laravelConfig = new LaravelConfig(); + $this->laravelConfig = new ConfigModel(); } /** @test */ @@ -67,7 +66,7 @@ public function it_create_a_new_config_parameter_with_tag(): void /** @test */ public function it_does_not_create_a_config_parameter_with_the_same_name(): void { - $config = factory(Config::class)->create(); + $config = factory(ConfigModel::class)->create(); $this->assertDatabaseHas(config('laravel-config.table'), ['name' => $config->name]); $factory = new ConfigFactory(); @@ -85,7 +84,7 @@ public function it_does_not_create_a_config_parameter_with_the_same_name(): void /** @test */ public function it_updates_existing_config_parameter(): void { - $config = factory(Config::class)->create(['val' => '0']); + $config = factory(ConfigModel::class)->create(['val' => '0']); $this->assertDatabaseHas(config('laravel-config.table'), ['name' => $config->name, 'val' => $config->val]); $factory = new ConfigFactory($config); @@ -94,7 +93,7 @@ public function it_updates_existing_config_parameter(): void ->setDescription('updated-description') ->get(); - $this->laravelConfig->update($config, $configItem); + $this->laravelConfig->update_config($configItem); $this->assertDatabaseHas(config('laravel-config.table'), [ 'name' => $config->name, @@ -108,10 +107,10 @@ public function it_updates_existing_config_parameter(): void public function it_deletes_an_existing_config_parameter(): void { $name = 'dummy-name'; - $config = factory(Config::class)->create(['name' => $name]); + $config = factory(ConfigModel::class)->create(['name' => $name]); $this->assertDatabaseHas(config('laravel-config.table'), ['name' => $config->name]); - $this->laravelConfig->delete($config); + $this->laravelConfig->delete_config($config); $this->assertDatabaseMissing(config('laravel-config.table'), ['name' => $name]); } @@ -119,7 +118,7 @@ public function it_deletes_an_existing_config_parameter(): void /** @test */ public function it_sets_a_value_to_existing_config_parameter(): void { - $config = factory(Config::class)->create(['val' => '1']); + $config = factory(ConfigModel::class)->create(['val' => '1']); $response = $this->laravelConfig->set($config->name, '0'); @@ -137,7 +136,7 @@ public function it_does_not_set_a_value_to_not_existing_config_parameter(): void /** @test */ public function it_returns_a_config_parameter_value_by_given_name(): void { - $config = factory(Config::class)->create(); + $config = factory(ConfigModel::class)->create(); $response = $this->laravelConfig->get($config->name); @@ -147,10 +146,10 @@ public function it_returns_a_config_parameter_value_by_given_name(): void /** @test */ public function it_returns_config_collection_by_tag_name(): void { - factory(Config::class, 3) + factory(ConfigModel::class, 3) ->create(); - $config = factory(Config::class, 5)->create([ + $config = factory(ConfigModel::class, 5)->create([ 'tags' => ['system'], ]); @@ -174,7 +173,7 @@ public function it_returns_if_a_config_parameter_is_exist(): void $response = $this->laravelConfig->has($name); $this->assertFalse($response); - factory(Config::class)->create(['name' => $name]); + factory(ConfigModel::class)->create(['name' => $name]); $response = $this->laravelConfig->has($name); $this->assertTrue($response); } @@ -182,7 +181,7 @@ public function it_returns_if_a_config_parameter_is_exist(): void /** @test */ public function it_returns_all_config_parameters(): void { - factory(Config::class)->times(2)->create(); + factory(ConfigModel::class)->times(2)->create(); $response = $this->laravelConfig->all(); @@ -192,12 +191,12 @@ public function it_returns_all_config_parameters(): void /** @test */ public function it_returns_nested_config_parameters(): void { - factory(Config::class)->create([ + factory(ConfigModel::class)->create([ 'name' => 'foo.bar', 'val' => true, ]); - factory(Config::class)->create([ + factory(ConfigModel::class)->create([ 'name' => 'foo.baz', 'val' => false, ]); @@ -212,7 +211,7 @@ public function it_returns_nested_config_parameters(): void /** @test */ public function it_returns_boolean_value_for_boolean_type_config_parameter_if_exists(): void { - $config = factory(Config::class)->create([ + $config = factory(ConfigModel::class)->create([ 'name' => 'yunus.was.here', 'val' => '1', 'type' => ConfigDataType::BOOLEAN, @@ -226,7 +225,7 @@ public function it_returns_boolean_value_for_boolean_type_config_parameter_if_ex /** @test */ public function it_returns_integer_value_for_integer_type_config_parameter_if_exists(): void { - $config = factory(Config::class)->create([ + $config = factory(ConfigModel::class)->create([ 'name' => 'yunus.was.here', 'val' => '123456', 'type' => ConfigDataType::INTEGER, @@ -240,7 +239,7 @@ public function it_returns_integer_value_for_integer_type_config_parameter_if_ex /** @test */ public function it_returns_datetime_value_for_datetime_type_config_parameter_if_exists(): void { - $config = factory(Config::class)->create([ + $config = factory(ConfigModel::class)->create([ 'name' => 'yunus.was.here', 'val' => '2024-02-29 12:00', 'type' => ConfigDataType::DATE_TIME, @@ -254,7 +253,7 @@ public function it_returns_datetime_value_for_datetime_type_config_parameter_if_ /** @test */ public function it_returns_date_value_for_date_type_config_parameter_if_exists(): void { - $config = factory(Config::class)->create([ + $config = factory(ConfigModel::class)->create([ 'name' => 'yunus.was.here', 'val' => '2024-02-29', 'type' => ConfigDataType::DATE, @@ -268,7 +267,7 @@ public function it_returns_date_value_for_date_type_config_parameter_if_exists() /** @test */ public function it_returns_json_value_for_json_type_config_parameter_if_exists(): void { - $config = factory(Config::class)->create([ + $config = factory(ConfigModel::class)->create([ 'name' => 'yunus.was.here', 'val' => '{"9":[7,8,9],"2":[7,8,9],"31":[10,11,12]}', 'type' => ConfigDataType::JSON, diff --git a/tests/TestCase.php b/tests/TestCase.php index b58bbd4..b6918b8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,23 +2,29 @@ namespace TarfinLabs\LaravelConfig\Tests; -use Illuminate\Support\Facades\Schema; +// use Illuminate\Support\Facades\Schema; +use Illuminate\Foundation\Testing\RefreshDatabase; use TarfinLabs\LaravelConfig\LaravelConfigServiceProvider; class TestCase extends \Orchestra\Testbench\TestCase { + use RefreshDatabase; + public function setUp(): void { parent::setUp(); $this->withFactories(__DIR__.'/../database/factories'); - Schema::dropAllTables(); + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); + + // Schema::dropAllTables(); - $this->artisan('migrate', [ - '--database' => 'mysql', - '--realpath' => realpath(__DIR__.'/../database/migrations'), - ]); + // $this->artisan('migrate', [ + // '--database' => 'mysql', + // '--realpath' => realpath(__DIR__.'/../database/migrations'), + // ]); + $this->artisan('laravel-config:install'); } /**