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,12 +58,12 @@ class Preferences implements Countable
* *
* @var array * @var array
*/ */
private $preferences = array(); protected $preferences = array();
/** /**
* Constructor * Constructor
* *
* @param array $preferences Preferences key-value array * @param array $preferences Preferences key-value array
*/ */
public function __construct(array $preferences = array()) public function __construct(array $preferences = array())
{ {
@ -75,7 +73,7 @@ class Preferences implements Countable
/** /**
* Count all preferences * Count all preferences
* *
* @return int The number of preferences * @return int The number of preferences
*/ */
public function count() public function count()
{ {
@ -85,7 +83,7 @@ class Preferences implements Countable
/** /**
* Determine whether a preference exists * Determine whether a preference exists
* *
* @param string $name * @param string $name
* *
* @return bool * @return bool
*/ */
@ -97,8 +95,8 @@ class Preferences implements Countable
/** /**
* Write data to a preference * Write data to a preference
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
*/ */
public function __set($name, $value) public function __set($name, $value)
{ {
@ -108,8 +106,8 @@ class Preferences implements Countable
/** /**
* Retrieve a preference and return $default if the preference is not set * Retrieve a preference and return $default if the preference is not set
* *
* @param string $name * @param string $name
* @param mixed $default * @param mixed $default
* *
* @return mixed * @return mixed
*/ */
@ -118,13 +116,14 @@ 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;
} }
/** /**
* Magic method so that $obj->value will work. * Magic method so that $obj->value will work.
* *
* @param string $name * @param string $name
* *
* @return mixed * @return mixed
*/ */
@ -136,7 +135,7 @@ class Preferences implements Countable
/** /**
* Remove a given preference * Remove a given preference
* *
* @param string $name Preference name * @param string $name Preference name
*/ */
public function remove($name) public function remove($name)
{ {
@ -146,7 +145,8 @@ 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)
@ -157,7 +157,7 @@ class Preferences implements Countable
/** /**
* Unset a given preference * Unset a given preference
* *
* @param string $name Preference name * @param string $name Preference name
*/ */
public function __unset($name) public function __unset($name)
{ {
@ -167,7 +167,7 @@ class Preferences implements Countable
/** /**
* Get preferences as array * Get preferences as array
* *
* @return array * @return array
*/ */
public function toArray() public function toArray()
{ {

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));
}
}