Skip to content
This repository has been archived by the owner on Feb 18, 2022. It is now read-only.

Commit

Permalink
Added RuntimeException when setting a value on a non-existent setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Jul 18, 2021
1 parent 4a514e4 commit 53052d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/SettingsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

namespace DarkGhostHunter\Laraconfig;

use DarkGhostHunter\Laraconfig\Eloquent\Setting;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Enumerable;
use Illuminate\Support\Traits\EnumeratesValues;
use RuntimeException;

/**
* Class SettingsCollection
*
* @package DarkGhostHunter\Laraconfig
*
* @method \DarkGhostHunter\Laraconfig\Eloquent\Setting get(string $name, mixed $default = null)
* @method Setting get(string $name, mixed $default = null)
*/
class SettingsCollection extends Collection
{
Expand Down Expand Up @@ -97,7 +99,11 @@ public function set(string|array $name, mixed $value, bool $force = true): void
}

foreach ($name as $key => $setting) {
$this->get($key)?->set($setting, $force);
if (! $instance = $this->get($key)) {
throw new RuntimeException("The setting [$key] doesn't exist.");
}

$instance->set($setting, $force);
}
}

Expand Down
5 changes: 3 additions & 2 deletions tests/HasConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DarkGhostHunter\Laraconfig\Eloquent\Setting;
use DarkGhostHunter\Laraconfig\HasConfig;
use DarkGhostHunter\Laraconfig\SettingsCollection;
use Error;
use Exception;
use Illuminate\Contracts\Cache\Factory;
use Illuminate\Contracts\Cache\Repository;
Expand Down Expand Up @@ -821,8 +822,8 @@ public function test_sets_value_dynamically(): void

public function test_sets_doesnt_sets_property_dynamically_into_collection(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Property [invalid] does not exist on this collection instance');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage("The setting [invalid] doesn't exist.");

$user = DummyModel::find(1);

Expand Down

0 comments on commit 53052d5

Please sign in to comment.