Skip to content

Commit

Permalink
Fix empty() and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
youchenlee committed Jan 29, 2018
1 parent ef55efc commit e5289be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Setting/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,14 @@ protected function hasByKey($key)
{
if (strpos($key, '.') !== false) {
$setting = $this->getSubValue($key);

return (empty($setting)) ? false : true;
} else {
if ($this->cache->has($key.'@'.$this->lang)) {
$setting = $this->cache->get($key.'@'.$this->lang);
} else {
$setting = $this->storage->retrieve($key, $this->lang);
}

return (empty($setting)) ? false : true;
}
return ($setting === null) ? false : true;
}

protected function forgetByKey($key)
Expand Down
23 changes: 23 additions & 0 deletions tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,29 @@ protected function setMock()
Schema::swap(Manager::Schema());
}

public function testNullValue()
{
$cache = m::mock(CacheContract::class);
$cache->shouldReceive('has')->andReturn(false);
$cache->shouldReceive('add')->andReturn(true);

$setting = new Setting(new EloquentStorage(), $cache);
$setting->set('a', null);
$this->assertTrue($setting->get('a') === null);
$this->assertTrue($setting->get('b') === null);

$setting->set('foo.bar', null);
$this->assertTrue($setting->get('foo.bar') === null);

$this->assertTrue($setting->get('foo.xxx') === null);

$setting->set('foo.zzz', 0);
$this->assertTrue($setting->get('foo.zzz') === 0);

$setting->set('foo.yyy', []);
$this->assertTrue($setting->get('foo.yyy') === []);
}

protected function migrationUp()
{
(new CreateSettingsTable())->up();
Expand Down

0 comments on commit e5289be

Please sign in to comment.