[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/test/tests/config/ -> configfilestorage_test.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/test/helpers/lifetypetestcase.class.php" );
   4      lt_include( PLOG_CLASS_PATH."class/config/configfilestorage.class.php" );        
   5  
   6      /**

   7       * \ingroup Test

   8       *

   9       * Test cases for the ConfigFileStorage class.

  10       *

  11       * It includes regression test for svn revisions 3768 and 3799 

  12       */
  13      class ConfigFileStorage_Test extends LifeTypeTestCase
  14      {
  15          /**

  16           * Creates a temporary test configuration file using both single quotes

  17           * and double quotes

  18           */
  19  		function setUp()
  20          {
  21              lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
  22              
  23              
  24              $config =& Config::getConfig();
  25              $tmpFolder = $config->getValue( "temp_folder" );
  26              
  27              $this->file1 = str_replace( "\\", "/", PLOG_CLASS_PATH ).$tmpFolder."/file1.properties.php";
  28              
  29              // create the first file

  30              $data1 = '<?php
  31              #

  32              # this is one comment

  33              #

  34              $config[\'test_key_1\'] = \'\';
  35              $config[\'test_key_2\'] = \'some value\';
  36              # 

  37              # this is

  38              # another

  39              # comment

  40              #

  41              $config["test_key_3"] = "value for test_key_3";
  42              #

  43              # an empty value

  44              #

  45              $config[\'test_empty_key\'] = \'\';
  46              ?>';
  47                
  48              $this->createFile( $this->file1, $data1 );             
  49          }
  50          
  51          /**

  52           * delete the temporary file that was created

  53           */
  54  		function tearDown()
  55          {
  56              lt_include( PLOG_CLASS_PATH."class/file/file.class.php" );            
  57              File::delete( $this->file1 );    
  58          }
  59          
  60          /**

  61           * @private

  62           */
  63  		function createFile( $file, $data )
  64          {
  65              lt_include( PLOG_CLASS_PATH."class/file/file.class.php" );
  66              $file = new File( $file );
  67              $writable = $file->open( 'w' );
  68              if ($writable) {
  69                  $file->write( $data );
  70                  $file->close();
  71                  return true;
  72              }
  73              else {
  74                  return false;
  75              }
  76          }
  77          
  78          /**

  79           * Check if the file was loaded properly and loads a value from a line

  80           * with single quotes

  81           */
  82  		function testGetSingleQuotesValue()
  83          {
  84              // open and load the file

  85              $cf = new ConfigFileStorage( Array( "file" => $this->file1 ));
  86              
  87              $this->assertEquals( "some value", $cf->getValue( "test_key_2" ), 
  88                                   "Error loading test_key_2 key from file ".$this->file1 );
  89          }
  90          
  91          /**

  92           * Check if the file was loaded properly and loads a value from a line

  93           * with double quotes

  94           */
  95  		function testGetDoubleQuotesValue()
  96          {
  97              // open and load the file

  98              $cf = new ConfigFileStorage( Array( "file" => $this->file1 ));
  99              
 100              $this->assertEquals( "value for test_key_3", $cf->getValue( "test_key_3" ), 
 101                                   "Error loading test_key_3 key from file ".$this->file1 );
 102          }
 103          
 104          /**

 105           * test whether new values are kept properly after loading the file

 106           */
 107  		function testSetNewValue()
 108          {
 109              // open and load the file

 110              $cf = new ConfigFileStorage( Array( "file" => $this->file1 ));
 111              
 112              // add a new key

 113              $newValue = "This is the value for test_key_new";
 114              $newKey = "test_key_new";
 115              $cf->setValue( $newKey, $newValue );
 116              
 117              $this->assertEquals( $newValue, $cf->getValue( $newKey ), "Error fetching $newKey" );
 118          }
 119          
 120          /**

 121           * test whether new values for keys defined with single quotes are saved properly back to the file

 122           */
 123  		function testSaveValue()
 124          {
 125              lt_include( PLOG_CLASS_PATH."class/file/file.class.php" );            
 126              
 127              // open and load the file

 128              $cf = new ConfigFileStorage( Array( "file" => $this->file1 ));            
 129              
 130              // and save a new one

 131              $newValue = "This is the value for test_empty_key";
 132              $newKey = "test_empty_key";
 133              $cf->setValue( $newKey, $newValue );
 134              $cf->save();
 135              
 136              // reopen and load the file

 137              $cf2 = new ConfigFileStorage( Array( "file" => $this->file1 ));
 138              $this->assertEquals( $newValue, $cf2->getValue( $newKey ),
 139                                   "$newKey was not saved properly to file ".$this->file1 );                                                                 
 140          }
 141          
 142          /**

 143           * test whether new values for keys defined with double quotes are saved properly back to the file

 144           */
 145  		function testSaveDoubleQuotesValue()
 146          {        
 147              // open and load the file

 148              $cf = new ConfigFileStorage( Array( "file" => $this->file1 ));            
 149              
 150              // and save a new one

 151              $newValue = "This is the value for test_key_3";
 152              $newKey = "test_key_3";
 153              $cf->setValue( $newKey, $newValue );
 154              $cf->save();
 155              
 156              // reopen and load the file

 157              $cf2 = new ConfigFileStorage( Array( "file" => $this->file1 ));
 158              $this->assertEquals( $newValue, $cf2->getValue( $newKey ),
 159                                   "$newKey was not saved properly to file ".$this->file1 );                                 
 160          }
 161          
 162          /**

 163           * Saves a value with single quotes, to check whether they're being escaped properly

 164           */
 165  		function testSaveValueWithSingleQuotes()
 166          {
 167              // open and load the file

 168              $cf = new ConfigFileStorage( Array( "file" => $this->file1 ));            
 169              
 170              // and save a new one

 171              $newValue = "This 'key' has plenty of 'single' quotes";
 172              $newKey = "test_key_3";
 173              $cf->setValue( $newKey, $newValue );
 174              $cf->save();
 175              
 176              // reopen and load the file

 177              $cf2 = new ConfigFileStorage( Array( "file" => $this->file1 ));
 178              $this->assertEquals( $newValue, $cf2->getValue( $newKey ),
 179                                   "$newKey was not saved properly to file ".$this->file1 );                                 
 180          }
 181          
 182          /**

 183           * Saves a value with double quotes, to check whether they're being escaped properly

 184           */
 185  		function testSaveValueWithDoubleQuotes()
 186          {
 187              // open and load the file

 188              $cf = new ConfigFileStorage( Array( "file" => $this->file1 ));            
 189              
 190              // and save a new one

 191              $newValue = "This \"key\" has plenty of \"single\" quotes";
 192              $newKey = "test_key_3";
 193              $cf->setValue( $newKey, $newValue );
 194              $cf->save();
 195              
 196              // reopen and load the file

 197              $cf2 = new ConfigFileStorage( Array( "file" => $this->file1 ));
 198              $this->assertEquals( $newValue, $cf2->getValue( $newKey ),
 199                                   "$newKey was not saved properly to file ".$this->file1 );                                             
 200          }
 201          
 202          /**

 203           * Saves a value with a dollar sign

 204           */
 205  		function testSaveValueWithDollarSign()
 206          {
 207              // open and load the file

 208              $cf = new ConfigFileStorage( Array( "file" => $this->file1 ));            
 209              
 210              // and save a new one

 211              $newValue = "This key has a dollar sign $";
 212              $newKey = "test_key_3";
 213              $cf->setValue( $newKey, $newValue );
 214              $cf->save();
 215              
 216              // reopen and load the file

 217              $cf2 = new ConfigFileStorage( Array( "file" => $this->file1 ));
 218              $this->assertEquals( $newValue, $cf2->getValue( $newKey ),
 219                                   "$newKey was not saved properly to file ".$this->file1 );                                             
 220          }        
 221          
 222          /** 

 223           * regression test for svn revision 3726. It basically tests whether the ConfigFileStorage::getValue()

 224           * method will return the default value specified as the second parameter when the provided

 225           * key doesn't exist.

 226           */
 227  		function testGetValueWithDefaultValue()
 228          {
 229              $cf = new ConfigFileStorage( Array( "file" => $this->file1 ));    
 230              
 231              // request a bogus key and see if we get the default value

 232              $defaultValue = "333";
 233              $value = $cf->getValue( "this_key_should_really_really_not_exist", $defaultValue );
 234              
 235              // check if they're equal (they should!)

 236              $this->assertEquals( $defaultValue, $value, "getValue() did not return the default value when a non-existant key was requested, please see svn revision 3726" );
 237          }        
 238      }
 239  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics