Add test for Icinga\User\Preferences

refs #6011
This commit is contained in:
Johannes Meyer 2014-04-22 10:18:31 +02:00
parent be410a685b
commit d44aaeb8d7
2 changed files with 62 additions and 15 deletions

View File

@ -44,8 +44,6 @@ use Countable;
* *
* $preferences = new Preferences(array('aPreference' => 'value')); // Start with initial preferences * $preferences = new Preferences(array('aPreference' => 'value')); // Start with initial preferences
* *
* $prefrences = $user->getPreferences(); // Retrieve preferences from a \Icinga\User instance
*
* $preferences->aNewPreference = 'value'; // Set a preference * $preferences->aNewPreference = 'value'; // Set a preference
* *
* unset($preferences->aPreference); // Unset a preference * unset($preferences->aPreference); // Unset a preference
@ -60,7 +58,7 @@ class Preferences implements Countable
* *
* @var array * @var array
*/ */
private $preferences = array(); protected $preferences = array();
/** /**
* Constructor * Constructor
@ -118,6 +116,7 @@ class Preferences implements Countable
if (array_key_exists($name, $this->preferences)) { if (array_key_exists($name, $this->preferences)) {
return $this->preferences[$name]; return $this->preferences[$name];
} }
return $default; return $default;
} }
@ -147,6 +146,7 @@ class Preferences implements Countable
* Determine if a preference is set and is not NULL * Determine if a preference is set and is not NULL
* *
* @param string $name Preference name * @param string $name Preference name
*
* @return bool * @return bool
*/ */
public function __isset($name) public function __isset($name)

View File

@ -0,0 +1,47 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Tests\Icinga\User;
use Icinga\User\Preferences;
use Icinga\Test\BaseTestCase;
class PreferfencesTest extends BaseTestCase
{
public function testWhetherPreferencesCanBeSet()
{
$prefs = new Preferences();
$prefs->key = 'value';
$this->assertTrue(isset($prefs->key));
$this->assertEquals('value', $prefs->key);
}
public function testWhetherPreferencesCanBeAccessed()
{
$prefs = new Preferences(array('key' => 'value'));
$this->assertTrue($prefs->has('key'));
$this->assertEquals('value', $prefs->get('key'));
}
public function testWhetherPreferencesCanBeRemoved()
{
$prefs = new Preferences(array('key' => 'value'));
unset($prefs->key);
$this->assertFalse(isset($prefs->key));
$prefs->key = 'value';
$prefs->remove('key');
$this->assertFalse($prefs->has('key'));
}
public function testWhetherPreferencesAreCountable()
{
$prefs = new Preferences(array('key1' => '1', 'key2' => '2'));
$this->assertEquals(2, count($prefs));
}
}